| 1 | #include "Dim.h"
|
|---|
| 2 | #include "Event.h"
|
|---|
| 3 | #include "Shell.h"
|
|---|
| 4 | #include "StateMachineDim.h"
|
|---|
| 5 | #include "Connection.h"
|
|---|
| 6 | #include "Configuration.h"
|
|---|
| 7 | #include "Console.h"
|
|---|
| 8 | #include "Converter.h"
|
|---|
| 9 | #include "DimServiceInfoList.h"
|
|---|
| 10 | #include "PixelMap.h"
|
|---|
| 11 |
|
|---|
| 12 | #include "tools.h"
|
|---|
| 13 | #include "DimData.h"
|
|---|
| 14 |
|
|---|
| 15 | #include "LocalControl.h"
|
|---|
| 16 |
|
|---|
| 17 | #include "HeadersFAD.h"
|
|---|
| 18 | #include "HeadersBIAS.h"
|
|---|
| 19 | #include "HeadersFTM.h"
|
|---|
| 20 |
|
|---|
| 21 | namespace ba = boost::asio;
|
|---|
| 22 | namespace bs = boost::system;
|
|---|
| 23 | namespace dummy = ba::placeholders;
|
|---|
| 24 |
|
|---|
| 25 | using namespace std;
|
|---|
| 26 |
|
|---|
| 27 | // ------------------------------------------------------------------------
|
|---|
| 28 |
|
|---|
| 29 | #include "DimDescriptionService.h"
|
|---|
| 30 |
|
|---|
| 31 | // ------------------------------------------------------------------------
|
|---|
| 32 |
|
|---|
| 33 | class StateMachineSmartFACT : public StateMachineDim, public DimInfoHandler
|
|---|
| 34 | {
|
|---|
| 35 | private:
|
|---|
| 36 | enum states_t
|
|---|
| 37 | {
|
|---|
| 38 | kStateDimNetworkNA = 1,
|
|---|
| 39 | kStateRunning,
|
|---|
| 40 | };
|
|---|
| 41 |
|
|---|
| 42 | // ------------------------- Internal variables -----------------------
|
|---|
| 43 |
|
|---|
| 44 | PixelMap fPixelMap;
|
|---|
| 45 |
|
|---|
| 46 | Time fLastUpdate;
|
|---|
| 47 |
|
|---|
| 48 | // ----------------------------- Data storage -------------------------
|
|---|
| 49 |
|
|---|
| 50 | enum weather_t { kTemp = 0, kDew, kHum, kPress, kWind, kGusts, kDir };
|
|---|
| 51 | float fMagicWeatherData[7];
|
|---|
| 52 |
|
|---|
| 53 | vector<float> fFeedbackCalibration;
|
|---|
| 54 | vector<float> fBiasControlVoltageVec;
|
|---|
| 55 |
|
|---|
| 56 | float fBiasControlVoltageMed;
|
|---|
| 57 | float fBiasControlCurrentMed;
|
|---|
| 58 | float fBiasControlCurrentMax;
|
|---|
| 59 |
|
|---|
| 60 | deque<float> fBiasControlCurrentHist;
|
|---|
| 61 |
|
|---|
| 62 | float fDriveControlPointingZd;
|
|---|
| 63 | string fDriveControlPointingAz;
|
|---|
| 64 | float fDriveControlTrackingDev;
|
|---|
| 65 | string fDriveControlSourceName;
|
|---|
| 66 |
|
|---|
| 67 | float fFtmControlTriggerRateCam;
|
|---|
| 68 | deque<float> fFtmControlTriggerRateHist;
|
|---|
| 69 |
|
|---|
| 70 | // ------------- Initialize variables before the Dim stuff ------------
|
|---|
| 71 |
|
|---|
| 72 | DimServiceInfoList fNetwork;
|
|---|
| 73 |
|
|---|
| 74 | pair<Time, int> fStatusDim;
|
|---|
| 75 | pair<Time, int> fStatusDriveControl;
|
|---|
| 76 | pair<Time, int> fStatusMagicWeather;
|
|---|
| 77 | pair<Time, int> fStatusFeedback;
|
|---|
| 78 | pair<Time, int> fStatusBiasControl;
|
|---|
| 79 | pair<Time, int> fStatusFtmControl;
|
|---|
| 80 | pair<Time, int> fStatusFadControl;
|
|---|
| 81 |
|
|---|
| 82 | DimStampedInfo fDim;
|
|---|
| 83 |
|
|---|
| 84 | DimStampedInfo fDimDriveControl;
|
|---|
| 85 | DimStampedInfo fDimDriveControlPointing;
|
|---|
| 86 | DimStampedInfo fDimDriveControlTracking;
|
|---|
| 87 | DimStampedInfo fDimDriveControlSource;
|
|---|
| 88 |
|
|---|
| 89 | DimStampedInfo fDimMagicWeather;
|
|---|
| 90 | DimStampedInfo fDimMagicWeatherData;
|
|---|
| 91 |
|
|---|
| 92 | DimStampedInfo fDimFeedback;
|
|---|
| 93 | DimStampedInfo fDimFeedbackCalibration;
|
|---|
| 94 |
|
|---|
| 95 | DimStampedInfo fDimBiasControl;
|
|---|
| 96 | DimStampedInfo fDimBiasControlVoltage;
|
|---|
| 97 | DimStampedInfo fDimBiasControlCurrent;
|
|---|
| 98 |
|
|---|
| 99 | DimStampedInfo fDimFtmControl;
|
|---|
| 100 | DimStampedInfo fDimFtmControlTriggerRates;
|
|---|
| 101 |
|
|---|
| 102 | DimStampedInfo fDimFadControl;
|
|---|
| 103 | DimStampedInfo *fDimFadControlEventData;
|
|---|
| 104 |
|
|---|
| 105 | // -------------------------------------------------------------------
|
|---|
| 106 |
|
|---|
| 107 | pair<Time, int> GetNewState(DimStampedInfo &info) const
|
|---|
| 108 | {
|
|---|
| 109 | const bool disconnected = info.getSize()==0;
|
|---|
| 110 |
|
|---|
| 111 | // Make sure getTimestamp is called _before_ getTimestampMillisecs
|
|---|
| 112 | const int tsec = info.getTimestamp();
|
|---|
| 113 | const int tms = info.getTimestampMillisecs();
|
|---|
| 114 |
|
|---|
| 115 | return make_pair(Time(tsec, tms*1000),
|
|---|
| 116 | disconnected ? -2 : info.getQuality());
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | bool UpdateState(DimInfo *curr, DimStampedInfo &service, pair<Time,int> &rc)
|
|---|
| 120 | {
|
|---|
| 121 | if (curr!=&service)
|
|---|
| 122 | return false;
|
|---|
| 123 |
|
|---|
| 124 | rc = GetNewState(service);
|
|---|
| 125 | return true;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | bool HandleService(DimInfo *curr, const DimInfo &service, void (StateMachineSmartFACT::*handle)(const DimData &))
|
|---|
| 129 | {
|
|---|
| 130 | if (curr!=&service)
|
|---|
| 131 | return false;
|
|---|
| 132 |
|
|---|
| 133 | (this->*handle)(DimData(curr));
|
|---|
| 134 | return true;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 | bool CheckDataSize(const DimData &d, const char *name, size_t size)
|
|---|
| 139 | {
|
|---|
| 140 | if (d.data.size()==size)
|
|---|
| 141 | return true;
|
|---|
| 142 |
|
|---|
| 143 | ostringstream msg;
|
|---|
| 144 | msg << name << " - Received service has " << d.data.size() << " bytes, but expected " << size << ".";
|
|---|
| 145 | Warn(msg);
|
|---|
| 146 | return false;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 | // -------------------------------------------------------------------
|
|---|
| 151 |
|
|---|
| 152 | template<class T>
|
|---|
| 153 | void WriteBinary(const string &fname, const T &t, double scale, double offset=0)
|
|---|
| 154 | {
|
|---|
| 155 | vector<uint8_t> val(t.size(), 0);
|
|---|
| 156 | for (uint64_t i=0; i<t.size(); i++)
|
|---|
| 157 | {
|
|---|
| 158 | float range = nearbyint(128*(t[i]+offset)/scale); // [-2V; 2V]
|
|---|
| 159 | if (range>127)
|
|---|
| 160 | range=127;
|
|---|
| 161 | if (range<0)
|
|---|
| 162 | range=0;
|
|---|
| 163 | val[i] = (uint8_t)range;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | const char *ptr = reinterpret_cast<char*>(val.data());
|
|---|
| 167 |
|
|---|
| 168 | ofstream fout("www/"+fname+".bin");
|
|---|
| 169 | fout.write(ptr, val.size()*sizeof(uint8_t));
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | // -------------------------------------------------------------------
|
|---|
| 173 |
|
|---|
| 174 | void HandleMagicWeatherData(const DimData &d)
|
|---|
| 175 | {
|
|---|
| 176 | if (!CheckDataSize(d, "MagicWeather:Data", 7*4+2))
|
|---|
| 177 | return;
|
|---|
| 178 |
|
|---|
| 179 | // FIXME: Check size (7*4+2)
|
|---|
| 180 |
|
|---|
| 181 | //const uint16_t status = d.get<uint16_t>();
|
|---|
| 182 | memcpy(fMagicWeatherData, d.ptr<float>(2), 7*sizeof(float));
|
|---|
| 183 |
|
|---|
| 184 | ostringstream out;
|
|---|
| 185 | out << uint64_t(d.time.UnixTime()*1000) << '\n';
|
|---|
| 186 |
|
|---|
| 187 | for (int i=0; i<7; i++)
|
|---|
| 188 | out << "#ffffff\t" << fMagicWeatherData[i] << '\n';
|
|---|
| 189 |
|
|---|
| 190 | ofstream fout("www/magicweather.txt");
|
|---|
| 191 | fout << out.str();
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | void HandleDriveControlPointing(const DimData &d)
|
|---|
| 195 | {
|
|---|
| 196 | if (!CheckDataSize(d, "DriveControl:Pointing", 16))
|
|---|
| 197 | return;
|
|---|
| 198 |
|
|---|
| 199 | fDriveControlPointingZd = d.get<double>();
|
|---|
| 200 |
|
|---|
| 201 | const double az = d.get<double>(8);
|
|---|
| 202 |
|
|---|
| 203 | static const char *dir[] =
|
|---|
| 204 | {
|
|---|
| 205 | "N", "NNE", "NE", "ENE",
|
|---|
| 206 | "E", "ESE", "SE", "SSE",
|
|---|
| 207 | "S", "SSW", "SW", "WSW",
|
|---|
| 208 | "W", "WNW", "NW", "NNW"
|
|---|
| 209 | };
|
|---|
| 210 |
|
|---|
| 211 | const uint16_t i = uint16_t(floor(fmod(az+360+11.25, 360)/22));
|
|---|
| 212 | fDriveControlPointingAz = dir[i];
|
|---|
| 213 |
|
|---|
| 214 | ostringstream out;
|
|---|
| 215 | out << uint64_t(d.time.UnixTime()*1000) << '\n';
|
|---|
| 216 |
|
|---|
| 217 | out << setprecision(5);
|
|---|
| 218 | out << fDriveControlPointingZd << '\n';
|
|---|
| 219 | out << az << '\n';
|
|---|
| 220 |
|
|---|
| 221 | ofstream fout("www/drive-pointing.txt");
|
|---|
| 222 | fout << out.str();
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | void HandleDriveControlTracking(const DimData &d)
|
|---|
| 226 | {
|
|---|
| 227 | if (!CheckDataSize(d, "DriveControl:Tracking", 56))
|
|---|
| 228 | return;
|
|---|
| 229 |
|
|---|
| 230 | const double zd = d.get<double>(4*8) * M_PI / 180;
|
|---|
| 231 | const double dzd = d.get<double>(6*8) * M_PI / 180;
|
|---|
| 232 | const double daz = d.get<double>(7*8) * M_PI / 180;
|
|---|
| 233 |
|
|---|
| 234 | // Correct:
|
|---|
| 235 | // const double d = cos(del) - sin(zd+dzd)*sin(zd)*(1.-cos(daz));
|
|---|
| 236 |
|
|---|
| 237 | // Simplified:
|
|---|
| 238 | const double dev = cos(dzd) - sin(zd)*sin(zd)*(1.-cos(daz));
|
|---|
| 239 | fDriveControlTrackingDev = acos(dev) * 180 / M_PI * 3600;
|
|---|
| 240 | if (fDriveControlTrackingDev<0.01)
|
|---|
| 241 | fDriveControlTrackingDev=0;
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | void HandleDriveControlSource(const DimData &d)
|
|---|
| 245 | {
|
|---|
| 246 | //if (!CheckDataSize(d, "DriveControl:Source", 7*4+2))
|
|---|
| 247 | // return;
|
|---|
| 248 |
|
|---|
| 249 | const double *ptr = d.ptr<double>();
|
|---|
| 250 |
|
|---|
| 251 | const double ra = ptr[0]; // Ra[h]
|
|---|
| 252 | const double dec = ptr[1]; // Dec[deg]
|
|---|
| 253 | const double woff = ptr[4]; // Wobble offset [deg]
|
|---|
| 254 | const double wang = ptr[5]; // Wobble angle [deg]
|
|---|
| 255 |
|
|---|
| 256 | fDriveControlSourceName = d.ptr<char>(6*8);
|
|---|
| 257 |
|
|---|
| 258 | ostringstream out;
|
|---|
| 259 | out << uint64_t(d.time.UnixTime()*1000) << '\n';
|
|---|
| 260 |
|
|---|
| 261 | out << "#ffffff\t" << fDriveControlSourceName << '\n';
|
|---|
| 262 | out << setprecision(5);
|
|---|
| 263 | out << "#ffffff\t" << ra << '\n';
|
|---|
| 264 | out << "#ffffff\t" << dec << '\n';
|
|---|
| 265 | out << setprecision(3);
|
|---|
| 266 | out << "#ffffff\t" << woff << '\n';
|
|---|
| 267 | out << "#ffffff\t" << wang << '\n';
|
|---|
| 268 |
|
|---|
| 269 | ofstream fout("www/drive.txt");
|
|---|
| 270 | fout << out.str();
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | void HandleFeedbackCalibration(const DimData &d)
|
|---|
| 274 | {
|
|---|
| 275 | if (!CheckDataSize(d, "Feedback:Calibration", 3*4*416))
|
|---|
| 276 | {
|
|---|
| 277 | fFeedbackCalibration.clear();
|
|---|
| 278 | return;
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | const float *ptr = d.ptr<float>();
|
|---|
| 282 | fFeedbackCalibration.assign(ptr+2*416, ptr+3*416);
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | void HandleBiasControlVoltage(const DimData &d)
|
|---|
| 286 | {
|
|---|
| 287 | if (!CheckDataSize(d, "BiasControl:Voltage", 1664))
|
|---|
| 288 | {
|
|---|
| 289 | fBiasControlVoltageVec.clear();
|
|---|
| 290 | return;
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | fBiasControlVoltageVec.assign(d.ptr<float>(), d.ptr<float>()+320);
|
|---|
| 294 |
|
|---|
| 295 | vector<float> v(fBiasControlVoltageVec);
|
|---|
| 296 | sort(v.begin(), v.end());
|
|---|
| 297 |
|
|---|
| 298 | fBiasControlVoltageMed = (v[159]+v[160])/2;
|
|---|
| 299 |
|
|---|
| 300 | const char *ptr = d.ptr<char>();
|
|---|
| 301 |
|
|---|
| 302 | ofstream fout("www/biascontrol-voltage.bin");
|
|---|
| 303 | fout.write(ptr, 320*sizeof(float));
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | void HandleBiasControlCurrent(const DimData &d)
|
|---|
| 307 | {
|
|---|
| 308 | if (!CheckDataSize(d, "BiasControl:Current", 832))
|
|---|
| 309 | return;
|
|---|
| 310 |
|
|---|
| 311 | // Convert dac counts to uA
|
|---|
| 312 | vector<float> v(320);
|
|---|
| 313 | for (int i=0; i<320; i++)
|
|---|
| 314 | v[i] = d.ptr<uint16_t>()[i] * 5000./4096;
|
|---|
| 315 |
|
|---|
| 316 | // Calibrate the data (subtract offset)
|
|---|
| 317 | if (fFeedbackCalibration.size()>0 && fBiasControlVoltageVec.size()>0)
|
|---|
| 318 | for (int i=0; i<320; i++)
|
|---|
| 319 | v[i] -= fBiasControlVoltageVec[i]/fFeedbackCalibration[i]*1e6;
|
|---|
| 320 |
|
|---|
| 321 | // Get the maximum of each patch
|
|---|
| 322 | vector<float> val(160, 0);
|
|---|
| 323 | for (int i=0; i<160; i++)
|
|---|
| 324 | val[i] = max(v[i*2], v[i*2+1]);
|
|---|
| 325 |
|
|---|
| 326 | // Write the 160 patch values to a file
|
|---|
| 327 | WriteBinary("biascontrol-current", val, 1000);
|
|---|
| 328 |
|
|---|
| 329 | // Now sort them to determine the median
|
|---|
| 330 | sort(v.begin(), v.end());
|
|---|
| 331 |
|
|---|
| 332 | // Exclude the three crazy channels
|
|---|
| 333 | fBiasControlCurrentMed = (v[159]+v[160])/2;
|
|---|
| 334 | fBiasControlCurrentMax = v[316];
|
|---|
| 335 |
|
|---|
| 336 | // Store a history of the last 60 entries
|
|---|
| 337 | fBiasControlCurrentHist.push_back(fBiasControlCurrentMed);
|
|---|
| 338 | if (fBiasControlCurrentHist.size()>60)
|
|---|
| 339 | fBiasControlCurrentHist.pop_front();
|
|---|
| 340 |
|
|---|
| 341 | // write the history to a file
|
|---|
| 342 | WriteBinary("biascontrol-current-hist", fBiasControlCurrentHist, 1000);
|
|---|
| 343 | }
|
|---|
| 344 |
|
|---|
| 345 | void HandleFtmControlTriggerRates(const DimData &d)
|
|---|
| 346 | {
|
|---|
| 347 | if (!CheckDataSize(d, "FtmControl:TriggerRates", 24+160+640+8))
|
|---|
| 348 | return;
|
|---|
| 349 |
|
|---|
| 350 | fFtmControlTriggerRateCam = d.get<float>(20);
|
|---|
| 351 |
|
|---|
| 352 | const float *brates = d.ptr<float>(24); // Board rate
|
|---|
| 353 | const float *prates = d.ptr<float>(24+160); // Patch rate
|
|---|
| 354 |
|
|---|
| 355 | // Store a history of the last 60 entries
|
|---|
| 356 | fFtmControlTriggerRateHist.push_back(fFtmControlTriggerRateCam);
|
|---|
| 357 | if (fFtmControlTriggerRateHist.size()>60)
|
|---|
| 358 | fFtmControlTriggerRateHist.pop_front();
|
|---|
| 359 |
|
|---|
| 360 | WriteBinary("ftmcontrol-triggerrate-hist",
|
|---|
| 361 | fFtmControlTriggerRateHist, 100);
|
|---|
| 362 | WriteBinary("ftmcontrol-boardrates",
|
|---|
| 363 | vector<float>(brates, brates+40), 50);
|
|---|
| 364 | WriteBinary("ftmcontrol-patchrates",
|
|---|
| 365 | vector<float>(prates, prates+160), 10);
|
|---|
| 366 |
|
|---|
| 367 | // for (int i=0; i<160; i++) cout << prates[i] << endl;
|
|---|
| 368 |
|
|---|
| 369 | ostringstream out;
|
|---|
| 370 | out << setprecision(3);
|
|---|
| 371 | out << uint64_t(d.time.UnixTime()*1000) << '\n';
|
|---|
| 372 | out << "#ffffff\t" << fFtmControlTriggerRateCam << '\n';
|
|---|
| 373 |
|
|---|
| 374 | ofstream fout("www/trigger.txt");
|
|---|
| 375 | fout << out.str();
|
|---|
| 376 | }
|
|---|
| 377 |
|
|---|
| 378 | void HandleFadControlEventData(const DimData &d)
|
|---|
| 379 | {
|
|---|
| 380 | if (!CheckDataSize(d, "FadControl:EventData", 23040))
|
|---|
| 381 | return;
|
|---|
| 382 |
|
|---|
| 383 |
|
|---|
| 384 | //const float *avg = d.ptr<float>();
|
|---|
| 385 | //const float *rms = d.ptr<float>(1440*sizeof(float));
|
|---|
| 386 | const float *dat = d.ptr<float>(1440*sizeof(float)*2);
|
|---|
| 387 | //const float *pos = d.ptr<float>(1440*sizeof(float)*3);
|
|---|
| 388 |
|
|---|
| 389 | vector<float> max(160, -2000);
|
|---|
| 390 | for (int i=0; i<1440; i++)
|
|---|
| 391 | {
|
|---|
| 392 | const int idx = fPixelMap.index(i).hw()/9;
|
|---|
| 393 | if (dat[i]>max[idx])
|
|---|
| 394 | max[idx]=dat[i];
|
|---|
| 395 | }
|
|---|
| 396 |
|
|---|
| 397 | switch (d.qos)
|
|---|
| 398 | {
|
|---|
| 399 | case 0: WriteBinary("fadcontrol-eventdata", max, 2000, 1000); break;
|
|---|
| 400 | case 1: WriteBinary("fadcontrol-eventdata", max, 2000, 0); break;
|
|---|
| 401 | default: WriteBinary("fadcontrol-eventdata", max, 1000, 500); break;
|
|---|
| 402 | }
|
|---|
| 403 | }
|
|---|
| 404 |
|
|---|
| 405 | // -------------------------------------------------------------------
|
|---|
| 406 |
|
|---|
| 407 | void infoHandler()
|
|---|
| 408 | {
|
|---|
| 409 | DimInfo *curr = getInfo(); // get current DimInfo address
|
|---|
| 410 | if (!curr)
|
|---|
| 411 | return;
|
|---|
| 412 |
|
|---|
| 413 | if (HandleService(curr, fDimMagicWeatherData, &StateMachineSmartFACT::HandleMagicWeatherData))
|
|---|
| 414 | return;
|
|---|
| 415 | if (HandleService(curr, fDimDriveControlPointing, &StateMachineSmartFACT::HandleDriveControlPointing))
|
|---|
| 416 | return;
|
|---|
| 417 | if (HandleService(curr, fDimDriveControlTracking, &StateMachineSmartFACT::HandleDriveControlTracking))
|
|---|
| 418 | return;
|
|---|
| 419 | if (HandleService(curr, fDimDriveControlSource, &StateMachineSmartFACT::HandleDriveControlSource))
|
|---|
| 420 | return;
|
|---|
| 421 | if (HandleService(curr, fDimFeedbackCalibration, &StateMachineSmartFACT::HandleFeedbackCalibration))
|
|---|
| 422 | return;
|
|---|
| 423 | if (HandleService(curr, fDimBiasControlVoltage, &StateMachineSmartFACT::HandleBiasControlVoltage))
|
|---|
| 424 | return;
|
|---|
| 425 | if (HandleService(curr, fDimBiasControlCurrent, &StateMachineSmartFACT::HandleBiasControlCurrent))
|
|---|
| 426 | return;
|
|---|
| 427 | if (HandleService(curr, fDimFtmControlTriggerRates, &StateMachineSmartFACT::HandleFtmControlTriggerRates))
|
|---|
| 428 | return;
|
|---|
| 429 | if (HandleService(curr, *fDimFadControlEventData, &StateMachineSmartFACT::HandleFadControlEventData))
|
|---|
| 430 | return;
|
|---|
| 431 |
|
|---|
| 432 | if (UpdateState(curr, fDimMagicWeather, fStatusMagicWeather))
|
|---|
| 433 | return;
|
|---|
| 434 | if (UpdateState(curr, fDimDriveControl, fStatusDriveControl))
|
|---|
| 435 | return;
|
|---|
| 436 | if (UpdateState(curr, fDimFeedback, fStatusFeedback))
|
|---|
| 437 | return;
|
|---|
| 438 | if (UpdateState(curr, fDimBiasControl, fStatusBiasControl))
|
|---|
| 439 | return;
|
|---|
| 440 | if (UpdateState(curr, fDimFtmControl, fStatusFtmControl))
|
|---|
| 441 | return;
|
|---|
| 442 | if (UpdateState(curr, fDimFadControl, fStatusFadControl))
|
|---|
| 443 | return;
|
|---|
| 444 |
|
|---|
| 445 | if (curr==&fDim)
|
|---|
| 446 | {
|
|---|
| 447 | fStatusDim = GetNewState(fDim);
|
|---|
| 448 | fStatusDim.second = curr->getSize()==4 ? curr->getInt() : 0;
|
|---|
| 449 | return;
|
|---|
| 450 | }
|
|---|
| 451 | }
|
|---|
| 452 |
|
|---|
| 453 | bool CheckEventSize(size_t has, const char *name, size_t size)
|
|---|
| 454 | {
|
|---|
| 455 | if (has==size)
|
|---|
| 456 | return true;
|
|---|
| 457 |
|
|---|
| 458 | ostringstream msg;
|
|---|
| 459 | msg << name << " - Received event has " << has << " bytes, but expected " << size << ".";
|
|---|
| 460 | Fatal(msg);
|
|---|
| 461 | return false;
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | void PrintState(const pair<Time,int> &state, const char *server)
|
|---|
| 465 | {
|
|---|
| 466 | const State rc = fNetwork.GetState(server, state.second);
|
|---|
| 467 |
|
|---|
| 468 | Out() << state.first.GetAsStr("%H:%M:%S.%f").substr(0, 12) << " - ";
|
|---|
| 469 | Out() << kBold << server << ": ";
|
|---|
| 470 | if (rc.index==-2)
|
|---|
| 471 | {
|
|---|
| 472 | Out() << kReset << "Offline" << endl;
|
|---|
| 473 | return;
|
|---|
| 474 | }
|
|---|
| 475 | Out() << rc.name << "[" << rc.index << "]";
|
|---|
| 476 | Out() << kReset << " - " << kBlue << rc.comment << endl;
|
|---|
| 477 | }
|
|---|
| 478 |
|
|---|
| 479 | int Print()
|
|---|
| 480 | {
|
|---|
| 481 | Out() << fStatusDim.first.GetAsStr("%H:%M:%S.%f").substr(0, 12) << " - ";
|
|---|
| 482 | Out() << kBold << "DIM_DNS: ";
|
|---|
| 483 | if (fStatusDim.second==0)
|
|---|
| 484 | Out() << "Offline" << endl;
|
|---|
| 485 | else
|
|---|
| 486 | Out() << "V" << fStatusDim.second/100 << 'r' << fStatusDim.second%100 << endl;
|
|---|
| 487 |
|
|---|
| 488 | PrintState(fStatusMagicWeather, "MAGIC_WEATHER");
|
|---|
| 489 | PrintState(fStatusDriveControl, "DRIVE_CONTROL");
|
|---|
| 490 | PrintState(fStatusFeedback, "FEEDBACK");
|
|---|
| 491 | PrintState(fStatusBiasControl, "BIAS_CONTROL");
|
|---|
| 492 | PrintState(fStatusFadControl, "FAD_CONTROL");
|
|---|
| 493 |
|
|---|
| 494 | return GetCurrentState();
|
|---|
| 495 | }
|
|---|
| 496 |
|
|---|
| 497 | int Execute()
|
|---|
| 498 | {
|
|---|
| 499 | // Dispatch (execute) at most one handler from the queue. In contrary
|
|---|
| 500 | // to run_one(), it doesn't wait until a handler is available
|
|---|
| 501 | // which can be dispatched, so poll_one() might return with 0
|
|---|
| 502 | // handlers dispatched. The handlers are always dispatched/executed
|
|---|
| 503 | // synchronously, i.e. within the call to poll_one()
|
|---|
| 504 | //poll_one();
|
|---|
| 505 |
|
|---|
| 506 | if (fStatusDim.second==0)
|
|---|
| 507 | return kStateDimNetworkNA;
|
|---|
| 508 |
|
|---|
| 509 | Time now;
|
|---|
| 510 | if (now-fLastUpdate<boost::posix_time::seconds(1))
|
|---|
| 511 | return kStateRunning;
|
|---|
| 512 |
|
|---|
| 513 | fLastUpdate=now;
|
|---|
| 514 |
|
|---|
| 515 | ostringstream out;
|
|---|
| 516 | out << uint64_t(nearbyint(now.UnixTime()*1000)) << '\n';
|
|---|
| 517 | out << setprecision(3);
|
|---|
| 518 |
|
|---|
| 519 | // -------------- System status --------------
|
|---|
| 520 | out << "n/a\n";
|
|---|
| 521 |
|
|---|
| 522 | const static string kWhite = "#ffffff";
|
|---|
| 523 | const static string kYellow = "#fffff0";
|
|---|
| 524 | const static string kRed = "#fff8f0";
|
|---|
| 525 | const static string kGreen = "#f0fff0";
|
|---|
| 526 | const static string kBlue = "#f0f0ff";
|
|---|
| 527 |
|
|---|
| 528 | // ------------------ Drive -----------------
|
|---|
| 529 | if (fStatusDriveControl.second>=5) // Armed, Moving, Tracking
|
|---|
| 530 | {
|
|---|
| 531 | const State rc = fNetwork.GetState("DRIVE_CONTROL", fStatusDriveControl.second);
|
|---|
| 532 | out << kWhite << '\t';
|
|---|
| 533 | out << rc.name << '\t';
|
|---|
| 534 | out << fDriveControlPointingZd << '\t';
|
|---|
| 535 | out << fDriveControlPointingAz << '\t';
|
|---|
| 536 | if (fStatusDriveControl.second==7)
|
|---|
| 537 | {
|
|---|
| 538 | out << fDriveControlTrackingDev << '\t';
|
|---|
| 539 | out << fDriveControlSourceName << '\n';
|
|---|
| 540 | }
|
|---|
| 541 | else
|
|---|
| 542 | out << "\t\n";
|
|---|
| 543 | }
|
|---|
| 544 | else
|
|---|
| 545 | out << kWhite << '\n';
|
|---|
| 546 |
|
|---|
| 547 | // --------------- MagicWeather -------------
|
|---|
| 548 | if (fStatusMagicWeather.second==3)
|
|---|
| 549 | {
|
|---|
| 550 | const float diff = fMagicWeatherData[kTemp]-fMagicWeatherData[kDew];
|
|---|
| 551 | string col1 = kRed;
|
|---|
| 552 | if (diff>0.3)
|
|---|
| 553 | col1 = kYellow;
|
|---|
| 554 | if (diff>0.7)
|
|---|
| 555 | col1 = kGreen;
|
|---|
| 556 |
|
|---|
| 557 | const float wind = fMagicWeatherData[kGusts];
|
|---|
| 558 | string col2 = kGreen;
|
|---|
| 559 | if (wind>35)
|
|---|
| 560 | col2 = kYellow;
|
|---|
| 561 | if (wind>50)
|
|---|
| 562 | col2 = kRed;
|
|---|
| 563 |
|
|---|
| 564 | out << col1 << '\t';
|
|---|
| 565 | out << fMagicWeatherData[kTemp] << '\t';
|
|---|
| 566 | out << fMagicWeatherData[kDew] << '\n';
|
|---|
| 567 | out << col2 << '\t';
|
|---|
| 568 | out << fMagicWeatherData[kGusts] << '\n';
|
|---|
| 569 | }
|
|---|
| 570 | else
|
|---|
| 571 | out << kWhite << "\n\n";
|
|---|
| 572 |
|
|---|
| 573 | // --------------- FtmControl -------------
|
|---|
| 574 | if (fStatusFtmControl.second>=FTM::kIdle)
|
|---|
| 575 | {
|
|---|
| 576 | string col = kGreen;
|
|---|
| 577 | if (fFtmControlTriggerRateCam<15)
|
|---|
| 578 | col = kYellow;
|
|---|
| 579 | if (fFtmControlTriggerRateCam>100)
|
|---|
| 580 | col = kRed;
|
|---|
| 581 |
|
|---|
| 582 | out << col << '\t' << fFtmControlTriggerRateCam << '\n';
|
|---|
| 583 | }
|
|---|
| 584 | else
|
|---|
| 585 | out << kWhite << '\n';
|
|---|
| 586 |
|
|---|
| 587 | // --------------- BiasControl -------------
|
|---|
| 588 | if (fStatusBiasControl.second==5 || // Ramping
|
|---|
| 589 | fStatusBiasControl.second==7 || // On
|
|---|
| 590 | fStatusBiasControl.second==9) // Off
|
|---|
| 591 | {
|
|---|
| 592 | string col = fBiasControlVoltageMed>3?kGreen:kWhite;
|
|---|
| 593 | if (fBiasControlCurrentMax>280)
|
|---|
| 594 | col = kYellow;
|
|---|
| 595 | if (fBiasControlCurrentMax>350)
|
|---|
| 596 | col = kRed;
|
|---|
| 597 |
|
|---|
| 598 | if (fFeedbackCalibration.size()==0)
|
|---|
| 599 | col = kBlue;
|
|---|
| 600 |
|
|---|
| 601 | out << col << '\t';
|
|---|
| 602 | out << fBiasControlCurrentMed << '\t';
|
|---|
| 603 | out << fBiasControlCurrentMax << '\t';
|
|---|
| 604 | out << fBiasControlVoltageMed << '\n';
|
|---|
| 605 | }
|
|---|
| 606 | else
|
|---|
| 607 | out << kWhite << '\n';
|
|---|
| 608 |
|
|---|
| 609 |
|
|---|
| 610 | // ------------------------------------------
|
|---|
| 611 | ofstream fout("www/fact.txt");
|
|---|
| 612 | fout << out.str();
|
|---|
| 613 |
|
|---|
| 614 | return kStateRunning;
|
|---|
| 615 | }
|
|---|
| 616 |
|
|---|
| 617 | public:
|
|---|
| 618 | StateMachineSmartFACT(ostream &out=cout) : StateMachineDim(out, "SMART_FACT"),
|
|---|
| 619 | //---
|
|---|
| 620 | fStatusDim (make_pair(Time(), -2)),
|
|---|
| 621 | fStatusDriveControl(make_pair(Time(), -2)),
|
|---|
| 622 | fStatusMagicWeather(make_pair(Time(), -2)),
|
|---|
| 623 | fStatusFeedback (make_pair(Time(), -2)),
|
|---|
| 624 | fStatusBiasControl (make_pair(Time(), -2)),
|
|---|
| 625 | fStatusFtmControl (make_pair(Time(), -2)),
|
|---|
| 626 | fStatusFadControl (make_pair(Time(), -2)),
|
|---|
| 627 | //---
|
|---|
| 628 | fDim ("DIS_DNS/VERSION_NUMBER", (void*)NULL, 0, this),
|
|---|
| 629 | //---
|
|---|
| 630 | fDimDriveControl ("DRIVE_CONTROL/STATE", (void*)NULL, 0, this),
|
|---|
| 631 | fDimDriveControlPointing ("DRIVE_CONTROL/POINTING_POSITION", (void*)NULL, 0, this),
|
|---|
| 632 | fDimDriveControlTracking ("DRIVE_CONTROL/TRACKING_POSITION", (void*)NULL, 0, this),
|
|---|
| 633 | fDimDriveControlSource ("DRIVE_CONTROL/SOURCE_POSITION", (void*)NULL, 0, this),
|
|---|
| 634 | //---
|
|---|
| 635 | fDimMagicWeather ("MAGIC_WEATHER/STATE", (void*)NULL, 0, this),
|
|---|
| 636 | fDimMagicWeatherData ("MAGIC_WEATHER/DATA", (void*)NULL, 0, this),
|
|---|
| 637 | //---
|
|---|
| 638 | fDimFeedback ("FEEDBACK/STATE", (void*)NULL, 0, this),
|
|---|
| 639 | fDimFeedbackCalibration ("FEEDBACK/CALIBRATION", (void*)NULL, 0, this),
|
|---|
| 640 | //---
|
|---|
| 641 | fDimBiasControl ("BIAS_CONTROL/STATE", (void*)NULL, 0, this),
|
|---|
| 642 | fDimBiasControlVoltage ("BIAS_CONTROL/VOLTAGE", (void*)NULL, 0, this),
|
|---|
| 643 | fDimBiasControlCurrent ("BIAS_CONTROL/CURRENT", (void*)NULL, 0, this),
|
|---|
| 644 | //---
|
|---|
| 645 | fDimFtmControl ("FTM_CONTROL/STATE", (void*)NULL, 0, this),
|
|---|
| 646 | fDimFtmControlTriggerRates("FTM_CONTROL/TRIGGER_RATES", (void*)NULL, 0, this),
|
|---|
| 647 | //-
|
|---|
| 648 | fDimFadControl ("FAD_CONTROL/STATE", (void*)NULL, 0, this),
|
|---|
| 649 | fDimFadControlEventData(0)
|
|---|
| 650 | {
|
|---|
| 651 | // State names
|
|---|
| 652 | AddStateName(kStateDimNetworkNA, "DimNetworkNotAvailable",
|
|---|
| 653 | "The Dim DNS is not reachable.");
|
|---|
| 654 |
|
|---|
| 655 | AddStateName(kStateRunning, "Running", "");
|
|---|
| 656 |
|
|---|
| 657 | // Verbosity commands
|
|---|
| 658 | // AddEvent("SET_VERBOSE", "B:1")
|
|---|
| 659 | // (bind(&StateMachineMCP::SetVerbosity, this, placeholders::_1))
|
|---|
| 660 | // ("set verbosity state"
|
|---|
| 661 | // "|verbosity[bool]:disable or enable verbosity for received data (yes/no), except dynamic data");
|
|---|
| 662 |
|
|---|
| 663 | AddEvent("PRINT")
|
|---|
| 664 | (bind(&StateMachineSmartFACT::Print, this))
|
|---|
| 665 | ("");
|
|---|
| 666 | }
|
|---|
| 667 | ~StateMachineSmartFACT()
|
|---|
| 668 | {
|
|---|
| 669 | delete fDimFadControlEventData;
|
|---|
| 670 | }
|
|---|
| 671 | int EvalOptions(Configuration &conf)
|
|---|
| 672 | {
|
|---|
| 673 | if (!fPixelMap.Read(conf.Get<string>("pixel-map-file")))
|
|---|
| 674 | {
|
|---|
| 675 | Error("Reading mapping table from "+conf.Get<string>("pixel-map-file")+" failed.");
|
|---|
| 676 | return 1;
|
|---|
| 677 | }
|
|---|
| 678 |
|
|---|
| 679 | // Pixel map is needed to deal with this service
|
|---|
| 680 | fDimFadControlEventData=new DimStampedInfo("FAD_CONTROL/EVENT_DATA", (void*)NULL, 0, this);
|
|---|
| 681 |
|
|---|
| 682 | return -1;
|
|---|
| 683 | }
|
|---|
| 684 | };
|
|---|
| 685 |
|
|---|
| 686 | // ------------------------------------------------------------------------
|
|---|
| 687 |
|
|---|
| 688 | #include "Main.h"
|
|---|
| 689 |
|
|---|
| 690 | template<class T>
|
|---|
| 691 | int RunShell(Configuration &conf)
|
|---|
| 692 | {
|
|---|
| 693 | return Main::execute<T, StateMachineSmartFACT>(conf);
|
|---|
| 694 | }
|
|---|
| 695 |
|
|---|
| 696 | void SetupConfiguration(Configuration &conf)
|
|---|
| 697 | {
|
|---|
| 698 | po::options_description control("Smart FACT");
|
|---|
| 699 | control.add_options()
|
|---|
| 700 | ("pixel-map-file", var<string>("FACTmapV5a.txt"), "Pixel mapping file. Used here to get the default reference voltage.")
|
|---|
| 701 | ;
|
|---|
| 702 |
|
|---|
| 703 | conf.AddOptions(control);
|
|---|
| 704 | }
|
|---|
| 705 |
|
|---|
| 706 | /*
|
|---|
| 707 | Extract usage clause(s) [if any] for SYNOPSIS.
|
|---|
| 708 | Translators: "Usage" and "or" here are patterns (regular expressions) which
|
|---|
| 709 | are used to match the usage synopsis in program output. An example from cp
|
|---|
| 710 | (GNU coreutils) which contains both strings:
|
|---|
| 711 | Usage: cp [OPTION]... [-T] SOURCE DEST
|
|---|
| 712 | or: cp [OPTION]... SOURCE... DIRECTORY
|
|---|
| 713 | or: cp [OPTION]... -t DIRECTORY SOURCE...
|
|---|
| 714 | */
|
|---|
| 715 | void PrintUsage()
|
|---|
| 716 | {
|
|---|
| 717 | cout <<
|
|---|
| 718 | "SmartFACT is a tool writing the files needed for the SmartFACT web interface.\n"
|
|---|
| 719 | "\n"
|
|---|
| 720 | "The default is that the program is started without user intercation. "
|
|---|
| 721 | "All actions are supposed to arrive as DimCommands. Using the -c "
|
|---|
| 722 | "option, a local shell can be initialized. With h or help a short "
|
|---|
| 723 | "help message about the usuage can be brought to the screen.\n"
|
|---|
| 724 | "\n"
|
|---|
| 725 | "Usage: smartfact [-c type] [OPTIONS]\n"
|
|---|
| 726 | " or: smartfact [OPTIONS]\n";
|
|---|
| 727 | cout << endl;
|
|---|
| 728 | }
|
|---|
| 729 |
|
|---|
| 730 | void PrintHelp()
|
|---|
| 731 | {
|
|---|
| 732 | Main::PrintHelp<StateMachineSmartFACT>();
|
|---|
| 733 |
|
|---|
| 734 | /* Additional help text which is printed after the configuration
|
|---|
| 735 | options goes here */
|
|---|
| 736 |
|
|---|
| 737 | /*
|
|---|
| 738 | cout << "bla bla bla" << endl << endl;
|
|---|
| 739 | cout << endl;
|
|---|
| 740 | cout << "Environment:" << endl;
|
|---|
| 741 | cout << "environment" << endl;
|
|---|
| 742 | cout << endl;
|
|---|
| 743 | cout << "Examples:" << endl;
|
|---|
| 744 | cout << "test exam" << endl;
|
|---|
| 745 | cout << endl;
|
|---|
| 746 | cout << "Files:" << endl;
|
|---|
| 747 | cout << "files" << endl;
|
|---|
| 748 | cout << endl;
|
|---|
| 749 | */
|
|---|
| 750 | }
|
|---|
| 751 |
|
|---|
| 752 | int main(int argc, const char* argv[])
|
|---|
| 753 | {
|
|---|
| 754 | Configuration conf(argv[0]);
|
|---|
| 755 | conf.SetPrintUsage(PrintUsage);
|
|---|
| 756 | Main::SetupConfiguration(conf);
|
|---|
| 757 | SetupConfiguration(conf);
|
|---|
| 758 |
|
|---|
| 759 | if (!conf.DoParse(argc, argv, PrintHelp))
|
|---|
| 760 | return -1;
|
|---|
| 761 |
|
|---|
| 762 | //try
|
|---|
| 763 | {
|
|---|
| 764 | // No console access at all
|
|---|
| 765 | if (!conf.Has("console"))
|
|---|
| 766 | {
|
|---|
| 767 | // if (conf.Get<bool>("no-dim"))
|
|---|
| 768 | // return RunShell<LocalStream, StateMachine, ConnectionFSC>(conf);
|
|---|
| 769 | // else
|
|---|
| 770 | return RunShell<LocalStream>(conf);
|
|---|
| 771 | }
|
|---|
| 772 | // Cosole access w/ and w/o Dim
|
|---|
| 773 | /* if (conf.Get<bool>("no-dim"))
|
|---|
| 774 | {
|
|---|
| 775 | if (conf.Get<int>("console")==0)
|
|---|
| 776 | return RunShell<LocalShell, StateMachine, ConnectionFSC>(conf);
|
|---|
| 777 | else
|
|---|
| 778 | return RunShell<LocalConsole, StateMachine, ConnectionFSC>(conf);
|
|---|
| 779 | }
|
|---|
| 780 | else
|
|---|
| 781 | */ {
|
|---|
| 782 | if (conf.Get<int>("console")==0)
|
|---|
| 783 | return RunShell<LocalShell>(conf);
|
|---|
| 784 | else
|
|---|
| 785 | return RunShell<LocalConsole>(conf);
|
|---|
| 786 | }
|
|---|
| 787 | }
|
|---|
| 788 | /*catch (std::exception& e)
|
|---|
| 789 | {
|
|---|
| 790 | cerr << "Exception: " << e.what() << endl;
|
|---|
| 791 | return -1;
|
|---|
| 792 | }*/
|
|---|
| 793 |
|
|---|
| 794 | return 0;
|
|---|
| 795 | }
|
|---|