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

Last change on this file since 17449 was 17397, checked in by tbretz, 11 years ago
There was a typo in the wind gusts warning; 98 is too high...
File size: 107.4 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> max(1440);
1637 for (int i=0; i<1440; i++)
1638 max[i] = dat[i%9==8 ? i-2 : i]/1000;
1639
1640 const uint32_t trig = d.GetQoS() & FAD::EventHeader::kLPext;
1641
1642 const float min = fFadControlDrsRuns[0]==0 ? -1 : 0;
1643
1644 float scale = 2;
1645 if (trig&FAD::EventHeader::kLPext)
1646 scale = 1;
1647 if (trig==0 || (trig&FAD::EventHeader::kPedestal))
1648 scale = 0.25;
1649
1650 // assume it is drs-gain
1651 //if ((trig&FAD::EventHeader::kPedestal) && fFadControlDrsRuns[0]>0 && fFadControlDrsRuns[1]==0)
1652 // min = 0.75;
1653
1654 WriteCam(d, "cam-fadcontrol-eventdata", max, scale, min);
1655
1656 return GetCurrentState();
1657 }
1658
1659 int HandleStats(const EventImp &d)
1660 {
1661 if (!CheckDataSize(d, "Stats", 4*8))
1662 {
1663 fFreeSpace = UINT64_MAX;
1664 return GetCurrentState();
1665 }
1666
1667 const DimWriteStatistics::Stats &s = d.Ref<DimWriteStatistics::Stats>();
1668 fFreeSpace = s.freeSpace;
1669
1670 return GetCurrentState();
1671 }
1672
1673 int HandleFscTemperature(const EventImp &d)
1674 {
1675 if (!CheckDataSize(d, "FscControl:Temperature", 240))
1676 return GetCurrentState();
1677
1678 const float *ptr = d.Ptr<float>(4);
1679
1680 double avg = 0;
1681 double rms = 0;
1682 double min = 99;
1683 double max = -99;
1684
1685 int num = 0;
1686 for (const float *t=ptr; t<ptr+31; t++)
1687 {
1688 if (*t==0)
1689 continue;
1690
1691 if (*t>max)
1692 max = *t;
1693
1694 if (*t<min)
1695 min = *t;
1696
1697 avg += *t;
1698 rms += *t * *t;
1699
1700 num++;
1701 }
1702
1703 avg /= num;
1704 rms /= num;
1705 rms += avg*avg;
1706 rms = rms<0 ? 0 : sqrt(rms);
1707
1708 // Clean broken reports
1709 static double pre_rms1 = 1.5;
1710 static double pre_rms2 = 0;
1711
1712 const double cut = pre_rms1 + 0.1;
1713
1714 const bool reject = rms>cut && pre_rms2<cut;
1715
1716 pre_rms2 = pre_rms1;
1717 pre_rms1 = rms;
1718
1719 if (reject)
1720 return GetCurrentState();
1721
1722
1723 if (!fMagicWeatherHist[kTemp].empty())
1724 {
1725 fFscControlTemperatureHist.push_back(avg-fMagicWeatherHist[kTemp].back());
1726 if (fFscControlTemperatureHist.size()>300)
1727 fFscControlTemperatureHist.pop_front();
1728 }
1729
1730 const Statistics stat(fFscControlTemperatureHist);
1731
1732 ostringstream out;
1733 out << setprecision(3);
1734 out << d.GetJavaDate() << '\n';
1735 out << HTML::kWhite << '\t' << fFscControlHumidityAvg << '\n';
1736 out << HTML::kWhite << '\t' << stat.min << '\n';
1737 out << HTML::kWhite << '\t' << stat.avg << '\n';
1738 out << HTML::kWhite << '\t' << stat.max << '\n';
1739
1740 ofstream(fPath+"/fsc.data") << out.str();
1741
1742 WriteHist(d, "hist-fsccontrol-temperature",
1743 fFscControlTemperatureHist, 10);
1744
1745 out.str("");
1746 out << setprecision(3);
1747 out << d.GetJavaDate() << '\n';
1748 out << HTML::kWhite << '\t' << max << '\n';
1749 out << HTML::kWhite << '\t' << avg << '\n';
1750 out << HTML::kWhite << '\t' << min << '\n';
1751
1752 ofstream(fPath+"/camtemp.data") << out.str();
1753
1754 return GetCurrentState();
1755 }
1756
1757 int HandleFscBiasTemp(const EventImp &d)
1758 {
1759 if (!CheckDataSize(d, "FscControl:BiasTemp", 323*4))
1760 return GetCurrentState();
1761
1762 const float *ptr = d.Ptr<float>(4);
1763 const float avg = d.Get<float>(321*4);
1764 //const float rms = d.Get<float>(322*4);
1765
1766 vector<double> tout(320);
1767 for (int i=0; i<320; i++)
1768 {
1769 const int idx = (fPixelMap.hv(i).hw()/9)*2+fPixelMap.hv(i).group();
1770 tout[idx] = ptr[i];
1771 }
1772
1773 WriteCam(d, "cam-fsccontrol-temperature", tout, 3, avg-1.75);
1774
1775 return GetCurrentState();
1776 }
1777
1778 int HandleFscHumidity(const EventImp &d)
1779 {
1780 if (!CheckDataSize(d, "FscControl:Humidity", 5*4))
1781 return GetCurrentState();
1782
1783 const float *ptr = d.Ptr<float>(4);
1784
1785 double avg =0;
1786 int num = 0;
1787
1788 for (const float *t=ptr; t<ptr+4; t++)
1789 if (*t>0 && *t<=100 && t!=ptr+2 /*excl broken sensor*/)
1790 {
1791 avg += *t;
1792 num++;
1793 }
1794
1795 fFscControlHumidityAvg = num>0 ? avg/num : 0;
1796
1797 return GetCurrentState();
1798 }
1799
1800 int HandleGpsNema(const EventImp &d)
1801 {
1802 if (!CheckDataSize(d, "GpsControl:Nema", sizeof(GPS::NEMA)))
1803 return GetCurrentState();
1804
1805 const GPS::NEMA &nema = d.Ref<GPS::NEMA>();
1806
1807 ostringstream out;
1808
1809 out << fixed;
1810 out << d.GetJavaDate() << '\n';
1811
1812 switch (nema.qos)
1813 {
1814 case 1: out << HTML::kGreen << "\tGPS fix [1]\n"; break;
1815 case 2: out << HTML::kGreen << "\tDifferential fix [2]\n"; break;
1816 default: out << HTML::kRed << "\tinvalid [" << nema.qos << "]\n"; break;
1817 }
1818
1819 out << HTML::kWhite << '\t' << nema.count << '\n';
1820 out << HTML::kWhite << '\t' << Time(floor(Time().Mjd())+nema.time).GetAsStr("%H:%M:%S") << '\n';
1821 out << HTML::kWhite << '\t' << setprecision(4) << nema.lat << '\n';
1822 out << HTML::kWhite << '\t' << setprecision(4) << nema.lng << '\n';
1823 out << HTML::kWhite << '\t' << setprecision(1) << nema.height << "\n";
1824 out << HTML::kWhite << '\t' << setprecision(1) << nema.hdop << "\n";
1825 out << HTML::kWhite << '\t' << setprecision(1) << nema.geosep << "\n";
1826
1827 ofstream(fPath+"/gps.data") << out.str();
1828
1829 return GetCurrentState();
1830 }
1831
1832 string GetTempColor(float t)
1833 {
1834 if (t>25 && t<30)
1835 return HTML::kGreen;
1836
1837 if (t<20 || t>35)
1838 return HTML::kRed;
1839
1840 return HTML::kYellow;
1841 }
1842
1843 int HandleTemperatureData(const EventImp &d)
1844 {
1845 if (!CheckDataSize(d, "Temperature:Data", 3*sizeof(float)))
1846 return GetCurrentState();
1847
1848 const float *temp = d.Ptr<float>();
1849
1850 ostringstream out;
1851
1852 out << fixed << setprecision(1);
1853 out << d.GetJavaDate() << '\n';
1854
1855 out << GetTempColor(temp[1]) << '\t' << temp[1] << '\n';
1856 out << GetTempColor(temp[0]) << '\t' << temp[0] << '\n';
1857 out << GetTempColor(temp[2]) << '\t' << temp[2] << '\n';
1858
1859 ofstream(fPath+"/temperature.data") << out.str();
1860
1861 fTemperatureControlHist.push_back(temp[0]);
1862 if (fTemperatureControlHist.size()>60) // 1h
1863 fTemperatureControlHist.pop_front();
1864
1865 WriteHist(d, "hist-temperaturecontrol",
1866 fTemperatureControlHist, 45, 0);
1867
1868 return GetCurrentState();
1869 }
1870
1871 int HandleAgilentData(const EventImp &d)
1872 {
1873 if (!CheckDataSize(d, "Agilent:Data", 4*sizeof(float)))
1874 return GetCurrentState();
1875
1876 const float *data = d.Ptr<float>();
1877
1878 ostringstream out;
1879
1880 out << fixed << setprecision(1);
1881 out << d.GetJavaDate() << '\n';
1882
1883 out << HTML::kWhite << '\t' << data[0] << '\n';
1884 out << HTML::kWhite << '\t' << data[1] << '\n';
1885 out << HTML::kWhite << '\t' << data[2] << '\n';
1886 out << HTML::kWhite << '\t' << data[3] << '\n';
1887
1888 ofstream(fPath+"/agilent.data") << out.str();
1889
1890 return GetCurrentState();
1891 }
1892
1893 int HandleRateScanData(const EventImp &d)
1894 {
1895 if (!CheckDataSize(d, "RateScan:Data", 824))
1896 return GetCurrentState();
1897
1898 const uint64_t id = d.Get<uint64_t>();
1899 const float *rate = d.Ptr<float>(20);
1900
1901 if (fRateScanDataId!=id)
1902 {
1903 for (int i=0; i<41; i++)
1904 fRateScanDataHist[i].clear();
1905 fRateScanDataId = id;
1906 }
1907 fRateScanDataHist[0].push_back(log10(rate[0]));
1908
1909 double max = 0;
1910 for (int i=1; i<41; i++)
1911 {
1912 fRateScanDataHist[i].push_back(log10(rate[i]));
1913 if (rate[i]>max)
1914 max = rate[i];
1915 }
1916
1917 // Cycle by time!
1918 fRateScanBoard ++;
1919 fRateScanBoard %= 40;
1920
1921 WriteHist(d, "hist-ratescan", fRateScanDataHist[0], 10, -2);
1922 WriteCam(d, "cam-ratescan-board", fRateScanDataHist[fRateScanBoard+1], 10, -4);
1923
1924 ostringstream out;
1925 out << setprecision(3);
1926 out << d.GetJavaDate() << '\n';
1927 out << HTML::kWhite << '\t' << fFtmBoardThresholdMed << '\n';
1928 out << HTML::kWhite << '\t' << fFtmPatchThresholdMed << '\n';
1929 out << HTML::kWhite << '\t' << floor(pow(10, fRateScanDataHist[0].back())+.5) << '\n';
1930 out << HTML::kWhite << '\t' << floor(max+.5) << '\n';
1931
1932 ofstream(fPath+"/ratescan.data") << out.str();
1933
1934 out.str("");
1935 out << d.GetJavaDate() << '\n';
1936 out << HTML::kWhite << '\t' << int(fRateScanBoard) << '\n';
1937 out << HTML::kWhite << '\t' << pow(10, fRateScanDataHist[fRateScanBoard+1].back()) << '\n';
1938
1939 ofstream(fPath+"/ratescan_board.data") << out.str();
1940
1941 return GetCurrentState();
1942 }
1943
1944 int HandleRateControlThreshold(const EventImp &d)
1945 {
1946 if (!CheckDataSize(d, "RateControl:Threshold", 18))
1947 return GetCurrentState();
1948
1949 const uint16_t th = d.Get<uint16_t>();
1950
1951 fRateControlThreshold.push_back(th);
1952 if (fRateControlThreshold.size()>300)
1953 fRateControlThreshold.pop_front();
1954
1955 WriteHist(d, "hist-ratecontrol-threshold", fRateControlThreshold, 1000);
1956
1957 return GetCurrentState();
1958 }
1959
1960 int HandleChatMsg(const EventImp &d)
1961 {
1962 if (d.GetSize()==0 || d.GetQoS()!=MessageImp::kComment)
1963 return GetCurrentState();
1964
1965 if (Time()<d.GetTime()+boost::posix_time::minutes(1))
1966 SetAudio("message");
1967
1968 fChatHist.add(d.GetText(), d.GetTime());
1969
1970 ostringstream out;
1971 out << setprecision(3);
1972 out << Header(d) << '\n';
1973 out << HTML::kWhite << '\t';
1974 out << "<->" << fChatHist.rget() << "</->";
1975 out << '\n';
1976
1977 ofstream(fPath+"/chat.data") << out.str();
1978
1979 return GetCurrentState();
1980 }
1981
1982 // -------------------------------------------------------------------
1983
1984 int HandleDoTest(const EventImp &d)
1985 {
1986 ostringstream out;
1987 out << d.GetJavaDate() << '\n';
1988
1989 switch (d.GetQoS())
1990 {
1991 case -3: out << HTML::kWhite << "\tNot running\n"; break;
1992 case -2: out << HTML::kBlue << "\tLoading\n"; break;
1993 case -1: out << HTML::kBlue << "\tStarted\n"; break;
1994 default: out << HTML::kGreen << "\tRunning [" << d.GetQoS() << "]\n"; break;
1995 }
1996
1997 ofstream(fPath+"/dotest.data") << out.str();
1998
1999 return StateMachineImp::kSM_KeepState;
2000 }
2001
2002 // -------------------------------------------------------------------
2003
2004 /*
2005 bool CheckEventSize(size_t has, const char *name, size_t size)
2006 {
2007 if (has==size)
2008 return true;
2009
2010 ostringstream msg;
2011 msg << name << " - Received event has " << has << " bytes, but expected " << size << ".";
2012 Fatal(msg);
2013 return false;
2014 }*/
2015
2016 int Print() const
2017 {
2018 Out() << fDimDNS << endl;
2019 Out() << fDimMcp << endl;
2020 Out() << fDimControl << endl;
2021 Out() << fDimDataLogger << endl;
2022 Out() << fDimDriveControl << endl;
2023 Out() << fDimTimeCheck << endl;
2024 Out() << fDimFadControl << endl;
2025 Out() << fDimFtmControl << endl;
2026 Out() << fDimBiasControl << endl;
2027 Out() << fDimFeedback << endl;
2028 Out() << fDimRateControl << endl;
2029 Out() << fDimFscControl << endl;
2030 Out() << fDimAgilentControl << endl;
2031 Out() << fDimPwrControl << endl;
2032 Out() << fDimLidControl << endl;
2033 Out() << fDimMagicWeather << endl;
2034 Out() << fDimTngWeather << endl;
2035 Out() << fDimMagicLidar << endl;
2036 Out() << fDimTemperature << endl;
2037 Out() << fDimRateScan << endl;
2038 Out() << fDimChat << endl;
2039 Out() << fDimSkypeClient << endl;
2040
2041 return GetCurrentState();
2042 }
2043
2044 string GetStateHtml(const DimState &state, int green) const
2045 {
2046 if (!state.online())
2047 return HTML::kWhite+"\t&mdash;\n";
2048
2049 if (&state==&fDimControl)
2050 return HTML::kGreen +'\t'+(state.state()==0?"Idle":fDimControl.shortmsg)+'\n';
2051
2052 const State rc = state.description();
2053
2054 // Sate not found in list, server online (-3: offline; -2: not found)
2055 if (rc.index==-2)
2056 {
2057 ostringstream out;
2058 out << HTML::kWhite << '\t' << state.state() << '\n';
2059 return out.str();
2060 }
2061
2062 //ostringstream msg;
2063 //msg << HTML::kWhite << '\t' << rc.name << " [" << rc.index << "]\n";
2064 //return msg.str();
2065
2066 if (rc.index<0)
2067 return HTML::kWhite + "\t&mdash;\n";
2068
2069 string col = HTML::kGreen;
2070 if (rc.index<green)
2071 col = HTML::kYellow;
2072 if (rc.index>0xff)
2073 col = HTML::kRed;
2074
2075 return col + '\t' + rc.name + '\n';
2076 }
2077
2078 bool SetError(bool b, const string &err)
2079 {
2080 if (!b)
2081 {
2082 fErrorList.erase(err);
2083 return 0;
2084 }
2085
2086 const bool isnew = fErrorList.insert(err).second;
2087 if (isnew)
2088 fErrorHist.add(err);
2089
2090 return isnew;
2091 }
2092
2093#ifdef HAVE_NOVA
2094
2095 vector<pair<Nova::EquPosn, double>> fMoonCoords;
2096
2097 void CalcMoonCoords(double jd)
2098 {
2099 jd = floor(jd);
2100
2101 fMoonCoords.clear();
2102 for (double h=0; h<1; h+=1./(24*12))
2103 {
2104 const Nova::EquPosn moon = Nova::GetLunarEquCoords(jd+h,0.01);
2105
2106 const double disk = Nova::GetLunarDisk(jd+h);
2107
2108 fMoonCoords.emplace_back(moon, disk);
2109 }
2110 }
2111
2112 pair<vector<float>, pair<Time, float>> GetVisibility(Nova::EquPosn *src, double jd)
2113 {
2114 jd = floor(jd);
2115
2116 const double jd0 = fmod(fSun.fSetAstronomical.JD(), 1);
2117 const double jd1 = fmod(fSun.fRiseAstronomical.JD(), 1);
2118
2119 Nova::EquPosn moon;
2120 Nova::EquPosn *pos = src ? src : &moon;
2121
2122 double max = 0;
2123 double maxjd = 0;
2124
2125 int cnt = 0;
2126
2127 int i=0;
2128
2129 vector<float> alt;
2130 for (double h=0; h<1; h+=1./(24*12), i++)
2131 {
2132 if (src==0)
2133 moon = fMoonCoords[i].first; //ln_get_lunar_equ_coords_prec(jd+h, &moon, 0.01);
2134
2135 const Nova::HrzPosn hrz = Nova::GetHrzFromEqu(*pos, jd+h);
2136
2137 if (h>jd0 && h<jd1)
2138 alt.push_back(hrz.alt);
2139
2140 if (hrz.alt>max)
2141 {
2142 max = hrz.alt;
2143 maxjd = jd+h;
2144 }
2145
2146 if (h>jd0 && h<jd1 && hrz.alt>15)
2147 cnt++;
2148 }
2149
2150 if (max<=15 || cnt==0)
2151 return make_pair(vector<float>(), make_pair(Time(), 0));
2152
2153 return make_pair(alt, make_pair(maxjd, maxjd>jd+jd0&&maxjd<jd+jd1?max:0));
2154 }
2155
2156 pair<vector<float>, pair<Time, float>> GetLightCondition(double jd)
2157 {
2158 jd = floor(jd);
2159
2160 const double jd0 = fmod(fSun.fSetAstronomical.JD(), 1);
2161 const double jd1 = fmod(fSun.fRiseAstronomical.JD(), 1);
2162
2163 double max = -1;
2164 double maxjd = 0;
2165
2166 int cnt = 0;
2167 int i = 0;
2168
2169 vector<float> vec;
2170 for (double h=0; h<1; h+=1./(24*12), i++)
2171 {
2172 double cur = -1;
2173
2174 if (h>jd0 && h<jd1)
2175 {
2176 Nova::EquPosn moon = fMoonCoords[i].first;//ln_get_lunar_equ_coords_prec(jd+h, &moon, 0.01);
2177 const double disk = fMoonCoords[i].second;//ln_get_lunar_disk(jd+h);
2178
2179 const Nova::HrzPosn hrz = Nova::GetHrzFromEqu(moon, jd+h);
2180 const double dist = Nova::GetLunarEarthDist(jd+h);
2181
2182 // Current prediction
2183 const double calt = sin(hrz.alt*M_PI/180);
2184
2185 const double lc = calt>0 ? calt*pow(disk, 2.2)*pow(dist, -2) : -1;
2186 cur = lc>0 ? 4+103*lc : -1;
2187
2188 vec.push_back(cur); // Covert LC to pixel current in uA
2189 }
2190
2191 if (cur>max)
2192 {
2193 max = cur;
2194 maxjd = jd+h;
2195 }
2196
2197 if (h>jd0 && h<jd1 && cur>0)
2198 cnt++;
2199 }
2200
2201 if (max<=0 || cnt==0)
2202 return make_pair(vector<float>(), make_pair(Time(), 0));
2203
2204 return make_pair(vec, make_pair(maxjd, maxjd>jd+jd0&&maxjd<jd+jd1?max:-1));
2205 }
2206#endif
2207
2208 void UpdateAstronomy()
2209 {
2210 Time now;
2211
2212 CalcMoonCoords(now.JD());
2213
2214 fSun = Sun (now);
2215 fMoon = Moon(now);
2216
2217 vector<string> color(8, HTML::kWhite);
2218 color[fSun.state%8] = HTML::kBlue;
2219
2220 ostringstream out;
2221 out << setprecision(3);
2222 out << now.JavaDate() << '\n';
2223 out << color[0] << '\t' << fSun.fRiseDarkTime.GetAsStr("%H:%M") << '\n';
2224 out << color[1] << '\t' << fSun.fRiseAstronomical.GetAsStr("%H:%M") << '\n';
2225 out << color[2] << '\t' << fSun.fRiseCivil.GetAsStr("%H:%M") << '\n';
2226 out << color[3] << '\t' << fSun.fRiseDayTime.GetAsStr("%H:%M") << '\n';
2227
2228 out << color[4] << '\t' << fSun.fSetDayTime.GetAsStr("%H:%M") << '\n';
2229 out << color[5] << '\t' << fSun.fSetCivil.GetAsStr("%H:%M") << '\n';
2230 out << color[6] << '\t' << fSun.fSetAstronomical.GetAsStr("%H:%M") << '\n';
2231 out << color[7] << '\t' << fSun.fSetDarkTime.GetAsStr("%H:%M") << '\n';
2232
2233 ofstream(fPath+"/sun.data") << out.str();
2234
2235 color.assign(3, HTML::kWhite);
2236 color[fMoon.state%3] = HTML::kBlue;
2237
2238 out.str("");
2239 out << now.JavaDate() << '\n';
2240
2241 out << color[0] << '\t' << fMoon.fRise.GetAsStr("%H:%M") << '\n';
2242 out << color[1] << '\t' << fMoon.fTransit.GetAsStr("%H:%M") << '\n';
2243 out << color[2] << '\t' << fMoon.fSet.GetAsStr("%H:%M") << '\n';
2244
2245 out << (fSun.isday?HTML::kWhite:fMoon.color) << '\t' << fMoon.description << '\n';
2246
2247 if (!fMoon.visible)
2248 out << HTML::kWhite << "\t&mdash;\t\n";
2249 else
2250 {
2251 string col = HTML::kWhite;
2252 if (!fSun.isday)
2253 {
2254 col = HTML::kGreen;
2255 if (fMoon.zd>25)
2256 col = HTML::kYellow;
2257 if (fMoon.zd>45 && fMoon.zd<80)
2258 col = HTML::kRed;
2259 if (fMoon.zd>=80)
2260 col = HTML::kRed;
2261 }
2262 out << col << '\t' << fMoon.zd << '\t' << GetDir(fMoon.az) << '\n';
2263 }
2264
2265 ostringstream out2, out3, out4;
2266 out2 << setprecision(3);
2267 out2 << now.JavaDate() << '\n';
2268 out3 << now.JavaDate() << '\n';
2269 out4 << now.JavaDate() << '\n';
2270
2271 struct Entry
2272 {
2273 string name;
2274 float value;
2275 int color;
2276 Entry(const string &n, float v, int c) : name(n), value(v), color(c%8) { }
2277
2278 const string &Col() const
2279 {
2280 // If this list is updatd the number count in the constructor needs
2281 // to be updated, too
2282 static const string hcol[] = { "888", "8cf", "c8f", "bbb", "8fc", "cf8", "f8c", "fc8" };
2283 return hcol[color];
2284 }
2285
2286 vector<float> GetColor(double scale, double offset=0) const
2287 {
2288 vector<float> rc(3);
2289 rc[0] = double(Col()[0])*scale/126+offset;
2290 rc[1] = double(Col()[1])*scale/126+offset;
2291 rc[2] = double(Col()[2])*scale/126+offset;
2292 return rc;
2293 }
2294 };
2295
2296 multimap<Time, Entry> culmination;
2297 multimap<Time, Entry> lightcond;
2298 vector<vector<float>> alt;
2299 vector<vector<float>> cur;
2300
2301#ifdef HAVE_NOVA
2302 int ccol = 0;
2303 int lcol = 0;
2304
2305 /*const*/ pair<vector<float>, pair<Time, float>> vism = GetVisibility(0, now.JD());
2306 if (!vism.first.empty())
2307 {
2308 const Entry entry("Moon", vism.second.second, ccol);
2309 culmination.insert(make_pair(vism.second.first, entry));
2310 const vector<float> col = entry.GetColor(75, 15);
2311 vism.first.insert(vism.first.begin(), col.begin(), col.end());
2312 alt.push_back(vism.first);
2313
2314 ccol++;
2315 }
2316#endif
2317
2318#ifdef HAVE_SQL
2319 try
2320 {
2321 const mysqlpp::StoreQueryResult res =
2322 Database(fDatabase).query("SELECT fSourceName, fRightAscension, fDeclination FROM Source WHERE fSourceTypeKEY=1").store();
2323
2324 out << HTML::kWhite << '\t';
2325 out2 << HTML::kWhite << '\t';
2326 out3 << HTML::kWhite << '\t';
2327 out4 << HTML::kWhite << '\t';
2328
2329 for (vector<mysqlpp::Row>::const_iterator v=res.begin(); v<res.end(); v++)
2330 {
2331 const string name = (*v)[0].c_str();
2332 const double ra = (*v)[1];
2333 const double dec = (*v)[2];
2334#ifdef HAVE_NOVA
2335 Nova::EquPosn pos;
2336 pos.ra = ra*15;
2337 pos.dec = dec;
2338
2339 const Nova::ZdAzPosn hrz = Nova::GetHrzFromEqu(pos, now.JD());
2340
2341 /*const*/ pair<vector<float>, pair<Time, float>> vis = GetVisibility(&pos, now.JD());
2342 if (!vis.first.empty())
2343 {
2344 const Entry entry(name, vis.second.second, ccol);
2345 culmination.insert(make_pair(vis.second.first, entry));
2346 const vector<float> col = entry.GetColor(75, 15);
2347 vis.first.insert(vis.first.begin(), col.begin(), col.end());
2348 alt.push_back(vis.first);
2349
2350 ccol++;
2351
2352 /*const*/ pair<vector<float>, pair<Time, float>> lc = GetLightCondition(now.JD());
2353 if (!lc.first.empty())
2354 {
2355 const Entry entry2(name, lc.second.second, lcol);
2356 lightcond.insert(make_pair(lc.second.first, entry2));
2357 const vector<float> col2 = entry2.GetColor(100);
2358 lc.first.insert(lc.first.begin(), col2.begin(), col2.end());
2359 cur.push_back(lc.first);
2360
2361 lcol++;
2362 }
2363 }
2364
2365 string col = HTML::kWhite;
2366 if (hrz.zd<85)
2367 col = HTML::kRed;
2368 if (hrz.zd<65)
2369 col = HTML::kYellow;
2370 if (hrz.zd<30)
2371 col = HTML::kGreen;
2372
2373 out2 << "<tr bgcolor='" << col << "'>";
2374 out2 << "<td>" << name << "</td>";
2375 if (hrz.zd<85)
2376 {
2377 out2 << "<td>" << hrz.zd << "&deg;</td>";
2378 out2 << "<td>" << GetDir(hrz.az) << "</td>";
2379 }
2380 else
2381 out2 << "<td/><td/>";
2382 out2 << "</tr>";
2383#endif
2384 const int32_t angle = fMoon.Angle(ra, dec);
2385
2386 out << "<tr bgcolor='" << Moon::Color(angle) << "'>";
2387 out << "<td>" << name << "</td>";
2388 out << "<td>" << round(angle) << "&deg;</td>";
2389 out << "</tr>";
2390 }
2391
2392 for (auto it=culmination.begin(); it!=culmination.end(); it++)
2393 {
2394 const Entry &e = it->second;
2395 if (it!=culmination.begin())
2396 out3 << ", ";
2397 out3 << "<B#" << e.Col() << ">" << e.name << "</B>";
2398 if (e.value>0)
2399 out3 << " [" << nearbyint(90-e.value) << "&deg;]";
2400 }
2401
2402 out4 << setprecision(3);
2403
2404 for (auto it=lightcond.begin(); it!=lightcond.end(); it++)
2405 {
2406 const Entry &e = it->second;
2407 if (it!=lightcond.begin())
2408 out4 << ", ";
2409 out4 << "<B#" << e.Col() << ">" << e.name << "</B>";
2410 if (e.value>0)
2411 out4 << " [" << nearbyint(e.value) << "]";
2412 }
2413
2414 if (fSun.fSetAstronomical>fSun.fRiseAstronomical)
2415 fSun.fSetAstronomical += boost::posix_time::hours(24);
2416
2417 ostringstream title;
2418 title << fSun.fSetAstronomical.GetAsStr("%H:%M");
2419 title << " / ";
2420 title << ((fSun.fRiseAstronomical-fSun.fSetAstronomical)/20).minutes();
2421 title << "' / ";
2422 title << fSun.fRiseAstronomical.GetAsStr("%H:%M");
2423
2424 out << '\n';
2425 out2 << '\n';
2426 out3 << '\n';
2427 out4 << '\n';
2428 out << HTML::kWhite << '\t' << Time()-now << '\n';
2429 out2 << HTML::kWhite << '\t' << Time()-now << '\n';
2430
2431 WriteBinaryVec(now, "hist-visibility", alt, 75, 15, "Alt "+title.str());
2432 WriteBinaryVec(now, "hist-current-prediction", cur, 100, 0, "I " +title.str());
2433 }
2434 catch (const exception &e)
2435 {
2436 out << '\n';
2437 out2 << '\n';
2438 out << HTML::kWhite << '\t' << "ERROR - "+string(e.what()) << '\n';
2439 out2 << HTML::kWhite << '\t' << "ERROR - "+string(e.what()) << '\n';
2440 out3 << HTML::kWhite << '\t' << "ERROR - "+string(e.what()) << '\n';
2441 out4 << HTML::kWhite << '\t' << "ERROR - "+string(e.what()) << '\n';
2442 }
2443#endif
2444
2445 ofstream(fPath+"/moon.data") << out.str();
2446 ofstream(fPath+"/source-list.data") << out2.str();
2447 ofstream(fPath+"/visibility.data") << out3.str();
2448 ofstream(fPath+"/current-prediction.data") << out4.str();
2449 }
2450
2451 int Execute()
2452 {
2453 Time now;
2454 if (now-fLastUpdate<boost::posix_time::seconds(1))
2455 return fDimDNS.online() ? kStateRunning : kStateDimNetworkNA;
2456 fLastUpdate=now;
2457
2458 // ==============================================================
2459
2460 const bool data_taking =
2461 fDimMcp.state()==MCP::State::kTriggerOn ||
2462 fDimMcp.state()==MCP::State::kTakingData;
2463
2464 const bool data_run =
2465 fMcpConfigurationName=="data" ||
2466 fMcpConfigurationName=="data-rt";
2467
2468 const bool bias_on =
2469 fDimBiasControl.state()==BIAS::State::kRamping ||
2470 fDimBiasControl.state()==BIAS::State::kOverCurrent ||
2471 fDimBiasControl.state()==BIAS::State::kVoltageOn;
2472
2473 const bool calibrated =
2474 fDimFeedback.state()>=Feedback::State::kCalibrated;
2475
2476 const bool haderr = !fErrorList.empty();
2477
2478 bool newerr = false;
2479
2480 newerr |= SetError(!fDimDNS.online(),
2481 "<b><#darkred>DIM network not available</#></b>");
2482 newerr |= SetError(!fDimControl.online(),
2483 "<b>no dimctrl server available</b>");
2484 newerr |= SetError(fDimDataLogger.state()<20 || fDimDataLogger.state()>40,
2485 "<b>datalogger not ready</b>");
2486
2487 //newerr |= SetError(fDimDriveControl.state()==Drive::State::kLocked,
2488 // "<b><#darkred>Drive in LOCKED state, drive was automatically parked</#></b>");
2489
2490 newerr |= SetError(fDimDriveControl.state()>0xff && data_taking && data_run,
2491 "Drive in ERROR state during data-run");
2492 newerr |= SetError(fDriveControlMoonDist>155,
2493 "Moon within the field-of-view of the cones");
2494 newerr |= SetError(fDriveControlMoonDist>=0 && fDriveControlMoonDist<3,
2495 "Moon within the field-of-view of the camera");
2496
2497 newerr |= SetError(fDimBiasControl.state()<BIAS::State::kRamping && data_taking && data_run,
2498 "BIAS not operating during data-run");
2499 newerr |= SetError(fDimBiasControl.state()==BIAS::State::kOverCurrent,
2500 "BIAS channels in OverCurrent");
2501 newerr |= SetError(fDimBiasControl.state()==BIAS::State::kNotReferenced,
2502 "BIAS voltage not at reference");
2503
2504
2505 newerr |= SetError(bias_on && calibrated && fBiasControlCurrentMed>90,
2506 "Median current exceeds 90&micro;A/pix");
2507 newerr |= SetError(bias_on && calibrated && fBiasControlCurrentMax>110,
2508 "Maximum current exceeds 110&micro;A/pix");
2509
2510 newerr |= SetError(fFscControlHumidityAvg>60,
2511 "Average camera humidity exceed 60%");
2512
2513 newerr |= SetError(!fMagicWeatherHist[kHum].empty() && fMagicWeatherHist[kHum].back()>98 && data_taking,
2514 "Outside humidity exceeds 98% during data-taking");
2515 newerr |= SetError(!fMagicWeatherHist[kGusts].empty() && fMagicWeatherHist[kGusts].back()>50 && (fDimDriveControl.state()==Drive::State::kTracking||fDimDriveControl.state()==Drive::State::kOnTrack),
2516 "Wind gusts exceed 50km/h during tracking");
2517
2518 newerr |= SetError(fDimFscControl.state()>=FSC::State::kConnected && !fFscControlTemperatureHist.empty() && fFscControlTemperatureHist.back()>15,
2519 "Sensor temperature exceeds outside temperature by more than 15&deg;C");
2520
2521 newerr |= SetError(fFtmControlTriggerRateTooLow>0,
2522 "Trigger rate below 1Hz while trigger switched on");
2523
2524 newerr |= SetError(fFtmControlState!=FTM::kFtmConfig && (fFtmControlState&FTM::kFtmLocked)==0,
2525 "FTM - clock conditioner not locked!");
2526
2527 newerr |= SetError(fDimTimeCheck.state()==1,
2528 "Warning NTP time difference of drive PC exceeds 1s");
2529 newerr |= SetError(fDimTimeCheck.state()<1,
2530 "Warning timecheck not running");
2531
2532 newerr |= SetError(fDimBiasControl.state()==BIAS::State::kVoltageOn &&
2533 fDimFeedback.state()<Feedback::State::kCalibrating &&
2534 fBiasControlVoltageMed>3,
2535 "Bias voltage switched on, but bias crate not calibrated");
2536
2537 newerr |= SetError(fLastRunFinishedWithZeroEvents,
2538 "Last run finshed, but contained zero events.");
2539
2540 newerr |= SetError(fFreeSpace<50000000000,
2541 "Less than 50GB disk space left.");
2542
2543 newerr |= SetError(fDimPwrControl.state()==Power::State::kCoolingFailure,
2544 "Cooling unit reports failure!");
2545
2546 for (auto it=fControlAlarmHist.begin(); it!=fControlAlarmHist.end(); it++)
2547 newerr |= SetError(it->time.IsValid(), it->msg);
2548 fControlAlarmHist.clean();;
2549
2550 fLastRunFinishedWithZeroEvents = false;
2551
2552 // FTM in Connected instead of Idle --> power cyclen
2553
2554 /* // Check offline and disconnected status?
2555 Out() << fDimMcp << endl;
2556 Out() << fDimControl << endl;
2557 Out() << fDimDataLogger << endl;
2558 Out() << fDimDriveControl << endl;
2559 Out() << fDimFadControl << endl;
2560 Out() << fDimFtmControl << endl;
2561 Out() << fDimBiasControl << endl;
2562 Out() << fDimFeedback << endl;
2563 Out() << fDimRateControl << endl;
2564 Out() << fDimFscControl << endl;
2565 Out() << fDimMagicWeather << endl;
2566 Out() << fDimRateScan << endl;
2567 Out() << fDimChat << endl;
2568 */
2569
2570 // FTU in error
2571 // FAD lost
2572
2573 // --------------------------------------------------------------
2574 ostringstream out;
2575
2576 if (newerr)
2577 {
2578 SetAudio("error");
2579
2580 out << now.JavaDate() << '\n';
2581 out << HTML::kWhite << '\t';
2582 out << "<->" << fErrorHist.rget() << "<->";
2583 out << '\n';
2584
2585 ofstream(fPath+"/errorhist.data") << out.str();
2586 }
2587
2588 out.str("");
2589 out << Header(now) << '\t' << (!fErrorList.empty()) << '\t' << (fDimControl.state()>0) << '\n';
2590 out << setprecision(3);
2591 out << HTML::kWhite << '\t';
2592 for (auto it=fErrorList.begin(); it!=fErrorList.end(); it++)
2593 out << *it << "<br/>";
2594 out << '\n';
2595
2596 if (haderr || !fErrorList.empty())
2597 ofstream(fPath+"/error.data") << out.str();
2598
2599 // ==============================================================
2600
2601 out.str("");
2602 out << Header(now) << '\t' << (!fErrorList.empty()) << '\t' << (fDimControl.state()>0) << '\n';
2603 out << setprecision(3);
2604
2605 // -------------- System status --------------
2606 if (fDimDNS.online() && fDimMcp.state()>=MCP::State::kIdle) // Idle
2607 {
2608 string col = HTML::kBlue;
2609 switch (fMcpConfigurationState)
2610 {
2611 case MCP::State::kIdle:
2612 case DimState::kOffline:
2613 col = HTML::kWhite;
2614 break;
2615 case MCP::State::kConfiguring1:
2616 case MCP::State::kConfiguring2:
2617 case MCP::State::kConfiguring3:
2618 case MCP::State::kConfigured:
2619 case MCP::State::kTriggerOn:
2620 col = HTML::kBlue;
2621 break;
2622 case MCP::State::kTakingData:
2623 col = HTML::kBlue;
2624 if (fDimFadControl.state()==FAD::State::kRunInProgress)
2625 col = HTML::kGreen;
2626 break;
2627 }
2628
2629 const bool other =
2630 fDimRateControl.state()==RateControl::State::kSettingGlobalThreshold ||
2631 fDimLidControl.state()==Lid::State::kMoving ||
2632 fDimRateScan.state()==RateScan::State::kInProgress;
2633
2634 if (other)
2635 col = HTML::kBlue;
2636
2637 out << col << '\t';
2638
2639 if (!other)
2640 {
2641 const string conf = fMcpConfigurationName.length()>0?" ["+fMcpConfigurationName+"]":"";
2642 switch (fMcpConfigurationState)
2643 {
2644 case MCP::State::kIdle:
2645 out << "Idle" << conf;
2646 break;
2647 case MCP::State::kConfiguring1:
2648 case MCP::State::kConfiguring2:
2649 case MCP::State::kConfiguring3:
2650 out << "Configuring" << conf;
2651 break;
2652 case MCP::State::kConfigured:
2653 out << "Configured" << conf;
2654 break;
2655 case MCP::State::kTriggerOn:
2656 case MCP::State::kTakingData:
2657 out << fMcpConfigurationName;
2658 if (fFadControlDrsRuns[2]>0)
2659 out << "(" << fFadControlDrsRuns[2] << ")";
2660 break;
2661 }
2662 }
2663 else
2664 if (fDimRateControl.state()==RateControl::State::kSettingGlobalThreshold)
2665 out << "Calibrating threshold";
2666 else
2667 if (fDimRateScan.state()==RateScan::State::kInProgress)
2668 out << "Rate scan in progress";
2669 else
2670 if (fDimLidControl.state()==Lid::State::kMoving)
2671 out << "Lid moving";
2672
2673
2674 if (fMcpConfigurationState>MCP::State::kConfigured &&
2675 fDimRateControl.state()!=RateControl::State::kSettingGlobalThreshold)
2676 {
2677 ostringstream evt;
2678 if (fMcpConfigurationMaxEvents>0)
2679 {
2680 const int64_t de = int64_t(fMcpConfigurationMaxEvents) - int64_t(fFadControlNumEvents);
2681 if (de>=0 && fMcpConfigurationState==MCP::State::kTakingData)
2682 evt << de;
2683 else
2684 evt << fMcpConfigurationMaxEvents;
2685 }
2686 else
2687 {
2688 if (fMcpConfigurationState==MCP::State::kTakingData)
2689 {
2690 if (fFadControlNumEvents>2999)
2691 evt << floor(fFadControlNumEvents/1000) << 'k';
2692 else
2693 evt << fFadControlNumEvents;
2694 }
2695 }
2696
2697 ostringstream tim;
2698 if (fMcpConfigurationMaxTime>0)
2699 {
2700 const uint32_t dt = (Time()-fMcpConfigurationRunStart).total_seconds();
2701 if (dt<=fMcpConfigurationMaxTime && fMcpConfigurationState==MCP::State::kTakingData)
2702 tim << fMcpConfigurationMaxTime-dt << 's';
2703 else
2704 tim << fMcpConfigurationMaxTime << 's';
2705 }
2706 else
2707 {
2708 if (fMcpConfigurationState==MCP::State::kTakingData)
2709 tim << fMcpConfigurationRunStart.SecondsTo();
2710 }
2711
2712 const bool has_evt = !evt.str().empty();
2713 const bool has_tim = !tim.str().empty();
2714
2715 if (has_evt || has_tim)
2716 out << " [";
2717 out << evt.str();
2718 if (has_evt && has_tim)
2719 out << '/';
2720 out << tim.str();
2721 if (has_evt || has_tim)
2722 out << ']';
2723 }
2724 }
2725 else
2726 out << HTML::kWhite;
2727 out << '\n';
2728
2729 // ------------------ Drive -----------------
2730 if (fDimDNS.online() && fDimDriveControl.state()>=Drive::State::kArmed) // Armed, Moving, Tracking, OnTrack, Error
2731 {
2732 const uint32_t dev = !fDriveControlTrackingDevHist.empty() ? round(fDriveControlTrackingDevHist.back()) : 0;
2733 const State rc = fDimDriveControl.description();
2734 string col = HTML::kGreen;
2735 if (fDimDriveControl.state()==Drive::State::kMoving) // Moving
2736 col = HTML::kBlue;
2737 if (fDimDriveControl.state()==Drive::State::kArmed) // Armed
2738 col = HTML::kWhite;
2739 if (fDimDriveControl.state()==Drive::State::kTracking || fDimDriveControl.state()==Drive::State::kOnTrack) // Tracking
2740 {
2741 if (dev>60) // ~1.5mm
2742 col = HTML::kYellow;
2743 if (dev>120) // ~1/4 of a pixel ~ 2.5mm
2744 col = HTML::kRed;
2745 }
2746 if (fDimDriveControl.state()==0x100)
2747 col = HTML::kRed;
2748 out << col << '\t';
2749
2750 //out << rc.name << '\t';
2751 out << fDriveControlPointingAz << ' ';
2752 out << fDriveControlPointingZd << "&deg;";
2753 out << setprecision(2);
2754 if (fDimDriveControl.state()==Drive::State::kTracking || fDimDriveControl.state()==Drive::State::kOnTrack)
2755 {
2756 out << " &plusmn; " << dev << '"';
2757 if (!fDriveControlSourceName.empty())
2758 out << " [" << fDriveControlSourceName << ']';
2759 }
2760 if (fDimDriveControl.state()==Drive::State::kMoving)
2761 out << " &#10227;";
2762 out << setprecision(3);
2763 }
2764 else
2765 out << HTML::kWhite << '\t';
2766
2767 if (fSun.time.IsValid() && fMoon.time.IsValid())
2768 {
2769 if (fSun.visible)
2770 {
2771 out << " &#9788;";
2772 if (fDimDriveControl.state()<Drive::State::kArmed)
2773 out << " [" << fSun.fSetCivil.MinutesTo() << "&darr;]";
2774 }
2775 else
2776 if (!fSun.visible && fMoon.visible)
2777 {
2778 out << " &#9790;";
2779 if (fDimDriveControl.state()<Drive::State::kArmed)
2780 out << " [" << fMoon.disk << "%]";
2781 }
2782 }
2783 if (fDimDNS.online() && fDimDriveControl.state()==0x100)
2784 out << " <ERR>";
2785 if (fDimDNS.online() && fDimDriveControl.state()==Drive::State::kLocked)
2786 out << " &otimes;";
2787 out << '\n';
2788
2789 // ------------------- FSC ------------------
2790 if (fDimDNS.online() && fDimFscControl.state()>FSC::State::kDisconnected && !fFscControlTemperatureHist.empty())
2791 {
2792 string col = HTML::kGreen;
2793 if (fFscControlTemperatureHist.back()>9)
2794 col = HTML::kYellow;
2795 if (fFscControlTemperatureHist.back()>15)
2796 col = HTML::kRed;
2797
2798 out << col << '\t' << fFscControlTemperatureHist.back() << '\n';
2799 }
2800 else
2801 out << HTML::kWhite << '\n';
2802
2803 // --------------- MagicWeather -------------
2804 if (fDimDNS.online() && fDimMagicWeather.state()==MagicWeather::State::kReceiving && !fMagicWeatherHist[kWeatherBegin].empty())
2805 {
2806 /*
2807 const float diff = fMagicWeatherHist[kTemp].back()-fMagicWeatherHist[kDew].back();
2808 string col1 = HTML::kRed;
2809 if (diff>0.3)
2810 col1 = HTML::kYellow;
2811 if (diff>0.7)
2812 col1 = HTML::kGreen;
2813 */
2814
2815 const float wind = fMagicWeatherHist[kGusts].back();
2816 const float hum = fMagicWeatherHist[kHum].back();
2817 string col = HTML::kGreen;
2818 if (wind>35 || hum>95)
2819 col = HTML::kYellow;
2820 if (wind>45 || hum>98)
2821 col = HTML::kRed;
2822
2823 out << col << '\t';
2824 out << fMagicWeatherHist[kHum].back() << '\t';
2825 out << setprecision(2);
2826 out << fMagicWeatherHist[kGusts].back() << '\n';
2827 out << setprecision(3);
2828 }
2829 else
2830 out << HTML::kWhite << "\n";
2831
2832 // --------------- FtmControl -------------
2833 if (fDimDNS.online() && fDimFtmControl.state()==FTM::State::kTriggerOn)
2834 {
2835 string col = HTML::kGreen;
2836 if (!fFtmControlTriggerRateHist.empty())
2837 {
2838 if (fFtmControlTriggerRateHist.back()<15)
2839 col = HTML::kYellow;
2840 if (fFtmControlTriggerRateHist.back()>100)
2841 col = HTML::kRed;
2842
2843 out << col << '\t' << fFtmControlTriggerRateHist.back() << " Hz";
2844 }
2845
2846 if (fDimBiasControl.state()==BIAS::State::kVoltageOn)
2847 out << " (" << fFtmPatchThresholdMed << ')';
2848 out << '\n';
2849 }
2850 else
2851 out << HTML::kWhite << '\n';
2852
2853 // --------------- BiasControl -------------
2854 const bool bias_off = fDimBiasControl.state()==BIAS::State::kVoltageOff;
2855 const bool bias_oc = fDimBiasControl.state()==BIAS::State::kOverCurrent;
2856
2857 if (fDimDNS.online() && (bias_on || bias_off))
2858 {
2859
2860 string col = fBiasControlVoltageMed>3?HTML::kGreen:HTML::kWhite;
2861 if (!fDimBiasControl.state()==BIAS::State::kVoltageOn)
2862 {
2863 if (fBiasControlCurrentMed>70 || fBiasControlCurrentMax>90)
2864 col = HTML::kYellow;
2865 if (fBiasControlCurrentMed>90 || fBiasControlCurrentMax>110)
2866 col = HTML::kRed;
2867 }
2868
2869 // Bias in overcurrent => Red
2870 if (bias_oc)
2871 col = HTML::kRed;
2872
2873 // MCP in ReadyForDatataking/Configuring/Configured/TriggerOn/TakingData
2874 // and Bias not in "data-taking state' => Red
2875 if (fMcpConfigurationState>MCP::State::kIdle && !bias_on)
2876 col = HTML::kWhite;
2877
2878 const bool cal = fDimFeedback.state()>=Feedback::State::kCalibrated;
2879
2880 // Feedback is currently calibrating => Blue
2881 if (fDimFeedback.state()==Feedback::State::kCalibrating)
2882 {
2883 out << HTML::kBlue << '\t';
2884 out << "***\t";
2885 out << "***\t";
2886 }
2887 else
2888 {
2889 out << col << '\t';
2890 out << setprecision(fBiasControlCurrentMed<100?2:3);
2891 out << (bias_off ? 0 : fBiasControlCurrentMed) << '\t';
2892 if (bias_oc)
2893 out << "(OC) ";
2894 else
2895 {
2896 if (cal)
2897 {
2898 out << setprecision(fBiasControlCurrentMax<100?2:3);
2899 out << (bias_off ? 0 : fBiasControlCurrentMax);
2900 }
2901 else
2902 out << "&mdash; ";
2903 }
2904 out << '\t';
2905 out << setprecision(3);
2906 }
2907 if (cal && fDimFeedback.state()!=Feedback::State::kCalibrating)
2908 out << setprecision(2) << fBiasControlPowerTot << " W" << setprecision(3);
2909 else
2910 out << (bias_off ? 0 : fBiasControlVoltageMed) << " V";
2911 out << '\n';
2912 }
2913 else
2914 out << HTML::kWhite << '\n';
2915
2916 ofstream(fPath+"/fact.data") << out.str();
2917
2918 // ==============================================================
2919
2920 out.str("");
2921 out << Header(now) << '\t' << (!fErrorList.empty()) << '\t' << (fDimControl.state()>0) << '\n';
2922
2923 if (!fDimDNS.online())
2924 out << HTML::kWhite << "\tOffline\n\n\n\n\n\n\n\n\n\n\n\n\n";
2925 else
2926 {
2927 ostringstream dt;
2928 dt << (Time()-fRunTime);
2929
2930 out << HTML::kGreen << '\t' << fDimDNS.version() << '\n';
2931
2932 out << GetStateHtml(fDimControl, 0);
2933 out << GetStateHtml(fDimMcp, MCP::State::kConnected);
2934 out << GetStateHtml(fDimDataLogger, 1);
2935 out << GetStateHtml(fDimDriveControl, Drive::State::kConnected);
2936 out << GetStateHtml(fDimTimeCheck, 1);
2937 out << GetStateHtml(fDimFadControl, FAD::State::kConnected);
2938 out << GetStateHtml(fDimFtmControl, FTM::State::kConnected);
2939 out << GetStateHtml(fDimBiasControl, BIAS::State::kConnected);
2940 out << GetStateHtml(fDimFeedback, Feedback::State::kConnected);
2941 out << GetStateHtml(fDimRateControl, RateControl::State::kConnected);
2942 out << GetStateHtml(fDimFscControl, FSC::State::kConnected);
2943 out << GetStateHtml(fDimGpsControl, GPS::State::kConnected);
2944 out << GetStateHtml(fDimAgilentControl, Agilent::State::kVoltageOff);
2945 out << GetStateHtml(fDimPwrControl, Power::State::kSystemOff);
2946 out << GetStateHtml(fDimLidControl, Lid::State::kConnected);
2947 out << GetStateHtml(fDimRateScan, RateScan::State::kConnected);
2948 out << GetStateHtml(fDimMagicWeather, MagicWeather::State::kConnected);
2949 out << GetStateHtml(fDimTngWeather, TNGWeather::State::kConnected);
2950 out << GetStateHtml(fDimMagicLidar, MagicLidar::State::kConnected);
2951 out << GetStateHtml(fDimTemperature, Temperature::State::kValid);
2952 out << GetStateHtml(fDimChat, 0);
2953 out << GetStateHtml(fDimSkypeClient, 1);
2954
2955 string col = HTML::kRed;
2956 if (fFreeSpace>uint64_t(199999999999))
2957 col = HTML::kYellow;
2958 if (fFreeSpace>uint64_t(999999999999))
2959 col = HTML::kGreen;
2960 if (fFreeSpace==UINT64_MAX)
2961 col = HTML::kWhite;
2962
2963 out << col << '\t' << Tools::Scientific(fFreeSpace) << "B\n";
2964
2965 out << HTML::kGreen << '\t' << dt.str().substr(0, dt.str().length()-7) << '\n';
2966 }
2967
2968 ofstream(fPath+"/status.data") << out.str();
2969
2970 if (now-fLastAstroCalc>boost::posix_time::seconds(15))
2971 {
2972 UpdateAstronomy();
2973 fLastAstroCalc = now;
2974 }
2975
2976 return fDimDNS.online() ? kStateRunning : kStateDimNetworkNA;
2977 }
2978
2979
2980public:
2981 StateMachineSmartFACT(ostream &out=cout) : StateMachineDim(out, fIsServer?"SMART_FACT":""),
2982 fLastAstroCalc(boost::date_time::neg_infin),
2983 fPath("www/smartfact/data"),
2984 fControlScriptDepth(0),
2985 fMcpConfigurationState(DimState::kOffline),
2986 fMcpConfigurationMaxTime(0),
2987 fMcpConfigurationMaxEvents(0),
2988 fLastRunFinishedWithZeroEvents(false),
2989 fTngWeatherDustTime(Time::none),
2990 fBiasControlVoltageMed(0),
2991 fBiasControlCurrentMed(0),
2992 fBiasControlCurrentMax(0),
2993 fFscControlHumidityAvg(0),
2994 fDriveControlMoonDist(-1),
2995 fFadControlNumEvents(0),
2996 fFadControlDrsRuns(3),
2997 fFtmControlState(FTM::kFtmLocked),
2998 fRateScanDataId(0),
2999 fRateScanBoard(0),
3000 fFreeSpace(UINT64_MAX),
3001 // ---
3002 fDimMcp ("MCP"),
3003 fDimDataLogger ("DATA_LOGGER"),
3004 fDimDriveControl ("DRIVE_CONTROL"),
3005 fDimTimeCheck ("TIME_CHECK"),
3006 fDimMagicWeather ("MAGIC_WEATHER"),
3007 fDimMagicLidar ("MAGIC_LIDAR"),
3008 fDimTngWeather ("TNG_WEATHER"),
3009 fDimTemperature ("TEMPERATURE"),
3010 fDimFeedback ("FEEDBACK"),
3011 fDimBiasControl ("BIAS_CONTROL"),
3012 fDimFtmControl ("FTM_CONTROL"),
3013 fDimFadControl ("FAD_CONTROL"),
3014 fDimFscControl ("FSC_CONTROL"),
3015 fDimGpsControl ("GPS_CONTROL"),
3016 fDimAgilentControl("AGILENT_CONTROL"),
3017 fDimPwrControl ("PWR_CONTROL"),
3018 fDimLidControl ("LID_CONTROL"),
3019 fDimRateControl ("RATE_CONTROL"),
3020 fDimRateScan ("RATE_SCAN"),
3021 fDimChat ("CHAT"),
3022 fDimSkypeClient ("SKYPE_CLIENT")
3023 {
3024 fDimDNS.Subscribe(*this);
3025 fDimControl.Subscribe(*this);
3026 fDimMcp.Subscribe(*this);
3027 fDimDataLogger.Subscribe(*this);
3028 fDimDriveControl.Subscribe(*this);
3029 fDimTimeCheck.Subscribe(*this);
3030 fDimMagicWeather.Subscribe(*this);
3031 fDimMagicLidar.Subscribe(*this);
3032 fDimTngWeather.Subscribe(*this);
3033 fDimTemperature.Subscribe(*this);
3034 fDimFeedback.Subscribe(*this);
3035 fDimBiasControl.Subscribe(*this);
3036 fDimFtmControl.Subscribe(*this);
3037 fDimFadControl.Subscribe(*this);
3038 fDimFscControl.Subscribe(*this);
3039 fDimGpsControl.Subscribe(*this);
3040 fDimAgilentControl.Subscribe(*this);
3041 fDimPwrControl.Subscribe(*this);
3042 fDimLidControl.Subscribe(*this);
3043 fDimRateControl.Subscribe(*this);
3044 fDimRateScan.Subscribe(*this);
3045 fDimChat.Subscribe(*this);
3046 fDimSkypeClient.Subscribe(*this);
3047
3048 fDimFscControl.SetCallback(bind(&StateMachineSmartFACT::HandleFscControlStateChange, this, placeholders::_1));
3049 //fDimFtmControl.SetCallback(bind(&StateMachineSmartFACT::HandleFtmControlStateChange, this));
3050 fDimDriveControl.SetCallback(bind(&StateMachineSmartFACT::HandleDriveControlStateChange, this, placeholders::_1));
3051 fDimControl.SetCallback(bind(&StateMachineSmartFACT::HandleControlStateChange, this, placeholders::_1));
3052 fDimControl.AddCallback("dotest.dim", bind(&StateMachineSmartFACT::HandleDoTest, this, placeholders::_1));
3053
3054 Subscribe("DIM_CONTROL/MESSAGE")
3055 (bind(&StateMachineSmartFACT::HandleDimControlMessage, this, placeholders::_1));
3056
3057 Subscribe("MCP/CONFIGURATION")
3058 (bind(&StateMachineSmartFACT::HandleMcpConfiguration, this, placeholders::_1));
3059
3060 Subscribe("DRIVE_CONTROL/POINTING_POSITION")
3061 (bind(&StateMachineSmartFACT::HandleDrivePointing, this, placeholders::_1));
3062 Subscribe("DRIVE_CONTROL/TRACKING_POSITION")
3063 (bind(&StateMachineSmartFACT::HandleDriveTracking, this, placeholders::_1));
3064 Subscribe("DRIVE_CONTROL/SOURCE_POSITION")
3065 (bind(&StateMachineSmartFACT::HandleDriveSource, this, placeholders::_1));
3066
3067 Subscribe("FSC_CONTROL/TEMPERATURE")
3068 (bind(&StateMachineSmartFACT::HandleFscTemperature, this, placeholders::_1));
3069 Subscribe("FSC_CONTROL/HUMIDITY")
3070 (bind(&StateMachineSmartFACT::HandleFscHumidity, this, placeholders::_1));
3071 Subscribe("FSC_CONTROL/BIAS_TEMP")
3072 (bind(&StateMachineSmartFACT::HandleFscBiasTemp, this, placeholders::_1));
3073
3074 Subscribe("GPS_CONTROL/NEMA")
3075 (bind(&StateMachineSmartFACT::HandleGpsNema, this, placeholders::_1));
3076
3077 Subscribe("TEMPERATURE/DATA")
3078 (bind(&StateMachineSmartFACT::HandleTemperatureData, this, placeholders::_1));
3079
3080 Subscribe("AGILENT_CONTROL/DATA")
3081 (bind(&StateMachineSmartFACT::HandleAgilentData, this, placeholders::_1));
3082
3083 Subscribe("MAGIC_WEATHER/DATA")
3084 (bind(&StateMachineSmartFACT::HandleMagicWeatherData, this, placeholders::_1));
3085 Subscribe("TNG_WEATHER/DUST")
3086 (bind(&StateMachineSmartFACT::HandleTngWeatherDust, this, placeholders::_1));
3087
3088 Subscribe("FEEDBACK/CALIBRATED_CURRENTS")
3089 (bind(&StateMachineSmartFACT::HandleFeedbackCalibratedCurrents, this, placeholders::_1));
3090
3091 Subscribe("BIAS_CONTROL/VOLTAGE")
3092 (bind(&StateMachineSmartFACT::HandleBiasVoltage, this, placeholders::_1));
3093 Subscribe("BIAS_CONTROL/CURRENT")
3094 (bind(&StateMachineSmartFACT::HandleBiasCurrent, this, placeholders::_1));
3095
3096 Subscribe("FAD_CONTROL/CONNECTIONS")
3097 (bind(&StateMachineSmartFACT::HandleFadConnections, this, placeholders::_1));
3098 Subscribe("FAD_CONTROL/EVENTS")
3099 (bind(&StateMachineSmartFACT::HandleFadEvents, this, placeholders::_1));
3100 Subscribe("FAD_CONTROL/START_RUN")
3101 (bind(&StateMachineSmartFACT::HandleFadStartRun, this, placeholders::_1));
3102 Subscribe("FAD_CONTROL/DRS_RUNS")
3103 (bind(&StateMachineSmartFACT::HandleFadDrsRuns, this, placeholders::_1));
3104 Subscribe("FAD_CONTROL/EVENT_DATA")
3105 (bind(&StateMachineSmartFACT::HandleFadEventData, this, placeholders::_1));
3106 Subscribe("FAD_CONTROL/STATS")
3107 (bind(&StateMachineSmartFACT::HandleStats, this, placeholders::_1));
3108
3109 Subscribe("DATA_LOGGER/STATS")
3110 (bind(&StateMachineSmartFACT::HandleStats, this, placeholders::_1));
3111
3112 Subscribe("FTM_CONTROL/TRIGGER_RATES")
3113 (bind(&StateMachineSmartFACT::HandleFtmTriggerRates, this, placeholders::_1));
3114 Subscribe("FTM_CONTROL/STATIC_DATA")
3115 (bind(&StateMachineSmartFACT::HandleFtmStaticData, this, placeholders::_1));
3116 Subscribe("FTM_CONTROL/FTU_LIST")
3117 (bind(&StateMachineSmartFACT::HandleFtmFtuList, this, placeholders::_1));
3118
3119 Subscribe("RATE_CONTROL/THRESHOLD")
3120 (bind(&StateMachineSmartFACT::HandleRateControlThreshold,this, placeholders::_1));
3121
3122 Subscribe("RATE_SCAN/DATA")
3123 (bind(&StateMachineSmartFACT::HandleRateScanData, this, placeholders::_1));
3124
3125 Subscribe("CHAT/MESSAGE")
3126 (bind(&StateMachineSmartFACT::HandleChatMsg, this, placeholders::_1));
3127
3128
3129 // =================================================================
3130
3131 // State names
3132 AddStateName(kStateDimNetworkNA, "DimNetworkNotAvailable",
3133 "The Dim DNS is not reachable.");
3134
3135 AddStateName(kStateRunning, "Running", "");
3136
3137 // =================================================================
3138
3139 AddEvent("PRINT")
3140 (bind(&StateMachineSmartFACT::Print, this))
3141 ("Print a list of the states of all connected servers.");
3142
3143 }
3144 int EvalOptions(Configuration &conf)
3145 {
3146 if (!fPixelMap.Read(conf.Get<string>("pixel-map-file")))
3147 {
3148 Error("Reading mapping table from "+conf.Get<string>("pixel-map-file")+" failed.");
3149 return 1;
3150 }
3151
3152 fPath = conf.Get<string>("path");
3153 fDatabase = conf.Get<string>("source-database");
3154
3155 struct stat st;
3156 if (stat(fPath.c_str(), &st))
3157 {
3158 Error(fPath+" does not exist!");
3159 return 2;
3160 }
3161
3162 if ((st.st_mode&S_IFDIR)==0)
3163 {
3164 Error(fPath+" not a directory!");
3165 return 3;
3166 }
3167
3168 if ((st.st_mode&S_IWUSR)==0)
3169 {
3170 Error(fPath+" has no write permission!");
3171 return 4;
3172 }
3173
3174 if ((st.st_mode&S_IXUSR)==0)
3175 {
3176 Error(fPath+" has no execute permission!");
3177 return 5;
3178 }
3179
3180 ostringstream out;
3181 out << Time().JavaDate() << '\n';
3182
3183 ofstream(fPath+"/error.data") << out.str();
3184
3185 return -1;
3186 }
3187};
3188
3189bool StateMachineSmartFACT::fIsServer = false;
3190
3191// ------------------------------------------------------------------------
3192
3193#include "Main.h"
3194
3195template<class T>
3196int RunShell(Configuration &conf)
3197{
3198 StateMachineSmartFACT::fIsServer = !conf.Get<bool>("client");
3199 return Main::execute<T, StateMachineSmartFACT>(conf);
3200}
3201
3202void SetupConfiguration(Configuration &conf)
3203{
3204 po::options_description control("Smart FACT");
3205 control.add_options()
3206 ("pixel-map-file", var<string>()->required(), "Pixel mapping file. Used here to get the default reference voltage")
3207 ("path", var<string>("www/smartfact/data"), "Output path for the data-files")
3208 ("source-database", var<string>(""), "Database link as in\n\tuser:password@server[:port]/database.")
3209 ("client", po_bool(false), "For a standalone client choose this option.")
3210 ;
3211
3212 conf.AddOptions(control);
3213}
3214
3215/*
3216 Extract usage clause(s) [if any] for SYNOPSIS.
3217 Translators: "Usage" and "or" here are patterns (regular expressions) which
3218 are used to match the usage synopsis in program output. An example from cp
3219 (GNU coreutils) which contains both strings:
3220 Usage: cp [OPTION]... [-T] SOURCE DEST
3221 or: cp [OPTION]... SOURCE... DIRECTORY
3222 or: cp [OPTION]... -t DIRECTORY SOURCE...
3223 */
3224void PrintUsage()
3225{
3226 cout <<
3227 "SmartFACT is a tool writing the files needed for the SmartFACT web interface.\n"
3228 "\n"
3229 "The default is that the program is started without user intercation. "
3230 "All actions are supposed to arrive as DimCommands. Using the -c "
3231 "option, a local shell can be initialized. With h or help a short "
3232 "help message about the usuage can be brought to the screen.\n"
3233 "\n"
3234 "Usage: smartfact [-c type] [OPTIONS]\n"
3235 " or: smartfact [OPTIONS]\n";
3236 cout << endl;
3237}
3238
3239void PrintHelp()
3240{
3241 Main::PrintHelp<StateMachineSmartFACT>();
3242
3243 /* Additional help text which is printed after the configuration
3244 options goes here */
3245
3246 /*
3247 cout << "bla bla bla" << endl << endl;
3248 cout << endl;
3249 cout << "Environment:" << endl;
3250 cout << "environment" << endl;
3251 cout << endl;
3252 cout << "Examples:" << endl;
3253 cout << "test exam" << endl;
3254 cout << endl;
3255 cout << "Files:" << endl;
3256 cout << "files" << endl;
3257 cout << endl;
3258 */
3259}
3260
3261int main(int argc, const char* argv[])
3262{
3263 Configuration conf(argv[0]);
3264 conf.SetPrintUsage(PrintUsage);
3265 Main::SetupConfiguration(conf);
3266 SetupConfiguration(conf);
3267
3268 if (!conf.DoParse(argc, argv, PrintHelp))
3269 return 127;
3270
3271 if (!conf.Has("console"))
3272 return RunShell<LocalStream>(conf);
3273
3274 if (conf.Get<int>("console")==0)
3275 return RunShell<LocalShell>(conf);
3276 else
3277 return RunShell<LocalConsole>(conf);
3278
3279 return 0;
3280}
Note: See TracBrowser for help on using the repository browser.