- Timestamp:
- 05/23/12 09:44:31 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/smartfact.cc
r13803 r13820 306 306 307 307 class DimState : public DimInfoHandler 308 { 309 public: 310 DimState(const string &n, const string s="STATE") : 311 server(n), info(make_pair(Time(), -4)), 312 dim((n+"/"+s).c_str(), (void*)NULL, 0, this) { } 313 314 string server; 315 pair<Time, int> info; 316 string msg; 317 318 DimStampedInfo dim; 319 320 void infoHandler() 321 { 322 DimInfo *curr = getInfo(); // get current DimInfo address 323 if (!curr || curr != &dim) 324 return; 325 326 const bool disconnected = dim.getSize()==0; 327 328 // Make sure getTimestamp is called _before_ getTimestampMillisecs 329 const int tsec = dim.getTimestamp(); 330 const int tms = dim.getTimestampMillisecs(); 331 332 info = make_pair(Time(tsec, tms*1000), 333 disconnected ? -4 : dim.getQuality()); 334 335 msg = disconnected ? "" : dim.getString(); 336 } 337 338 const Time &time() const { return info.first; } 339 const int &state() const { return info.second; } 340 341 bool online() const { return info.second>-4; } 342 343 const string &name() const { return server; } 344 }; 345 346 class DimVersion : public DimState 347 { 348 public: 349 DimVersion() : DimState("DIS_DNS", "VERSION_NUMBER") { } 350 351 void infoHandler() 352 { 353 DimInfo *curr = getInfo(); // get current DimInfo address 354 if (!curr || curr != &dim) 355 return; 356 357 DimState::infoHandler(); 358 359 info.second = dim.getSize()==4 ? dim.getInt() : 0; 360 } 361 362 string version() const 363 { 364 if (info.second==0) 365 return "Offline"; 366 367 ostringstream out; 368 out << "V" << info.second/100 << 'r' << info.second%100; 369 return out.str(); 370 } 371 }; 372 373 class DimControl : public DimState 374 { 375 typedef function<void(const DimData &)> callback; 376 map<string, callback> fCallbacks; 377 public: 378 DimControl() : DimState("DIM_CONTROL") { } 379 380 void AddCallback(const string &script, const callback &cb) 381 { 382 fCallbacks[script] = cb; 383 } 384 385 void infoHandler() 386 { 387 DimInfo *curr = getInfo(); // get current DimInfo address 388 if (!curr || curr != &dim) 389 return; 390 391 DimState::infoHandler(); 392 393 // Evaluate msg 394 const size_t p0 = msg.find_first_of(':'); 395 if (p0==string::npos) 396 return; 397 398 const size_t p1 = msg.find_last_of('['); 399 if (p1==string::npos) 400 return; 401 402 const size_t p2 = msg.find_first_of(':', p0+1); 403 404 const size_t p3 = p2==string::npos || p2>p1 ? p1-1 : p2; 405 406 const string file = msg.substr(p0+2, p3-p0-2); 407 408 const auto func = fCallbacks.find(file); 409 if (func==fCallbacks.end()) 410 return; 411 412 // Call callback 413 func->second(DimData(curr)); 414 } 415 }; 308 { 309 protected: 310 typedef function<void(const DimData &)> callback; 311 312 public: 313 DimState(const string &n, const string s="STATE") : server(n), 314 info(make_pair(Time(), -4)), 315 dim((n+"/"+s).c_str(), (void*)NULL, 0, this) { } 316 317 string server; 318 pair<Time, int> info; 319 string msg; 320 321 DimStampedInfo dim; 322 323 callback fCallback; 324 325 void SetCallback(const callback &cb) 326 { 327 fCallback = cb; 328 } 329 330 void infoHandler() 331 { 332 DimInfo *curr = getInfo(); // get current DimInfo address 333 if (!curr || curr != &dim) 334 return; 335 336 const bool disconnected = dim.getSize()==0; 337 338 // Make sure getTimestamp is called _before_ getTimestampMillisecs 339 const int tsec = dim.getTimestamp(); 340 const int tms = dim.getTimestampMillisecs(); 341 342 info = make_pair(Time(tsec, tms*1000), 343 disconnected ? -4 : dim.getQuality()); 344 345 msg = disconnected ? "" : dim.getString(); 346 347 if (fCallback) 348 fCallback(DimData(curr)); 349 } 350 351 const Time &time() const { return info.first; } 352 const int &state() const { return info.second; } 353 354 bool online() const { return info.second>-4; } 355 356 const string &name() const { return server; } 357 }; 358 359 class DimVersion : public DimState 360 { 361 public: 362 DimVersion() : DimState("DIS_DNS", "VERSION_NUMBER") { } 363 364 void infoHandler() 365 { 366 DimInfo *curr = getInfo(); // get current DimInfo address 367 if (!curr || curr != &dim) 368 return; 369 370 DimState::infoHandler(); 371 372 info.second = dim.getSize()==4 ? dim.getInt() : 0; 373 } 374 375 string version() const 376 { 377 if (info.second==0) 378 return "Offline"; 379 380 ostringstream out; 381 out << "V" << info.second/100 << 'r' << info.second%100; 382 return out.str(); 383 } 384 }; 385 386 class DimControl : public DimState 387 { 388 map<string, callback> fCallbacks; 389 public: 390 DimControl() : DimState("DIM_CONTROL") { } 391 392 void AddCallback(const string &script, const callback &cb) 393 { 394 fCallbacks[script] = cb; 395 } 396 397 void infoHandler() 398 { 399 DimInfo *curr = getInfo(); // get current DimInfo address 400 if (!curr || curr != &dim) 401 return; 402 403 DimState::infoHandler(); 404 405 // Evaluate msg 406 const size_t p0 = msg.find_first_of(':'); 407 if (p0==string::npos) 408 return; 409 410 const size_t p1 = msg.find_last_of('['); 411 if (p1==string::npos) 412 return; 413 414 const size_t p2 = msg.find_first_of(':', p0+1); 415 416 const size_t p3 = p2==string::npos || p2>p1 ? p1-1 : p2; 417 418 const string file = msg.substr(p0+2, p3-p0-2); 419 420 const auto func = fCallbacks.find(file); 421 if (func==fCallbacks.end()) 422 return; 423 424 // Call callback 425 func->second(DimData(curr)); 426 } 427 }; 416 428 417 429 struct DimSubscriptions … … 434 446 DimState fChatServer; 435 447 448 DimStampedInfo fControlMessage; 449 436 450 DimStampedInfo fMcpConfiguration; 437 451 … … 475 489 fRateScan ("RATE_SCAN"), 476 490 fChatServer ("CHAT_SERVER"), 491 //--- 492 fControlMessage ("DIM_CONTROL/MESSAGE", (void*)NULL, 0, h), 477 493 //--- 478 494 fMcpConfiguration ("MCP/CONFIGURATION", (void*)NULL, 0, h), … … 537 553 // ----------------------------- Data storage ------------------------- 538 554 555 deque<string> fControlMessageHist; 556 539 557 uint32_t fMcpConfigurationState; 540 558 int64_t fMcpConfigurationMaxTime; … … 578 596 579 597 uint64_t fRateScanDataId; 580 deque<float> fRateScanDataHist; 598 uint8_t fRateScanBoard; 599 deque<float> fRateScanDataHist[41]; 581 600 582 601 // ------------- Initialize variables before the Dim stuff ------------ … … 676 695 }; 677 696 697 void HandleControlMessageImp(const DimData &d) 698 { 699 if (d.size()==0) 700 return; 701 702 const string str = fControlMessageHist.size()>0 ? fControlMessageHist.back() : "<pre> : : </pre> "; 703 const string time = "<pre>"+d.time.GetAsStr("%H:%M:%S")+"</pre> "; 704 705 ostringstream tst; 706 tst << d.qos; 707 708 string msg; 709 msg += str.substr(0, time.length())==time ? "<pre> </pre> " : time; 710 msg += d.ptr<char>(); 711 712 fControlMessageHist.push_back(msg); 713 714 ostringstream out; 715 out << setprecision(3); 716 out << d.time.JavaDate() << '\n'; 717 out << "#ffffff\t"; 718 719 for (auto it=fControlMessageHist.begin(); it!=fControlMessageHist.end(); it++) 720 out << *it << "<br/>"; 721 722 out << '\n'; 723 724 ofstream(fPath+"/test.txt") << out.str(); 725 } 726 727 void HandleControlMessage(const DimData &d) 728 { 729 if (d.qos==90) 730 HandleControlMessageImp(d); 731 } 732 733 void HandleControlStateChange(const DimData &d) 734 { 735 if (d.qos==-2) 736 fControlMessageHist.clear(); 737 738 if (d.qos<0) 739 HandleControlMessageImp(d); 740 } 741 678 742 void HandleMcpConfiguration(const DimData &d) 679 743 { … … 729 793 }; 730 794 731 const uint16_t idx = uint16_t(floor(fmod(fMagicWeatherHist[kDir].back()+360+11.25, 360)/22.5)); 795 796 const uint16_t idx = uint16_t(floor(fMagicWeatherHist[kDir].back()/22.5+16.5))%16; 797 //const uint16_t idx = uint16_t(floor(fmod(fMagicWeatherHist[kDir].back()/22.5+360+11.25, 360)/22.5))%16; 732 798 733 799 Astro astro(-(17.+53./60+26.525/3600), 28.+45./60+42.462/3600); 734 Moon moon (-(17.+53./60+26.525/3600), 28.+45./60+42.462/3600);800 Moon moon (-(17.+53./60+26.525/3600), 28.+45./60+42.462/3600); 735 801 736 802 ostringstream out; … … 789 855 }; 790 856 791 const uint16_t i = uint16_t(floor(fmod(az+360+11.25, 360)/22.5));792 fDriveControlPointingAz = dir[i ];857 const uint16_t idx = uint16_t(floor(az/22.5+16.5))%16; 858 fDriveControlPointingAz = dir[idx]; 793 859 794 860 ostringstream out; … … 1366 1432 1367 1433 const uint64_t id = d.get<uint64_t>(); 1368 const float rate = log10(d.get<float>(20));1434 const float *rate = d.ptr<float>(20); 1369 1435 1370 1436 if (fRateScanDataId!=id) 1371 1437 { 1372 fRateScanDataHist.clear(); 1438 for (int i=0; i<41; i++) 1439 fRateScanDataHist[i].clear(); 1373 1440 fRateScanDataId = id; 1374 1441 } 1375 fRateScanDataHist.push_back(rate); 1376 1377 WriteBinary(d, "ratescan-hist", fRateScanDataHist, 10, -2); 1442 fRateScanDataHist[0].push_back(log10(rate[0])); 1443 1444 double max = 0; 1445 for (int i=1; i<41; i++) 1446 { 1447 fRateScanDataHist[i].push_back(log10(rate[i])); 1448 if (rate[i]>max) 1449 max = rate[i]; 1450 } 1451 1452 fRateScanBoard ++; 1453 fRateScanBoard %= 40; 1454 1455 WriteBinary(d, "ratescan-hist", fRateScanDataHist[0], 10, -2); 1456 WriteBinary(d, "ratescan-board", fRateScanDataHist[fRateScanBoard+1], 10, -2); 1457 1458 ostringstream out; 1459 out << setprecision(3); 1460 out << d.time.JavaDate() << '\n'; 1461 out << "#ffffff\t" << pow(10, fRateScanDataHist[0].back()) << '\n'; 1462 out << "#ffffff\t" << max << '\n'; 1463 1464 ofstream(fPath+"/ratecan.txt") << out.str(); 1465 1466 out.str(""); 1467 out << d.time.JavaDate() << '\n'; 1468 out << "#ffffff\t" << int(fRateScanBoard) << '\n'; 1469 out << "#ffffff\t" << pow(10, fRateScanDataHist[fRateScanBoard+1].back()) << '\n'; 1470 1471 ofstream(fPath+"/ratecan_board.txt") << out.str(); 1378 1472 } 1379 1473 … … 1436 1530 return; 1437 1531 if (HandleService(curr, fDim->fFscControlHumidity, &StateMachineSmartFACT::HandleFscControlHumidity)) 1532 return; 1533 if (HandleService(curr, fDim->fControlMessage, &StateMachineSmartFACT::HandleControlMessage)) 1438 1534 return; 1439 1535 if (HandleService(curr, fDim->fRateScanData, &StateMachineSmartFACT::HandleRateScanData)) … … 1819 1915 fMcpConfigurationMaxTime(0), 1820 1916 fMcpConfigurationMaxEvents(0), 1917 fFscControlHumidityAvg(0), 1821 1918 fRateScanDataId(0), 1919 fRateScanBoard(0), 1822 1920 fDim(0) 1823 1921 { … … 1848 1946 1849 1947 fDim = new DimSubscriptions(this); 1948 fDim->fControl.SetCallback(bind(&StateMachineSmartFACT::HandleControlStateChange, this, placeholders::_1)); 1850 1949 fDim->fControl.AddCallback("dotest.dim", bind(&StateMachineSmartFACT::HandleDoTest, this, placeholders::_1)); 1851 1950
Note:
See TracChangeset
for help on using the changeset viewer.