| 1 | #include "MCeCoCom.h"
|
|---|
| 2 |
|
|---|
| 3 | #include <iostream>
|
|---|
| 4 |
|
|---|
| 5 | using namespace std;
|
|---|
| 6 |
|
|---|
| 7 | bool MCeCoCom::InterpreteCmd(TString cmd, TString str)
|
|---|
| 8 | {
|
|---|
| 9 | cout << cmd << ": " << str << endl;
|
|---|
| 10 | return true;
|
|---|
| 11 | }
|
|---|
| 12 |
|
|---|
| 13 | bool MCeCoCom::InterpreteReport(TString &str)
|
|---|
| 14 | {
|
|---|
| 15 | int y, m, d, h, min, s, ms, len;
|
|---|
| 16 |
|
|---|
| 17 | int n=sscanf(str.Data(),
|
|---|
| 18 | "%02d %04d %02d %02d %02d %02d %02d %03d %n",
|
|---|
| 19 | &fCCStatus, &y, &m, &d, &h, &min, &s, &ms, &len);
|
|---|
| 20 | if (n!=8)
|
|---|
| 21 | {
|
|---|
| 22 | cout << "Communication Problem: Wrong number of arguments" << endl;
|
|---|
| 23 | fComStat = kComProblem;
|
|---|
| 24 | return false;
|
|---|
| 25 | }
|
|---|
| 26 | str.Remove(0, len);
|
|---|
| 27 |
|
|---|
| 28 | fT.SetTimer(y, m, d, h, min, s, ms/1000.);
|
|---|
| 29 |
|
|---|
| 30 | // Remove the 30 tokens of the subsystem status
|
|---|
| 31 | // table 12.1 p59
|
|---|
| 32 | for (int i=0; i<30; i++)
|
|---|
| 33 | str.Remove(0, str.First(' ')+1);
|
|---|
| 34 |
|
|---|
| 35 | // skip solar_irr_Wm2, wind_speed, UPS
|
|---|
| 36 | float zd, az, dec, ra, temp, solar, wind, hum;
|
|---|
| 37 | n=sscanf(str.Data(),
|
|---|
| 38 | "%f %f %f %f %f %f %f %f %*f %*f %n",
|
|---|
| 39 | &zd, &az, &dec, &ra, &temp, &solar, &wind, &hum, &len);
|
|---|
| 40 | if (n!=8)
|
|---|
| 41 | {
|
|---|
| 42 | cout << "Communication Problem: Wrong number of arguments" << endl;
|
|---|
| 43 | fComStat = kComProblem;
|
|---|
| 44 | return false;
|
|---|
| 45 | }
|
|---|
| 46 | str.Remove(0, len);
|
|---|
| 47 |
|
|---|
| 48 | if (str!=(TString)"OVER")
|
|---|
| 49 | {
|
|---|
| 50 | cout << "Communication Problem: Termination (OVER) too far away." << endl;
|
|---|
| 51 | fComStat = kComProblem;
|
|---|
| 52 | return false;
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | cout << "Zd/Az: " << zd << "/" << az << " ";
|
|---|
| 56 | cout << "Ra/Dec: " << ra << "/" << dec << " ";
|
|---|
| 57 | cout << "Temp: " << temp << "'C ";
|
|---|
| 58 | cout << "Hum: " << hum << "% ";
|
|---|
| 59 | cout << "SolRad: " << solar << "W/m² ";
|
|---|
| 60 | cout << "Wind: " << wind << "km/h" << endl;
|
|---|
| 61 |
|
|---|
| 62 | fHumidity = hum;
|
|---|
| 63 | fTemperature = temp;
|
|---|
| 64 | fSolarRadiation = solar;
|
|---|
| 65 | fWindSpeed = wind;
|
|---|
| 66 |
|
|---|
| 67 | fComStat = kCmdReceived;
|
|---|
| 68 | return true;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | bool MCeCoCom::InterpreteStr(TString str)
|
|---|
| 72 | {
|
|---|
| 73 | const Ssiz_t tok = str.First(' ');
|
|---|
| 74 | const TString cmd = tok<0 ? str : str(0, tok);
|
|---|
| 75 | if (tok<0)
|
|---|
| 76 | str = "";
|
|---|
| 77 | else
|
|---|
| 78 | str.Remove(0, tok+1);
|
|---|
| 79 |
|
|---|
| 80 | if (cmd==(TString)"CC-REPORT" && str.EndsWith("OVER"))
|
|---|
| 81 | return InterpreteReport(str);
|
|---|
| 82 |
|
|---|
| 83 | const bool rc = InterpreteCmd(cmd, str.Strip(TString::kBoth));
|
|---|
| 84 | fComStat = rc ? kCmdReceived : kComProblem;
|
|---|
| 85 | return rc;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | bool MCeCoCom::Send(const char *str)
|
|---|
| 89 | {
|
|---|
| 90 | Timer t;
|
|---|
| 91 | t.Now();
|
|---|
| 92 |
|
|---|
| 93 | const char *msg =
|
|---|
| 94 | Form("%s "
|
|---|
| 95 | "%02d %04d %02d %02d %02d %02d %02d %03d "
|
|---|
| 96 | "%02d %04d %02d %02d %02d %02d %02d %03d "
|
|---|
| 97 | "%s\n", (const char*)fCommand,
|
|---|
| 98 | fStatus, t.Year(), t.Month(), t.Day(), t.H(), t.M(), t.S(), t.MilliSec(),
|
|---|
| 99 | fComStat, fT.Year(), fT.Month(), fT.Day(), fT.H(), fT.M(), fT.S(), fT.MilliSec(),
|
|---|
| 100 | str);
|
|---|
| 101 |
|
|---|
| 102 | const bool rc = MTcpIpIO::Send(msg);
|
|---|
| 103 | fComStat = rc ? kNoCmdReceived : kComProblem;
|
|---|
| 104 | return rc;
|
|---|
| 105 | }
|
|---|