source: trunk/FACT++/src/smartfact.cc@ 17624

Last change on this file since 17624 was 17520, checked in by tbretz, 11 years ago
Check humidity only when the lid is open, not when a run is started, this gives a warning otherwise at the start of each run. Fixed some minor display issues like the number of valid digits.
File size: 107.6 KB
Line 
1#ifdef HAVE_NOVA
2#include "externals/nova.h"
3#endif
4
5#ifdef HAVE_SQL
6#include "Database.h"
7#endif
8
9#include <sys/stat.h> //for file stats
10
11#include "Dim.h"
12#include "Event.h"
13#include "Shell.h"
14#include "StateMachineDim.h"
15#include "Connection.h"
16#include "Configuration.h"
17#include "Console.h"
18#include "DimWriteStatistics.h"
19#include "externals/PixelMap.h"
20
21#include "tools.h"
22
23#include "LocalControl.h"
24
25#include "HeadersFAD.h"
26#include "HeadersBIAS.h"
27#include "HeadersFTM.h"
28#include "HeadersFSC.h"
29#include "HeadersGPS.h"
30#include "HeadersMCP.h"
31#include "HeadersLid.h"
32#include "HeadersDrive.h"
33#include "HeadersPower.h"
34#include "HeadersAgilent.h"
35#include "HeadersFeedback.h"
36#include "HeadersRateScan.h"
37#include "HeadersRateControl.h"
38#include "HeadersTNGWeather.h"
39#include "HeadersMagicLidar.h"
40#include "HeadersMagicWeather.h"
41#include "HeadersTemperature.h"
42
43#include <boost/filesystem.hpp>
44
45using namespace std;
46
47// ------------------------------------------------------------------------
48
49#include "DimDescriptionService.h"
50#include "DimState.h"
51
52// ------------------------------------------------------------------------
53/*
54template<class T>
55 class buffer : public deque<T>
56 {
57 int32_t max_size;
58
59 public:
60 buffer(int32_t max=-1) : max_size(max) { }
61 const T &operator=(const T &t) const { push_back(t); if (max_size>0 && deque<T>::size()>max_size) deque<T>::pop_front(); }
62 operator T() const { return deque<T>::size()>0 ? deque<T>::back() : T(); }
63 bool valid() const { return deque<T>::size()>0; }
64 };
65*/
66
67// ------------------------------------------------------------------------
68
69namespace HTML
70{
71 const static string kWhite = "#ffffff";
72 const static string kYellow = "#fffff0";
73 const static string kRed = "#fff8f0";
74 const static string kGreen = "#f0fff0";
75 const static string kBlue = "#f0f0ff";
76};
77
78// ========================================================================
79// ========================================================================
80// ========================================================================
81
82class Sun
83{
84public:
85 Time time;
86
87 Time fRiseDayTime;
88 Time fRiseCivil;
89 Time fRiseAstronomical;
90 Time fRiseDarkTime;
91
92 Time fSetDayTime;
93 Time fSetCivil;
94 Time fSetAstronomical;
95 Time fSetDarkTime;
96
97 int state;
98 string description;
99 string color;
100
101 bool isday;
102 bool visible;
103
104public:
105 Sun() : time(Time::none)
106 {
107 }
108
109 // Could be done more efficient: Only recalcuate if
110 // the current time exceeds at least on of the stored times
111 Sun(const Time &t) : time(t)
112 {
113#ifdef HAVE_LIBNOVA
114 // get Julian day from local time
115 const double JD = time.JD();
116
117 // Warning: return code of 1 means circumpolar and is not checked!
118 Nova::RstTime sun_day = Nova::GetSolarRst(JD-0.5);
119 Nova::RstTime sun_civil = Nova::GetSolarRst(JD-0.5, -6);
120 Nova::RstTime sun_astronomical = Nova::GetSolarRst(JD-0.5, -12);
121 Nova::RstTime sun_dark = Nova::GetSolarRst(JD-0.5, -18);
122
123 fSetDayTime = Time(sun_day.set);
124 fSetCivil = Time(sun_civil.set);
125 fSetAstronomical = Time(sun_astronomical.set);
126 fSetDarkTime = Time(sun_dark.set);
127
128 fRiseDayTime = Time(sun_day.rise);
129 fRiseCivil = Time(sun_civil.rise);
130 fRiseAstronomical = Time(sun_astronomical.rise);
131 fRiseDarkTime = Time(sun_dark.rise);
132
133 const bool is_day = JD>sun_day.rise;
134 const bool is_night = JD>sun_dark.set;
135
136 sun_day = Nova::GetSolarRst(JD+0.5);
137 sun_civil = Nova::GetSolarRst(JD+0.5, -6);
138 sun_astronomical = Nova::GetSolarRst(JD+0.5, -12);
139 sun_dark = Nova::GetSolarRst(JD+0.5, -18);
140
141 if (is_day)
142 {
143 fRiseDayTime = Time(sun_day.rise);
144 fRiseCivil = Time(sun_civil.rise);
145 fRiseAstronomical = Time(sun_astronomical.rise);
146 fRiseDarkTime = Time(sun_dark.rise);
147 }
148
149 if (is_night)
150 {
151 fSetDayTime = Time(sun_day.set);
152 fSetCivil = Time(sun_civil.set);
153 fSetAstronomical = Time(sun_astronomical.set);
154 fSetDarkTime = Time(sun_dark.set);
155 }
156
157 // case 0: midnight to sun-rise | !is_day && !is_night | rise/set
158 // case 1: sun-rise to sun-set | is_day && !is_night | set /rise
159 // case 2: sun-set to midnight | is_day && is_night | rise/set
160
161 isday = is_day^is_night;
162
163 state = isday ? 4 : 0;
164 if (time>fSetDayTime) state++;
165 if (time>fSetCivil) state++;
166 if (time>fSetAstronomical) state++;
167 if (time>fSetDarkTime) state++;
168
169 if (time>fRiseDarkTime) state++;
170 if (time>fRiseAstronomical) state++;
171 if (time>fRiseCivil) state++;
172 if (time>fRiseDayTime) state++;
173
174 string name[] =
175 {
176 "dark time",
177 "astron. twilight",
178 "civil twilight",
179 "sunrise",
180 "day time",
181 "sunset",
182 "civil twilight",
183 "astron. twilight",
184 "dark time"
185 };
186
187 description = name[state];
188
189 const string arr = isday ?
190 fSetDarkTime.MinutesTo(time)+"&darr;" :
191 fRiseDarkTime.MinutesTo(time)+"&uarr;";
192
193 description += " ["+arr+"]";
194
195 switch (state)
196 {
197 case 0: case 1: color = HTML::kGreen; break;
198 case 2: case 3: color = HTML::kYellow; break;
199 case 4: color = HTML::kRed; break;
200 case 5: case 6: color = HTML::kYellow; break;
201 case 7: case 8: color = HTML::kGreen; break;
202 }
203
204 visible = state>=3 && state<=5;
205#endif
206 }
207};
208
209class Moon
210{
211public:
212 Time time;
213
214 double ra;
215 double dec;
216
217 double zd;
218 double az;
219
220 double disk;
221
222 bool visible;
223
224 Time fRise;
225 Time fTransit;
226 Time fSet;
227
228 string description;
229 string color;
230
231 int state;
232
233 Moon() : time(Time::none)
234 {
235 }
236
237 // Could be done more efficient: Only recalcuate if
238 // the current time exceeds at least on of the stored times
239 Moon(const Time &t) : time(t)
240 {
241#ifdef HAVE_LIBNOVA
242 const double JD = time.JD();
243
244 Nova::RstTime moon = Nova::GetLunarRst(JD-0.5);
245
246 fRise = Time(moon.rise);
247 fTransit = Time(moon.transit);
248 fSet = Time(moon.set);
249
250 //visible =
251 // ((JD>moon.rise && JD<moon.set ) && moon.rise<moon.set) ||
252 // ((JD<moon.set || JD>moon.rise) && moon.rise>moon.set);
253
254 const bool is_up = JD>moon.rise;
255 const bool is_sinking = JD>moon.transit;
256 const bool is_dn = JD>moon.set;
257
258 moon = Nova::GetLunarRst(JD+0.5);
259 if (is_up)
260 fRise = Time(moon.rise);
261 if (is_sinking)
262 fTransit = Time(moon.transit);
263 if (is_dn)
264 fSet = Time(moon.set);
265
266 const Nova::EquPosn pos = Nova::GetLunarEquCoords(JD);
267 const Nova::ZdAzPosn hrz = Nova::GetHrzFromEqu(pos, JD);
268
269 az = hrz.az;
270 zd = hrz.zd;
271
272 ra = pos.ra/15;
273 dec = pos.dec;
274
275 disk = Nova::GetLunarDisk(JD)*100;
276 state = 0;
277 if (fRise <fTransit && fRise <fSet) state = 0; // not visible
278 if (fTransit<fSet && fTransit<fRise) state = 1; // before culm
279 if (fSet <fRise && fSet <fTransit) state = 2; // after culm
280
281 visible = state!=0;
282
283 // 0: not visible
284 // 1: visible before cul
285 // 2: visible after cul
286
287 if (!visible || disk<25)
288 color = HTML::kGreen;
289 else
290 color = disk>75 ? HTML::kRed : HTML::kYellow;
291
292 const string arr = fSet<fRise ?
293 fSet.MinutesTo(time) +"&darr;" :
294 fRise.MinutesTo(time)+"&uarr;";
295
296 ostringstream out;
297 out << setprecision(2);
298 out << (visible?"visible ":"") << (disk<0.1?0:disk) << "% [" << arr << "]";
299
300 description = out.str();
301#endif
302 }
303
304 double Angle(double r, double d) const
305 {
306 const double theta0 = M_PI/2-d*M_PI/180;
307 const double phi0 = r*M_PI/12;
308
309 const double theta1 = M_PI/2-dec*M_PI/180;
310 const double phi1 = ra*M_PI/12;
311
312 const double x0 = sin(theta0) * cos(phi0);
313 const double y0 = sin(theta0) * sin(phi0);
314 const double z0 = cos(theta0);
315
316 const double x1 = sin(theta1) * cos(phi1);
317 const double y1 = sin(theta1) * sin(phi1);
318 const double z1 = cos(theta1);
319
320 double arg = x0*x1 + y0*y1 + z0*z1;
321 if(arg > 1.0) arg = 1.0;
322 if(arg < -1.0) arg = -1.0;
323
324 return acos(arg) * 180/M_PI;
325 }
326
327 static string Color(double angle)
328 {
329 if (angle<10 || angle>150)
330 return HTML::kRed;
331 if (angle<20 || angle>140)
332 return HTML::kYellow;
333 return HTML::kGreen;
334 }
335};
336
337// ========================================================================
338// ========================================================================
339// ========================================================================
340
341class StateMachineSmartFACT : public StateMachineDim
342{
343public:
344 static bool fIsServer;
345
346private:
347 enum states_t
348 {
349 kStateDimNetworkNA = 1,
350 kStateRunning,
351 };
352
353 // ------------------------- History classes -----------------------
354
355 struct EventElement
356 {
357 Time time;
358 string msg;
359
360 EventElement(const Time &t, const string &s) : time(t), msg(s) { }
361 };
362
363 class EventHist : public list<EventElement>
364 {
365 const boost::posix_time::time_duration deltat; //boost::posix_time::pos_infin
366 const uint64_t max;
367
368 public:
369 EventHist(const boost::posix_time::time_duration &dt=boost::posix_time::hours(12), uint64_t mx=UINT64_MAX) : deltat(dt), max(mx) { }
370
371 void add(const string &s, const Time &t=Time())
372 {
373 while (!empty() && (front().time+deltat<t || size()>max))
374 pop_front();
375
376 emplace_back(t, s);
377 }
378
379 void clean()
380 {
381 for (auto it=begin(); it!=end();)
382 if (!it->time)
383 {
384 const auto is = it++;
385 erase(is);
386 }
387 }
388
389 string get() const
390 {
391 ostringstream out;
392
393 string last = "";
394 for (auto it=begin(); it!=end(); it++)
395 {
396 const string tm = it->time.GetAsStr("%H:%M:%S ");
397 out << (tm!=last?tm:"--:--:-- ") << it->msg << "<br/>";
398 last = tm;
399 }
400
401 return out.str();
402 }
403 string rget() const
404 {
405 ostringstream out;
406
407 for (auto it=rbegin(); it!=rend(); it++)
408 out << it->time.GetAsStr("%H:%M:%S ") << it->msg << "<br/>";
409
410 return out.str();
411 }
412 };
413
414 // ------------------------- Internal variables -----------------------
415
416 const Time fRunTime;
417
418 PixelMap fPixelMap;
419
420 string fDatabase;
421
422 Time fLastUpdate;
423 Time fLastAstroCalc;
424
425 string fPath;
426
427 // ----------------------------- Data storage -------------------------
428
429 EventHist fControlMessageHist;
430 EventHist fControlAlarmHist;
431 int32_t fControlScriptDepth;
432
433 int32_t fMcpConfigurationState; // For consistency
434 int64_t fMcpConfigurationMaxTime;
435 int64_t fMcpConfigurationMaxEvents;
436 string fMcpConfigurationName;
437 Time fMcpConfigurationRunStart;
438 EventHist fMcpConfigurationHist;
439 bool fLastRunFinishedWithZeroEvents;
440
441 enum weather_t { kWeatherBegin=0, kTemp = kWeatherBegin, kDew, kHum, kPress, kWind, kGusts, kDir, kWeatherEnd = kDir+1 };
442 deque<float> fMagicWeatherHist[kWeatherEnd];
443
444 deque<float> fTngWeatherDustHist;
445 Time fTngWeatherDustTime;
446
447 vector<float> fBiasControlVoltageVec;
448
449 float fBiasControlPowerTot;
450 float fBiasControlVoltageMed;
451 float fBiasControlCurrentMed;
452 float fBiasControlCurrentMax;
453
454 deque<float> fBiasControlCurrentHist;
455 deque<float> fFscControlTemperatureHist;
456
457 float fFscControlHumidityAvg;
458
459 deque<float> fTemperatureControlHist;
460
461 float fDriveControlPointingZd;
462 string fDriveControlPointingAz;
463 string fDriveControlSourceName;
464 float fDriveControlMoonDist;
465
466 deque<float> fDriveControlTrackingDevHist;
467
468 int64_t fFadControlNumEvents;
469 int64_t fFadControlStartRun;
470 int32_t fFadControlDrsStep;
471 vector<uint32_t> fFadControlDrsRuns;
472
473 deque<float> fFtmControlTriggerRateHist;
474 int32_t fFtmControlTriggerRateTooLow;
475 int fFtmControlState;
476
477 float fFtmPatchThresholdMed;
478 float fFtmBoardThresholdMed;
479
480 bool fFtmControlFtuOk;
481
482 deque<float> fRateControlThreshold;
483
484 uint64_t fRateScanDataId;
485 uint8_t fRateScanBoard;
486 deque<float> fRateScanDataHist[41];
487
488 set<string> fErrorList;
489 EventHist fErrorHist;
490 EventHist fChatHist;
491
492 uint64_t fFreeSpace;
493
494 Sun fSun;
495 Moon fMoon;
496
497 // --------------------------- File header ----------------------------
498
499 Time fAudioTime;
500 string fAudioName;
501
502 string Header(const Time &d)
503 {
504 ostringstream msg;
505 msg << d.JavaDate() << '\t' << fAudioTime.JavaDate() << '\t' << fAudioName;
506 return msg.str();
507 }
508
509 string Header(const EventImp &d)
510 {
511 return Header(d.GetTime());
512 }
513
514 void SetAudio(const string &name)
515 {
516 fAudioName = name;
517 fAudioTime = Time();
518 }
519
520 // ------------- Initialize variables before the Dim stuff ------------
521
522 DimVersion fDimDNS;
523 DimControl fDimControl;
524 DimDescribedState fDimMcp;
525 DimDescribedState fDimDataLogger;
526 DimDescribedState fDimDriveControl;
527 DimDescribedState fDimTimeCheck;
528 DimDescribedState fDimMagicWeather;
529 DimDescribedState fDimMagicLidar;
530 DimDescribedState fDimTngWeather;
531 DimDescribedState fDimTemperature;
532 DimDescribedState fDimFeedback;
533 DimDescribedState fDimBiasControl;
534 DimDescribedState fDimFtmControl;
535 DimDescribedState fDimFadControl;
536 DimDescribedState fDimFscControl;
537 DimDescribedState fDimGpsControl;
538 DimDescribedState fDimAgilentControl;
539 DimDescribedState fDimPwrControl;
540 DimDescribedState fDimLidControl;
541 DimDescribedState fDimRateControl;
542 DimDescribedState fDimRateScan;
543 DimDescribedState fDimChat;
544 DimDescribedState fDimSkypeClient;
545
546 // -------------------------------------------------------------------
547
548 string GetDir(const double angle)
549 {
550 static const char *dir[] =
551 {
552 "N", "NNE", "NE", "ENE",
553 "E", "ESE", "SE", "SSE",
554 "S", "SSW", "SW", "WSW",
555 "W", "WNW", "NW", "NNW"
556 };
557
558 const uint16_t idx = uint16_t(floor(angle/22.5+16.5))%16;
559 return dir[idx];
560 }
561
562 // -------------------------------------------------------------------
563
564 bool CheckDataSize(const EventImp &d, const char *name, size_t size, bool min=false)
565 {
566 if (d.GetSize()==0)
567 return false;
568
569 if ((!min && d.GetSize()==size) || (min && d.GetSize()>size))
570 return true;
571
572 ostringstream msg;
573 msg << name << " - Received service has " << d.GetSize() << " bytes, but expected ";
574 if (min)
575 msg << "more than ";
576 msg << size << ".";
577 Warn(msg);
578 return false;
579 }
580
581 // -------------------------------------------------------------------
582
583 template<class T>
584 void WriteBinaryVec(const Time &tm, const string &fname, const vector<T> &vec, double scale, double offset=0, const string &title="", const string &col="")
585 {
586 if (vec.empty())
587 return;
588
589 ostringstream out;
590 out << tm.JavaDate() << '\n';
591 out << offset << '\n';
592 out << offset+scale << '\n';
593 out << setprecision(3);
594 if (!title.empty())
595 out << title << '\x7f';
596 else
597 {
598 const Statistics stat(vec[0]);
599 out << stat.min << '\n';
600 out << stat.med << '\n';
601 out << stat.max << '\x7f';
602 }
603 if (!col.empty())
604 out << col;
605 for (auto it=vec.cbegin(); it!=vec.cend(); it++)
606 {
607 // The valid range is from 1 to 127
608 // \0 is used to seperate different curves
609 vector<uint8_t> val(it->size());
610 for (uint64_t i=0; i<it->size(); i++)
611 {
612 float range = nearbyint(126*(double(it->at(i))-offset)/scale); // [-2V; 2V]
613 if (range>126)
614 range=126;
615 if (range<0)
616 range=0;
617 val[i] = (uint8_t)range;
618 }
619
620 const char *ptr = reinterpret_cast<char*>(val.data());
621 out.write(ptr, val.size()*sizeof(uint8_t));
622 out << '\x7f';
623 }
624
625 ofstream(fPath+"/"+fname+".bin") << out.str();
626 }
627 /*
628 template<class T>
629 void WriteBinaryVec(const EventImp &d, const string &fname, const vector<T> &vec, double scale, double offset=0, const string &title="")
630 {
631 WriteBinaryVec(d.GetTime(), fname, vec, scale, offset, title);
632 }
633
634 template<class T>
635 void WriteBinary(const Time &tm, const string &fname, const T &t, double scale, double offset=0)
636 {
637 WriteBinaryVec(tm, fname, vector<T>(&t, &t+1), scale, offset);
638 }
639
640 template<class T>
641 void WriteBinary(const EventImp &d, const string &fname, const T &t, double scale, double offset=0)
642 {
643 WriteBinaryVec(d.GetTime(), fname, vector<T>(&t, &t+1), scale, offset);
644 }*/
645
646 template<class T>
647 void WriteHist(const EventImp &d, const string &fname, const T &t, double scale, double offset=0)
648 {
649 WriteBinaryVec(d.GetTime(), fname, vector<T>(&t, &t+1), scale, offset, "", "000");
650 }
651
652 template<class T>
653 void WriteCam(const EventImp &d, const string &fname, const T &t, double scale, double offset=0)
654 {
655 WriteBinaryVec(d.GetTime(), fname, vector<T>(&t, &t+1), scale, offset, "", "");
656 }
657
658
659 // -------------------------------------------------------------------
660
661 struct Statistics
662 {
663 float min;
664 float max;
665 float med;
666 float avg;
667 //float rms;
668
669 template<class T>
670 Statistics(const T &t, size_t offset_min=0, size_t offset_max=0)
671 : min(0), max(0), med(0), avg(0)
672 {
673 if (t.empty())
674 return;
675
676 T copy(t);
677 sort(copy.begin(), copy.end());
678
679 if (offset_min>t.size())
680 offset_min = 0;
681 if (offset_max>t.size())
682 offset_max = 0;
683
684 min = copy[offset_min];
685 max = copy[copy.size()-1-offset_max];
686 avg = accumulate (t.begin(), t.end(), 0.)/t.size();
687
688 const size_t p = copy.size()/2;
689 med = copy.size()%2 ? copy[p] : (copy[p-1]+copy[p])/2.;
690 }
691 };
692
693 void HandleControlMessageImp(const EventImp &d)
694 {
695 if (d.GetSize()==0)
696 return;
697
698 fControlMessageHist.add(d.GetText(), d.GetTime());
699
700 ostringstream out;
701 out << setprecision(3);
702 out << Header(d) << '\n';
703 out << HTML::kWhite << '\t';
704 out << "<->" << fControlMessageHist.get() << "</->";
705 out << '\n';
706
707 ofstream(fPath+"/scriptlog.data") << out.str();
708 }
709
710 int HandleDimControlMessage(const EventImp &d)
711 {
712 if (d.GetSize()==0)
713 return GetCurrentState();
714
715 if (d.GetQoS()==MessageImp::kAlarm)
716 {
717 if (d.GetSize()<2)
718 for (auto it=fControlAlarmHist.begin(); it!=fControlAlarmHist.end(); it++)
719 it->time = Time(Time::none);
720 else
721 fControlAlarmHist.add(d.GetText(), d.GetTime());
722 }
723
724 if (d.GetQoS()==MessageImp::kComment && d.GetSize()>1)
725 HandleControlMessageImp(d);
726
727 return GetCurrentState();
728 }
729
730 int HandleControlStateChange(const EventImp &d)
731 {
732 if (d.GetSize()==0)
733 return StateMachineImp::kSM_KeepState;
734
735 if (fDimControl.scriptdepth>0)
736 return StateMachineImp::kSM_KeepState;
737
738 if (d.GetQoS()>=2)
739 return StateMachineImp::kSM_KeepState;
740
741#if BOOST_VERSION < 104600
742 const string file = boost::filesystem::path(fDimControl.file).filename();
743#else
744 const string file = boost::filesystem::path(fDimControl.file).filename().string();
745#endif
746
747 // [0] DimControl::kIdle
748 // [1] DimControl::kLoading
749 // [2] DimControl::kCompiling
750 // [3] DimControl::kRunning
751 if (d.GetQoS()==1)
752 {
753 fControlMessageHist.clear();
754 HandleControlMessageImp(Event(d, "========================================", 41));
755 }
756
757 HandleControlMessageImp(Event(d, ("----- "+fDimControl.shortmsg+" -----").data(), fDimControl.shortmsg.length()+13));
758 if (!file.empty() && d.GetQoS()<2)
759 HandleControlMessageImp(Event(d, file.data(), file.length()+1));
760
761 // Note that this will also "ding" just after program startup
762 // if the dimctrl is still in state -3
763 if (d.GetQoS()==0)
764 {
765 HandleControlMessageImp(Event(d, "========================================", 41));
766 if (fDimControl.last.second!=DimState::kOffline)
767 SetAudio("ding");
768 }
769
770 return StateMachineImp::kSM_KeepState;
771 }
772
773 void AddMcpConfigurationHist(const EventImp &d, const string &msg)
774 {
775 fMcpConfigurationHist.add(msg, d.GetTime());
776
777 ostringstream out;
778 out << d.GetJavaDate() << '\n';
779 out << HTML::kWhite << '\t';
780 out << "<->" << fMcpConfigurationHist.rget() << "</->";
781 out << '\n';
782
783 ofstream(fPath+"/observations.data") << out.str();
784 }
785
786 int HandleFscControlStateChange(const EventImp &d)
787 {
788 const int32_t &last = fDimFscControl.last.second;
789 const int32_t &state = fDimFscControl.state();
790
791 if (last==DimState::kOffline || state==DimState::kOffline)
792 return StateMachineImp::kSM_KeepState;
793
794 if (last<FSC::State::kConnected && state==FSC::State::kConnected)
795 {
796 AddMcpConfigurationHist(d, "<B>Camera swiched on</B>");
797 SetAudio("startup");
798 }
799
800 if (last==FSC::State::kConnected && state<FSC::State::kConnected)
801 {
802 AddMcpConfigurationHist(d, "<B>Camera swiched off</B>");
803 SetAudio("shutdown");
804 }
805
806 return StateMachineImp::kSM_KeepState;
807 }
808
809 int HandleMcpConfiguration(const EventImp &d)
810 {
811 if (!CheckDataSize(d, "Mcp:Configuration", 16, true))
812 {
813 fMcpConfigurationState = DimState::kOffline;
814 fMcpConfigurationMaxTime = 0;
815 fMcpConfigurationMaxEvents = 0;
816 fMcpConfigurationName = "";
817 fMcpConfigurationRunStart = Time(Time::none);
818 return GetCurrentState();
819 }
820
821 // If a run ends...
822 if (fMcpConfigurationState==MCP::State::kTakingData && d.GetQoS()==MCP::State::kIdle)
823 {
824 // ...and no script is running just play a simple 'tick'
825 // ...and a script is running just play a simple 'tick'
826 if (/*fDimControl.state()<-2 &&*/ fDimControl.scriptdepth==0)
827 SetAudio("dong");
828 else
829 SetAudio("losticks");
830
831 fLastRunFinishedWithZeroEvents = fFadControlNumEvents==0;
832
833 ostringstream out;
834 out << "<#darkred>" << d.Ptr<char>(16);
835 if (!fDriveControlSourceName.empty())
836 out << " [" << fDriveControlSourceName << ']';
837 out << " (N=" << fFadControlNumEvents << ')';
838 out << "</#>";
839
840 AddMcpConfigurationHist(d, out.str());
841 }
842
843 if (d.GetQoS()==MCP::State::kTakingData)
844 {
845 fMcpConfigurationRunStart = Time();
846 SetAudio("losticks");
847
848 ostringstream out;
849 out << "<#darkgreen>" << fMcpConfigurationName;
850 if (!fDriveControlSourceName.empty())
851 out << " [" << fDriveControlSourceName << ']';
852 if (fFadControlStartRun>0)
853 out << " (Run " << fFadControlStartRun << ')';
854 out << "</#>";
855
856 AddMcpConfigurationHist(d, out.str());
857 }
858
859 fMcpConfigurationState = d.GetQoS();
860 fMcpConfigurationMaxTime = d.Get<uint64_t>();
861 fMcpConfigurationMaxEvents = d.Get<uint64_t>(8);
862 fMcpConfigurationName = d.Ptr<char>(16);
863
864 return GetCurrentState();
865 }
866
867 void WriteWeather(const EventImp &d, const string &name, int i, float min, float max)
868 {
869 const Statistics stat(fMagicWeatherHist[i]);
870
871 ostringstream out;
872 out << setprecision(3);
873 out << d.GetJavaDate() << '\n';
874
875 out << HTML::kWhite << '\t' << fMagicWeatherHist[i].back() << '\n';
876 out << HTML::kWhite << '\t' << stat.min << '\n';
877 out << HTML::kWhite << '\t' << stat.avg << '\n';
878 out << HTML::kWhite << '\t' << stat.max << '\n';
879
880 ofstream(fPath+"/"+name+".data") << out.str();
881
882 WriteHist(d, "hist-magicweather-"+name, fMagicWeatherHist[i], max-min, min);
883 }
884
885 int HandleMagicWeatherData(const EventImp &d)
886 {
887 if (!CheckDataSize(d, "MagicWeather:Data", 7*4+2))
888 return GetCurrentState();
889
890 // Store a history of the last 300 entries
891 for (int i=kWeatherBegin; i<kWeatherEnd; i++)
892 {
893 fMagicWeatherHist[i].push_back(d.Ptr<float>(2)[i]);
894 if (fMagicWeatherHist[i].size()>300)
895 fMagicWeatherHist[i].pop_front();
896 }
897
898 ostringstream out;
899 out << d.GetJavaDate() << '\n';
900 if (fSun.time.IsValid() && fMoon.time.IsValid())
901 {
902 out << fSun.color << '\t' << fSun.description << '\n';
903 out << setprecision(2);
904 out << (fSun.isday?HTML::kWhite:fMoon.color) << '\t' << fMoon.description << '\n';
905 }
906 else
907 out << "\n\n";
908 out << setprecision(3);
909 for (int i=0; i<6; i++)
910 out << HTML::kWhite << '\t' << fMagicWeatherHist[i].back() << '\n';
911 out << HTML::kWhite << '\t' << GetDir(fMagicWeatherHist[kDir].back()) << '\n';
912 out << HTML::kWhite << '\t';
913 if (!fTngWeatherDustHist.empty())
914 out << fTngWeatherDustHist.back() << '\t' << fTngWeatherDustTime.GetAsStr("%H:%M") << '\n';
915 else
916 out << "\t\n";
917
918 ofstream(fPath+"/weather.data") << out.str();
919
920 WriteWeather(d, "temp", kTemp, -5, 35);
921 WriteWeather(d, "dew", kDew, -5, 35);
922 WriteWeather(d, "hum", kHum, 0, 100);
923 WriteWeather(d, "wind", kWind, 0, 100);
924 WriteWeather(d, "gusts", kGusts, 0, 100);
925 WriteWeather(d, "press", kPress, 700, 1000);
926
927 return GetCurrentState();
928 }
929
930 int HandleTngWeatherDust(const EventImp &d)
931 {
932 if (!CheckDataSize(d, "TngWeather:Dust", 4))
933 return GetCurrentState();
934
935 fTngWeatherDustTime = d.GetTime();
936
937 fTngWeatherDustHist.push_back(d.GetFloat());
938 if (fTngWeatherDustHist.size()>300)
939 fTngWeatherDustHist.pop_front();
940
941 const Statistics stat(fTngWeatherDustHist);
942
943 const double scale = stat.max>0 ? pow(10, ceil(log10(stat.max))) : 0;
944
945 WriteHist(d, "hist-tng-dust", fTngWeatherDustHist, scale);
946
947 ostringstream out;
948 out << d.GetJavaDate() << '\n';
949
950 ofstream(fPath+"/tngdust.data") << out.str();
951
952 return GetCurrentState();
953 }
954
955 int HandleDriveControlStateChange(const EventImp &d)
956 {
957 const int32_t &last = fDimFscControl.last.second;
958 const int32_t &state = fDimFscControl.state();
959
960 if (last==DimState::kOffline || state==DimState::kOffline)
961 return StateMachineImp::kSM_KeepState;
962
963 if (last<Drive::State::kArmed && state>=Drive::State::kArmed)
964 AddMcpConfigurationHist(d, "Drive connected");
965
966 if (last>=Drive::State::kArmed && state<Drive::State::kArmed)
967 AddMcpConfigurationHist(d, "Drive disconnected");
968
969 return StateMachineImp::kSM_KeepState;
970 }
971
972 int HandleDrivePointing(const EventImp &d)
973 {
974 if (!CheckDataSize(d, "DriveControl:Pointing", 16))
975 return GetCurrentState();
976
977 fDriveControlPointingZd = d.Get<double>();
978
979 const double az = d.Get<double>(8);
980
981 fDriveControlPointingAz = GetDir(az);
982
983 ostringstream out;
984 out << d.GetJavaDate() << '\n';
985
986 out << setprecision(0) << fixed;
987 out << HTML::kWhite << '\t' << az << '\t' << fDriveControlPointingAz << '\n';
988 out << HTML::kWhite << '\t' << fDriveControlPointingZd << '\n';
989
990 ofstream(fPath+"/pointing.data") << out.str();
991
992 return GetCurrentState();
993 }
994
995 int HandleDriveTracking(const EventImp &d)
996 {
997 if (!CheckDataSize(d, "DriveControl:Tracking", 64))
998 return GetCurrentState();
999
1000 const double Ra = d.Get<double>(0*8);
1001 const double Dec = d.Get<double>(1*8);
1002 const double Zd = d.Get<double>(3*8);
1003 const double Az = d.Get<double>(4*8);
1004
1005 const double dev = d.Get<double>(7*8);
1006
1007 fDriveControlTrackingDevHist.push_back(dev);
1008 if (fDriveControlTrackingDevHist.size()>300)
1009 fDriveControlTrackingDevHist.pop_front();
1010
1011 WriteHist(d, "hist-control-deviation", fDriveControlTrackingDevHist, 120);
1012
1013 ostringstream out;
1014 out << d.GetJavaDate() << '\n';
1015
1016 out << HTML::kWhite << '\t' << fDriveControlSourceName << '\n';
1017 out << setprecision(5);
1018 out << HTML::kWhite << '\t' << Ra << '\n';
1019 out << HTML::kWhite << '\t' << Dec << '\n';
1020 out << setprecision(3);
1021 out << HTML::kWhite << '\t' << Zd << '\n';
1022 out << HTML::kWhite << '\t' << Az << '\n';
1023 out << HTML::kWhite << '\t' << dev << '\n';
1024
1025 fDriveControlMoonDist = -1;
1026
1027 if (fMoon.visible)
1028 {
1029 const double angle = fMoon.Angle(Ra, Dec);
1030 out << Moon::Color(angle) << '\t' << setprecision(3) << angle << '\n';
1031
1032 fDriveControlMoonDist = angle;
1033 }
1034 else
1035 out << HTML::kWhite << "\t&mdash; \n";
1036
1037 ofstream(fPath+"/tracking.data") << out.str();
1038
1039 return GetCurrentState();
1040 }
1041
1042 int HandleDriveSource(const EventImp &d)
1043 {
1044 if (!CheckDataSize(d, "DriveControl:Source", 7*4+2, true))
1045 return GetCurrentState();
1046
1047 const double *ptr = d.Ptr<double>();
1048
1049 const double ra = ptr[0]; // Ra[h]
1050 const double dec = ptr[1]; // Dec[deg]
1051 const double woff = ptr[4]; // Wobble offset [deg]
1052 const double wang = ptr[5]; // Wobble angle [deg]
1053
1054 fDriveControlSourceName = d.Ptr<char>(6*8);
1055
1056 ostringstream out;
1057 out << d.GetJavaDate() << '\n';
1058
1059 out << HTML::kWhite << '\t' << fDriveControlSourceName << '\n';
1060 out << setprecision(5);
1061 out << HTML::kWhite << '\t' << ra << '\n';
1062 out << HTML::kWhite << '\t' << dec << '\n';
1063 out << setprecision(3);
1064 out << HTML::kWhite << '\t' << woff << '\n';
1065 out << HTML::kWhite << '\t' << wang << '\n';
1066
1067 ofstream(fPath+"/source.data") << out.str();
1068
1069 return GetCurrentState();
1070 }
1071
1072 int HandleFeedbackCalibratedCurrents(const EventImp &d)
1073 {
1074 if (!CheckDataSize(d, "Feedback:CalibratedCurrents", (416+1+1+1+1+1+416+1+1)*sizeof(float)+sizeof(uint32_t)))
1075 return GetCurrentState();
1076
1077 const float *ptr = d.Ptr<float>();
1078
1079 double power_tot = 0;
1080 double power_apd = 0;
1081
1082 if (fBiasControlVoltageVec.size()>0)
1083 {
1084 // Calibrate the data (subtract offset)
1085 for (int i=0; i<320; i++)
1086 {
1087 // Group index (0 or 1) of the of the pixel (4 or 5 pixel patch)
1088 const int N = fPixelMap.hv(i).count();
1089
1090 // Serial resistor of the individual G-APDs
1091 double R5 = 3900/N;
1092
1093 // This is also valid for the patches with wrong resistors,
1094 // because Iapd is a factor f larger but R a factor f smaller
1095 double Iapd = ptr[i] * 1e-6; // [A]
1096 double Iout = Iapd*N; // [A]
1097
1098 double UdrpCam = 1000 *Iout; // Voltage seen by everything in Camera
1099 double UdrpApd = (R5+2000)*Iout; // Voltage seen by G-APD
1100
1101 const double pwrCam = Iapd * (fBiasControlVoltageVec[i]-UdrpCam);
1102 const double pwrApd = Iapd * (fBiasControlVoltageVec[i]-UdrpApd);
1103
1104 // Total power participated in the camera at the G-APD
1105 // and the serial resistors (total voltage minus voltage
1106 // drop at resistors in bias crate)
1107 power_tot += pwrCam;
1108
1109 // Power consumption per G-APD
1110 power_apd += pwrApd;
1111 }
1112 }
1113
1114 // Divide by number of summed channels, convert to mW
1115 power_apd /= 320e-3; // [mW]
1116
1117 if (power_tot<1e-3)
1118 power_tot = 0;
1119 if (power_apd<1e-3)
1120 power_apd = 0;
1121
1122 fBiasControlPowerTot = power_tot;
1123
1124 // --------------------------------------------------------
1125
1126 // Get the maximum of each patch
1127 vector<float> val(320, 0);
1128 for (int i=0; i<320; i++)
1129 {
1130 const int idx = (fPixelMap.hv(i).hw()/9)*2+fPixelMap.hv(i).group();
1131 val[idx] = ptr[i];
1132 }
1133
1134 // Write the 160 patch values to a file
1135 WriteCam(d, "cam-biascontrol-current", val, 100);
1136
1137 // --------------------------------------------------------
1138
1139 const Statistics stat(vector<float>(ptr, ptr+320));
1140
1141 // Exclude the three crazy channels
1142 fBiasControlCurrentMed = stat.med;
1143 fBiasControlCurrentMax = stat.max;
1144
1145 // Store a history of the last 60 entries
1146 fBiasControlCurrentHist.push_back(fBiasControlCurrentMed);
1147 if (fBiasControlCurrentHist.size()>360)
1148 fBiasControlCurrentHist.pop_front();
1149
1150 // write the history to a file
1151 WriteHist(d, "hist-biascontrol-current", fBiasControlCurrentHist, 125);
1152
1153 // --------------------------------------------------------
1154
1155 string col1 = HTML::kGreen;
1156 string col2 = HTML::kGreen;
1157 string col3 = HTML::kGreen;
1158 string col4 = HTML::kGreen;
1159
1160 if (stat.min>90)
1161 col1 = HTML::kYellow;
1162 if (stat.min>110)
1163 col1 = HTML::kRed;
1164
1165 if (stat.med>90)
1166 col2 = HTML::kYellow;
1167 if (stat.med>110)
1168 col2 = HTML::kRed;
1169
1170 if (stat.avg>90)
1171 col3 = HTML::kYellow;
1172 if (stat.avg>110)
1173 col3 = HTML::kRed;
1174
1175 if (stat.max>90)
1176 col4 = HTML::kYellow;
1177 if (stat.max>110)
1178 col4 = HTML::kRed;
1179
1180 ostringstream out;
1181 out << setprecision(3);
1182 out << d.GetJavaDate() << '\n';
1183 out << HTML::kGreen << '\t' << "yes" << '\n';
1184 out << col1 << '\t' << stat.min << '\n';
1185 out << col2 << '\t' << stat.med << '\n';
1186 out << col3 << '\t' << stat.avg << '\n';
1187 out << col4 << '\t' << stat.max << '\n';
1188 out << HTML::kWhite << '\t' << power_tot << "W [" << power_apd << "mW]\n";
1189 ofstream(fPath+"/current.data") << out.str();
1190
1191 // --------------------------------------------------------
1192
1193 const float Unom = ptr[2*416+6];
1194 const float Utmp = ptr[2*416+7];
1195
1196 vector<float> Uov(ptr+416+6, ptr+416+6+320);
1197
1198 WriteCam(d, "cam-feedback-overvoltage", Uov, 0.2, 1.0);
1199
1200 const Statistics stat2(Uov);
1201
1202 out.str("");
1203 out << d.GetJavaDate() << '\n';
1204 out << setprecision(3);
1205 out << HTML::kWhite << '\t' << Utmp << '\n';
1206 out << HTML::kWhite << '\t' << Unom << '\n';
1207 out << HTML::kWhite << '\t' << stat2.min << '\n';
1208 out << HTML::kWhite << '\t' << stat2.med << '\n';
1209 out << HTML::kWhite << '\t' << stat2.avg << '\n';
1210 out << HTML::kWhite << '\t' << stat2.max << '\n';
1211 ofstream(fPath+"/feedback.data") << out.str();
1212
1213 return GetCurrentState();
1214 }
1215
1216 int HandleBiasCurrent(const EventImp &d)
1217 {
1218 if (fDimFeedback.state()>=Feedback::State::kCalibrated)
1219 return GetCurrentState();
1220
1221 if (!CheckDataSize(d, "BiasControl:Current", 832))
1222 return GetCurrentState();
1223
1224 // Convert dac counts to uA
1225 vector<float> v(320);
1226 for (int i=0; i<320; i++)
1227 v[i] = d.Ptr<uint16_t>()[i] * 5000./4096;
1228
1229 fBiasControlPowerTot = 0;
1230
1231 // Get the maximum of each patch
1232 vector<float> val(320, 0);
1233 for (int i=0; i<320; i++)
1234 {
1235 const PixelMapEntry &hv = fPixelMap.hv(i);
1236 if (!hv)
1237 continue;
1238
1239 const int idx = (hv.hw()/9)*2+hv.group();
1240 val[idx] = v[i];
1241 }
1242
1243 // Write the 160 patch values to a file
1244 WriteCam(d, "cam-biascontrol-current", val, 1000);
1245
1246 const Statistics stat(v, 0, 3);
1247
1248 // Exclude the three crazy channels
1249 fBiasControlCurrentMed = stat.med;
1250 fBiasControlCurrentMax = stat.max;
1251
1252 // Store a history of the last 60 entries
1253 fBiasControlCurrentHist.push_back(fBiasControlCurrentMed);
1254 if (fBiasControlCurrentHist.size()>360)
1255 fBiasControlCurrentHist.pop_front();
1256
1257 // write the history to a file
1258 WriteHist(d, "hist-biascontrol-current", fBiasControlCurrentHist, 1000);
1259
1260 ostringstream out;
1261 out << setprecision(3);
1262 out << d.GetJavaDate() << '\n';
1263 out << HTML::kWhite<< '\t' << "no" << '\n';
1264 out << HTML::kWhite << '\t' << stat.min << '\n';
1265 out << HTML::kWhite << '\t' << stat.med << '\n';
1266 out << HTML::kWhite << '\t' << stat.avg << '\n';
1267 out << HTML::kWhite << '\t' << stat.max << '\n';
1268 out << HTML::kWhite << '\t' << "---\n";
1269 ofstream(fPath+"/current.data") << out.str();
1270
1271 return GetCurrentState();
1272 }
1273
1274 int HandleBiasVoltage(const EventImp &d)
1275 {
1276 if (!CheckDataSize(d, "BiasControl:Voltage", 1664))
1277 {
1278 fBiasControlVoltageVec.clear();
1279 return GetCurrentState();
1280 }
1281
1282 fBiasControlVoltageVec.assign(d.Ptr<float>(), d.Ptr<float>()+320);
1283
1284 const Statistics stat(fBiasControlVoltageVec);
1285
1286 fBiasControlVoltageMed = stat.med;
1287
1288 vector<float> val(320, 0);
1289 for (int i=0; i<320; i++)
1290 {
1291 const int idx = (fPixelMap.hv(i).hw()/9)*2+fPixelMap.hv(i).group();
1292 val[idx] = fBiasControlVoltageVec[i];
1293 }
1294
1295 if (fDimBiasControl.state()==BIAS::State::kVoltageOn || fDimBiasControl.state()==BIAS::State::kRamping)
1296 WriteCam(d, "cam-biascontrol-voltage", val, 10, 65);
1297 else
1298 WriteCam(d, "cam-biascontrol-voltage", val, 75);
1299
1300 ostringstream out;
1301 out << setprecision(3);
1302 out << d.GetJavaDate() << '\n';
1303 out << HTML::kWhite << '\t' << stat.min << '\n';
1304 out << HTML::kWhite << '\t' << stat.med << '\n';
1305 out << HTML::kWhite << '\t' << stat.avg << '\n';
1306 out << HTML::kWhite << '\t' << stat.max << '\n';
1307 ofstream(fPath+"/voltage.data") << out.str();
1308
1309 return GetCurrentState();
1310 }
1311
1312 int HandleFadEvents(const EventImp &d)
1313 {
1314 if (!CheckDataSize(d, "FadControl:Events", 4*4))
1315 {
1316 fFadControlNumEvents = -1;
1317 return GetCurrentState();
1318 }
1319
1320 fFadControlNumEvents = d.Get<uint32_t>();
1321
1322 return GetCurrentState();
1323 }
1324
1325 int HandleFadStartRun(const EventImp &d)
1326 {
1327 if (!CheckDataSize(d, "FadControl:StartRun", 16))
1328 {
1329 fFadControlStartRun = -1;
1330 return GetCurrentState();
1331 }
1332
1333 fFadControlStartRun = d.Get<int64_t>();
1334
1335 return GetCurrentState();
1336 }
1337
1338 int HandleFadDrsRuns(const EventImp &d)
1339 {
1340 if (!CheckDataSize(d, "FadControl:DrsRuns", 5*4))
1341 {
1342 fFadControlDrsStep = -1;
1343 return GetCurrentState();
1344 }
1345
1346 const uint32_t *ptr = d.Ptr<uint32_t>();
1347 fFadControlDrsStep = ptr[0];
1348 fFadControlDrsRuns[0] = ptr[1];
1349 fFadControlDrsRuns[1] = ptr[2];
1350 fFadControlDrsRuns[2] = ptr[3];
1351
1352 return GetCurrentState();
1353 }
1354
1355 int HandleFadConnections(const EventImp &d)
1356 {
1357 if (!CheckDataSize(d, "FadControl:Connections", 41))
1358 {
1359 //fStatusEventBuilderLabel->setText("Offline");
1360 return GetCurrentState();
1361 }
1362
1363 string rc(40, '-'); // orange/red [45]
1364
1365 const uint8_t *ptr = d.Ptr<uint8_t>();
1366
1367 int c[4] = { '.', '.', '.', '.' };
1368
1369 for (int i=0; i<40; i++)
1370 {
1371 const uint8_t stat1 = ptr[i]&3;
1372 const uint8_t stat2 = ptr[i]>>3;
1373
1374 if (stat1==0 && stat2==0)
1375 rc[i] = '.'; // gray [46]
1376 else
1377 if (stat1>=2 && stat2==8)
1378 rc[i] = stat1==2?'+':'*'; // green [43] : check [42]
1379
1380 if (rc[i]<c[i/10])
1381 c[i/10] = rc[i];
1382 }
1383
1384 string col[4];
1385 for (int i=0; i<4; i++)
1386 switch (c[i])
1387 {
1388 case '.': col[i]=HTML::kWhite; break;
1389 case '-': col[i]=HTML::kRed; break;
1390 case '+': col[i]=HTML::kYellow; break;
1391 case '*': col[i]=HTML::kGreen; break;
1392 }
1393
1394 ostringstream out;
1395 out << setprecision(3);
1396 out << d.GetJavaDate() << '\n';
1397 out << col[0] << '\t' << rc.substr( 0, 10) << '\n';
1398 out << col[1] << '\t' << rc.substr(10, 10) << '\n';
1399 out << col[2] << '\t' << rc.substr(20, 10) << '\n';
1400 out << col[3] << '\t' << rc.substr(30, 10) << '\n';
1401 ofstream(fPath+"/fad.data") << out.str();
1402
1403 return GetCurrentState();
1404 }
1405
1406 /*
1407 int HandleFtmControlStateChange()
1408 {
1409 const int32_t &last = fDimFtmControl.last.second;
1410 const int32_t &state = fDimFtmControl.state();
1411
1412 // If a new run has been started ensure that the counter
1413 // is reset. The reset in HandleFtmTriggerRates might
1414 // arrive only after the run was started.
1415 if (last!=FTM::State::kTriggerOn && state==MCP::State::kTriggerOn)
1416 fFtmControlTriggerRateTooLow = -1;
1417
1418 return StateMachineImp::kSM_KeepState;
1419 }*/
1420
1421
1422 int HandleFtmTriggerRates(const EventImp &d)
1423 {
1424 if (!CheckDataSize(d, "FtmControl:TriggerRates", 24+160+640+8))
1425 {
1426 fFtmControlTriggerRateTooLow = 0;
1427 return GetCurrentState();
1428 }
1429
1430 const FTM::DimTriggerRates &dim = d.Ref<FTM::DimTriggerRates>();
1431
1432 // If the trigger rate is too low...
1433 // ... and the run was not just started (can lead to very small elapsed times)
1434 // ... and the trigger is switched on
1435 // ... and there was no state change (then the trigger was started or stopped)
1436 fFtmControlTriggerRateTooLow =
1437 dim.fTriggerRate<1 && dim.fElapsedTime>0.45 &&
1438 (fFtmControlState&FTM::kFtmStates)==FTM::kFtmRunning &&
1439 (fFtmControlState&FTM::kFtmStates)==(d.GetQoS()&FTM::kFtmStates);
1440
1441 fFtmControlState = d.GetQoS();
1442
1443 const float *brates = dim.fBoardRate; // Board rate
1444 const float *prates = dim.fPatchRate; // Patch rate
1445
1446 // Store a history of the last 60 entries
1447 fFtmControlTriggerRateHist.push_back(dim.fTriggerRate);
1448 if (fFtmControlTriggerRateHist.size()>300)
1449 fFtmControlTriggerRateHist.pop_front();
1450
1451 // FIXME: Add statistics for all kind of rates
1452
1453 WriteHist(d, "hist-ftmcontrol-triggerrate",
1454 fFtmControlTriggerRateHist, 100);
1455 WriteCam(d, "cam-ftmcontrol-boardrates",
1456 vector<float>(brates, brates+40), 10);
1457 WriteCam(d, "cam-ftmcontrol-patchrates",
1458 vector<float>(prates, prates+160), 10);
1459
1460 ostringstream out;
1461 out << setprecision(3);
1462 out << d.GetJavaDate() << '\n';
1463 out << HTML::kWhite << '\t' << dim.fTriggerRate << '\n';
1464
1465 ofstream(fPath+"/trigger.data") << out.str();
1466
1467 const Statistics bstat(vector<float>(brates, brates+ 40));
1468 const Statistics pstat(vector<float>(prates, prates+160));
1469
1470 out.str("");
1471 out << d.GetJavaDate() << '\n';
1472 out << HTML::kWhite << '\t' << bstat.min << '\n';
1473 out << HTML::kWhite << '\t' << bstat.med << '\n';
1474 out << HTML::kWhite << '\t' << bstat.avg << '\n';
1475 out << HTML::kWhite << '\t' << bstat.max << '\n';
1476 ofstream(fPath+"/boardrates.data") << out.str();
1477
1478 out.str("");
1479 out << d.GetJavaDate() << '\n';
1480 out << HTML::kWhite << '\t' << pstat.min << '\n';
1481 out << HTML::kWhite << '\t' << pstat.med << '\n';
1482 out << HTML::kWhite << '\t' << pstat.avg << '\n';
1483 out << HTML::kWhite << '\t' << pstat.max << '\n';
1484 ofstream(fPath+"/patchrates.data") << out.str();
1485
1486 return GetCurrentState();
1487 }
1488
1489 int HandleFtmStaticData(const EventImp &d)
1490 {
1491 if (!CheckDataSize(d, "FtmControl:StaticData", sizeof(FTM::DimStaticData)))
1492 return GetCurrentState();
1493
1494 // If the FTM is in state Configuring, the clock conditioner
1495 // is always reported to be unlocked
1496 fFtmControlState = d.GetQoS();
1497
1498 const FTM::DimStaticData &dat = d.Ref<FTM::DimStaticData>();
1499
1500 vector<uint16_t> vecp(dat.fThreshold, dat.fThreshold+160);
1501 vector<uint16_t> vecb(dat.fMultiplicity, dat.fMultiplicity+40);
1502
1503 WriteCam(d, "cam-ftmcontrol-thresholds-patch", vecp, 1000);
1504 WriteCam(d, "cam-ftmcontrol-thresholds-board", vecb, 100);
1505
1506 const Statistics statp(vecp);
1507 const Statistics statb(vecb);
1508
1509 fFtmPatchThresholdMed = statp.med;
1510 fFtmBoardThresholdMed = statb.med;
1511
1512 ostringstream out;
1513 out << d.GetJavaDate() << '\n';
1514 out << HTML::kWhite << '\t' << statb.min << '\n';
1515 out << HTML::kWhite << '\t' << statb.med << '\n';
1516 out << HTML::kWhite << '\t' << statb.max << '\n';
1517 ofstream(fPath+"/thresholds-board.data") << out.str();
1518
1519 out.str("");
1520 out << d.GetJavaDate() << '\n';
1521 out << HTML::kWhite << '\t' << statp.min << '\n';
1522 out << HTML::kWhite << '\t' << statp.med << '\n';
1523 out << HTML::kWhite << '\t' << statp.max << '\n';
1524 ofstream(fPath+"/thresholds-patch.data") << out.str();
1525
1526 out.str("");
1527 out << d.GetJavaDate() << '\n';
1528 out << HTML::kWhite << '\t' << statb.med << '\n';
1529 out << HTML::kWhite << '\t' << statp.med << '\n';
1530 ofstream(fPath+"/thresholds.data") << out.str();
1531
1532 out.str("");
1533 out << d.GetJavaDate() << '\n';
1534 out << HTML::kWhite << '\t' << dat.fTriggerInterval << '\n';
1535 out << HTML::kWhite << '\t';
1536 if (dat.HasPedestal())
1537 out << dat.fTriggerSeqPed;
1538 else
1539 out << "&ndash;";
1540 out << ':';
1541 if (dat.HasLPext())
1542 out << dat.fTriggerSeqLPext;
1543 else
1544 out << "&ndash;";
1545 out << ':';
1546 if (dat.HasLPint())
1547 out << dat.fTriggerSeqLPint;
1548 else
1549 out << "&ndash;";
1550 out << '\n';
1551
1552 out << HTML::kWhite << '\t' << (dat.HasTrigger()?"on":"off") << " / " << (dat.HasExt1()?"on":"off") << " / " << (dat.HasExt2()?"on":"off") << '\n';
1553 out << HTML::kWhite << '\t' << (dat.HasVeto()?"on":"off") << " / " << (dat.HasClockConditioner()?"time cal":"marker") << '\n';
1554 out << HTML::kWhite << '\t' << dat.fMultiplicityPhysics << " / " << dat.fMultiplicityCalib << '\n';
1555 out << HTML::kWhite << '\t' << dat.fWindowPhysics << '\t' << dat.fWindowCalib << '\n';
1556 out << HTML::kWhite << '\t' << dat.fDelayTrigger << '\t' << dat.fDelayTimeMarker << '\n';
1557 out << HTML::kWhite << '\t' << dat.fDeadTime << '\n';
1558
1559 int64_t vp = dat.fPrescaling[0];
1560 for (int i=1; i<40; i++)
1561 if (vp!=dat.fPrescaling[i])
1562 vp = -1;
1563
1564 if (vp<0)
1565 out << HTML::kYellow << "\tdifferent\n";
1566 else
1567 out << HTML::kWhite << '\t' << 0.5*vp << "\n";
1568
1569 ofstream(fPath+"/ftm.data") << out.str();
1570
1571 // Active FTUs: IsActive(i)
1572 // Enabled Pix: IsEnabled(i)
1573
1574 return GetCurrentState();
1575 }
1576
1577 int HandleFtmFtuList(const EventImp &d)
1578 {
1579 if (!CheckDataSize(d, "FtmControl:FtuList", sizeof(FTM::DimFtuList)))
1580 return GetCurrentState();
1581
1582 const FTM::DimFtuList &sdata = d.Ref<FTM::DimFtuList>();
1583
1584 ostringstream out;
1585 out << d.GetJavaDate() << '\n';
1586
1587 int cnt = 0;
1588 for (int i=0; i<4; i++)
1589 {
1590 out << HTML::kWhite << '\t';
1591 for (int j=0; j<10; j++)
1592 if (sdata.IsActive(i*10+j))
1593 {
1594 if (sdata.fPing[i*10+j]==1)
1595 {
1596 out << '*';
1597 cnt++;
1598 }
1599 else
1600 out << sdata.fPing[i*10+j];
1601 }
1602 else
1603 out << '-';
1604 out << '\n';
1605 }
1606
1607 fFtmControlFtuOk = cnt==40;
1608
1609 ofstream(fPath+"/ftu.data") << out.str();
1610
1611 return GetCurrentState();
1612 }
1613
1614 int HandleFadEventData(const EventImp &d)
1615 {
1616 if (!CheckDataSize(d, "FadControl:EventData", 23040))
1617 return GetCurrentState();
1618
1619 const float *dat = d.Ptr<float>(1440*sizeof(float)*2);
1620
1621 /*
1622 vector<float> max(320, 0);
1623 for (int i=0; i<1440; i++)
1624 {
1625 if (i%9==8)
1626 continue;
1627
1628 const int idx = (fPixelMap.hw(i).hw()/9)*2+fPixelMap.hw(i).group();
1629 const double v = dat[i]/1000;
1630 //if (v>max[idx])
1631 // max[idx]=v;
1632
1633 max[idx] += v/4;
1634 } */
1635
1636 vector<float> val(1440);
1637 for (int i=0; i<1440; i++)
1638 val[i] = dat[i%9==8 ? i-2 : i]/1000;
1639
1640 vector<float> sorted(val);
1641 nth_element(sorted.begin(), sorted.begin()+3, sorted.end(),
1642 std::greater<float>());
1643
1644 const uint32_t trig = d.GetQoS() & FAD::EventHeader::kLPext;
1645
1646 const float min = fFadControlDrsRuns[0]==0 ? -1 : 0;
1647
1648 float scale = 2;
1649 if (trig&FAD::EventHeader::kLPext)
1650 scale = 1;
1651 if (trig&FAD::EventHeader::kPedestal)
1652 scale = 0.25;
1653 if (trig==0)
1654 scale = max(0.25f, sorted[3]);
1655
1656 // assume it is drs-gain
1657 //if ((trig&FAD::EventHeader::kPedestal) && fFadControlDrsRuns[0]>0 && fFadControlDrsRuns[1]==0)
1658 // min = 0.75;
1659
1660 WriteCam(d, "cam-fadcontrol-eventdata", val, scale, min);
1661
1662 return GetCurrentState();
1663 }
1664
1665 int HandleStats(const EventImp &d)
1666 {
1667 if (!CheckDataSize(d, "Stats", 4*8))
1668 {
1669 fFreeSpace = UINT64_MAX;
1670 return GetCurrentState();
1671 }
1672
1673 const DimWriteStatistics::Stats &s = d.Ref<DimWriteStatistics::Stats>();
1674 fFreeSpace = s.freeSpace;
1675
1676 return GetCurrentState();
1677 }
1678
1679 int HandleFscTemperature(const EventImp &d)
1680 {
1681 if (!CheckDataSize(d, "FscControl:Temperature", 240))
1682 return GetCurrentState();
1683
1684 const float *ptr = d.Ptr<float>(4);
1685
1686 double avg = 0;
1687 double rms = 0;
1688 double min = 99;
1689 double max = -99;
1690
1691 int num = 0;
1692 for (const float *t=ptr; t<ptr+31; t++)
1693 {
1694 if (*t==0)
1695 continue;
1696
1697 if (*t>max)
1698 max = *t;
1699
1700 if (*t<min)
1701 min = *t;
1702
1703 avg += *t;
1704 rms += *t * *t;
1705
1706 num++;
1707 }
1708
1709 avg /= num;
1710 rms /= num;
1711 rms += avg*avg;
1712 rms = rms<0 ? 0 : sqrt(rms);
1713
1714 // Clean broken reports
1715 static double pre_rms1 = 1.5;
1716 static double pre_rms2 = 0;
1717
1718 const double cut = pre_rms1 + 0.1;
1719
1720 const bool reject = rms>cut && pre_rms2<cut;
1721
1722 pre_rms2 = pre_rms1;
1723 pre_rms1 = rms;
1724
1725 if (reject)
1726 return GetCurrentState();
1727
1728
1729 if (!fMagicWeatherHist[kTemp].empty())
1730 {
1731 fFscControlTemperatureHist.push_back(avg-fMagicWeatherHist[kTemp].back());
1732 if (fFscControlTemperatureHist.size()>300)
1733 fFscControlTemperatureHist.pop_front();
1734 }
1735
1736 const Statistics stat(fFscControlTemperatureHist);
1737
1738 ostringstream out;
1739 out << setprecision(3);
1740 out << d.GetJavaDate() << '\n';
1741 out << HTML::kWhite << '\t' << fFscControlHumidityAvg << '\n';
1742 out << HTML::kWhite << '\t' << stat.min << '\n';
1743 out << HTML::kWhite << '\t' << stat.avg << '\n';
1744 out << HTML::kWhite << '\t' << stat.max << '\n';
1745
1746 ofstream(fPath+"/fsc.data") << out.str();
1747
1748 WriteHist(d, "hist-fsccontrol-temperature",
1749 fFscControlTemperatureHist, 10);
1750
1751 out.str("");
1752 out << setprecision(3);
1753 out << d.GetJavaDate() << '\n';
1754 out << HTML::kWhite << '\t' << max << '\n';
1755 out << HTML::kWhite << '\t' << avg << '\n';
1756 out << HTML::kWhite << '\t' << min << '\n';
1757
1758 ofstream(fPath+"/camtemp.data") << out.str();
1759
1760 return GetCurrentState();
1761 }
1762
1763 int HandleFscBiasTemp(const EventImp &d)
1764 {
1765 if (!CheckDataSize(d, "FscControl:BiasTemp", 323*4))
1766 return GetCurrentState();
1767
1768 const float *ptr = d.Ptr<float>(4);
1769 const float avg = d.Get<float>(321*4);
1770 //const float rms = d.Get<float>(322*4);
1771
1772 vector<double> tout(320);
1773 for (int i=0; i<320; i++)
1774 {
1775 const int idx = (fPixelMap.hv(i).hw()/9)*2+fPixelMap.hv(i).group();
1776 tout[idx] = ptr[i];
1777 }
1778
1779 WriteCam(d, "cam-fsccontrol-temperature", tout, 3, avg-1.75);
1780
1781 return GetCurrentState();
1782 }
1783
1784 int HandleFscHumidity(const EventImp &d)
1785 {
1786 if (!CheckDataSize(d, "FscControl:Humidity", 5*4))
1787 return GetCurrentState();
1788
1789 const float *ptr = d.Ptr<float>(4);
1790
1791 double avg =0;
1792 int num = 0;
1793
1794 for (const float *t=ptr; t<ptr+4; t++)
1795 if (*t>0 && *t<=100 && t!=ptr+2 /*excl broken sensor*/)
1796 {
1797 avg += *t;
1798 num++;
1799 }
1800
1801 fFscControlHumidityAvg = num>0 ? avg/num : 0;
1802
1803 return GetCurrentState();
1804 }
1805
1806 int HandleGpsNema(const EventImp &d)
1807 {
1808 if (!CheckDataSize(d, "GpsControl:Nema", sizeof(GPS::NEMA)))
1809 return GetCurrentState();
1810
1811 const GPS::NEMA &nema = d.Ref<GPS::NEMA>();
1812
1813 ostringstream out;
1814
1815 out << fixed;
1816 out << d.GetJavaDate() << '\n';
1817
1818 switch (nema.qos)
1819 {
1820 case 1: out << HTML::kGreen << "\tGPS fix [1]\n"; break;
1821 case 2: out << HTML::kGreen << "\tDifferential fix [2]\n"; break;
1822 default: out << HTML::kRed << "\tinvalid [" << nema.qos << "]\n"; break;
1823 }
1824
1825 out << HTML::kWhite << '\t' << nema.count << '\n';
1826 out << HTML::kWhite << '\t' << Time(floor(Time().Mjd())+nema.time).GetAsStr("%H:%M:%S") << '\n';
1827 out << HTML::kWhite << '\t' << setprecision(4) << nema.lat << '\n';
1828 out << HTML::kWhite << '\t' << setprecision(4) << nema.lng << '\n';
1829 out << HTML::kWhite << '\t' << setprecision(1) << nema.height << "\n";
1830 out << HTML::kWhite << '\t' << setprecision(1) << nema.hdop << "\n";
1831 out << HTML::kWhite << '\t' << setprecision(1) << nema.geosep << "\n";
1832
1833 ofstream(fPath+"/gps.data") << out.str();
1834
1835 return GetCurrentState();
1836 }
1837
1838 string GetTempColor(float t)
1839 {
1840 if (t>25 && t<30)
1841 return HTML::kGreen;
1842
1843 if (t<20 || t>35)
1844 return HTML::kRed;
1845
1846 return HTML::kYellow;
1847 }
1848
1849 int HandleTemperatureData(const EventImp &d)
1850 {
1851 if (!CheckDataSize(d, "Temperature:Data", 3*sizeof(float)))
1852 return GetCurrentState();
1853
1854 const float *temp = d.Ptr<float>();
1855
1856 ostringstream out;
1857
1858 out << fixed << setprecision(1);
1859 out << d.GetJavaDate() << '\n';
1860
1861 out << GetTempColor(temp[1]) << '\t' << temp[1] << '\n';
1862 out << GetTempColor(temp[0]) << '\t' << temp[0] << '\n';
1863 out << GetTempColor(temp[2]) << '\t' << temp[2] << '\n';
1864
1865 ofstream(fPath+"/temperature.data") << out.str();
1866
1867 fTemperatureControlHist.push_back(temp[0]);
1868 if (fTemperatureControlHist.size()>60) // 1h
1869 fTemperatureControlHist.pop_front();
1870
1871 WriteHist(d, "hist-temperaturecontrol",
1872 fTemperatureControlHist, 45, 0);
1873
1874 return GetCurrentState();
1875 }
1876
1877 int HandleAgilentData(const EventImp &d)
1878 {
1879 if (!CheckDataSize(d, "Agilent:Data", 4*sizeof(float)))
1880 return GetCurrentState();
1881
1882 const float *data = d.Ptr<float>();
1883
1884 ostringstream out;
1885
1886 out << fixed << setprecision(1);
1887 out << d.GetJavaDate() << '\n';
1888
1889 out << HTML::kWhite << '\t' << data[0] << '\n';
1890 out << HTML::kWhite << '\t' << data[1] << '\n';
1891 out << HTML::kWhite << '\t' << data[2] << '\n';
1892 out << HTML::kWhite << '\t' << data[3] << '\n';
1893
1894 ofstream(fPath+"/agilent.data") << out.str();
1895
1896 return GetCurrentState();
1897 }
1898
1899 int HandleRateScanData(const EventImp &d)
1900 {
1901 if (!CheckDataSize(d, "RateScan:Data", 824))
1902 return GetCurrentState();
1903
1904 const uint64_t id = d.Get<uint64_t>();
1905 const float *rate = d.Ptr<float>(20);
1906
1907 if (fRateScanDataId!=id)
1908 {
1909 for (int i=0; i<41; i++)
1910 fRateScanDataHist[i].clear();
1911 fRateScanDataId = id;
1912 }
1913 fRateScanDataHist[0].push_back(log10(rate[0]));
1914
1915 double max = 0;
1916 for (int i=1; i<41; i++)
1917 {
1918 fRateScanDataHist[i].push_back(log10(rate[i]));
1919 if (rate[i]>max)
1920 max = rate[i];
1921 }
1922
1923 // Cycle by time!
1924 fRateScanBoard ++;
1925 fRateScanBoard %= 40;
1926
1927 WriteHist(d, "hist-ratescan", fRateScanDataHist[0], 10, -2);
1928 WriteCam(d, "cam-ratescan-board", fRateScanDataHist[fRateScanBoard+1], 10, -4);
1929
1930 ostringstream out;
1931 out << setprecision(3);
1932 out << d.GetJavaDate() << '\n';
1933 out << HTML::kWhite << '\t' << fFtmBoardThresholdMed << '\n';
1934 out << HTML::kWhite << '\t' << fFtmPatchThresholdMed << '\n';
1935 out << HTML::kWhite << '\t' << floor(pow(10, fRateScanDataHist[0].back())+.5) << '\n';
1936 out << HTML::kWhite << '\t' << floor(max+.5) << '\n';
1937
1938 ofstream(fPath+"/ratescan.data") << out.str();
1939
1940 out.str("");
1941 out << d.GetJavaDate() << '\n';
1942 out << HTML::kWhite << '\t' << int(fRateScanBoard) << '\n';
1943 out << HTML::kWhite << '\t' << pow(10, fRateScanDataHist[fRateScanBoard+1].back()) << '\n';
1944
1945 ofstream(fPath+"/ratescan_board.data") << out.str();
1946
1947 return GetCurrentState();
1948 }
1949
1950 int HandleRateControlThreshold(const EventImp &d)
1951 {
1952 if (!CheckDataSize(d, "RateControl:Threshold", 18))
1953 return GetCurrentState();
1954
1955 const uint16_t th = d.Get<uint16_t>();
1956
1957 fRateControlThreshold.push_back(th);
1958 if (fRateControlThreshold.size()>300)
1959 fRateControlThreshold.pop_front();
1960
1961 WriteHist(d, "hist-ratecontrol-threshold", fRateControlThreshold, 1000);
1962
1963 return GetCurrentState();
1964 }
1965
1966 int HandleChatMsg(const EventImp &d)
1967 {
1968 if (d.GetSize()==0 || d.GetQoS()!=MessageImp::kComment)
1969 return GetCurrentState();
1970
1971 if (Time()<d.GetTime()+boost::posix_time::minutes(1))
1972 SetAudio("message");
1973
1974 fChatHist.add(d.GetText(), d.GetTime());
1975
1976 ostringstream out;
1977 out << setprecision(3);
1978 out << Header(d) << '\n';
1979 out << HTML::kWhite << '\t';
1980 out << "<->" << fChatHist.rget() << "</->";
1981 out << '\n';
1982
1983 ofstream(fPath+"/chat.data") << out.str();
1984
1985 return GetCurrentState();
1986 }
1987
1988 // -------------------------------------------------------------------
1989
1990 int HandleDoTest(const EventImp &d)
1991 {
1992 ostringstream out;
1993 out << d.GetJavaDate() << '\n';
1994
1995 switch (d.GetQoS())
1996 {
1997 case -3: out << HTML::kWhite << "\tNot running\n"; break;
1998 case -2: out << HTML::kBlue << "\tLoading\n"; break;
1999 case -1: out << HTML::kBlue << "\tStarted\n"; break;
2000 default: out << HTML::kGreen << "\tRunning [" << d.GetQoS() << "]\n"; break;
2001 }
2002
2003 ofstream(fPath+"/dotest.data") << out.str();
2004
2005 return StateMachineImp::kSM_KeepState;
2006 }
2007
2008 // -------------------------------------------------------------------
2009
2010 /*
2011 bool CheckEventSize(size_t has, const char *name, size_t size)
2012 {
2013 if (has==size)
2014 return true;
2015
2016 ostringstream msg;
2017 msg << name << " - Received event has " << has << " bytes, but expected " << size << ".";
2018 Fatal(msg);
2019 return false;
2020 }*/
2021
2022 int Print() const
2023 {
2024 Out() << fDimDNS << endl;
2025 Out() << fDimMcp << endl;
2026 Out() << fDimControl << endl;
2027 Out() << fDimDataLogger << endl;
2028 Out() << fDimDriveControl << endl;
2029 Out() << fDimTimeCheck << endl;
2030 Out() << fDimFadControl << endl;
2031 Out() << fDimFtmControl << endl;
2032 Out() << fDimBiasControl << endl;
2033 Out() << fDimFeedback << endl;
2034 Out() << fDimRateControl << endl;
2035 Out() << fDimFscControl << endl;
2036 Out() << fDimAgilentControl << endl;
2037 Out() << fDimPwrControl << endl;
2038 Out() << fDimLidControl << endl;
2039 Out() << fDimMagicWeather << endl;
2040 Out() << fDimTngWeather << endl;
2041 Out() << fDimMagicLidar << endl;
2042 Out() << fDimTemperature << endl;
2043 Out() << fDimRateScan << endl;
2044 Out() << fDimChat << endl;
2045 Out() << fDimSkypeClient << endl;
2046
2047 return GetCurrentState();
2048 }
2049
2050 string GetStateHtml(const DimState &state, int green) const
2051 {
2052 if (!state.online())
2053 return HTML::kWhite+"\t&mdash;\n";
2054
2055 if (&state==&fDimControl)
2056 return HTML::kGreen +'\t'+(state.state()==0?"Idle":fDimControl.shortmsg)+'\n';
2057
2058 const State rc = state.description();
2059
2060 // Sate not found in list, server online (-3: offline; -2: not found)
2061 if (rc.index==-2)
2062 {
2063 ostringstream out;
2064 out << HTML::kWhite << '\t' << state.state() << '\n';
2065 return out.str();
2066 }
2067
2068 //ostringstream msg;
2069 //msg << HTML::kWhite << '\t' << rc.name << " [" << rc.index << "]\n";
2070 //return msg.str();
2071
2072 if (rc.index<0)
2073 return HTML::kWhite + "\t&mdash;\n";
2074
2075 string col = HTML::kGreen;
2076 if (rc.index<green)
2077 col = HTML::kYellow;
2078 if (rc.index>0xff)
2079 col = HTML::kRed;
2080
2081 return col + '\t' + rc.name + '\n';
2082 }
2083
2084 bool SetError(bool b, const string &err)
2085 {
2086 if (!b)
2087 {
2088 fErrorList.erase(err);
2089 return 0;
2090 }
2091
2092 const bool isnew = fErrorList.insert(err).second;
2093 if (isnew)
2094 fErrorHist.add(err);
2095
2096 return isnew;
2097 }
2098
2099#ifdef HAVE_NOVA
2100
2101 vector<pair<Nova::EquPosn, double>> fMoonCoords;
2102
2103 void CalcMoonCoords(double jd)
2104 {
2105 jd = floor(jd);
2106
2107 fMoonCoords.clear();
2108 for (double h=0; h<1; h+=1./(24*12))
2109 {
2110 const Nova::EquPosn moon = Nova::GetLunarEquCoords(jd+h,0.01);
2111
2112 const double disk = Nova::GetLunarDisk(jd+h);
2113
2114 fMoonCoords.emplace_back(moon, disk);
2115 }
2116 }
2117
2118 pair<vector<float>, pair<Time, float>> GetVisibility(Nova::EquPosn *src, double jd)
2119 {
2120 jd = floor(jd);
2121
2122 const double jd0 = fmod(fSun.fSetAstronomical.JD(), 1);
2123 const double jd1 = fmod(fSun.fRiseAstronomical.JD(), 1);
2124
2125 Nova::EquPosn moon;
2126 Nova::EquPosn *pos = src ? src : &moon;
2127
2128 double max = 0;
2129 double maxjd = 0;
2130
2131 int cnt = 0;
2132
2133 int i=0;
2134
2135 vector<float> alt;
2136 for (double h=0; h<1; h+=1./(24*12), i++)
2137 {
2138 if (src==0)
2139 moon = fMoonCoords[i].first; //ln_get_lunar_equ_coords_prec(jd+h, &moon, 0.01);
2140
2141 const Nova::HrzPosn hrz = Nova::GetHrzFromEqu(*pos, jd+h);
2142
2143 if (h>jd0 && h<jd1)
2144 alt.push_back(hrz.alt);
2145
2146 if (hrz.alt>max)
2147 {
2148 max = hrz.alt;
2149 maxjd = jd+h;
2150 }
2151
2152 if (h>jd0 && h<jd1 && hrz.alt>15)
2153 cnt++;
2154 }
2155
2156 if (max<=15 || cnt==0)
2157 return make_pair(vector<float>(), make_pair(Time(), 0));
2158
2159 return make_pair(alt, make_pair(maxjd, maxjd>jd+jd0&&maxjd<jd+jd1?max:0));
2160 }
2161
2162 pair<vector<float>, pair<Time, float>> GetLightCondition(double jd)
2163 {
2164 jd = floor(jd);
2165
2166 const double jd0 = fmod(fSun.fSetAstronomical.JD(), 1);
2167 const double jd1 = fmod(fSun.fRiseAstronomical.JD(), 1);
2168
2169 double max = -1;
2170 double maxjd = 0;
2171
2172 int cnt = 0;
2173 int i = 0;
2174
2175 vector<float> vec;
2176 for (double h=0; h<1; h+=1./(24*12), i++)
2177 {
2178 double cur = -1;
2179
2180 if (h>jd0 && h<jd1)
2181 {
2182 Nova::EquPosn moon = fMoonCoords[i].first;//ln_get_lunar_equ_coords_prec(jd+h, &moon, 0.01);
2183 const double disk = fMoonCoords[i].second;//ln_get_lunar_disk(jd+h);
2184
2185 const Nova::HrzPosn hrz = Nova::GetHrzFromEqu(moon, jd+h);
2186 const double dist = Nova::GetLunarEarthDist(jd+h);
2187
2188 // Current prediction
2189 const double calt = sin(hrz.alt*M_PI/180);
2190
2191 const double lc = calt>0 ? calt*pow(disk, 2.2)*pow(dist, -2) : -1;
2192 cur = lc>0 ? 4+103*lc : -1;
2193
2194 vec.push_back(cur); // Covert LC to pixel current in uA
2195 }
2196
2197 if (cur>max)
2198 {
2199 max = cur;
2200 maxjd = jd+h;
2201 }
2202
2203 if (h>jd0 && h<jd1 && cur>0)
2204 cnt++;
2205 }
2206
2207 if (max<=0 || cnt==0)
2208 return make_pair(vector<float>(), make_pair(Time(), 0));
2209
2210 return make_pair(vec, make_pair(maxjd, maxjd>jd+jd0&&maxjd<jd+jd1?max:-1));
2211 }
2212#endif
2213
2214 void UpdateAstronomy()
2215 {
2216 Time now;
2217
2218 CalcMoonCoords(now.JD());
2219
2220 fSun = Sun (now);
2221 fMoon = Moon(now);
2222
2223 vector<string> color(8, HTML::kWhite);
2224 color[fSun.state%8] = HTML::kBlue;
2225
2226 ostringstream out;
2227 out << setprecision(3);
2228 out << now.JavaDate() << '\n';
2229 out << color[0] << '\t' << fSun.fRiseDarkTime.GetAsStr("%H:%M") << '\n';
2230 out << color[1] << '\t' << fSun.fRiseAstronomical.GetAsStr("%H:%M") << '\n';
2231 out << color[2] << '\t' << fSun.fRiseCivil.GetAsStr("%H:%M") << '\n';
2232 out << color[3] << '\t' << fSun.fRiseDayTime.GetAsStr("%H:%M") << '\n';
2233
2234 out << color[4] << '\t' << fSun.fSetDayTime.GetAsStr("%H:%M") << '\n';
2235 out << color[5] << '\t' << fSun.fSetCivil.GetAsStr("%H:%M") << '\n';
2236 out << color[6] << '\t' << fSun.fSetAstronomical.GetAsStr("%H:%M") << '\n';
2237 out << color[7] << '\t' << fSun.fSetDarkTime.GetAsStr("%H:%M") << '\n';
2238
2239 ofstream(fPath+"/sun.data") << out.str();
2240
2241 color.assign(3, HTML::kWhite);
2242 color[fMoon.state%3] = HTML::kBlue;
2243
2244 out.str("");
2245 out << now.JavaDate() << '\n';
2246
2247 out << color[0] << '\t' << fMoon.fRise.GetAsStr("%H:%M") << '\n';
2248 out << color[1] << '\t' << fMoon.fTransit.GetAsStr("%H:%M") << '\n';
2249 out << color[2] << '\t' << fMoon.fSet.GetAsStr("%H:%M") << '\n';
2250
2251 out << (fSun.isday?HTML::kWhite:fMoon.color) << '\t' << fMoon.description << '\n';
2252
2253 if (!fMoon.visible)
2254 out << HTML::kWhite << "\t&mdash;\t\n";
2255 else
2256 {
2257 string col = HTML::kWhite;
2258 if (!fSun.isday)
2259 {
2260 col = HTML::kGreen;
2261 if (fMoon.zd>25)
2262 col = HTML::kYellow;
2263 if (fMoon.zd>45 && fMoon.zd<80)
2264 col = HTML::kRed;
2265 if (fMoon.zd>=80)
2266 col = HTML::kRed;
2267 }
2268 out << col << '\t' << fMoon.zd << '\t' << GetDir(fMoon.az) << '\n';
2269 }
2270
2271 ostringstream out2, out3, out4;
2272 out2 << setprecision(3);
2273 out2 << now.JavaDate() << '\n';
2274 out3 << now.JavaDate() << '\n';
2275 out4 << now.JavaDate() << '\n';
2276
2277 struct Entry
2278 {
2279 string name;
2280 float value;
2281 int color;
2282 Entry(const string &n, float v, int c) : name(n), value(v), color(c%8) { }
2283
2284 const string &Col() const
2285 {
2286 // If this list is updatd the number count in the constructor needs
2287 // to be updated, too
2288 static const string hcol[] = { "888", "8cf", "c8f", "bbb", "8fc", "cf8", "f8c", "fc8" };
2289 return hcol[color];
2290 }
2291
2292 vector<float> GetColor(double scale, double offset=0) const
2293 {
2294 vector<float> rc(3);
2295 rc[0] = double(Col()[0])*scale/126+offset;
2296 rc[1] = double(Col()[1])*scale/126+offset;
2297 rc[2] = double(Col()[2])*scale/126+offset;
2298 return rc;
2299 }
2300 };
2301
2302 multimap<Time, Entry> culmination;
2303 multimap<Time, Entry> lightcond;
2304 vector<vector<float>> alt;
2305 vector<vector<float>> cur;
2306
2307#ifdef HAVE_NOVA
2308 int ccol = 0;
2309 int lcol = 0;
2310
2311 /*const*/ pair<vector<float>, pair<Time, float>> vism = GetVisibility(0, now.JD());
2312 if (!vism.first.empty())
2313 {
2314 const Entry entry("Moon", vism.second.second, ccol);
2315 culmination.insert(make_pair(vism.second.first, entry));
2316 const vector<float> col = entry.GetColor(75, 15);
2317 vism.first.insert(vism.first.begin(), col.begin(), col.end());
2318 alt.push_back(vism.first);
2319
2320 ccol++;
2321 }
2322#endif
2323
2324#ifdef HAVE_SQL
2325 try
2326 {
2327 const mysqlpp::StoreQueryResult res =
2328 Database(fDatabase).query("SELECT fSourceName, fRightAscension, fDeclination FROM Source WHERE fSourceTypeKEY=1").store();
2329
2330 out << HTML::kWhite << '\t';
2331 out2 << HTML::kWhite << '\t';
2332 out3 << HTML::kWhite << '\t';
2333 out4 << HTML::kWhite << '\t';
2334
2335 for (vector<mysqlpp::Row>::const_iterator v=res.begin(); v<res.end(); v++)
2336 {
2337 const string name = (*v)[0].c_str();
2338 const double ra = (*v)[1];
2339 const double dec = (*v)[2];
2340#ifdef HAVE_NOVA
2341 Nova::EquPosn pos;
2342 pos.ra = ra*15;
2343 pos.dec = dec;
2344
2345 const Nova::ZdAzPosn hrz = Nova::GetHrzFromEqu(pos, now.JD());
2346
2347 /*const*/ pair<vector<float>, pair<Time, float>> vis = GetVisibility(&pos, now.JD());
2348 if (!vis.first.empty())
2349 {
2350 const Entry entry(name, vis.second.second, ccol);
2351 culmination.insert(make_pair(vis.second.first, entry));
2352 const vector<float> col = entry.GetColor(75, 15);
2353 vis.first.insert(vis.first.begin(), col.begin(), col.end());
2354 alt.push_back(vis.first);
2355
2356 ccol++;
2357
2358 /*const*/ pair<vector<float>, pair<Time, float>> lc = GetLightCondition(now.JD());
2359 if (!lc.first.empty())
2360 {
2361 const Entry entry2(name, lc.second.second, lcol);
2362 lightcond.insert(make_pair(lc.second.first, entry2));
2363 const vector<float> col2 = entry2.GetColor(100);
2364 lc.first.insert(lc.first.begin(), col2.begin(), col2.end());
2365 cur.push_back(lc.first);
2366
2367 lcol++;
2368 }
2369 }
2370
2371 string col = HTML::kWhite;
2372 if (hrz.zd<85)
2373 col = HTML::kRed;
2374 if (hrz.zd<65)
2375 col = HTML::kYellow;
2376 if (hrz.zd<30)
2377 col = HTML::kGreen;
2378
2379 out2 << "<tr bgcolor='" << col << "'>";
2380 out2 << "<td>" << name << "</td>";
2381 if (hrz.zd<85)
2382 {
2383 out2 << "<td>" << hrz.zd << "&deg;</td>";
2384 out2 << "<td>" << GetDir(hrz.az) << "</td>";
2385 }
2386 else
2387 out2 << "<td/><td/>";
2388 out2 << "</tr>";
2389#endif
2390 const int32_t angle = fMoon.Angle(ra, dec);
2391
2392 out << "<tr bgcolor='" << Moon::Color(angle) << "'>";
2393 out << "<td>" << name << "</td>";
2394 out << "<td>" << round(angle) << "&deg;</td>";
2395 out << "</tr>";
2396 }
2397
2398 for (auto it=culmination.begin(); it!=culmination.end(); it++)
2399 {
2400 const Entry &e = it->second;
2401 if (it!=culmination.begin())
2402 out3 << ", ";
2403 out3 << "<B#" << e.Col() << ">" << e.name << "</B>";
2404 if (e.value>0)
2405 out3 << " [" << nearbyint(90-e.value) << "&deg;]";
2406 }
2407
2408 out4 << setprecision(3);
2409
2410 for (auto it=lightcond.begin(); it!=lightcond.end(); it++)
2411 {
2412 const Entry &e = it->second;
2413 if (it!=lightcond.begin())
2414 out4 << ", ";
2415 out4 << "<B#" << e.Col() << ">" << e.name << "</B>";
2416 if (e.value>0)
2417 out4 << " [" << nearbyint(e.value) << "]";
2418 }
2419
2420 if (fSun.fSetAstronomical>fSun.fRiseAstronomical)
2421 fSun.fSetAstronomical += boost::posix_time::hours(24);
2422
2423 ostringstream title;
2424 title << fSun.fSetAstronomical.GetAsStr("%H:%M");
2425 title << " / ";
2426 title << ((fSun.fRiseAstronomical-fSun.fSetAstronomical)/20).minutes();
2427 title << "' / ";
2428 title << fSun.fRiseAstronomical.GetAsStr("%H:%M");
2429
2430 out << '\n';
2431 out2 << '\n';
2432 out3 << '\n';
2433 out4 << '\n';
2434 out << HTML::kWhite << '\t' << Time()-now << '\n';
2435 out2 << HTML::kWhite << '\t' << Time()-now << '\n';
2436
2437 WriteBinaryVec(now, "hist-visibility", alt, 75, 15, "Alt "+title.str());
2438 WriteBinaryVec(now, "hist-current-prediction", cur, 100, 0, "I " +title.str());
2439 }
2440 catch (const exception &e)
2441 {
2442 out << '\n';
2443 out2 << '\n';
2444 out << HTML::kWhite << '\t' << "ERROR - "+string(e.what()) << '\n';
2445 out2 << HTML::kWhite << '\t' << "ERROR - "+string(e.what()) << '\n';
2446 out3 << HTML::kWhite << '\t' << "ERROR - "+string(e.what()) << '\n';
2447 out4 << HTML::kWhite << '\t' << "ERROR - "+string(e.what()) << '\n';
2448 }
2449#endif
2450
2451 ofstream(fPath+"/moon.data") << out.str();
2452 ofstream(fPath+"/source-list.data") << out2.str();
2453 ofstream(fPath+"/visibility.data") << out3.str();
2454 ofstream(fPath+"/current-prediction.data") << out4.str();
2455 }
2456
2457 int Execute()
2458 {
2459 Time now;
2460 if (now-fLastUpdate<boost::posix_time::seconds(1))
2461 return fDimDNS.online() ? kStateRunning : kStateDimNetworkNA;
2462 fLastUpdate=now;
2463
2464 // ==============================================================
2465
2466 const bool data_taking =
2467 fDimMcp.state()==MCP::State::kTriggerOn ||
2468 fDimMcp.state()==MCP::State::kTakingData;
2469
2470 const bool data_run =
2471 fMcpConfigurationName=="data" ||
2472 fMcpConfigurationName=="data-rt";
2473
2474 const bool bias_on =
2475 fDimBiasControl.state()==BIAS::State::kRamping ||
2476 fDimBiasControl.state()==BIAS::State::kOverCurrent ||
2477 fDimBiasControl.state()==BIAS::State::kVoltageOn;
2478
2479 const bool calibrated =
2480 fDimFeedback.state()>=Feedback::State::kCalibrated;
2481
2482 const bool haderr = !fErrorList.empty();
2483
2484 bool newerr = false;
2485
2486 newerr |= SetError(!fDimDNS.online(),
2487 "<b><#darkred>DIM network not available</#></b>");
2488 newerr |= SetError(!fDimControl.online(),
2489 "<b>no dimctrl server available</b>");
2490 newerr |= SetError(fDimDataLogger.state()<20 || fDimDataLogger.state()>40,
2491 "<b>datalogger not ready</b>");
2492
2493 //newerr |= SetError(fDimDriveControl.state()==Drive::State::kLocked,
2494 // "<b><#darkred>Drive in LOCKED state, drive was automatically parked</#></b>");
2495
2496 newerr |= SetError(fDimDriveControl.state()>0xff && data_taking && data_run,
2497 "Drive in ERROR state during data-run");
2498 newerr |= SetError(fDriveControlMoonDist>155,
2499 "Moon within the field-of-view of the cones");
2500 newerr |= SetError(fDriveControlMoonDist>=0 && fDriveControlMoonDist<3,
2501 "Moon within the field-of-view of the camera");
2502
2503 newerr |= SetError(fDimBiasControl.state()<BIAS::State::kRamping && data_taking && data_run,
2504 "BIAS not operating during data-run");
2505 newerr |= SetError(fDimBiasControl.state()==BIAS::State::kOverCurrent,
2506 "BIAS channels in OverCurrent");
2507 newerr |= SetError(fDimBiasControl.state()==BIAS::State::kNotReferenced,
2508 "BIAS voltage not at reference");
2509
2510
2511 newerr |= SetError(bias_on && calibrated && fBiasControlCurrentMed>90,
2512 "Median current exceeds 90&micro;A/pix");
2513 newerr |= SetError(bias_on && calibrated && fBiasControlCurrentMax>110,
2514 "Maximum current exceeds 110&micro;A/pix");
2515
2516 newerr |= SetError(fFscControlHumidityAvg>60,
2517 "Average camera humidity exceed 60%");
2518
2519 newerr |= SetError(!fMagicWeatherHist[kHum].empty() && fMagicWeatherHist[kHum].back()>98 && fDimLidControl.state()==Lid::State::kOpen,
2520 "Outside humidity exceeds 98% while lid is open");
2521 newerr |= SetError(!fMagicWeatherHist[kGusts].empty() && fMagicWeatherHist[kGusts].back()>50 && (fDimDriveControl.state()==Drive::State::kTracking||fDimDriveControl.state()==Drive::State::kOnTrack),
2522 "Wind gusts exceed 50km/h during tracking");
2523
2524 newerr |= SetError(fDimFscControl.state()>=FSC::State::kConnected && !fFscControlTemperatureHist.empty() && fFscControlTemperatureHist.back()>15,
2525 "Sensor temperature exceeds outside temperature by more than 15&deg;C");
2526
2527 newerr |= SetError(fFtmControlTriggerRateTooLow>0,
2528 "Trigger rate below 1Hz while trigger switched on");
2529
2530 newerr |= SetError(fFtmControlState!=FTM::kFtmConfig && (fFtmControlState&FTM::kFtmLocked)==0,
2531 "FTM - clock conditioner not locked!");
2532
2533 newerr |= SetError(fDimTimeCheck.state()==1,
2534 "Warning NTP time difference of drive PC exceeds 1s");
2535 newerr |= SetError(fDimTimeCheck.state()<1,
2536 "Warning timecheck not running");
2537
2538 newerr |= SetError(fDimBiasControl.state()==BIAS::State::kVoltageOn &&
2539 fDimFeedback.state()<Feedback::State::kCalibrating &&
2540 fBiasControlVoltageMed>3,
2541 "Bias voltage switched on, but bias crate not calibrated");
2542
2543 newerr |= SetError(fLastRunFinishedWithZeroEvents,
2544 "Last run finshed, but contained zero events.");
2545
2546 newerr |= SetError(fFreeSpace<50000000000,
2547 "Less than 50GB disk space left.");
2548
2549 newerr |= SetError(fDimPwrControl.state()==Power::State::kCoolingFailure,
2550 "Cooling unit reports failure!");
2551
2552 for (auto it=fControlAlarmHist.begin(); it!=fControlAlarmHist.end(); it++)
2553 newerr |= SetError(it->time.IsValid(), it->msg);
2554 fControlAlarmHist.clean();;
2555
2556 fLastRunFinishedWithZeroEvents = false;
2557
2558 // FTM in Connected instead of Idle --> power cyclen
2559
2560 /* // Check offline and disconnected status?
2561 Out() << fDimMcp << endl;
2562 Out() << fDimControl << endl;
2563 Out() << fDimDataLogger << endl;
2564 Out() << fDimDriveControl << endl;
2565 Out() << fDimFadControl << endl;
2566 Out() << fDimFtmControl << endl;
2567 Out() << fDimBiasControl << endl;
2568 Out() << fDimFeedback << endl;
2569 Out() << fDimRateControl << endl;
2570 Out() << fDimFscControl << endl;
2571 Out() << fDimMagicWeather << endl;
2572 Out() << fDimRateScan << endl;
2573 Out() << fDimChat << endl;
2574 */
2575
2576 // FTU in error
2577 // FAD lost
2578
2579 // --------------------------------------------------------------
2580 ostringstream out;
2581
2582 if (newerr)
2583 {
2584 SetAudio("error");
2585
2586 out << now.JavaDate() << '\n';
2587 out << HTML::kWhite << '\t';
2588 out << "<->" << fErrorHist.rget() << "<->";
2589 out << '\n';
2590
2591 ofstream(fPath+"/errorhist.data") << out.str();
2592 }
2593
2594 out.str("");
2595 out << Header(now) << '\t' << (!fErrorList.empty()) << '\t' << (fDimControl.state()>0) << '\n';
2596 out << setprecision(3);
2597 out << HTML::kWhite << '\t';
2598 for (auto it=fErrorList.begin(); it!=fErrorList.end(); it++)
2599 out << *it << "<br/>";
2600 out << '\n';
2601
2602 if (haderr || !fErrorList.empty())
2603 ofstream(fPath+"/error.data") << out.str();
2604
2605 // ==============================================================
2606
2607 out.str("");
2608 out << Header(now) << '\t' << (!fErrorList.empty()) << '\t' << (fDimControl.state()>0) << '\n';
2609 out << setprecision(3);
2610
2611 // -------------- System status --------------
2612 if (fDimDNS.online() && fDimMcp.state()>=MCP::State::kIdle) // Idle
2613 {
2614 string col = HTML::kBlue;
2615 switch (fMcpConfigurationState)
2616 {
2617 case MCP::State::kIdle:
2618 case DimState::kOffline:
2619 col = HTML::kWhite;
2620 break;
2621 case MCP::State::kConfiguring1:
2622 case MCP::State::kConfiguring2:
2623 case MCP::State::kConfiguring3:
2624 case MCP::State::kConfigured:
2625 case MCP::State::kTriggerOn:
2626 col = HTML::kBlue;
2627 break;
2628 case MCP::State::kTakingData:
2629 col = HTML::kBlue;
2630 if (fDimFadControl.state()==FAD::State::kRunInProgress)
2631 col = HTML::kGreen;
2632 break;
2633 }
2634
2635 const bool other =
2636 fDimRateControl.state()==RateControl::State::kSettingGlobalThreshold ||
2637 fDimLidControl.state()==Lid::State::kMoving ||
2638 fDimRateScan.state()==RateScan::State::kInProgress;
2639
2640 if (other)
2641 col = HTML::kBlue;
2642
2643 out << col << '\t';
2644
2645 if (!other)
2646 {
2647 const string conf = fMcpConfigurationName.length()>0?" ["+fMcpConfigurationName+"]":"";
2648 switch (fMcpConfigurationState)
2649 {
2650 case MCP::State::kIdle:
2651 out << "Idle" << conf;
2652 break;
2653 case MCP::State::kConfiguring1:
2654 case MCP::State::kConfiguring2:
2655 case MCP::State::kConfiguring3:
2656 out << "Configuring" << conf;
2657 break;
2658 case MCP::State::kConfigured:
2659 out << "Configured" << conf;
2660 break;
2661 case MCP::State::kTriggerOn:
2662 case MCP::State::kTakingData:
2663 out << fMcpConfigurationName;
2664 if (fFadControlDrsRuns[2]>0)
2665 out << "(" << fFadControlDrsRuns[2] << ")";
2666 break;
2667 }
2668 }
2669 else
2670 if (fDimRateControl.state()==RateControl::State::kSettingGlobalThreshold)
2671 out << "Calibrating threshold";
2672 else
2673 if (fDimRateScan.state()==RateScan::State::kInProgress)
2674 out << "Rate scan in progress";
2675 else
2676 if (fDimLidControl.state()==Lid::State::kMoving)
2677 out << "Lid moving";
2678
2679
2680 if (fMcpConfigurationState>MCP::State::kConfigured &&
2681 fDimRateControl.state()!=RateControl::State::kSettingGlobalThreshold)
2682 {
2683 ostringstream evt;
2684 if (fMcpConfigurationMaxEvents>0)
2685 {
2686 const int64_t de = int64_t(fMcpConfigurationMaxEvents) - int64_t(fFadControlNumEvents);
2687 if (de>=0 && fMcpConfigurationState==MCP::State::kTakingData)
2688 evt << de;
2689 else
2690 evt << fMcpConfigurationMaxEvents;
2691 }
2692 else
2693 {
2694 if (fMcpConfigurationState==MCP::State::kTakingData)
2695 {
2696 if (fFadControlNumEvents>2999)
2697 evt << floor(fFadControlNumEvents/1000) << 'k';
2698 else
2699 evt << fFadControlNumEvents;
2700 }
2701 }
2702
2703 ostringstream tim;
2704 if (fMcpConfigurationMaxTime>0)
2705 {
2706 const uint32_t dt = (Time()-fMcpConfigurationRunStart).total_seconds();
2707 if (dt<=fMcpConfigurationMaxTime && fMcpConfigurationState==MCP::State::kTakingData)
2708 tim << fMcpConfigurationMaxTime-dt << 's';
2709 else
2710 tim << fMcpConfigurationMaxTime << 's';
2711 }
2712 else
2713 {
2714 if (fMcpConfigurationState==MCP::State::kTakingData)
2715 tim << fMcpConfigurationRunStart.SecondsTo();
2716 }
2717
2718 const bool has_evt = !evt.str().empty();
2719 const bool has_tim = !tim.str().empty();
2720
2721 if (has_evt || has_tim)
2722 out << " [";
2723 out << evt.str();
2724 if (has_evt && has_tim)
2725 out << '/';
2726 out << tim.str();
2727 if (has_evt || has_tim)
2728 out << ']';
2729 }
2730 }
2731 else
2732 out << HTML::kWhite;
2733 out << '\n';
2734
2735 // ------------------ Drive -----------------
2736 if (fDimDNS.online() && fDimDriveControl.state()>=Drive::State::kArmed) // Armed, Moving, Tracking, OnTrack, Error
2737 {
2738 const uint32_t dev = !fDriveControlTrackingDevHist.empty() ? round(fDriveControlTrackingDevHist.back()) : 0;
2739 const State rc = fDimDriveControl.description();
2740 string col = HTML::kGreen;
2741 if (fDimDriveControl.state()==Drive::State::kMoving) // Moving
2742 col = HTML::kBlue;
2743 if (fDimDriveControl.state()==Drive::State::kArmed) // Armed
2744 col = HTML::kWhite;
2745 if (fDimDriveControl.state()==Drive::State::kTracking || fDimDriveControl.state()==Drive::State::kOnTrack) // Tracking
2746 {
2747 if (dev>60) // ~1.5mm
2748 col = HTML::kYellow;
2749 if (dev>120) // ~1/4 of a pixel ~ 2.5mm
2750 col = HTML::kRed;
2751 }
2752 if (fDimDriveControl.state()==0x100)
2753 col = HTML::kRed;
2754 out << col << '\t';
2755
2756 //out << rc.name << '\t';
2757 out << fDriveControlPointingAz << ' ';
2758 out << fDriveControlPointingZd << "&deg;";
2759 out << setprecision(2);
2760 if (fDimDriveControl.state()==Drive::State::kTracking || fDimDriveControl.state()==Drive::State::kOnTrack)
2761 {
2762 out << " &plusmn; " << dev << '"';
2763 if (!fDriveControlSourceName.empty())
2764 out << " [" << fDriveControlSourceName << ']';
2765 }
2766 if (fDimDriveControl.state()==Drive::State::kMoving)
2767 out << " &#10227;";
2768 out << setprecision(3);
2769 }
2770 else
2771 out << HTML::kWhite << '\t';
2772
2773 if (fSun.time.IsValid() && fMoon.time.IsValid())
2774 {
2775 if (fSun.visible)
2776 {
2777 out << " &#9788;";
2778 if (fDimDriveControl.state()<Drive::State::kArmed)
2779 out << " [" << fSun.fSetCivil.MinutesTo() << "&darr;]";
2780 }
2781 else
2782 if (!fSun.visible && fMoon.visible)
2783 {
2784 out << " &#9790;";
2785 if (fDimDriveControl.state()<Drive::State::kArmed)
2786 out << " [" << fMoon.disk << "%]";
2787 }
2788 }
2789 if (fDimDNS.online() && fDimDriveControl.state()==0x100)
2790 out << " <ERR>";
2791 if (fDimDNS.online() && fDimDriveControl.state()==Drive::State::kLocked)
2792 out << " &otimes;";
2793 out << '\n';
2794
2795 // ------------------- FSC ------------------
2796 if (fDimDNS.online() && fDimFscControl.state()>FSC::State::kDisconnected && !fFscControlTemperatureHist.empty())
2797 {
2798 string col = HTML::kGreen;
2799 if (fFscControlTemperatureHist.back()>9)
2800 col = HTML::kYellow;
2801 if (fFscControlTemperatureHist.back()>15)
2802 col = HTML::kRed;
2803
2804 out << col << '\t' << fFscControlTemperatureHist.back() << '\n';
2805 }
2806 else
2807 out << HTML::kWhite << '\n';
2808
2809 // --------------- MagicWeather -------------
2810 if (fDimDNS.online() && fDimMagicWeather.state()==MagicWeather::State::kReceiving && !fMagicWeatherHist[kWeatherBegin].empty())
2811 {
2812 /*
2813 const float diff = fMagicWeatherHist[kTemp].back()-fMagicWeatherHist[kDew].back();
2814 string col1 = HTML::kRed;
2815 if (diff>0.3)
2816 col1 = HTML::kYellow;
2817 if (diff>0.7)
2818 col1 = HTML::kGreen;
2819 */
2820
2821 const float wind = fMagicWeatherHist[kGusts].back();
2822 const float hum = fMagicWeatherHist[kHum].back();
2823 string col = HTML::kGreen;
2824 if (wind>35 || hum>95)
2825 col = HTML::kYellow;
2826 if (wind>45 || hum>98)
2827 col = HTML::kRed;
2828
2829 out << col << '\t';
2830 out << fMagicWeatherHist[kHum].back() << '\t';
2831 out << setprecision(2);
2832 out << fMagicWeatherHist[kGusts].back() << '\n';
2833 out << setprecision(3);
2834 }
2835 else
2836 out << HTML::kWhite << "\n";
2837
2838 // --------------- FtmControl -------------
2839 if (fDimDNS.online() && fDimFtmControl.state()==FTM::State::kTriggerOn)
2840 {
2841 string col = HTML::kGreen;
2842 if (!fFtmControlTriggerRateHist.empty())
2843 {
2844 if (fFtmControlTriggerRateHist.back()<15)
2845 col = HTML::kYellow;
2846 if (fFtmControlTriggerRateHist.back()>100)
2847 col = HTML::kRed;
2848
2849 out << col << '\t' << fFtmControlTriggerRateHist.back() << " Hz";
2850 }
2851
2852 if (bias_on)
2853 out << " (" << setprecision(4) << fFtmPatchThresholdMed << ')';
2854 out << '\n';
2855 }
2856 else
2857 out << HTML::kWhite << '\n';
2858
2859 // --------------- BiasControl -------------
2860 const bool bias_off = fDimBiasControl.state()==BIAS::State::kVoltageOff;
2861 const bool bias_oc = fDimBiasControl.state()==BIAS::State::kOverCurrent;
2862
2863 if (fDimDNS.online() && (bias_on || bias_off))
2864 {
2865
2866 string col = fBiasControlVoltageMed>3?HTML::kGreen:HTML::kWhite;
2867 if (bias_on)
2868 {
2869 if (fBiasControlCurrentMed>70 || fBiasControlCurrentMax>90)
2870 col = HTML::kYellow;
2871 if (fBiasControlCurrentMed>90 || fBiasControlCurrentMax>110)
2872 col = HTML::kRed;
2873 }
2874
2875 // Bias in overcurrent => Red
2876 if (bias_oc)
2877 col = HTML::kRed;
2878
2879 // MCP in ReadyForDatataking/Configuring/Configured/TriggerOn/TakingData
2880 // and Bias not in "data-taking state' => Red
2881 if (fMcpConfigurationState>MCP::State::kIdle && !bias_on)
2882 col = HTML::kWhite;
2883
2884 const bool cal = fDimFeedback.state()>=Feedback::State::kCalibrated;
2885
2886 // Feedback is currently calibrating => Blue
2887 if (fDimFeedback.state()==Feedback::State::kCalibrating)
2888 {
2889 out << HTML::kBlue << '\t';
2890 out << "***\t";
2891 out << "***\t";
2892 }
2893 else
2894 {
2895 out << col << '\t';
2896 out << setprecision(fBiasControlCurrentMed<100?2:3);
2897 out << (bias_off ? 0 : (fBiasControlCurrentMed<10?fBiasControlCurrentMed:floor(fBiasControlCurrentMed))) << '\t';
2898 if (bias_oc)
2899 out << "(OC) ";
2900 else
2901 {
2902 if (cal)
2903 {
2904 out << setprecision(fBiasControlCurrentMax<100?2:3);
2905 out << (bias_off ? 0 : (fBiasControlCurrentMax<10?fBiasControlCurrentMax:floor(fBiasControlCurrentMax)));
2906 }
2907 else
2908 out << "&mdash; ";
2909 }
2910 out << '\t';
2911 }
2912 if (cal && fDimFeedback.state()!=Feedback::State::kCalibrating)
2913 out << setprecision(2) << fBiasControlPowerTot << " W";
2914 else
2915 out << setprecision(3) << (bias_off ? 0 : fBiasControlVoltageMed) << " V";
2916 out << '\n';
2917 }
2918 else
2919 out << HTML::kWhite << '\n';
2920
2921 ofstream(fPath+"/fact.data") << out.str();
2922
2923 // ==============================================================
2924
2925 out.str("");
2926 out << Header(now) << '\t' << (!fErrorList.empty()) << '\t' << (fDimControl.state()>0) << '\n';
2927
2928 if (!fDimDNS.online())
2929 out << HTML::kWhite << "\tOffline\n\n\n\n\n\n\n\n\n\n\n\n\n";
2930 else
2931 {
2932 ostringstream dt;
2933 dt << (Time()-fRunTime);
2934
2935 out << HTML::kGreen << '\t' << fDimDNS.version() << '\n';
2936
2937 out << GetStateHtml(fDimControl, 0);
2938 out << GetStateHtml(fDimMcp, MCP::State::kConnected);
2939 out << GetStateHtml(fDimDataLogger, 1);
2940 out << GetStateHtml(fDimDriveControl, Drive::State::kConnected);
2941 out << GetStateHtml(fDimTimeCheck, 1);
2942 out << GetStateHtml(fDimFadControl, FAD::State::kConnected);
2943 out << GetStateHtml(fDimFtmControl, FTM::State::kConnected);
2944 out << GetStateHtml(fDimBiasControl, BIAS::State::kConnected);
2945 out << GetStateHtml(fDimFeedback, Feedback::State::kConnected);
2946 out << GetStateHtml(fDimRateControl, RateControl::State::kConnected);
2947 out << GetStateHtml(fDimFscControl, FSC::State::kConnected);
2948 out << GetStateHtml(fDimGpsControl, GPS::State::kConnected);
2949 out << GetStateHtml(fDimAgilentControl, Agilent::State::kVoltageOff);
2950 out << GetStateHtml(fDimPwrControl, Power::State::kSystemOff);
2951 out << GetStateHtml(fDimLidControl, Lid::State::kConnected);
2952 out << GetStateHtml(fDimRateScan, RateScan::State::kConnected);
2953 out << GetStateHtml(fDimMagicWeather, MagicWeather::State::kConnected);
2954 out << GetStateHtml(fDimTngWeather, TNGWeather::State::kConnected);
2955 out << GetStateHtml(fDimMagicLidar, MagicLidar::State::kConnected);
2956 out << GetStateHtml(fDimTemperature, Temperature::State::kValid);
2957 out << GetStateHtml(fDimChat, 0);
2958 out << GetStateHtml(fDimSkypeClient, 1);
2959
2960 string col = HTML::kRed;
2961 if (fFreeSpace>uint64_t(199999999999))
2962 col = HTML::kYellow;
2963 if (fFreeSpace>uint64_t(999999999999))
2964 col = HTML::kGreen;
2965 if (fFreeSpace==UINT64_MAX)
2966 col = HTML::kWhite;
2967
2968 out << col << '\t' << Tools::Scientific(fFreeSpace) << "B\n";
2969
2970 out << HTML::kGreen << '\t' << dt.str().substr(0, dt.str().length()-7) << '\n';
2971 }
2972
2973 ofstream(fPath+"/status.data") << out.str();
2974
2975 if (now-fLastAstroCalc>boost::posix_time::seconds(15))
2976 {
2977 UpdateAstronomy();
2978 fLastAstroCalc = now;
2979 }
2980
2981 return fDimDNS.online() ? kStateRunning : kStateDimNetworkNA;
2982 }
2983
2984
2985public:
2986 StateMachineSmartFACT(ostream &out=cout) : StateMachineDim(out, fIsServer?"SMART_FACT":""),
2987 fLastAstroCalc(boost::date_time::neg_infin),
2988 fPath("www/smartfact/data"),
2989 fControlScriptDepth(0),
2990 fMcpConfigurationState(DimState::kOffline),
2991 fMcpConfigurationMaxTime(0),
2992 fMcpConfigurationMaxEvents(0),
2993 fLastRunFinishedWithZeroEvents(false),
2994 fTngWeatherDustTime(Time::none),
2995 fBiasControlVoltageMed(0),
2996 fBiasControlCurrentMed(0),
2997 fBiasControlCurrentMax(0),
2998 fFscControlHumidityAvg(0),
2999 fDriveControlMoonDist(-1),
3000 fFadControlNumEvents(0),
3001 fFadControlDrsRuns(3),
3002 fFtmControlState(FTM::kFtmLocked),
3003 fRateScanDataId(0),
3004 fRateScanBoard(0),
3005 fFreeSpace(UINT64_MAX),
3006 // ---
3007 fDimMcp ("MCP"),
3008 fDimDataLogger ("DATA_LOGGER"),
3009 fDimDriveControl ("DRIVE_CONTROL"),
3010 fDimTimeCheck ("TIME_CHECK"),
3011 fDimMagicWeather ("MAGIC_WEATHER"),
3012 fDimMagicLidar ("MAGIC_LIDAR"),
3013 fDimTngWeather ("TNG_WEATHER"),
3014 fDimTemperature ("TEMPERATURE"),
3015 fDimFeedback ("FEEDBACK"),
3016 fDimBiasControl ("BIAS_CONTROL"),
3017 fDimFtmControl ("FTM_CONTROL"),
3018 fDimFadControl ("FAD_CONTROL"),
3019 fDimFscControl ("FSC_CONTROL"),
3020 fDimGpsControl ("GPS_CONTROL"),
3021 fDimAgilentControl("AGILENT_CONTROL"),
3022 fDimPwrControl ("PWR_CONTROL"),
3023 fDimLidControl ("LID_CONTROL"),
3024 fDimRateControl ("RATE_CONTROL"),
3025 fDimRateScan ("RATE_SCAN"),
3026 fDimChat ("CHAT"),
3027 fDimSkypeClient ("SKYPE_CLIENT")
3028 {
3029 fDimDNS.Subscribe(*this);
3030 fDimControl.Subscribe(*this);
3031 fDimMcp.Subscribe(*this);
3032 fDimDataLogger.Subscribe(*this);
3033 fDimDriveControl.Subscribe(*this);
3034 fDimTimeCheck.Subscribe(*this);
3035 fDimMagicWeather.Subscribe(*this);
3036 fDimMagicLidar.Subscribe(*this);
3037 fDimTngWeather.Subscribe(*this);
3038 fDimTemperature.Subscribe(*this);
3039 fDimFeedback.Subscribe(*this);
3040 fDimBiasControl.Subscribe(*this);
3041 fDimFtmControl.Subscribe(*this);
3042 fDimFadControl.Subscribe(*this);
3043 fDimFscControl.Subscribe(*this);
3044 fDimGpsControl.Subscribe(*this);
3045 fDimAgilentControl.Subscribe(*this);
3046 fDimPwrControl.Subscribe(*this);
3047 fDimLidControl.Subscribe(*this);
3048 fDimRateControl.Subscribe(*this);
3049 fDimRateScan.Subscribe(*this);
3050 fDimChat.Subscribe(*this);
3051 fDimSkypeClient.Subscribe(*this);
3052
3053 fDimFscControl.SetCallback(bind(&StateMachineSmartFACT::HandleFscControlStateChange, this, placeholders::_1));
3054 //fDimFtmControl.SetCallback(bind(&StateMachineSmartFACT::HandleFtmControlStateChange, this));
3055 fDimDriveControl.SetCallback(bind(&StateMachineSmartFACT::HandleDriveControlStateChange, this, placeholders::_1));
3056 fDimControl.SetCallback(bind(&StateMachineSmartFACT::HandleControlStateChange, this, placeholders::_1));
3057 fDimControl.AddCallback("dotest.dim", bind(&StateMachineSmartFACT::HandleDoTest, this, placeholders::_1));
3058
3059 Subscribe("DIM_CONTROL/MESSAGE")
3060 (bind(&StateMachineSmartFACT::HandleDimControlMessage, this, placeholders::_1));
3061
3062 Subscribe("MCP/CONFIGURATION")
3063 (bind(&StateMachineSmartFACT::HandleMcpConfiguration, this, placeholders::_1));
3064
3065 Subscribe("DRIVE_CONTROL/POINTING_POSITION")
3066 (bind(&StateMachineSmartFACT::HandleDrivePointing, this, placeholders::_1));
3067 Subscribe("DRIVE_CONTROL/TRACKING_POSITION")
3068 (bind(&StateMachineSmartFACT::HandleDriveTracking, this, placeholders::_1));
3069 Subscribe("DRIVE_CONTROL/SOURCE_POSITION")
3070 (bind(&StateMachineSmartFACT::HandleDriveSource, this, placeholders::_1));
3071
3072 Subscribe("FSC_CONTROL/TEMPERATURE")
3073 (bind(&StateMachineSmartFACT::HandleFscTemperature, this, placeholders::_1));
3074 Subscribe("FSC_CONTROL/HUMIDITY")
3075 (bind(&StateMachineSmartFACT::HandleFscHumidity, this, placeholders::_1));
3076 Subscribe("FSC_CONTROL/BIAS_TEMP")
3077 (bind(&StateMachineSmartFACT::HandleFscBiasTemp, this, placeholders::_1));
3078
3079 Subscribe("GPS_CONTROL/NEMA")
3080 (bind(&StateMachineSmartFACT::HandleGpsNema, this, placeholders::_1));
3081
3082 Subscribe("TEMPERATURE/DATA")
3083 (bind(&StateMachineSmartFACT::HandleTemperatureData, this, placeholders::_1));
3084
3085 Subscribe("AGILENT_CONTROL/DATA")
3086 (bind(&StateMachineSmartFACT::HandleAgilentData, this, placeholders::_1));
3087
3088 Subscribe("MAGIC_WEATHER/DATA")
3089 (bind(&StateMachineSmartFACT::HandleMagicWeatherData, this, placeholders::_1));
3090 Subscribe("TNG_WEATHER/DUST")
3091 (bind(&StateMachineSmartFACT::HandleTngWeatherDust, this, placeholders::_1));
3092
3093 Subscribe("FEEDBACK/CALIBRATED_CURRENTS")
3094 (bind(&StateMachineSmartFACT::HandleFeedbackCalibratedCurrents, this, placeholders::_1));
3095
3096 Subscribe("BIAS_CONTROL/VOLTAGE")
3097 (bind(&StateMachineSmartFACT::HandleBiasVoltage, this, placeholders::_1));
3098 Subscribe("BIAS_CONTROL/CURRENT")
3099 (bind(&StateMachineSmartFACT::HandleBiasCurrent, this, placeholders::_1));
3100
3101 Subscribe("FAD_CONTROL/CONNECTIONS")
3102 (bind(&StateMachineSmartFACT::HandleFadConnections, this, placeholders::_1));
3103 Subscribe("FAD_CONTROL/EVENTS")
3104 (bind(&StateMachineSmartFACT::HandleFadEvents, this, placeholders::_1));
3105 Subscribe("FAD_CONTROL/START_RUN")
3106 (bind(&StateMachineSmartFACT::HandleFadStartRun, this, placeholders::_1));
3107 Subscribe("FAD_CONTROL/DRS_RUNS")
3108 (bind(&StateMachineSmartFACT::HandleFadDrsRuns, this, placeholders::_1));
3109 Subscribe("FAD_CONTROL/EVENT_DATA")
3110 (bind(&StateMachineSmartFACT::HandleFadEventData, this, placeholders::_1));
3111 Subscribe("FAD_CONTROL/STATS")
3112 (bind(&StateMachineSmartFACT::HandleStats, this, placeholders::_1));
3113
3114 Subscribe("DATA_LOGGER/STATS")
3115 (bind(&StateMachineSmartFACT::HandleStats, this, placeholders::_1));
3116
3117 Subscribe("FTM_CONTROL/TRIGGER_RATES")
3118 (bind(&StateMachineSmartFACT::HandleFtmTriggerRates, this, placeholders::_1));
3119 Subscribe("FTM_CONTROL/STATIC_DATA")
3120 (bind(&StateMachineSmartFACT::HandleFtmStaticData, this, placeholders::_1));
3121 Subscribe("FTM_CONTROL/FTU_LIST")
3122 (bind(&StateMachineSmartFACT::HandleFtmFtuList, this, placeholders::_1));
3123
3124 Subscribe("RATE_CONTROL/THRESHOLD")
3125 (bind(&StateMachineSmartFACT::HandleRateControlThreshold,this, placeholders::_1));
3126
3127 Subscribe("RATE_SCAN/DATA")
3128 (bind(&StateMachineSmartFACT::HandleRateScanData, this, placeholders::_1));
3129
3130 Subscribe("CHAT/MESSAGE")
3131 (bind(&StateMachineSmartFACT::HandleChatMsg, this, placeholders::_1));
3132
3133
3134 // =================================================================
3135
3136 // State names
3137 AddStateName(kStateDimNetworkNA, "DimNetworkNotAvailable",
3138 "The Dim DNS is not reachable.");
3139
3140 AddStateName(kStateRunning, "Running", "");
3141
3142 // =================================================================
3143
3144 AddEvent("PRINT")
3145 (bind(&StateMachineSmartFACT::Print, this))
3146 ("Print a list of the states of all connected servers.");
3147
3148 }
3149 int EvalOptions(Configuration &conf)
3150 {
3151 if (!fPixelMap.Read(conf.Get<string>("pixel-map-file")))
3152 {
3153 Error("Reading mapping table from "+conf.Get<string>("pixel-map-file")+" failed.");
3154 return 1;
3155 }
3156
3157 fPath = conf.Get<string>("path");
3158 fDatabase = conf.Get<string>("source-database");
3159
3160 struct stat st;
3161 if (stat(fPath.c_str(), &st))
3162 {
3163 Error(fPath+" does not exist!");
3164 return 2;
3165 }
3166
3167 if ((st.st_mode&S_IFDIR)==0)
3168 {
3169 Error(fPath+" not a directory!");
3170 return 3;
3171 }
3172
3173 if ((st.st_mode&S_IWUSR)==0)
3174 {
3175 Error(fPath+" has no write permission!");
3176 return 4;
3177 }
3178
3179 if ((st.st_mode&S_IXUSR)==0)
3180 {
3181 Error(fPath+" has no execute permission!");
3182 return 5;
3183 }
3184
3185 ostringstream out;
3186 out << Time().JavaDate() << '\n';
3187
3188 ofstream(fPath+"/error.data") << out.str();
3189
3190 return -1;
3191 }
3192};
3193
3194bool StateMachineSmartFACT::fIsServer = false;
3195
3196// ------------------------------------------------------------------------
3197
3198#include "Main.h"
3199
3200template<class T>
3201int RunShell(Configuration &conf)
3202{
3203 StateMachineSmartFACT::fIsServer = !conf.Get<bool>("client");
3204 return Main::execute<T, StateMachineSmartFACT>(conf);
3205}
3206
3207void SetupConfiguration(Configuration &conf)
3208{
3209 po::options_description control("Smart FACT");
3210 control.add_options()
3211 ("pixel-map-file", var<string>()->required(), "Pixel mapping file. Used here to get the default reference voltage")
3212 ("path", var<string>("www/smartfact/data"), "Output path for the data-files")
3213 ("source-database", var<string>(""), "Database link as in\n\tuser:password@server[:port]/database.")
3214 ("client", po_bool(false), "For a standalone client choose this option.")
3215 ;
3216
3217 conf.AddOptions(control);
3218}
3219
3220/*
3221 Extract usage clause(s) [if any] for SYNOPSIS.
3222 Translators: "Usage" and "or" here are patterns (regular expressions) which
3223 are used to match the usage synopsis in program output. An example from cp
3224 (GNU coreutils) which contains both strings:
3225 Usage: cp [OPTION]... [-T] SOURCE DEST
3226 or: cp [OPTION]... SOURCE... DIRECTORY
3227 or: cp [OPTION]... -t DIRECTORY SOURCE...
3228 */
3229void PrintUsage()
3230{
3231 cout <<
3232 "SmartFACT is a tool writing the files needed for the SmartFACT web interface.\n"
3233 "\n"
3234 "The default is that the program is started without user intercation. "
3235 "All actions are supposed to arrive as DimCommands. Using the -c "
3236 "option, a local shell can be initialized. With h or help a short "
3237 "help message about the usuage can be brought to the screen.\n"
3238 "\n"
3239 "Usage: smartfact [-c type] [OPTIONS]\n"
3240 " or: smartfact [OPTIONS]\n";
3241 cout << endl;
3242}
3243
3244void PrintHelp()
3245{
3246 Main::PrintHelp<StateMachineSmartFACT>();
3247
3248 /* Additional help text which is printed after the configuration
3249 options goes here */
3250
3251 /*
3252 cout << "bla bla bla" << endl << endl;
3253 cout << endl;
3254 cout << "Environment:" << endl;
3255 cout << "environment" << endl;
3256 cout << endl;
3257 cout << "Examples:" << endl;
3258 cout << "test exam" << endl;
3259 cout << endl;
3260 cout << "Files:" << endl;
3261 cout << "files" << endl;
3262 cout << endl;
3263 */
3264}
3265
3266int main(int argc, const char* argv[])
3267{
3268 Configuration conf(argv[0]);
3269 conf.SetPrintUsage(PrintUsage);
3270 Main::SetupConfiguration(conf);
3271 SetupConfiguration(conf);
3272
3273 if (!conf.DoParse(argc, argv, PrintHelp))
3274 return 127;
3275
3276 if (!conf.Has("console"))
3277 return RunShell<LocalStream>(conf);
3278
3279 if (conf.Get<int>("console")==0)
3280 return RunShell<LocalShell>(conf);
3281 else
3282 return RunShell<LocalConsole>(conf);
3283
3284 return 0;
3285}
Note: See TracBrowser for help on using the repository browser.