Check for device state on update.
[yandex-traffic] / traffic.hpp
1 #ifndef __TRAFFIC_H__
2 #define __TRAFFIC_H__
3
4 #include <QtCore>
5 #include <QtNetwork>
6 #include <QtXml>
7
8
9 #include "http_fetcher.hpp"
10
11 // Base data of traffic information
12 class TrafficInfo
13 {
14 private:
15     float _len1, _len2, _len;
16     QDateTime _ts;
17     QString _isotime;
18     QString _localtime;
19
20     bool _valid;
21
22 protected:
23     float getFloatNode (const QDomElement& elem, const char* node, float def);
24     int getIntNode (const QDomElement& elem, const char* node, int def);
25     QString getStringNode (const QDomElement& elem, const char* node) throw (const QString&);
26     QDateTime getTSNode (const QDomElement& elem, const char* node) throw (const QString&);
27
28     void setValid (bool new_val)
29     { _valid = new_val; };
30
31 public:
32     TrafficInfo ()
33         : _valid (false)
34     {};
35
36     TrafficInfo (const QDomElement& elem) throw (const QString&);
37
38     bool valid () const
39     { return _valid; };
40
41     QString localtime () const
42     { return _localtime; };
43
44     QDateTime ts () const
45     { return _ts; };
46 };
47
48
49 class ExtendedTrafficInfo : public TrafficInfo
50 {
51 public:
52     enum light_color {
53         Unknown,
54         Red,
55         Yellow,
56         Green
57     };
58
59 private:
60     float _level_raw;
61     int _level;
62     light_color _color;
63     int _tend;
64     QString _hint;
65
66 public:
67     ExtendedTrafficInfo ()
68         : TrafficInfo ()
69     {};
70
71     ExtendedTrafficInfo (const QDomElement& elem) throw (const QString&);
72
73     int level () const
74     { return _level; };
75
76     int tend () const
77     { return _tend; };
78
79     QString hint () const
80     { return _hint; };
81
82     ExtendedTrafficInfo::light_color color () const
83     { return _color; };
84 };
85
86
87 class Traffic : public QObject
88 {
89     Q_OBJECT
90
91 private:
92     QDateTime _ts;
93
94     QMap<QString, TrafficInfo> _info;
95     QMap<QString, ExtendedTrafficInfo> _ext_info;
96
97     HttpFetcher _fetcher;
98
99     bool parse_traffic_data (const QString& xml);
100
101 private slots:
102     void fetchDone (const QByteArray& data);
103
104 signals:
105     void updated ();
106
107 public:
108     Traffic ();
109
110     void update ();
111
112     QDateTime ts () const
113     { return _ts; };
114
115     TrafficInfo lookup (const QString &id) const;
116     ExtendedTrafficInfo lookup_ext (const QString &id) const;
117 };
118
119
120 #endif // __TRAFFIC_H__