1 | #include "MCeCoCom.h"
|
---|
2 |
|
---|
3 | #include <iostream>
|
---|
4 |
|
---|
5 | #include "MLog.h"
|
---|
6 |
|
---|
7 | #include "MString.h"
|
---|
8 |
|
---|
9 | using namespace std;
|
---|
10 |
|
---|
11 | /*
|
---|
12 | // THIS COULD NEVER WORK BECAUSE IT IGNORES WHETHER
|
---|
13 | // WE SEND STARGUIDER OR DRIVE REPORTS!
|
---|
14 | bool MCeCoCom::SendMsg(const char *msg, bool force=kFALSE)
|
---|
15 | {
|
---|
16 | const MTime t(-1);
|
---|
17 |
|
---|
18 | if ((double)t-(double)fTime<0.001*fSendInterval && !force)
|
---|
19 | return true;
|
---|
20 |
|
---|
21 | if (lout.Lock("MTcpIpIO::Send"))
|
---|
22 | {
|
---|
23 | //const Int_t rc = lout.IsOutputDeviceEnabled(MLog::eGui);
|
---|
24 | //lout.DisableOutputDevice(MLog::eGui);
|
---|
25 | lout << msg << flush;
|
---|
26 | lout.UnLock("MTcpIpIO::Send");
|
---|
27 | //if (rc)
|
---|
28 | // lout.EnableOutputDevice(MLog::eGui);
|
---|
29 | }
|
---|
30 |
|
---|
31 | fTime = t;
|
---|
32 |
|
---|
33 | return Send(msg, strlen(msg));
|
---|
34 | }
|
---|
35 | */
|
---|
36 | bool MCeCoCom::InterpreteCmd(TString cmd, TString str)
|
---|
37 | {
|
---|
38 | cout << cmd << ": " << str << endl;
|
---|
39 | return true;
|
---|
40 | }
|
---|
41 |
|
---|
42 | bool MCeCoCom::InterpreteReport(TString &str)
|
---|
43 | {
|
---|
44 | if (!str.EndsWith("OVER"))
|
---|
45 | {
|
---|
46 | cout << "Communication Problem: Termination (OVER) not found." << endl;
|
---|
47 | fComStat = kComProblem;
|
---|
48 | return false;
|
---|
49 | }
|
---|
50 |
|
---|
51 | int y, m, d, h, min, s, ms, len;
|
---|
52 |
|
---|
53 | int n=sscanf(str.Data(),
|
---|
54 | "%02d %04d %02d %02d %02d %02d %02d %03d %n",
|
---|
55 | &fCCStatus, &y, &m, &d, &h, &min, &s, &ms, &len);
|
---|
56 | if (n!=8)
|
---|
57 | {
|
---|
58 | cout << "Communication Problem: Wrong number of arguments" << endl;
|
---|
59 | fComStat = kComProblem;
|
---|
60 | return false;
|
---|
61 | }
|
---|
62 | str.Remove(0, len);
|
---|
63 |
|
---|
64 | fT.Set(y, m, d, h, min, s, ms);
|
---|
65 |
|
---|
66 | // Remove the 30 tokens of the subsystem status
|
---|
67 | // table 12.1 p59
|
---|
68 | for (int i=0; i<31; i++)
|
---|
69 | str.Remove(0, str.First(' ')+1);
|
---|
70 |
|
---|
71 | // skip solar_irr_Wm2, wind_speed, UPS
|
---|
72 | float zd, az, dec, ra, temp, solar, wind, hum;
|
---|
73 | n=sscanf(str.Data(),
|
---|
74 | /*"%f %f %f %f %f %f %f %f %*f %*f %*s %*s %*s %*s %n",*/
|
---|
75 | "%f %f %f %f %f %f %f %f %*f %*f %n",
|
---|
76 | &zd, &az, &dec, &ra, &temp, &solar, &wind, &hum, &len);
|
---|
77 | if (n!=8)
|
---|
78 | {
|
---|
79 | cout << "Communication Problem: Wrong number of arguments" << endl;
|
---|
80 | fComStat = kComProblem;
|
---|
81 | return false;
|
---|
82 | }
|
---|
83 | /*
|
---|
84 | str.Remove(0, len);
|
---|
85 |
|
---|
86 | if (str!=(TString)"OVER")
|
---|
87 | {
|
---|
88 | cout << "Communication Problem: Termination (OVER) too far away." << endl;
|
---|
89 | fComStat = kComProblem;
|
---|
90 | return false;
|
---|
91 | }
|
---|
92 | */
|
---|
93 |
|
---|
94 | fHumidity = hum;
|
---|
95 | fTemperature = temp;
|
---|
96 | fSolarRadiation = solar;
|
---|
97 | fWindSpeed = wind;
|
---|
98 |
|
---|
99 | if (fWindSpeed>50)
|
---|
100 | fAlarmCounter++;
|
---|
101 | else
|
---|
102 | fAlarmCounter=0;
|
---|
103 |
|
---|
104 | cout << "Zd/Az: " << zd << "/" << az << " ";
|
---|
105 | cout << "Ra/Dec: " << ra/15 << "h/" << dec << " ";
|
---|
106 | //cout << "SolRad: " << solar << "W/m²";
|
---|
107 | cout << endl;
|
---|
108 |
|
---|
109 | fComStat = kCmdReceived;
|
---|
110 | return true;
|
---|
111 | }
|
---|
112 |
|
---|
113 | bool MCeCoCom::InterpreteStr(TString str)
|
---|
114 | {
|
---|
115 | const Ssiz_t tok = str.First(' ');
|
---|
116 | const TString cmd = tok<0 ? str : str(0, tok);
|
---|
117 | if (tok<0)
|
---|
118 | str = "";
|
---|
119 | else
|
---|
120 | str.Remove(0, tok+1);
|
---|
121 |
|
---|
122 | if (cmd==(TString)"CC-REPORT" && str.EndsWith("OVER"))
|
---|
123 | return InterpreteReport(str);
|
---|
124 |
|
---|
125 | return InterpreteCmd(cmd, str.Strip(TString::kBoth));
|
---|
126 | }
|
---|
127 |
|
---|
128 | bool MCeCoCom::SendRep(const char *cmd, const char *str, bool force)
|
---|
129 | {
|
---|
130 |
|
---|
131 | UShort_t y1, y2, ms1, ms2;
|
---|
132 | Byte_t mon1, mon2, d1, d2, h1, h2, m1, m2, s1, s2;
|
---|
133 |
|
---|
134 | fT.GetDate(y2, mon2, d2);
|
---|
135 | fT.GetTime(h2, m2, s2, ms2);
|
---|
136 |
|
---|
137 | MTime t(-1);
|
---|
138 | t.GetDate(y1, mon1, d1);
|
---|
139 | t.GetTime(h1, m1, s1, ms1);
|
---|
140 |
|
---|
141 | if (t-fT>20)
|
---|
142 | fComStat = kComProblem;
|
---|
143 |
|
---|
144 | const TString msg =
|
---|
145 | MString::Format("%s M%d "
|
---|
146 | "%02d %04d %02d %02d %02d %02d %02d %03d "
|
---|
147 | "%02d %04d %02d %02d %02d %02d %02d %03d "
|
---|
148 | "%s\n", cmd, fTelescope,
|
---|
149 | fStatus, y1, mon1, d1, h1, m1, s1, ms1,
|
---|
150 | fComStat, y2, mon2, d2, h2, m2, s2, ms2,
|
---|
151 | str);
|
---|
152 |
|
---|
153 | // Send report to CC
|
---|
154 | const bool rc = Send(msg.Data(), msg.Length());
|
---|
155 |
|
---|
156 | // Write report to stream
|
---|
157 | if (fOut)
|
---|
158 | if (fOut->Lock("MTcpIpIO::Send"))
|
---|
159 | {
|
---|
160 | *fOut << msg << flush;
|
---|
161 | fOut->UnLock("MTcpIpIO::Send");
|
---|
162 | }
|
---|
163 |
|
---|
164 | return rc;
|
---|
165 | }
|
---|
166 |
|
---|
167 | TString MCeCoCom::GetWeather() const
|
---|
168 | {
|
---|
169 | if (fSolarRadiation<0 || (double)MTime(-1)-(double)fT>11)
|
---|
170 | return "";
|
---|
171 |
|
---|
172 | return MString::Format("Temp: %.1f'C Hum: %.1f%% Wind: %.1fkm/h",
|
---|
173 | fTemperature, fHumidity, fWindSpeed);
|
---|
174 | }
|
---|
175 |
|
---|
176 | Int_t MCeCoCom::GetWeatherStatus() const
|
---|
177 | {
|
---|
178 | if (fSolarRadiation<0 || (double)MTime(-1)-(double)fT>11)
|
---|
179 | return 0;
|
---|
180 |
|
---|
181 | Int_t rc = 0;
|
---|
182 | if (fHumidity>80)
|
---|
183 | rc++;
|
---|
184 | if (fWindSpeed>30)
|
---|
185 | rc++;
|
---|
186 | if (fWindSpeed>40)
|
---|
187 | rc++;
|
---|
188 | if (fWindSpeed>50)
|
---|
189 | rc++;
|
---|
190 |
|
---|
191 | return rc;
|
---|
192 | }
|
---|