1 #include "otsdaq-utilities/ControlsDashboard/ControlsDashboardSupervisor.h"
28 #include "otsdaq-core/ControlsCore/ControlsVInterface.h"
29 #include "otsdaq-core/PluginMakers/MakeControls.h"
36 #define PAGES_DIRECTORY std::string(getenv("SERVICE_DATA_PATH")) + "/ControlsDashboardData/pages/";
43 : CoreSupervisorBase(stub)
45 INIT_MF(
"ControlsDashboardSupervisor");
52 ControlsDashboardSupervisor::~ControlsDashboardSupervisor(
void)
60 void ControlsDashboardSupervisor::setSupervisorPropertyDefaults()
62 CorePropertySupervisorBase::setSupervisorProperty(CorePropertySupervisorBase::SUPERVISOR_PROPERTIES.CheckUserLockRequestTypes,
69 void ControlsDashboardSupervisor::forceSupervisorPropertyValues()
71 CorePropertySupervisorBase::setSupervisorProperty(CorePropertySupervisorBase::SUPERVISOR_PROPERTIES.AutomatedRequestTypes,
76 void ControlsDashboardSupervisor::request(
const std::string& requestType, cgicc::Cgicc& cgiIn,
77 HttpXmlDocument& xmlOut,
const WebUsers::RequestUserInfo& userInfo)
133 __COUT__ << std::endl;
135 if(requestType ==
"poll")
137 std::string uid = CgiDataUtilities::getOrPostData(cgiIn,
"uid");
138 Poll(cgiIn, xmlOut, uid);
140 else if(requestType ==
"generateUID")
142 std::string pvList = CgiDataUtilities::getOrPostData(cgiIn,
"PVList");
143 GenerateUID(cgiIn, xmlOut, pvList);
145 else if(requestType ==
"GetPVSettings")
147 std::string pvList = CgiDataUtilities::getOrPostData(cgiIn,
"PVList");
148 GetPVSettings(cgiIn, xmlOut, pvList);
149 xmlOut.addTextElementToData(
"id", CgiDataUtilities::getData(cgiIn,
"id"));
151 else if(requestType ==
"getList")
153 GetList(cgiIn, xmlOut);
155 else if(requestType ==
"getPages")
157 GetPages(cgiIn, xmlOut);
159 else if(requestType ==
"loadPage")
161 std::string page = CgiDataUtilities::getData(cgiIn,
"Page");
162 __COUT__ << this->getApplicationDescriptor()->getLocalId() <<
" " << page << std::endl;
164 loadPage(cgiIn, xmlOut, page);
166 __COUT__ << std::endl;
174 void ControlsDashboardSupervisor::init(
void)
179 __COUT__ << std::endl;
180 std::string t =
"test";
181 std::string nodeName = theConfigurationManager_->__GET_CONFIG__(XDAQContextConfiguration)->getConfigurationName();
182 __COUT__ << nodeName << std::endl;
183 ConfigurationTree node = theConfigurationManager_->getNode(nodeName);
184 __COUT__ << node << std::endl;
186 interface_ = makeControls(
187 "ControlsOtsInterface"
191 __COUT__ << std::endl;
197 void ControlsDashboardSupervisor::destroy(
void)
204 void ControlsDashboardSupervisor::Poll(cgicc::Cgicc& cgiIn, HttpXmlDocument& xmlOut, std::string UID )
207 __COUT__ << this->getApplicationDescriptor()->getLocalId() <<
" " <<
"Polling on UID:" << UID << std::endl;
209 std::map<int, std::set<std::string>>::iterator mapReference;
211 if( UID !=
"" && (mapReference = pvDependencyLookupMap_.find(std::stoi(UID))) != pvDependencyLookupMap_.end())
213 std::string JSONMessage =
"{ ";
215 for(
auto pv : mapReference->second)
217 __COUT__ << pv << std::endl;
221 std::array<std::string, 4> pvInformation = interface_->getCurrentValue(pv);
223 __COUT__ << pv <<
": " << pvInformation[1] <<
" : " << pvInformation[3] << std::endl;
226 if(pvInformation[0] !=
"NO_CHANGE")
229 JSONMessage +=
"\"" + pv +
"\": {";
237 JSONMessage +=
"\"Timestamp\" : \"" + pvInformation[0] +
"\",";
238 JSONMessage +=
"\"Value\" : \"" + pvInformation[1] +
"\",";
239 JSONMessage +=
"\"Status\" : \"" + pvInformation[2] +
"\",";
240 JSONMessage +=
"\"Severity\" : \"" + pvInformation[3] +
"\"},";
245 __COUT__ <<
"No change in value since last poll: " << pv << std::endl;
249 if(pvInformation[3] ==
"INVALID")
251 interface_->subscribe(pv);
261 JSONMessage = JSONMessage.substr(0, JSONMessage.length() - 1);
263 __COUT__ << JSONMessage << std::endl;
264 xmlOut.addTextElementToData(
"JSON", JSONMessage);
289 xmlOut.addTextElementToData(
"JSON",
"{ \"message\": \"NOT_FOUND\"}");
293 void ControlsDashboardSupervisor::GetPVSettings(cgicc::Cgicc& cgiIn, HttpXmlDocument& xmlOut, std::string pvList )
295 __COUT__ << this->getApplicationDescriptor()->getLocalId() <<
" " <<
"Getting settings for " << pvList << std::endl;
297 std::string JSONMessage =
"{ ";
302 size_t lastIndex = pvList.find_last_of(
",");
303 std::cout <<
"**********************" << pvList.size() << std::endl;
304 if(pvList.size() > 0)
307 while((nextPos = pvList.find(
",", pos)) != std::string::npos)
309 pv = pvList.substr(pos, nextPos-pos);
312 __COUT__ << pv << std::endl;
314 std::array<std::string, 9> pvSettings = interface_->getSettings(pv);
317 JSONMessage +=
"\"" + pv +
"\": {";
318 JSONMessage +=
"\"Units \": \"" + pvSettings[0] +
"\",";
319 JSONMessage +=
"\"Upper_Display_Limit\": \"" + pvSettings[1] +
"\",";
320 JSONMessage +=
"\"Lower_Display_Limit\": \"" + pvSettings[2] +
"\",";
321 JSONMessage +=
"\"Upper_Alarm_Limit \": \"" + pvSettings[3] +
"\",";
322 JSONMessage +=
"\"Upper_Warning_Limit\": \"" + pvSettings[4] +
"\",";
323 JSONMessage +=
"\"Lower_Warning_Limit\": \"" + pvSettings[5] +
"\",";
324 JSONMessage +=
"\"Lower_Alarm_Limit \": \"" + pvSettings[6] +
"\",";
325 JSONMessage +=
"\"Upper_Control_Limit\": \"" + pvSettings[7] +
"\",";
326 JSONMessage +=
"\"Lower_Control_Limit\": \"" + pvSettings[8] +
"\"},";
331 JSONMessage = JSONMessage.substr(0, JSONMessage.length() - 1);
334 __COUT__ << JSONMessage << std::endl;
335 xmlOut.addTextElementToData(
"JSON", JSONMessage);
340 xmlOut.addTextElementToData(
"JSON",
"{ \"message\": \"GetPVSettings\"}");
348 void ControlsDashboardSupervisor::GenerateUID(cgicc::Cgicc& cgiIn, HttpXmlDocument& xmlOut, std::string pvlist )
351 __COUT__ << this->getApplicationDescriptor()->getLocalId() <<
" " <<
"Generating UID" << std::endl;
353 std::set<std::string> pvDependencies;
358 size_t lastIndex = pvlist.find_last_of(
",");
360 if(pvlist.size() > 0)
363 __COUT__ << pvlist << std::endl;
365 while((nextPos = pvlist.find(
",", pos)) != std::string::npos)
367 pv = pvlist.substr(pos, nextPos-pos);
369 pvDependencies.insert(pv);
373 pvDependencyLookupMap_.insert(std::pair<
int, std::set<std::string>> (++UID_, pvDependencies) );
375 uid = (std::string(
"{ \"message\": \"") + std::to_string(UID_) +
"\"}");
378 __COUT__ << this->getApplicationDescriptor()->getLocalId() <<
" PVList invalid: " << pvlist << std::endl;
379 uid =
"{ \"message\": \"-1\"}";
383 __COUT__ << this->getApplicationDescriptor()->getLocalId() <<
" NEW UID: " << UID_ << std::endl;
385 xmlOut.addTextElementToData(
"JSON", uid);
390 void ControlsDashboardSupervisor::GetList(cgicc::Cgicc& cgiIn, HttpXmlDocument& xmlOut)
393 __COUT__ << this->getApplicationDescriptor()->getLocalId() << std::endl;
394 std::cout <<
" " << interface_->getList(
"JSON") << std::endl;
396 xmlOut.addTextElementToData(
"JSON", interface_->getList(
"JSON"));
400 void ControlsDashboardSupervisor::GetPages(cgicc::Cgicc& cgiIn, HttpXmlDocument& xmlOut)
423 std::vector<std::string> pages;
425 listFiles(
"",
true, &pages);
427 std::string returnJSON =
"[";
428 for(
auto it = pages.begin(); it != pages.end(); it++)
430 if(*it !=
"." && *it !=
"..")
431 returnJSON +=
"\"" + *it +
"\", ";
433 if(returnJSON.size() > 2 && returnJSON.compare(
"[") != 0)
435 __COUT__ <<
"Found pages on server!" << std::endl;
436 returnJSON.resize(returnJSON.size()-2);
442 __COUT__ <<
"No pages found on server!" << std::endl;
443 returnJSON =
"[\"None\"]";
445 std::cout << returnJSON << std::endl;
447 xmlOut.addTextElementToData(
"JSON", returnJSON);
451 void ControlsDashboardSupervisor::loadPage(cgicc::Cgicc& cgiIn, HttpXmlDocument& xmlOut, std::string page )
455 if(page.find(
"..") != std::string::npos)
457 __COUT__ << this->getApplicationDescriptor()->getLocalId() <<
"Error! Request using '..': " << page << std::endl;
459 else if (page.find(
"~") != std::string::npos)
461 __COUT__ << this->getApplicationDescriptor()->getLocalId() <<
"Error! Request using '~': " << page << std::endl;
463 else if(!(stat(page.c_str(), &buffer) == 0))
465 __COUT__ << this->getApplicationDescriptor()->getLocalId() <<
"Error! File not found: " << page << std::endl;
468 std::string file = PAGES_DIRECTORY
470 __COUT__ << this->getApplicationDescriptor()->getLocalId() <<
"Trying to load page: " << page << std::endl;
471 __COUT__ << this->getApplicationDescriptor()->getLocalId() <<
"Trying to load page: " << file << std::endl;
475 std::ifstream infile(file);
476 std::cout <<
"Reading file" << std::endl;
477 std::string JSONpage =
"";
478 for(std::string line; getline(infile, line);)
480 std::cout << line << std::endl;
483 std::cout <<
"Finished reading file" << std::endl;
485 xmlOut.addTextElementToData(
"JSON", JSONpage);
489 void ControlsDashboardSupervisor::Subscribe(cgicc::Cgicc& cgiIn, HttpXmlDocument& xmlOut)
495 void ControlsDashboardSupervisor::Unsubscribe(cgicc::Cgicc& cgiIn, HttpXmlDocument& xmlOut)
504 bool ControlsDashboardSupervisor::isDir(std::string dir)
506 struct stat fileInfo;
507 stat(dir.c_str(), &fileInfo);
508 if (S_ISDIR(fileInfo.st_mode))
518 void ControlsDashboardSupervisor::listFiles(std::string baseDir,
bool recursive, std::vector<std::string> * pages )
520 std::string base = PAGES_DIRECTORY;
525 if ((dp = opendir(base.c_str())) == NULL) {
526 std::cout <<
"[ERROR: " << errno <<
" ] Couldn't open " << base <<
"." << std::endl;
531 while ((dirp = readdir(dp)) != NULL) {
532 if (dirp->d_name != std::string(
".") && dirp->d_name != std::string(
".."))
534 if (isDir(base + dirp->d_name) ==
true && recursive ==
true)
537 std::cout <<
"[DIR]\t" << baseDir << dirp->d_name <<
"/" << std::endl;
538 listFiles(baseDir + dirp->d_name +
"/",
true, pages);
542 pages->push_back(baseDir + dirp->d_name);
543 std::cout <<
"[FILE]\t" << baseDir << dirp->d_name << std::endl;