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

Last change on this file since 4308 was 4105, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 3.4 KB
Line 
1#include "MCeCoCom.h"
2
3#include <iostream>
4
5#include "MString.h"
6
7using namespace std;
8
9bool MCeCoCom::InterpreteCmd(TString cmd, TString str)
10{
11 cout << cmd << ": " << str << endl;
12 return true;
13}
14
15bool MCeCoCom::InterpreteReport(TString &str)
16{
17 int y, m, d, h, min, s, ms, len;
18
19 int n=sscanf(str.Data(),
20 "%02d %04d %02d %02d %02d %02d %02d %03d %n",
21 &fCCStatus, &y, &m, &d, &h, &min, &s, &ms, &len);
22 if (n!=8)
23 {
24 cout << "Communication Problem: Wrong number of arguments" << endl;
25 fComStat = kComProblem;
26 return false;
27 }
28 str.Remove(0, len);
29
30 fT.Set(y, m, d, h, min, s, ms);
31
32 // Remove the 30 tokens of the subsystem status
33 // table 12.1 p59
34 for (int i=0; i<30; i++)
35 str.Remove(0, str.First(' ')+1);
36
37 // skip solar_irr_Wm2, wind_speed, UPS
38 float zd, az, dec, ra, temp, solar, wind, hum;
39 n=sscanf(str.Data(),
40 "%f %f %f %f %f %f %f %f %*f %*f %n",
41 &zd, &az, &dec, &ra, &temp, &solar, &wind, &hum, &len);
42 if (n!=8)
43 {
44 cout << "Communication Problem: Wrong number of arguments" << endl;
45 fComStat = kComProblem;
46 return false;
47 }
48 str.Remove(0, len);
49
50 if (str!=(TString)"OVER")
51 {
52 cout << "Communication Problem: Termination (OVER) too far away." << endl;
53 fComStat = kComProblem;
54 return false;
55 }
56
57 fHumidity = hum;
58 fTemperature = temp;
59 fSolarRadiation = solar;
60 fWindSpeed = wind;
61
62 if (fWindSpeed>30)
63 fAlarmCounter++;
64 else
65 fAlarmCounter=0;
66
67 cout << "Zd/Az: " << zd << "/" << az << " ";
68 cout << "Ra/Dec: " << ra/15 << "h/" << dec << " ";
69 cout << "SolRad: " << solar << "W/m²" << endl;
70
71 fComStat = kCmdReceived;
72 return true;
73}
74
75bool MCeCoCom::InterpreteStr(TString str)
76{
77 const Ssiz_t tok = str.First(' ');
78 const TString cmd = tok<0 ? str : str(0, tok);
79 if (tok<0)
80 str = "";
81 else
82 str.Remove(0, tok+1);
83
84 if (cmd==(TString)"CC-REPORT" && str.EndsWith("OVER"))
85 return InterpreteReport(str);
86
87 const bool rc = InterpreteCmd(cmd, str.Strip(TString::kBoth));
88 fComStat = rc ? kCmdReceived : kComProblem;
89 return rc;
90}
91
92bool MCeCoCom::Send(const char *cmd, const char *str)
93{
94 MTime t;
95 t.Now();
96
97 UShort_t y1, y2, ms1, ms2;
98 Byte_t mon1, mon2, d1, d2, h1, h2, m1, m2, s1, s2;
99
100 t.GetDate(y1, mon1, d1);
101 t.GetTime(h1, m1, s1, ms1);
102
103 fT.GetDate(y2, mon2, d2);
104 fT.GetTime(h2, m2, s2, ms2);
105
106 MString msg;
107 msg.Print("%s "
108 "%02d %04d %02d %02d %02d %02d %02d %03d "
109 "%02d %04d %02d %02d %02d %02d %02d %03d "
110 "%s\n", cmd,
111 fStatus, y1, mon1, d1, h1, m1, s1, ms1,
112 fComStat, y2, mon2, d2, h2, m2, s2, ms2,
113 str);
114
115 const bool rc = MTcpIpIO::Send(msg);
116 fComStat = rc ? kNoCmdReceived : kComProblem;
117 return rc;
118}
119
120TString MCeCoCom::GetWeather() const
121{
122 if (fSolarRadiation<0 || (double)MTime(-1)-(double)fT>11)
123 return "";
124
125 MString str;
126 return str.Print("Temp: %.1f'C Hum: %.1f%% Wind: %.1fkm/h",
127 fTemperature, fHumidity, fWindSpeed);
128}
129
130Int_t MCeCoCom::GetWeatherStatus() const
131{
132 if (fSolarRadiation<0 || (double)MTime(-1)-(double)fT>11)
133 return 0;
134
135 Int_t rc = 0;
136 if (fHumidity>80)
137 rc++;
138 if (fWindSpeed>10)
139 rc++;
140 if (fWindSpeed>20)
141 rc++;
142 if (fWindSpeed>30)
143 rc++;
144
145 return rc;
146}
Note: See TracBrowser for help on using the repository browser.