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

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