1 #include "otsdaq-utilities/SlowControlsDashboard/SlowControlsDashboardSupervisor.h"
6 #include "otsdaq/PluginMakers/MakeSlowControls.h"
7 #include "otsdaq/SlowControlsCore/SlowControlsVInterface.h"
9 #include <boost/regex.hpp>
13 #define CONTROLS_SUPERVISOR_DATA_PATH \
14 std::string(__ENV__("SERVICE_DATA_PATH")) + "/ControlsDashboardData/"
15 #define PAGES_DIRECTORY CONTROLS_SUPERVISOR_DATA_PATH + "pages/"
21 xdaq::ApplicationStub* stub)
22 : CoreSupervisorBase(stub)
24 __SUP_COUT__ <<
"Constructor." << __E__;
29 mkdir(((std::string)(CONTROLS_SUPERVISOR_DATA_PATH)).c_str(), 0755);
30 mkdir(((std::string)(PAGES_DIRECTORY)).c_str(), 0755);
33 alarmNotifyRefreshRate_ = 60;
37 __SUP_COUT__ <<
"Constructed." << __E__;
41 SlowControlsDashboardSupervisor::~SlowControlsDashboardSupervisor(
void)
43 __SUP_COUT__ <<
"Destructor." << __E__;
45 __SUP_COUT__ <<
"Destructed." << __E__;
49 void SlowControlsDashboardSupervisor::destroy(
void)
56 void SlowControlsDashboardSupervisor::init(
void)
61 __SUP_COUT__ << __E__;
62 ConfigurationTree node = CorePropertySupervisorBase::getSupervisorTableNode();
63 std::string pluginType;
68 node.getNode(
"SlowControlsInterfacePluginType").getValue<std::string>();
73 __COUT__ <<
"Pluging type was not definded through supervisor table, trying "
74 "supervisor property..."
76 pluginType = CorePropertySupervisorBase::getSupervisorProperty(
77 "ControlsInterfacePluginType");
80 __COUTV__(pluginType);
83 makeSlowControls(pluginType,
84 CorePropertySupervisorBase::getSupervisorUID(),
85 CorePropertySupervisorBase::getContextTreeNode(),
86 CorePropertySupervisorBase::getSupervisorConfigurationPath());
96 std::lock_guard<std::mutex> lock(cs->pluginBusyMutex_);
98 cs->interface_->initialize();
113 cs->checkSlowControlsAlarms(cs);
118 __SUP_COUT__ <<
"Finished init() w/ interface: " << pluginType << __E__;
121 CoreSupervisorBase::theStateMachineImplementation_.push_back(interface_);
126 void SlowControlsDashboardSupervisor::checkSlowControlsAlarms(
130 std::lock_guard<std::mutex> lock(cs->alarmCheckThreadErrorMutex_);
131 cs->alarmCheckThreadError_ =
"";
138 for(
const auto& alarm : cs->interface_->checkAlarmNotifications())
142 time_t rawtime =
static_cast<time_t
>(std::stoi(alarm[1]));
143 char* dt = ctime(&rawtime);
144 std::string subject =
"Slow Control Alarm Notification";
145 std::string message =
146 "PV: " + alarm[0] +
"\n" +
" at time: " + dt +
"\n" +
147 " value: " + alarm[2] +
"" +
" status: " + alarm[3] +
"" +
148 " severity: " + alarm[4];
162 theRemoteWebUsers_.sendSystemMessage(
163 alarm[6], subject, message, alarm[7] ==
"Yes" ?
true :
false);
167 catch(
const std::runtime_error& e)
169 __SS__ << e.what() <<
'\n';
170 std::lock_guard<std::mutex> lock(cs->alarmCheckThreadErrorMutex_);
171 cs->alarmCheckThreadError_ = ss.str();
172 __COUT_ERR__ << ss.str();
174 catch(
const std::exception& e)
176 __SS__ << e.what() <<
'\n';
177 std::lock_guard<std::mutex> lock(cs->alarmCheckThreadErrorMutex_);
178 cs->alarmCheckThreadError_ = ss.str();
179 __COUT_ERR__ << ss.str();
183 __SS__ <<
"checkSlowControlsAlarms() ERROR While sendin alarm messages"
185 std::lock_guard<std::mutex> lock(cs->alarmCheckThreadErrorMutex_);
186 cs->alarmCheckThreadError_ = ss.str();
187 __COUT_ERR__ << ss.str();
190 sleep(alarmNotifyRefreshRate_);
191 __COUT__ <<
"checkSlowControlsAlarms() n. "
192 << cs->interface_->checkAlarmNotifications().size() << __E__;
198 void SlowControlsDashboardSupervisor::checkSubscriptions(
201 __COUT__ <<
"checkSubscriptions() initializing..." << __E__;
202 std::vector<std::string> channelList;
203 std::vector<int> channelRefreshRates;
206 channelList = {
"FIRST VALUE"};
207 channelRefreshRates = {};
208 std::map<int, std::set<std::string>>::iterator mapReference =
209 cs->channelDependencyLookupMap_.begin();
210 while(mapReference !=
211 cs->channelDependencyLookupMap_
214 for(
auto channel : mapReference->second)
216 int refreshRate = 15;
217 channelRefreshRates.push_back(refreshRate);
219 __COUT__ <<
"THREAD actual time: " << std::time(NULL)
220 <<
"; uidPollTimeMap + 10 * refreshTime: "
221 << cs->uidPollTimeMap_.at(mapReference->first) + 10 * refreshRate
222 <<
" seconds" << __E__;
224 cs->uidPollTimeMap_.at(mapReference->first) + 10 * refreshRate)
228 cs->channelDependencyLookupMap_.erase(mapReference->first);
231 catch(
const std::exception& e)
237 std::vector<std::string>::iterator it =
238 find(channelList.begin(), channelList.end(), channel);
239 if(it == channelList.end())
243 channelList.push_back(channel);
244 __COUT__ <<
"Channel: " << channel <<
" refreshRate: " << refreshRate
245 <<
" seconds" << __E__;
246 __COUT__ <<
"channelDependencyLookupMap_.size(): "
247 << cs->channelDependencyLookupMap_.size()
248 <<
" UID: " << mapReference->first
249 <<
" mapReference->second.size(): "
250 << mapReference->second.size() << __E__;
258 if(channelRefreshRates.size() > 0)
260 *min_element(channelRefreshRates.begin(), channelRefreshRates.end());
262 __COUT__ <<
"Loop over channels subscribing - waiting time: " << minTime
263 <<
" seconds" << __E__;
271 void SlowControlsDashboardSupervisor::setSupervisorPropertyDefaults()
273 CorePropertySupervisorBase::setSupervisorProperty(
274 CorePropertySupervisorBase::SUPERVISOR_PROPERTIES.CheckUserLockRequestTypes,
281 void SlowControlsDashboardSupervisor::forceSupervisorPropertyValues()
283 CorePropertySupervisorBase::setSupervisorProperty(
284 CorePropertySupervisorBase::SUPERVISOR_PROPERTIES.AutomatedRequestTypes,
289 void SlowControlsDashboardSupervisor::request(
const std::string& requestType,
291 HttpXmlDocument& xmlOut,
292 const WebUsers::RequestUserInfo& userInfo)
296 if(requestType !=
"getPages" && !pluginBusyMutex_.try_lock())
298 __SUP_SS__ <<
"Controls plugin is still initializing. Please try again in a "
304 __SUP_COUT__ <<
"User name is " << userInfo.username_ <<
"." << __E__;
305 __SUP_COUT__ <<
"User permission level for request '" << requestType <<
"' is "
306 << unsigned(userInfo.permissionLevel_)
307 <<
"(isAdmin=" << (userInfo.isAdmin() ?
"Yes" :
"No") <<
")."
311 handleRequest(requestType, xmlOut, cgiIn, userInfo);
313 catch(
const std::runtime_error& e)
315 __SUP_SS__ <<
"Error occurred handling request '" << requestType
316 <<
"': " << e.what() << __E__;
317 __SUP_COUT__ << ss.str();
318 xmlOut.addTextElementToData(
"Error", ss.str());
322 __SS__ <<
"Unknown error occurred handling request '" << requestType <<
"!'"
324 __SUP_COUT__ << ss.str();
325 xmlOut.addTextElementToData(
"Error", ss.str());
328 pluginBusyMutex_.unlock();
329 __SUP_COUT__ << __E__;
333 void SlowControlsDashboardSupervisor::handleRequest(
334 const std::string Command,
335 HttpXmlDocument& xmlOut,
337 const WebUsers::RequestUserInfo& userInfo)
340 __SUP_COUT__ << __E__;
342 if(Command ==
"poll")
344 std::string uid = CgiDataUtilities::getOrPostData(cgiIn,
"uid");
345 Poll(cgiIn, xmlOut, uid);
347 else if(Command ==
"userActivityHeartbeat")
353 else if(Command ==
"generateUID")
355 std::string channelList = CgiDataUtilities::getOrPostData(cgiIn,
"pvList");
356 GenerateUID(cgiIn, xmlOut, channelList);
358 else if(Command ==
"isUserAdmin")
360 std::string json = std::string(
"{ \"message\": \"");
361 json += (userInfo.isAdmin() ?
"Yes" :
"No");
363 xmlOut.addTextElementToData(
"JSON", json.c_str());
365 else if(Command ==
"getUserPermissions")
367 GetUserPermissions(cgiIn, xmlOut, userInfo);
369 else if(Command ==
"getPVSettings")
371 __SUP_COUT__ <<
"Channel settings requested from server! " << __E__;
372 GetChannelSettings(cgiIn, xmlOut);
373 xmlOut.addTextElementToData(
"id", CgiDataUtilities::getData(cgiIn,
"id"));
375 else if(Command ==
"getPVArchiverData")
377 __SUP_COUT__ <<
"Archived Channel data requested from server! " << __E__;
378 GetChannelArchiverData(cgiIn, xmlOut);
379 xmlOut.addTextElementToData(
"id", CgiDataUtilities::getData(cgiIn,
"id"));
381 else if(Command ==
"getList")
383 __SUP_COUT__ <<
"Channel List requested from server! " << __E__;
384 GetList(cgiIn, xmlOut);
386 else if(Command ==
"getPages")
388 __SUP_COUT__ <<
"Requesting pages from server! " << __E__;
389 GetPages(cgiIn, xmlOut);
391 else if(Command ==
"loadPage")
393 std::string page = CgiDataUtilities::getData(cgiIn,
"Page");
394 __SUP_COUT__ << this->getApplicationDescriptor()->getLocalId() <<
" " << page
397 loadPage(cgiIn, xmlOut, page, userInfo);
399 else if(Command ==
"loadPhoebusPage")
401 std::string page = CgiDataUtilities::getData(cgiIn,
"Page");
402 __SUP_COUT__ << this->getApplicationDescriptor()->getLocalId() <<
" " << page
405 loadPhoebusPage(cgiIn, xmlOut, page, userInfo);
407 else if(Command ==
"createControlsPage")
409 SaveControlsPage(cgiIn, xmlOut, userInfo);
411 else if(Command ==
"createPhoebusControlsPage")
413 SavePhoebusControlsPage(cgiIn, xmlOut, userInfo);
415 else if(Command ==
"getLastAlarmsData")
417 __SUP_COUT__ <<
"Last Alams Data requested from server! " << __E__;
418 GetLastAlarmsData(cgiIn, xmlOut);
419 xmlOut.addTextElementToData(
"id", CgiDataUtilities::getData(cgiIn,
"id"));
421 else if(Command ==
"getAlarmsLogData")
423 __SUP_COUT__ <<
"Alams Log Data requested from server! " << __E__;
424 GetAlarmsLogData(cgiIn, xmlOut);
425 xmlOut.addTextElementToData(
"id", CgiDataUtilities::getData(cgiIn,
"id"));
428 __SUP_COUT__ <<
"" << __E__;
432 void SlowControlsDashboardSupervisor::Poll(cgicc::Cgicc& ,
433 HttpXmlDocument& xmlOut,
436 __SUP_COUT__ << this->getApplicationDescriptor()->getLocalId() <<
" "
437 <<
"Polling on UID:" << UID << __E__;
439 std::map<int, std::set<std::string>>::iterator mapReference;
441 if(UID !=
"" && (mapReference = channelDependencyLookupMap_.find(std::stoi(UID))) !=
442 channelDependencyLookupMap_
445 uidPollTimeMap_.at(std::stoi(UID)) = std::time(NULL);
446 std::string JSONMessage =
"{ ";
448 for(
auto channel : mapReference->second)
452 __SUP_COUT__ << channel << __E__;
454 std::array<std::string, 4> channelInformation =
455 interface_->getCurrentValue(channel);
457 __SUP_COUT__ << channel <<
": " << channelInformation[1] <<
" : "
458 << channelInformation[3] << __E__;
460 if(channelInformation[0] !=
"NO_CHANGE")
463 JSONMessage +=
"\"" + channel +
"\": {";
464 JSONMessage +=
"\"Timestamp\":\"" + channelInformation[0] +
"\",";
465 JSONMessage +=
"\"Value\":\"" + channelInformation[1] +
"\",";
466 JSONMessage +=
"\"Status\":\"" + channelInformation[2] +
"\",";
467 JSONMessage +=
"\"Severity\":\"" + channelInformation[3] +
"\"},";
471 __SUP_COUT__ <<
"No change in value since last poll: " << channel
476 if(channelInformation[3] ==
"INVALID")
478 interface_->unsubscribe(channel);
479 interface_->subscribe(channel);
483 JSONMessage = JSONMessage.substr(0, JSONMessage.length() - 1);
485 __SUP_COUT__ << JSONMessage << __E__;
486 xmlOut.addTextElementToData(
"JSON", JSONMessage);
490 xmlOut.addTextElementToData(
"JSON",
491 "{ \"message\": \"NOT_FOUND\"}");
496 void SlowControlsDashboardSupervisor::GetChannelSettings(cgicc::Cgicc& cgiIn,
497 HttpXmlDocument& xmlOut)
499 std::string channelList = CgiDataUtilities::postData(cgiIn,
"pvList");
501 __SUP_COUT__ << this->getApplicationDescriptor()->getLocalId() <<
" "
502 <<
"Getting settings for " << channelList << __E__;
504 std::string JSONMessage =
"{ ";
506 __SUP_COUT__ <<
"**********************" << channelList.size() << __E__;
507 if(channelList.size() > 0)
512 while((nextPos = channelList.find(
",", pos)) != std::string::npos)
514 channel = channelList.substr(pos, nextPos - pos);
516 __SUP_COUT__ << channel << __E__;
518 std::array<std::string, 9> channelSettings = interface_->getSettings(channel);
520 JSONMessage +=
"\"" + channel +
"\": {";
521 JSONMessage +=
"\"Units\": \"" + channelSettings[0] +
"\",";
522 JSONMessage +=
"\"Upper_Display_Limit\": \"" + channelSettings[1] +
"\",";
523 JSONMessage +=
"\"Lower_Display_Limit\": \"" + channelSettings[2] +
"\",";
524 JSONMessage +=
"\"Upper_Alarm_Limit\": \"" + channelSettings[3] +
"\",";
525 JSONMessage +=
"\"Upper_Warning_Limit\": \"" + channelSettings[4] +
"\",";
526 JSONMessage +=
"\"Lower_Warning_Limit\": \"" + channelSettings[5] +
"\",";
527 JSONMessage +=
"\"Lower_Alarm_Limit\": \"" + channelSettings[6] +
"\",";
528 JSONMessage +=
"\"Upper_Control_Limit\": \"" + channelSettings[7] +
"\",";
529 JSONMessage +=
"\"Lower_Control_Limit\": \"" + channelSettings[8] +
"\"},";
534 JSONMessage = JSONMessage.substr(0, JSONMessage.length() - 1);
537 __SUP_COUT__ << JSONMessage << __E__;
538 xmlOut.addTextElementToData(
"JSON", JSONMessage);
542 __SUP_COUT__ <<
"Did not find any settings because Channel list is length zero!"
545 xmlOut.addTextElementToData(
546 "JSON",
"{ \"message\": \"GetPVSettings\"}");
551 void SlowControlsDashboardSupervisor::GetChannelArchiverData(cgicc::Cgicc& cgiIn,
552 HttpXmlDocument& xmlOut)
554 __SUP_COUT__ <<
"Requesting archived data!" << __E__;
556 std::string channelList = CgiDataUtilities::postData(cgiIn,
"pvList");
558 __SUP_COUT__ << this->getApplicationDescriptor()->getLocalId() <<
" "
559 <<
"Getting History for " << channelList << __E__;
561 __SUP_COUT__ <<
"channelList.size(): " << channelList.size() << __E__;
562 if(channelList.size() > 0)
567 while((nextPos = channelList.find(
",", pos)) != std::string::npos)
569 channel = channelList.substr(pos, nextPos - pos);
571 __SUP_COUT__ << channel << __E__;
573 std::vector<std::vector<std::string>> channelInformation =
574 interface_->getChannelHistory(channel);
575 __SUP_COUT__ << channel <<
": " << channelInformation[0][1] <<
" : "
576 << channelInformation[0][3] << __E__;
578 for(
auto channelData : channelInformation)
580 std::string JSONMessage =
"{ ";
581 JSONMessage +=
"\"" + channel +
"\": {";
582 JSONMessage +=
"\"Timestamp\":\"" + channelData[0] +
"\",";
583 JSONMessage +=
"\"Value\":\"" + channelData[1] +
"\",";
584 JSONMessage +=
"\"Status\":\"" + channelData[2] +
"\",";
585 JSONMessage +=
"\"Severity\":\"" + channelData[3] +
"\"},";
587 JSONMessage = JSONMessage.substr(0, JSONMessage.length() - 1);
589 xmlOut.addTextElementToData(
"JSON", JSONMessage);
597 __SUP_COUT__ <<
"Did not find any data because Channel list is length zero!"
600 xmlOut.addTextElementToData(
601 "JSON",
"{ \"message\": \"GetChannelArchiverData\"}");
606 void SlowControlsDashboardSupervisor::GetLastAlarmsData(cgicc::Cgicc& cgiIn,
607 HttpXmlDocument& xmlOut)
609 __SUP_COUT__ <<
"Requesting last alarms data!" << __E__;
611 std::string channelList = CgiDataUtilities::postData(cgiIn,
"pvList");
613 __SUP_COUT__ << this->getApplicationDescriptor()->getLocalId() <<
" "
614 <<
"Getting last Alarms for " << channelList << __E__;
615 __SUP_COUT__ <<
"channelList.size(): " << channelList.size() << __E__;
617 std::vector<std::vector<std::string>> alarms;
620 std::function<void(HttpXmlDocument&, std::vector<std::vector<std::string>>&)>
621 jsonFiller = [](HttpXmlDocument& xmlOut,
622 std::vector<std::vector<std::string>>& alarms) {
624 for(
auto& alarmRow : alarms)
626 std::string JSONMessage =
"{ ";
627 JSONMessage +=
"\"id\":\"" + alarmRow[0] +
"\",";
628 JSONMessage +=
"\"pvName\":\"" + alarmRow[1] +
"\",";
629 JSONMessage +=
"\"pvDescription\":\"" + alarmRow[2] +
"\",";
630 JSONMessage +=
"\"pvValue\":\"" + alarmRow[3] +
"\",";
631 JSONMessage +=
"\"pvStatus\":\"" + alarmRow[4] +
"\",";
632 JSONMessage +=
"\"pvSeverity\":\"" + alarmRow[5] +
"\",";
633 JSONMessage +=
"\"pvTime\":\"" + alarmRow[6] +
"\",";
635 JSONMessage = JSONMessage.substr(0, JSONMessage.length() - 1);
637 xmlOut.addTextElementToData(
"JSON", JSONMessage);
642 if(channelList.size() > 0)
647 while((nextPos = channelList.find(
",", pos)) != std::string::npos)
649 channel = channelList.substr(pos, nextPos - pos);
651 alarms = interface_->getLastAlarms(channel);
652 __SUP_COUT__ <<
"get Last Alarms for channel: " << channel << __E__;
653 jsonFiller(xmlOut, alarms);
659 alarms = interface_->getLastAlarms(
"");
660 __SUP_COUT__ <<
"get Last Alarms for all channels" << __E__;
661 jsonFiller(xmlOut, alarms);
666 void SlowControlsDashboardSupervisor::GetAlarmsLogData(cgicc::Cgicc& cgiIn,
667 HttpXmlDocument& xmlOut)
669 __SUP_COUT__ <<
"Requesting alarms log data!" << __E__;
671 std::string channelList = CgiDataUtilities::postData(cgiIn,
"pvList");
673 __SUP_COUT__ << this->getApplicationDescriptor()->getLocalId() <<
" "
674 <<
"Getting Alarms Log for " << channelList << __E__;
675 __SUP_COUT__ <<
"channelList.size(): " << channelList.size() << __E__;
677 std::vector<std::vector<std::string>> alarmsLog;
680 std::function<void(HttpXmlDocument&, std::vector<std::vector<std::string>>&)>
681 jsonFiller = [](HttpXmlDocument& xmlOut,
682 std::vector<std::vector<std::string>>& alarmsLog) {
683 if (alarmsLog.size())
684 for(
auto& alarmRow : alarmsLog)
686 std::string JSONMessage =
"{ ";
687 JSONMessage +=
"\"id\":\"" + alarmRow[0] +
"\",";
688 JSONMessage +=
"\"pvName\":\"" + alarmRow[1] +
"\",";
689 JSONMessage +=
"\"pvValue\":\"" + alarmRow[2] +
"\",";
690 JSONMessage +=
"\"pvStatus\":\"" + alarmRow[3] +
"\",";
691 JSONMessage +=
"\"pvSeverity\":\"" + alarmRow[4] +
"\",";
692 JSONMessage +=
"\"pvTime\":\"" + alarmRow[5] +
"\",";
694 JSONMessage = JSONMessage.substr(0, JSONMessage.length() - 1);
696 xmlOut.addTextElementToData(
"JSON", JSONMessage);
701 if(channelList.size() > 0)
706 while((nextPos = channelList.find(
",", pos)) != std::string::npos)
708 channel = channelList.substr(pos, nextPos - pos);
710 alarmsLog = interface_->getAlarmsLog(channel);
711 __SUP_COUT__ <<
"get Alarms Log for channel: " << channel << __E__;
712 jsonFiller(xmlOut, alarmsLog);
718 alarmsLog = interface_->getAlarmsLog(
"");
719 __SUP_COUT__ <<
"get Alarms Log for all channels" << __E__;
720 jsonFiller(xmlOut, alarmsLog);
726 SlowControlsDashboardSupervisor::GetUserPermissions(
729 const WebUsers::RequestUserInfo& )
735 void SlowControlsDashboardSupervisor::GenerateUID(cgicc::Cgicc& ,
736 HttpXmlDocument& xmlOut,
737 std::string channelList)
739 __SUP_COUT__ << this->getApplicationDescriptor()->getLocalId() <<
" "
740 <<
"Generating UID" << __E__;
742 std::set<std::string> channelDependencies;
743 StringMacros::getSetFromString(channelList, channelDependencies);
746 channelDependencyLookupMap_.insert(
747 std::pair<
int, std::set<std::string>>(++UID_, channelDependencies));
749 uidPollTimeMap_.insert(std::pair<int, long int>(UID_, std::time(NULL)));
751 __SUP_COUT__ << this->getApplicationDescriptor()->getLocalId() <<
" NEW UID: " << UID_
752 <<
" maps to " << channelDependencies.size() <<
" channels" << __E__;
754 xmlOut.addTextElementToData(
"JSON",
755 std::string(
"{ \"message\": \"") +
756 std::to_string(UID_) +
761 void SlowControlsDashboardSupervisor::GetList(cgicc::Cgicc& ,
762 HttpXmlDocument& xmlOut)
764 if(interface_ != NULL)
766 __SUP_COUT__ <<
"Interface is defined! Attempting to get list!" << __E__;
769 __SUP_COUT__ <<
" " << interface_->getList(
"JSON") << __E__;
771 xmlOut.addTextElementToData(
"JSON",
772 interface_->getList(
"JSON"));
776 __SUP_COUT__ <<
"Interface undefined! Failed to get list!" << __E__;
777 xmlOut.addTextElementToData(
"JSON",
"[\"None\"]");
782 void SlowControlsDashboardSupervisor::GetPages(cgicc::Cgicc& ,
783 HttpXmlDocument& xmlOut)
785 std::vector<std::string> pages;
787 listFiles(
"",
true, &pages);
789 std::string returnJSON =
"[";
790 for(
auto it = pages.begin(); it != pages.end(); it++)
792 if(*it !=
"." && *it !=
"..")
793 returnJSON +=
"\"" + *it +
"\", ";
795 if(returnJSON.size() > 2 && returnJSON.compare(
"[") != 0)
797 __SUP_COUT__ <<
"Found pages on server!" << __E__;
798 returnJSON.resize(returnJSON.size() - 2);
804 __SUP_COUT__ <<
"No pages found on server!" << __E__;
805 returnJSON =
"[\"None\"]";
807 __SUP_COUT__ << returnJSON << __E__;
809 xmlOut.addTextElementToData(
"JSON", returnJSON);
813 void SlowControlsDashboardSupervisor::loadPage(cgicc::Cgicc& ,
814 HttpXmlDocument& xmlOut,
816 const WebUsers::RequestUserInfo& )
818 page = StringMacros::decodeURIComponent(page);
822 if(page.find(
"..") != std::string::npos)
824 __SUP_COUT__ << this->getApplicationDescriptor()->getLocalId()
825 <<
"Error! Request using '..': " << page << __E__;
827 else if(page.find(
"~") != std::string::npos)
829 __SUP_COUT__ << this->getApplicationDescriptor()->getLocalId()
830 <<
"Error! Request using '~': " << page << __E__;
832 else if(!(stat(page.c_str(), &buffer) == 0))
834 __SUP_COUT__ << this->getApplicationDescriptor()->getLocalId()
835 <<
"Error! File not found: " << page << __E__;
839 __SUP_COUT__ << page << __E__;
841 if(page.at(0) ==
'/')
843 __SUP_COUT__ <<
"First character is '/'" << __E__;
844 page.erase(page.begin(), page.begin() + 1);
845 __SUP_COUT__ << page << __E__;
848 std::string file = CONTROLS_SUPERVISOR_DATA_PATH;
850 __SUP_COUT__ << this->getApplicationDescriptor()->getLocalId()
851 <<
"Trying to load page: " << page << __E__;
852 __SUP_COUT__ << this->getApplicationDescriptor()->getLocalId()
853 <<
"Trying to load page: " << file << __E__;
857 std::ifstream infile(file);
860 __SUP_COUT__ <<
"Failed reading file: " << file << __E__;
862 xmlOut.addTextElementToData(
"Time",
"[\"Not Found\"]");
863 xmlOut.addTextElementToData(
"Notes",
"[\"Not Found\"]");
864 xmlOut.addTextElementToData(
865 "Page", StringMacros::encodeURIComponent(page));
868 __SUP_COUT__ <<
"Reading file" << __E__;
870 std::string time =
"";
871 std::string notes =
"";
872 std::string controlsPage =
"";
874 for(std::string line; getline(infile, line);)
876 __SUP_COUT__ << line << __E__;
877 if(!line.substr(0, 5).compare(
"Time:"))
879 time = line.substr(6);
881 else if(!line.substr(0, 6).compare(
"Notes:"))
883 notes = line.substr(7);
885 else if(!line.substr(0, 5).compare(
"Page:"))
887 controlsPage = line.substr(6);
890 __SUP_COUT__ <<
"Finished reading file" << __E__;
892 __SUP_COUTV__(notes);
893 __SUP_COUTV__(controlsPage);
895 xmlOut.addTextElementToData(
"Time", time);
896 xmlOut.addTextElementToData(
"Notes", notes);
897 xmlOut.addTextElementToData(
"Page", controlsPage);
900 void SlowControlsDashboardSupervisor::loadPhoebusPage(
902 HttpXmlDocument& xmlOut,
904 const WebUsers::RequestUserInfo& )
906 page = StringMacros::decodeURIComponent(page);
910 if(page.find(
"..") != std::string::npos)
912 __SUP_COUT__ << this->getApplicationDescriptor()->getLocalId()
913 <<
"Error! Request using '..': " << page << __E__;
915 else if(page.find(
"~") != std::string::npos)
917 __SUP_COUT__ << this->getApplicationDescriptor()->getLocalId()
918 <<
"Error! Request using '~': " << page << __E__;
920 else if(!(stat(page.c_str(), &buffer) == 0))
922 __SUP_COUT__ << this->getApplicationDescriptor()->getLocalId()
923 <<
"Error! File not found: " << page << __E__;
927 __SUP_COUT__ << page << __E__;
929 if(page.at(0) ==
'/')
931 __SUP_COUT__ <<
"First character is '/'" << __E__;
932 page.erase(page.begin(), page.begin() + 1);
933 __SUP_COUT__ << page << __E__;
936 std::string file = CONTROLS_SUPERVISOR_DATA_PATH;
938 __SUP_COUT__ << this->getApplicationDescriptor()->getLocalId()
939 <<
"Trying to load page: " << page << __E__;
940 __SUP_COUT__ << this->getApplicationDescriptor()->getLocalId()
941 <<
"Trying to load page: " << file << __E__;
944 __SUP_COUT__ <<
"Reading file" << __E__;
945 std::ifstream infile(file);
948 __SUP_COUT__ <<
"Failed reading file: " << file << __E__;
949 xmlOut.addTextElementToData(
950 "Page", StringMacros::encodeURIComponent(page));
955 for(std::string line; getline(infile, line);)
959 __SUP_COUT__ << xml << __E__;
960 xmlOut.addTextElementToData(
"PHOEBUS", xml);
965 void SlowControlsDashboardSupervisor::SaveControlsPage(
968 const WebUsers::RequestUserInfo& )
970 __SUP_COUT__ <<
"ControlsDashboard wants to create a Controls Page!" << __E__;
972 std::string controlsPageName = CgiDataUtilities::postData(cgiIn,
"Name");
973 std::string pageString = CgiDataUtilities::postData(cgiIn,
"Page");
974 std::string Time = CgiDataUtilities::postData(cgiIn,
"Time");
976 StringMacros::decodeURIComponent(CgiDataUtilities::postData(cgiIn,
"Notes"));
977 std::string isControlsPagePublic = CgiDataUtilities::postData(cgiIn,
"isPublic");
979 __SUP_COUTV__(controlsPageName);
980 __SUP_COUTV__(pageString);
981 __SUP_COUTV__(Notes);
983 __SUP_COUTV__(isControlsPagePublic);
985 if(controlsPageName ==
"")
988 __SUP_COUTV__(CONTROLS_SUPERVISOR_DATA_PATH);
990 std::string fullPath;
991 if(isControlsPagePublic ==
"true")
992 fullPath = (std::string)CONTROLS_SUPERVISOR_DATA_PATH +
"public/";
994 fullPath = (std::string)CONTROLS_SUPERVISOR_DATA_PATH +
"private/";
996 __SUP_COUTV__(fullPath);
998 std::string file = fullPath + controlsPageName;
1000 __SUP_COUTV__(
"Saving Controls Page to: " + file);
1002 std::string extension = file.substr(file.length() - 4, 4);
1003 if(extension !=
".dat")
1005 __SUP_COUT__ <<
"Extension : " << extension << __E__;
1006 file += std::string(
".dat");
1008 __SUP_COUT__ << this->getApplicationDescriptor()->getLocalId()
1009 <<
"Trying to save page: " << controlsPageName << __E__;
1010 __SUP_COUT__ << this->getApplicationDescriptor()->getLocalId()
1011 <<
"Trying to save page as: " << file << __E__;
1015 std::ofstream outputFile;
1016 outputFile.open(file);
1017 outputFile <<
"Time: " << Time <<
"\n";
1018 outputFile <<
"Notes: " << Notes <<
"\n";
1019 outputFile <<
"Page: " << pageString;
1022 __SUP_COUT__ <<
"Finished writing file" << __E__;
1028 void SlowControlsDashboardSupervisor::SavePhoebusControlsPage(
1029 cgicc::Cgicc& cgiIn,
1031 const WebUsers::RequestUserInfo& )
1033 __SUP_COUT__ <<
"ControlsDashboard wants to create a Controls Page!" << __E__;
1035 std::string controlsPageName = CgiDataUtilities::postData(cgiIn,
"Name");
1036 std::string pageString = CgiDataUtilities::postData(cgiIn,
"Page");
1037 std::string isControlsPagePublic = CgiDataUtilities::postData(cgiIn,
"isPublic");
1039 __SUP_COUTV__(controlsPageName);
1040 __SUP_COUTV__(pageString);
1041 __SUP_COUTV__(isControlsPagePublic);
1043 if(controlsPageName ==
"")
1046 __SUP_COUTV__(CONTROLS_SUPERVISOR_DATA_PATH);
1048 std::string fullPath;
1049 if(isControlsPagePublic ==
"true")
1050 fullPath = (std::string)CONTROLS_SUPERVISOR_DATA_PATH +
"public/";
1052 fullPath = (std::string)CONTROLS_SUPERVISOR_DATA_PATH +
"private/";
1054 __SUP_COUTV__(fullPath);
1056 std::string file = fullPath + controlsPageName;
1058 __SUP_COUTV__(
"Saving Controls Page to: " + file);
1060 std::string extension = file.substr(file.length() - 4, 4);
1061 if(extension !=
".bob")
1063 __SUP_COUT__ <<
"Extension : " << extension << __E__;
1064 file += std::string(
".bob");
1066 __SUP_COUT__ << this->getApplicationDescriptor()->getLocalId()
1067 <<
"Trying to save page: " << controlsPageName << __E__;
1068 __SUP_COUT__ << this->getApplicationDescriptor()->getLocalId()
1069 <<
"Trying to save page as: " << file << __E__;
1073 std::ofstream outputFile;
1074 outputFile.open(file);
1075 outputFile << pageString <<
"\n";
1078 __SUP_COUT__ <<
"Finished writing file" << __E__;
1084 void SlowControlsDashboardSupervisor::Subscribe(cgicc::Cgicc& ,
1090 void SlowControlsDashboardSupervisor::Unsubscribe(cgicc::Cgicc& ,
1100 bool SlowControlsDashboardSupervisor::isDir(std::string dir)
1102 struct stat fileInfo;
1103 stat(dir.c_str(), &fileInfo);
1104 if(S_ISDIR(fileInfo.st_mode))
1115 void SlowControlsDashboardSupervisor::listFiles(std::string baseDir,
1117 std::vector<std::string>* pages)
1119 std::string base = CONTROLS_SUPERVISOR_DATA_PATH;
1123 struct dirent* dirp;
1124 if((dp = opendir(base.c_str())) == NULL)
1126 __SUP_COUT__ <<
"[ERROR: " << errno <<
" ] Couldn't open " << base <<
"."
1132 while((dirp = readdir(dp)) != NULL)
1134 if(dirp->d_name != std::string(
".") && dirp->d_name != std::string(
".."))
1136 if(isDir(base + dirp->d_name) ==
true && recursive ==
true)
1139 __SUP_COUT__ <<
"[DIR]\t" << baseDir << dirp->d_name <<
"/" << __E__;
1140 listFiles(baseDir + dirp->d_name +
"/",
true, pages);
1144 pages->push_back(baseDir + dirp->d_name);
1145 __SUP_COUT__ <<
"[FILE]\t" << baseDir << dirp->d_name << __E__;