1 | #include "Dim.h"
|
---|
2 | #include "Event.h"
|
---|
3 | #include "Shell.h"
|
---|
4 | #include "StateMachineDim.h"
|
---|
5 | #include "Connection.h"
|
---|
6 | #include "Configuration.h"
|
---|
7 | #include "Console.h"
|
---|
8 | #include "Converter.h"
|
---|
9 | #include "DimServiceInfoList.h"
|
---|
10 |
|
---|
11 | #include "tools.h"
|
---|
12 |
|
---|
13 | #include "LocalControl.h"
|
---|
14 |
|
---|
15 | #include "HeadersFTM.h"
|
---|
16 | #include "HeadersFAD.h"
|
---|
17 |
|
---|
18 |
|
---|
19 | namespace ba = boost::asio;
|
---|
20 | namespace bs = boost::system;
|
---|
21 | namespace dummy = ba::placeholders;
|
---|
22 |
|
---|
23 | using namespace std;
|
---|
24 |
|
---|
25 | // ------------------------------------------------------------------------
|
---|
26 |
|
---|
27 | #include "DimDescriptionService.h"
|
---|
28 |
|
---|
29 | // ------------------------------------------------------------------------
|
---|
30 |
|
---|
31 | class StateMachineMCP : public StateMachineDim, public DimInfoHandler
|
---|
32 | {
|
---|
33 | /*
|
---|
34 | int Wrap(boost::function<void()> f)
|
---|
35 | {
|
---|
36 | f();
|
---|
37 | return T::GetCurrentState();
|
---|
38 | }
|
---|
39 |
|
---|
40 | boost::function<int(const EventImp &)> Wrapper(boost::function<void()> func)
|
---|
41 | {
|
---|
42 | return bind(&StateMachineMCP::Wrap, this, func);
|
---|
43 | }*/
|
---|
44 |
|
---|
45 | private:
|
---|
46 | enum states_t
|
---|
47 | {
|
---|
48 | kStateDimNetworkNA = 1,
|
---|
49 | kStateDisconnected,
|
---|
50 | kStateConnecting,
|
---|
51 | kStateConnected,
|
---|
52 | kStateIdle,
|
---|
53 | kStateReadyForDataTaking,
|
---|
54 | kStateConfiguring1,
|
---|
55 | kStateConfiguring2,
|
---|
56 | kStateConfiguring3,
|
---|
57 | kStateConfigured,
|
---|
58 | };
|
---|
59 |
|
---|
60 | DimServiceInfoList fNetwork;
|
---|
61 |
|
---|
62 | pair<Time, int> fStatusDim;
|
---|
63 | pair<Time, int> fStatusFTM;
|
---|
64 | pair<Time, int> fStatusFAD;
|
---|
65 | pair<Time, int> fStatusLog;
|
---|
66 |
|
---|
67 | DimStampedInfo fDim;
|
---|
68 | DimStampedInfo fFTM;
|
---|
69 | DimStampedInfo fFAD;
|
---|
70 | DimStampedInfo fLog;
|
---|
71 |
|
---|
72 | pair<Time, int> GetNewState(DimStampedInfo &info) const
|
---|
73 | {
|
---|
74 | const bool disconnected = info.getSize()==0;
|
---|
75 |
|
---|
76 | // Make sure getTimestamp is called _before_ getTimestampMillisecs
|
---|
77 | const int tsec = info.getTimestamp();
|
---|
78 | const int tms = info.getTimestampMillisecs();
|
---|
79 |
|
---|
80 | return make_pair(Time(tsec, tms*1000),
|
---|
81 | disconnected ? -2 : info.getQuality());
|
---|
82 | }
|
---|
83 |
|
---|
84 | void infoHandler()
|
---|
85 | {
|
---|
86 | DimInfo *curr = getInfo(); // get current DimInfo address
|
---|
87 | if (!curr)
|
---|
88 | return;
|
---|
89 |
|
---|
90 | if (curr==&fFTM)
|
---|
91 | {
|
---|
92 | fStatusFTM = GetNewState(fFTM);
|
---|
93 | return;
|
---|
94 | }
|
---|
95 |
|
---|
96 | if (curr==&fFAD)
|
---|
97 | {
|
---|
98 | fStatusFAD = GetNewState(fFAD);
|
---|
99 | return;
|
---|
100 | }
|
---|
101 |
|
---|
102 | if (curr==&fLog)
|
---|
103 | {
|
---|
104 | fStatusLog = GetNewState(fLog);
|
---|
105 | return;
|
---|
106 | }
|
---|
107 |
|
---|
108 | if (curr==&fDim)
|
---|
109 | {
|
---|
110 | fStatusDim = GetNewState(fDim);
|
---|
111 | fStatusDim.second = curr->getSize()==4 ? curr->getInt() : 0;
|
---|
112 | return;
|
---|
113 | }
|
---|
114 |
|
---|
115 | }
|
---|
116 |
|
---|
117 | bool CheckEventSize(size_t has, const char *name, size_t size)
|
---|
118 | {
|
---|
119 | if (has==size)
|
---|
120 | return true;
|
---|
121 |
|
---|
122 | ostringstream msg;
|
---|
123 | msg << name << " - Received event has " << has << " bytes, but expected " << size << ".";
|
---|
124 | Fatal(msg);
|
---|
125 | return false;
|
---|
126 | }
|
---|
127 |
|
---|
128 | int SetVerbosity(const EventImp &)
|
---|
129 | {
|
---|
130 | /*
|
---|
131 | if (!CheckEventSize(evt.GetSize(), "SetVerbosity", 1))
|
---|
132 | return T::kSM_FatalError;
|
---|
133 |
|
---|
134 | fFSC.SetVerbose(evt.GetBool());
|
---|
135 |
|
---|
136 | */
|
---|
137 |
|
---|
138 | return GetCurrentState();
|
---|
139 | }
|
---|
140 |
|
---|
141 |
|
---|
142 | void PrintState(const pair<Time,int> &state, const char *server)
|
---|
143 | {
|
---|
144 | const State rc = fNetwork.GetState(server, state.second);
|
---|
145 |
|
---|
146 | Out() << state.first.GetAsStr("%H:%M:%S.%f").substr(0, 12) << " - ";
|
---|
147 | Out() << kBold << server << ": ";
|
---|
148 | Out() << rc.name << "[" << rc.index << "]";
|
---|
149 | Out() << kReset << " - " << kBlue << rc.comment << endl;
|
---|
150 | }
|
---|
151 |
|
---|
152 | int Print()
|
---|
153 | {
|
---|
154 | Out() << fStatusDim.first.GetAsStr("%H:%M:%S.%f").substr(0, 12) << " - ";
|
---|
155 | Out() << kBold << "DIM_DNS: ";
|
---|
156 | if (fStatusDim.second==0)
|
---|
157 | Out() << "Offline" << endl;
|
---|
158 | else
|
---|
159 | Out() << "V" << fStatusDim.second/100 << 'r' << fStatusDim.second%100 << endl;
|
---|
160 |
|
---|
161 | PrintState(fStatusFTM, "FTM_CONTROL");
|
---|
162 | PrintState(fStatusFAD, "FAD_CONTROL");
|
---|
163 | PrintState(fStatusLog, "DATA_LOGGER");
|
---|
164 |
|
---|
165 | return GetCurrentState();
|
---|
166 | }
|
---|
167 |
|
---|
168 | int GetReady()
|
---|
169 | {
|
---|
170 |
|
---|
171 | return GetCurrentState();
|
---|
172 | }
|
---|
173 |
|
---|
174 | int StopRun(const EventImp &)
|
---|
175 | {
|
---|
176 | if (fStatusFTM.second==FTM::kTakingData)
|
---|
177 | {
|
---|
178 | Message("Stopping FTM");
|
---|
179 | Dim::SendCommand("FTM_CONTROL/STOP_RUN");
|
---|
180 | }
|
---|
181 |
|
---|
182 | // FIXME: Do step 2 only when FTM is stopped
|
---|
183 | if (fStatusFAD.second==FAD::kConnected)
|
---|
184 | {
|
---|
185 | //Dim::SendCommand("FAD_CONTROL/ENABLE_TRIGGER_LINE", bool(false));
|
---|
186 | Message("Stopping FAD");
|
---|
187 | Dim::SendCommand("FAD_CONTROL/ENABLE_CONTINOUS_TRIGGER", bool(false));
|
---|
188 | }
|
---|
189 |
|
---|
190 | return GetCurrentState();
|
---|
191 | }
|
---|
192 |
|
---|
193 | int Reset(const EventImp &)
|
---|
194 | {
|
---|
195 | fRunType = "";
|
---|
196 | Message("Reseting configuration states of FAD and FTM");
|
---|
197 | Dim::SendCommand("FTM_CONTROL/RESET_CONFIGURE");
|
---|
198 | Dim::SendCommand("FAD_CONTROL/RESET_CONFIGURE");
|
---|
199 | return kStateIdle;
|
---|
200 | /*
|
---|
201 | // FIMXE: Handle error states!
|
---|
202 | if (fStatusLog.second>=20)//kSM_NightlyOpen
|
---|
203 | Dim::SendCommand("DATA_LOGGER/STOP");
|
---|
204 |
|
---|
205 | if (fStatusLog.second==0)
|
---|
206 | Dim::SendCommand("DATA_LOGGER/WAIT_FOR_RUN_NUMBER");
|
---|
207 |
|
---|
208 | if (fStatusFAD.second==FAD::kConnected)
|
---|
209 | {
|
---|
210 | Dim::SendCommand("FAD_CONTROL/ENABLE_TRIGGER_LINE", bool(false));
|
---|
211 | Dim::SendCommand("FAD_CONTROL/ENABLE_CONTINOUS_TRIGGER", bool(false));
|
---|
212 | }
|
---|
213 |
|
---|
214 | if (fStatusFTM.second==FTM::kTakingData)
|
---|
215 | Dim::SendCommand("FTM_CONTROL/STOP");
|
---|
216 |
|
---|
217 | return GetCurrentState(); */
|
---|
218 | }
|
---|
219 |
|
---|
220 | uint64_t fMaxTime;
|
---|
221 | uint64_t fNumEvents;
|
---|
222 | string fRunType;
|
---|
223 |
|
---|
224 | int StartRun(const EventImp &evt)
|
---|
225 | {
|
---|
226 | fMaxTime = evt.Get<int64_t>();
|
---|
227 | fNumEvents = evt.Get<int64_t>(8);
|
---|
228 | fRunType = evt.Ptr<char>(16);
|
---|
229 |
|
---|
230 | ostringstream str;
|
---|
231 | str << "Starting configuration '" << fRunType << "' for new run";
|
---|
232 | if (fNumEvents>0 || fMaxTime>0)
|
---|
233 | str << " [";
|
---|
234 | if (fNumEvents>0)
|
---|
235 | str << fNumEvents << " events";
|
---|
236 | if (fNumEvents>0 && fMaxTime>0)
|
---|
237 | str << " / ";
|
---|
238 | if (fMaxTime>0)
|
---|
239 | str << fMaxTime << "s";
|
---|
240 | if (fNumEvents>0 || fMaxTime>0)
|
---|
241 | str << "]";
|
---|
242 | Message(str);
|
---|
243 |
|
---|
244 | return kStateConfiguring1;
|
---|
245 | }
|
---|
246 |
|
---|
247 | void ConfigureFAD()
|
---|
248 | {
|
---|
249 | struct Value
|
---|
250 | {
|
---|
251 | uint64_t time;
|
---|
252 | uint64_t nevts;
|
---|
253 | char type[];
|
---|
254 | };
|
---|
255 |
|
---|
256 | const size_t len = sizeof(Value)+fRunType.length()+1;
|
---|
257 |
|
---|
258 | char *buf = new char[len];
|
---|
259 |
|
---|
260 | Value *val = reinterpret_cast<Value*>(buf);
|
---|
261 |
|
---|
262 | val->time = fMaxTime;
|
---|
263 | val->nevts = fNumEvents;
|
---|
264 |
|
---|
265 | strcpy(val->type, fRunType.c_str());
|
---|
266 |
|
---|
267 | Message("Configuring FAD");
|
---|
268 | Dim::SendCommand("FAD_CONTROL/CONFIGURE", buf, len);
|
---|
269 |
|
---|
270 | delete buf;
|
---|
271 | }
|
---|
272 |
|
---|
273 | int Execute()
|
---|
274 | {
|
---|
275 | // Dispatch (execute) at most one handler from the queue. In contrary
|
---|
276 | // to run_one(), it doesn't wait until a handler is available
|
---|
277 | // which can be dispatched, so poll_one() might return with 0
|
---|
278 | // handlers dispatched. The handlers are always dispatched/executed
|
---|
279 | // synchronously, i.e. within the call to poll_one()
|
---|
280 | //poll_one();
|
---|
281 |
|
---|
282 | if (fStatusDim.second==0)
|
---|
283 | return kStateDimNetworkNA;
|
---|
284 |
|
---|
285 | if (fStatusFTM.second >= FTM::kConnected &&
|
---|
286 | fStatusFAD.second >= FAD::kConnected &&
|
---|
287 | fStatusLog.second >= kSM_Ready)
|
---|
288 | {
|
---|
289 | if (GetCurrentState()==kStateConfiguring1)
|
---|
290 | {
|
---|
291 | if (fStatusLog.second<30/*kSM_WaitForRun*/)
|
---|
292 | {
|
---|
293 | Message("Starting datalogger");
|
---|
294 | Dim::SendCommand("DATA_LOGGER/START_RUN_LOGGING");
|
---|
295 | }
|
---|
296 | Message("Configuring Trigger (FTM)");
|
---|
297 | Dim::SendCommand("FTM_CONTROL/CONFIGURE", fRunType);
|
---|
298 | return kStateConfiguring2;
|
---|
299 | }
|
---|
300 |
|
---|
301 | if (GetCurrentState()==kStateConfiguring2)
|
---|
302 | {
|
---|
303 | if ((fStatusFTM.second != FTM::kConfiguring2 &&
|
---|
304 | fStatusFTM.second != FTM::kConfigured) ||
|
---|
305 | fStatusLog.second < 30/*kSM_WaitForRun*/)
|
---|
306 | return GetCurrentState();
|
---|
307 |
|
---|
308 | Message("Starting FAD");
|
---|
309 | ConfigureFAD();
|
---|
310 | return kStateConfiguring3;
|
---|
311 | }
|
---|
312 |
|
---|
313 | if (GetCurrentState()==kStateConfiguring3)
|
---|
314 | {
|
---|
315 | if (fStatusFTM.second != FTM::kConfigured ||
|
---|
316 | fStatusFAD.second != FAD::kConfigured)
|
---|
317 | return GetCurrentState();
|
---|
318 |
|
---|
319 | Message("Starting Trigger (FTM)");
|
---|
320 | Dim::SendCommand("FTM_CONTROL/START_RUN");
|
---|
321 | return kStateConfigured;
|
---|
322 | }
|
---|
323 |
|
---|
324 | if (GetCurrentState()==kStateConfigured)
|
---|
325 | return GetCurrentState();
|
---|
326 |
|
---|
327 | return kStateIdle;
|
---|
328 | }
|
---|
329 |
|
---|
330 | /*
|
---|
331 | if (fStatusFTM.second >= FTM::kConnected &&
|
---|
332 | fStatusFAD.second >= FAD::kConnected &&
|
---|
333 | fStatusLog.second >= kSM_Ready)
|
---|
334 | return kStateIdle;
|
---|
335 | */
|
---|
336 | if (fStatusFTM.second >-2 &&
|
---|
337 | fStatusFAD.second >-2 &&
|
---|
338 | fStatusLog.second >-2)
|
---|
339 | return kStateConnected;
|
---|
340 |
|
---|
341 | if (fStatusFTM.second >-2 ||
|
---|
342 | fStatusFAD.second >-2 ||
|
---|
343 | fStatusLog.second >-2)
|
---|
344 | return kStateConnecting;
|
---|
345 |
|
---|
346 | return kStateDisconnected;
|
---|
347 | }
|
---|
348 |
|
---|
349 | public:
|
---|
350 | StateMachineMCP(ostream &out=cout) : StateMachineDim(out, "MCP"),
|
---|
351 | fStatusDim(make_pair(Time(), -2)),
|
---|
352 | fStatusFTM(make_pair(Time(), -2)),
|
---|
353 | fStatusFAD(make_pair(Time(), -2)),
|
---|
354 | fStatusLog(make_pair(Time(), -2)),
|
---|
355 | fDim("DIS_DNS/VERSION_NUMBER", (void*)NULL, 0, this),
|
---|
356 | fFTM("FTM_CONTROL/STATE", (void*)NULL, 0, this),
|
---|
357 | fFAD("FAD_CONTROL/STATE", (void*)NULL, 0, this),
|
---|
358 | fLog("DATA_LOGGER/STATE", (void*)NULL, 0, this)
|
---|
359 | {
|
---|
360 | // ba::io_service::work is a kind of keep_alive for the loop.
|
---|
361 | // It prevents the io_service to go to stopped state, which
|
---|
362 | // would prevent any consecutive calls to run()
|
---|
363 | // or poll() to do nothing. reset() could also revoke to the
|
---|
364 | // previous state but this might introduce some overhead of
|
---|
365 | // deletion and creation of threads and more.
|
---|
366 |
|
---|
367 | // State names
|
---|
368 | AddStateName(kStateDimNetworkNA, "DimNetworkNotAvailable",
|
---|
369 | ".");
|
---|
370 |
|
---|
371 | AddStateName(kStateDisconnected, "Disconnected",
|
---|
372 | ".");
|
---|
373 |
|
---|
374 | AddStateName(kStateConnecting, "Connecting",
|
---|
375 | ".");
|
---|
376 |
|
---|
377 | AddStateName(kStateConnected, "Connected",
|
---|
378 | ".");
|
---|
379 |
|
---|
380 | AddStateName(kStateIdle, "Idle",
|
---|
381 | ".");
|
---|
382 |
|
---|
383 | AddStateName(kStateReadyForDataTaking, "ReadyForDataTaking",
|
---|
384 | ".");
|
---|
385 |
|
---|
386 | AddStateName(kStateConfiguring1, "Configuring1",
|
---|
387 | ".");
|
---|
388 |
|
---|
389 | AddStateName(kStateConfiguring2, "Configuring2",
|
---|
390 | "Waiting for FTM and Datalogger to get ready");
|
---|
391 |
|
---|
392 | AddStateName(kStateConfiguring3, "Configuring3",
|
---|
393 | "Waiting for FAD to get ready");
|
---|
394 |
|
---|
395 | AddStateName(kStateConfigured, "Configured",
|
---|
396 | ".");
|
---|
397 |
|
---|
398 |
|
---|
399 | AddEvent("START", "X:2;C")//, kStateIdle)
|
---|
400 | (bind(&StateMachineMCP::StartRun, this, placeholders::_1))
|
---|
401 | ("");
|
---|
402 |
|
---|
403 | AddEvent("STOP")
|
---|
404 | (bind(&StateMachineMCP::StopRun, this, placeholders::_1))
|
---|
405 | ("");
|
---|
406 |
|
---|
407 | AddEvent("RESET", kStateConfiguring1, kStateConfiguring2, kStateConfiguring3, kStateConfigured)
|
---|
408 | (bind(&StateMachineMCP::Reset, this, placeholders::_1))
|
---|
409 | ("");
|
---|
410 |
|
---|
411 | // Verbosity commands
|
---|
412 | AddEvent("SET_VERBOSE", "B:1")
|
---|
413 | (bind(&StateMachineMCP::SetVerbosity, this, placeholders::_1))
|
---|
414 | ("set verbosity state"
|
---|
415 | "|verbosity[bool]:disable or enable verbosity for received data (yes/no), except dynamic data");
|
---|
416 |
|
---|
417 | AddEvent("PRINT")
|
---|
418 | (bind(&StateMachineMCP::Print, this))
|
---|
419 | ("");
|
---|
420 | }
|
---|
421 |
|
---|
422 | int EvalOptions(Configuration &)
|
---|
423 | {
|
---|
424 | //SetEndpoint(conf.Get<string>("addr"));
|
---|
425 |
|
---|
426 | //fFSC.SetVerbose(!conf.Get<bool>("quiet"));
|
---|
427 |
|
---|
428 | return -1;
|
---|
429 | }
|
---|
430 | };
|
---|
431 |
|
---|
432 | // ------------------------------------------------------------------------
|
---|
433 |
|
---|
434 | #include "Main.h"
|
---|
435 |
|
---|
436 | template<class T>
|
---|
437 | int RunShell(Configuration &conf)
|
---|
438 | {
|
---|
439 | return Main::execute<T, StateMachineMCP>(conf);
|
---|
440 | }
|
---|
441 |
|
---|
442 | /*
|
---|
443 | Extract usage clause(s) [if any] for SYNOPSIS.
|
---|
444 | Translators: "Usage" and "or" here are patterns (regular expressions) which
|
---|
445 | are used to match the usage synopsis in program output. An example from cp
|
---|
446 | (GNU coreutils) which contains both strings:
|
---|
447 | Usage: cp [OPTION]... [-T] SOURCE DEST
|
---|
448 | or: cp [OPTION]... SOURCE... DIRECTORY
|
---|
449 | or: cp [OPTION]... -t DIRECTORY SOURCE...
|
---|
450 | */
|
---|
451 | void PrintUsage()
|
---|
452 | {
|
---|
453 | cout <<
|
---|
454 | "The ftmctrl controls the FSC (FACT Slow Control) board.\n"
|
---|
455 | "\n"
|
---|
456 | "The default is that the program is started without user intercation. "
|
---|
457 | "All actions are supposed to arrive as DimCommands. Using the -c "
|
---|
458 | "option, a local shell can be initialized. With h or help a short "
|
---|
459 | "help message about the usuage can be brought to the screen.\n"
|
---|
460 | "\n"
|
---|
461 | "Usage: fscctrl [-c type] [OPTIONS]\n"
|
---|
462 | " or: fscctrl [OPTIONS]\n";
|
---|
463 | cout << endl;
|
---|
464 | }
|
---|
465 |
|
---|
466 | void PrintHelp()
|
---|
467 | {
|
---|
468 | Main::PrintHelp<StateMachineMCP>();
|
---|
469 |
|
---|
470 | /* Additional help text which is printed after the configuration
|
---|
471 | options goes here */
|
---|
472 |
|
---|
473 | /*
|
---|
474 | cout << "bla bla bla" << endl << endl;
|
---|
475 | cout << endl;
|
---|
476 | cout << "Environment:" << endl;
|
---|
477 | cout << "environment" << endl;
|
---|
478 | cout << endl;
|
---|
479 | cout << "Examples:" << endl;
|
---|
480 | cout << "test exam" << endl;
|
---|
481 | cout << endl;
|
---|
482 | cout << "Files:" << endl;
|
---|
483 | cout << "files" << endl;
|
---|
484 | cout << endl;
|
---|
485 | */
|
---|
486 | }
|
---|
487 |
|
---|
488 | int main(int argc, const char* argv[])
|
---|
489 | {
|
---|
490 | Configuration conf(argv[0]);
|
---|
491 | conf.SetPrintUsage(PrintUsage);
|
---|
492 | Main::SetupConfiguration(conf);
|
---|
493 |
|
---|
494 | if (!conf.DoParse(argc, argv, PrintHelp))
|
---|
495 | return -1;
|
---|
496 |
|
---|
497 | //try
|
---|
498 | {
|
---|
499 | // No console access at all
|
---|
500 | if (!conf.Has("console"))
|
---|
501 | {
|
---|
502 | // if (conf.Get<bool>("no-dim"))
|
---|
503 | // return RunShell<LocalStream, StateMachine, ConnectionFSC>(conf);
|
---|
504 | // else
|
---|
505 | return RunShell<LocalStream>(conf);
|
---|
506 | }
|
---|
507 | // Cosole access w/ and w/o Dim
|
---|
508 | /* if (conf.Get<bool>("no-dim"))
|
---|
509 | {
|
---|
510 | if (conf.Get<int>("console")==0)
|
---|
511 | return RunShell<LocalShell, StateMachine, ConnectionFSC>(conf);
|
---|
512 | else
|
---|
513 | return RunShell<LocalConsole, StateMachine, ConnectionFSC>(conf);
|
---|
514 | }
|
---|
515 | else
|
---|
516 | */ {
|
---|
517 | if (conf.Get<int>("console")==0)
|
---|
518 | return RunShell<LocalShell>(conf);
|
---|
519 | else
|
---|
520 | return RunShell<LocalConsole>(conf);
|
---|
521 | }
|
---|
522 | }
|
---|
523 | /*catch (std::exception& e)
|
---|
524 | {
|
---|
525 | cerr << "Exception: " << e.what() << endl;
|
---|
526 | return -1;
|
---|
527 | }*/
|
---|
528 |
|
---|
529 | return 0;
|
---|
530 | }
|
---|