source: trunk/MagicSoft/Cosy/tcpip/MCeCoCom.cc@ 9029

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