1 | #ifndef FACT_DimState
|
---|
2 | #define FACT_DimState
|
---|
3 |
|
---|
4 | class DimState
|
---|
5 | {
|
---|
6 | protected:
|
---|
7 | typedef function<void(const EventImp &)> callback;
|
---|
8 |
|
---|
9 | callback fCallback;
|
---|
10 |
|
---|
11 | public:
|
---|
12 | DimState(const string &n, const string s="STATE") : server(n),
|
---|
13 | service(n+"/"+s), info(make_pair(Time(), -4))
|
---|
14 | {
|
---|
15 | }
|
---|
16 | virtual ~DimState()
|
---|
17 | {
|
---|
18 | }
|
---|
19 |
|
---|
20 | string server;
|
---|
21 | string service;
|
---|
22 |
|
---|
23 | pair<Time, int> info;
|
---|
24 | string msg;
|
---|
25 |
|
---|
26 | virtual void Subscribe(StateMachineImp &imp)
|
---|
27 | {
|
---|
28 | imp.Subscribe(service.c_str())
|
---|
29 | (imp.Wrap(bind(&DimState::Handler, this, placeholders::_1)));
|
---|
30 | }
|
---|
31 |
|
---|
32 | void SetCallback(const callback &cb)
|
---|
33 | {
|
---|
34 | fCallback = cb;
|
---|
35 | }
|
---|
36 |
|
---|
37 | virtual void Handler(const EventImp &evt)
|
---|
38 | {
|
---|
39 | const bool disconnected = evt.GetSize()==0;
|
---|
40 |
|
---|
41 | info = make_pair(evt.GetTime(), disconnected ? -4 : evt.GetQoS());
|
---|
42 |
|
---|
43 | msg = disconnected ? "" : evt.GetString();
|
---|
44 |
|
---|
45 | if (fCallback)
|
---|
46 | fCallback(evt);
|
---|
47 | }
|
---|
48 |
|
---|
49 | const Time &time() const { return info.first; }
|
---|
50 | const int &state() const { return info.second; }
|
---|
51 |
|
---|
52 | bool online() const { return info.second>-4; }
|
---|
53 |
|
---|
54 | virtual State description() const { return State(-5, ""); }
|
---|
55 | };
|
---|
56 |
|
---|
57 | ostream &operator<<(ostream& out, const DimState &s)
|
---|
58 | {
|
---|
59 | const State rc = s.description();
|
---|
60 |
|
---|
61 | out << s.time().GetAsStr("%H:%M:%S.%f").substr(0, 12) << " - ";
|
---|
62 | out << kBold << s.server;
|
---|
63 |
|
---|
64 | if (s.state()==-4)
|
---|
65 | return out << ": Offline";
|
---|
66 |
|
---|
67 | if (rc.index==-5)
|
---|
68 | return out;
|
---|
69 |
|
---|
70 | out << ": ";
|
---|
71 |
|
---|
72 | if (rc.index==-2)
|
---|
73 | out << s.state();
|
---|
74 | else
|
---|
75 | out << rc.name << "[" << rc.index << "]";
|
---|
76 |
|
---|
77 | if (!rc.comment.empty())
|
---|
78 | out << " - " << kBlue << rc.comment;
|
---|
79 |
|
---|
80 | return out;
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | class DimDescribedState : public DimState
|
---|
85 | {
|
---|
86 | vector<State> states;
|
---|
87 |
|
---|
88 | public:
|
---|
89 | DimDescribedState(const string &n) : DimState(n)
|
---|
90 | {
|
---|
91 | }
|
---|
92 |
|
---|
93 | virtual void Subscribe(StateMachineImp &imp)
|
---|
94 | {
|
---|
95 | imp.Subscribe((server+"/STATE_LIST").c_str())
|
---|
96 | (imp.Wrap(bind(&DimDescribedState::HandleDesc, this, placeholders::_1)));
|
---|
97 |
|
---|
98 | DimState::Subscribe(imp);
|
---|
99 | }
|
---|
100 |
|
---|
101 | void HandleDesc(const EventImp &evt)
|
---|
102 | {
|
---|
103 | if (evt.GetSize()>0)
|
---|
104 | {
|
---|
105 | states = State::SplitStates(evt.GetString());
|
---|
106 | states.push_back(State(-4, "Offline"));
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | State description() const
|
---|
111 | {
|
---|
112 | for (auto it=states.begin(); it!=states.end(); it++)
|
---|
113 | if (it->index==state())
|
---|
114 | return *it;
|
---|
115 |
|
---|
116 | return State(-5, "n/a");
|
---|
117 | }
|
---|
118 | };
|
---|
119 |
|
---|
120 | class DimVersion : public DimState
|
---|
121 | {
|
---|
122 | public:
|
---|
123 | DimVersion() : DimState("DIS_DNS", "VERSION_NUMBER") { }
|
---|
124 |
|
---|
125 | void Handler(const EventImp &evt)
|
---|
126 | {
|
---|
127 | DimState::Handler(evt);
|
---|
128 |
|
---|
129 | info.second = evt.GetSize()==4 ? evt.GetInt() : -4;
|
---|
130 | }
|
---|
131 |
|
---|
132 | string version() const
|
---|
133 | {
|
---|
134 | if (info.second==0)
|
---|
135 | return "Offline";
|
---|
136 |
|
---|
137 | ostringstream out;
|
---|
138 | out << "V" << info.second/100 << 'r' << info.second%100;
|
---|
139 | return out.str();
|
---|
140 | }
|
---|
141 |
|
---|
142 | State description() const
|
---|
143 | {
|
---|
144 | return State(state(), version());
|
---|
145 | }
|
---|
146 | };
|
---|
147 |
|
---|
148 | class DimControl : public DimState
|
---|
149 | {
|
---|
150 | map<string, callback> fCallbacks;
|
---|
151 |
|
---|
152 | public:
|
---|
153 | DimControl() : DimState("DIM_CONTROL") { }
|
---|
154 |
|
---|
155 | void AddCallback(const string &script, const callback &cb)
|
---|
156 | {
|
---|
157 | fCallbacks[script] = cb;
|
---|
158 | }
|
---|
159 |
|
---|
160 | void Handler(const EventImp &evt)
|
---|
161 | {
|
---|
162 | DimState::Handler(evt);
|
---|
163 |
|
---|
164 | // Evaluate msg
|
---|
165 | const size_t p0 = msg.find_first_of(':');
|
---|
166 | if (p0==string::npos)
|
---|
167 | return;
|
---|
168 |
|
---|
169 | const size_t p1 = msg.find_last_of('[');
|
---|
170 | if (p1==string::npos)
|
---|
171 | return;
|
---|
172 |
|
---|
173 | const size_t p2 = msg.find_first_of(':', p0+1);
|
---|
174 |
|
---|
175 | const size_t p3 = p2==string::npos || p2>p1 ? p1-1 : p2;
|
---|
176 |
|
---|
177 | const string file = msg.substr(p0+2, p3-p0-2);
|
---|
178 |
|
---|
179 | const auto func = fCallbacks.find(file);
|
---|
180 | if (func==fCallbacks.end())
|
---|
181 | return;
|
---|
182 |
|
---|
183 | // Call callback
|
---|
184 | func->second(evt);
|
---|
185 | }
|
---|
186 |
|
---|
187 | State description() const
|
---|
188 | {
|
---|
189 | return State(state(), "Current label");
|
---|
190 | }
|
---|
191 |
|
---|
192 | };
|
---|
193 |
|
---|
194 | #endif
|
---|