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