1 #include "otsdaq-utilities/ECLWriter/ECLSupervisor.h"
2 #include "otsdaq/CgiDataUtilities/CgiDataUtilities.h"
3 #include "otsdaq/ConfigurationInterface/ConfigurationManager.h"
4 #include "otsdaq/Macros/CoutMacros.h"
5 #include "otsdaq/MessageFacility/MessageFacility.h"
6 #include "otsdaq/SOAPUtilities/SOAPCommand.h"
7 #include "otsdaq/SOAPUtilities/SOAPParameters.h"
8 #include "otsdaq/SOAPUtilities/SOAPUtilities.h"
9 #include "otsdaq/TablePlugins/XDAQContextTable.h"
10 #include "otsdaq/XmlUtilities/HttpXmlDocument.h"
12 #include "otsdaq-utilities/ECLWriter/ECLConnection.h"
17 #include <xdaq/NamespaceURI.h>
21 #include "otsdaq/TableCore/TableGroupKey.h"
26 #define __MF_SUBJECT__ "ECL"
32 : CoreSupervisorBase(s)
33 , theConfigurationManager_(
34 new ConfigurationManager)
38 , supervisorContextUID_(
39 theConfigurationManager_->__GET_CONFIG__(XDAQContextTable)
40 ->getContextUID(getApplicationContext()->getContextDescriptor()->getURL()))
41 , supervisorApplicationUID_(
42 theConfigurationManager_->__GET_CONFIG__(XDAQContextTable)
44 getApplicationContext()->getContextDescriptor()->getURL(),
45 getApplicationDescriptor()->getLocalId()))
46 , supervisorConfigurationPath_("/" + supervisorContextUID_ +
47 "/LinkToApplicationTable/" +
48 supervisorApplicationUID_ + "/LinkToSupervisorTable")
51 __COUT__ << __PRETTY_FUNCTION__ << std::endl;
52 __COUT__ << __PRETTY_FUNCTION__ << std::endl;
53 __COUT__ << __PRETTY_FUNCTION__ << std::endl;
54 __COUT__ << __PRETTY_FUNCTION__ << std::endl;
55 __COUT__ << __PRETTY_FUNCTION__ << std::endl;
56 __COUT__ << __PRETTY_FUNCTION__ << std::endl;
57 __COUT__ << __PRETTY_FUNCTION__ << std::endl;
58 __COUT__ << __PRETTY_FUNCTION__ << std::endl;
59 __COUT__ << __PRETTY_FUNCTION__ << std::endl;
60 __COUT__ << __PRETTY_FUNCTION__ << std::endl;
62 __COUT__ << __PRETTY_FUNCTION__ <<
"done data manager" << std::endl;
68 ECLSupervisor::~ECLSupervisor(
void) { destroy(); }
70 void ECLSupervisor::init(
void)
77 void ECLSupervisor::destroy(
void)
80 delete theConfigurationManager_;
84 void ECLSupervisor::defaultPage(xgi::Input* , xgi::Output* out)
89 *out <<
"<!DOCTYPE HTML><html lang='en'><frameset col='100%' row='100%'><frame "
90 "src='/WebPath/html/ECL.html?urn="
91 << this->getApplicationDescriptor()->getLocalId() <<
"'></frameset></html>";
95 void ECLSupervisor::transitionConfiguring(toolbox::Event::Reference )
104 std::pair<std::string , TableGroupKey> theGroup(
105 SOAPUtilities::translate(theStateMachine_.getCurrentMessage())
107 .getValue(
"ConfigurationTableGroupName"),
108 TableGroupKey(SOAPUtilities::translate(theStateMachine_.getCurrentMessage())
110 .getValue(
"ConfigurationTableGroupKey")));
112 __COUT__ <<
"Configuration table group name: " << theGroup.first
113 <<
" key: " << theGroup.second << std::endl;
115 theConfigurationManager_->loadTableGroup(theGroup.first, theGroup.second,
true);
117 ConfigurationTree configLinkNode =
118 theConfigurationManager_->getSupervisorTableNode(supervisorContextUID_,
119 supervisorApplicationUID_);
121 ECLUser = configLinkNode.getNode(
"ECLUserName").getValue<std::string>();
122 ECLHost = configLinkNode.getNode(
"ECLInstanceURL").getValue<std::string>();
123 ECLPwd = configLinkNode.getNode(
"ECLPassword").getValue<std::string>();
124 ExperimentName = configLinkNode.getNode(
"ExperimentName").getValue<std::string>();
135 void ECLSupervisor::transitionStarting(toolbox::Event::Reference )
140 __COUT_INFO__ <<
"ECLSupervisor sending Start Run log message to ECL"
142 run = SOAPUtilities::translate(theStateMachine_.getCurrentMessage())
144 .getValue(
"RunNumber");
145 run_start = std::chrono::steady_clock::now();
147 Write(WriteState::kStart);
151 __COUT_INFO__ <<
"ERROR! Couldn't Start the ECLSupervisor" << std::endl;
156 void ECLSupervisor::transitionStopping(toolbox::Event::Reference )
161 __COUT_INFO__ <<
"ECLSupervisor sending Stop Run log message to ECL" << std::endl;
162 Write(WriteState::kStop);
166 __COUT_INFO__ <<
"ERROR! Couldn't Stop the ECLSupervisor" << std::endl;
171 void ECLSupervisor::transitionPausing(toolbox::Event::Reference )
176 __COUT_INFO__ <<
"ECLSupervisor sending Pause Run log message to ECL"
178 Write(WriteState::kPause);
179 duration_ms += std::chrono::duration_cast<std::chrono::milliseconds>(
180 std::chrono::steady_clock::now() - run_start)
185 __COUT_INFO__ <<
"ERROR! Couldn't Pause the ECLSupervisor" << std::endl;
190 void ECLSupervisor::transitionResuming(toolbox::Event::Reference )
195 __COUT_INFO__ <<
"ECLSupervisor sending Resume Run log message to ECL"
197 run_start = std::chrono::steady_clock::now();
198 Write(WriteState::kResume);
202 __COUT_INFO__ <<
"ERROR! Couldn't Resume the ECLSupervisor" << std::endl;
206 void ECLSupervisor::enteringError(toolbox::Event::Reference )
210 __COUT_INFO__ <<
"ECLSupervisor sending Error log message to ECL" << std::endl;
211 Write(WriteState::kError);
215 __COUT_INFO__ <<
"ERROR! Couldn't Error the ECLSupervisor" << std::endl;
223 xoap::MessageReference ECLSupervisor::MakeSystemLogbookEntry(xoap::MessageReference msg)
226 SOAPParameters parameters(
"EntryText");
229 SOAPUtilities::receive(msg, parameters);
230 std::string EntryText = parameters.getValue(
"EntryText");
232 __COUT__ <<
"Received External Supervisor System Entry " << EntryText << std::endl;
234 std::string retStr =
"Success";
237 eclEntry.author(ECLUser);
238 eclEntry.category(
"Facility/DAQ");
241 Form_t::field_sequence fields;
243 theRemoteWebUsers_.getActiveUserList();
245 form.name(
"OTSDAQ System Logbook Entry");
247 field =
Field_t(EscapeECLString(ExperimentName),
"Experiment");
248 fields.push_back(field);
250 field =
Field_t(EscapeECLString(run),
"RunNumber");
251 fields.push_back(field);
253 field =
Field_t(EscapeECLString(users),
"ActiveUsers");
254 fields.push_back(field);
256 field =
Field_t(EscapeECLString(EntryText),
"Entry");
257 fields.push_back(field);
260 if(!eclConn.Post(eclEntry))
266 SOAPParameters retParameters(
"Status", retStr);
268 return SOAPUtilities::makeSOAPMessageReference(
"LogbookEntryStatusResponse",
272 int ECLSupervisor::Write(WriteState state)
275 eclEntry.author(ECLUser);
276 eclEntry.category(
"Facility/DAQ");
279 Form_t::field_sequence fields;
281 theRemoteWebUsers_.getActiveUserList();
285 case WriteState::kStart:
286 form.name(
"OTSDAQ Start Run");
288 case WriteState::kStop:
289 form.name(
"OTSDAQ Stop Run");
291 case WriteState::kResume:
292 form.name(
"OTSDAQ Resume Run");
294 case WriteState::kPause:
295 form.name(
"OTSDAQ Pause Run");
297 case WriteState::kError:
298 form.name(
"OTSDAQ Run Error");
302 field =
Field_t(EscapeECLString(ExperimentName),
"Experiment");
303 fields.push_back(field);
305 field =
Field_t(EscapeECLString(run),
"RunNumber");
306 fields.push_back(field);
308 field =
Field_t(EscapeECLString(users),
"ActiveUsers");
309 fields.push_back(field);
311 if(state != WriteState::kStart && state != WriteState::kResume)
313 int dur = std::chrono::duration_cast<std::chrono::milliseconds>(
314 std::chrono::steady_clock::now() - run_start)
317 int dur_s = dur / 1000;
319 int dur_m = dur_s / 60;
321 int dur_h = dur_m / 60;
324 std::ostringstream dur_ss;
325 dur_ss << std::setw(2) << std::setfill(
'0') << dur_h <<
":" << std::setw(2)
326 << std::setfill(
'0') << dur_m <<
":" << std::setw(2) << std::setfill(
'0')
327 << dur_s <<
"." << dur;
329 field =
Field_t(EscapeECLString(dur_ss.str()),
"Duration");
330 fields.push_back(field);
338 if(!eclConn.Post(eclEntry))
348 std::string ECLSupervisor::EscapeECLString(std::string input)
350 std::string output = input;
351 size_t pos = output.find(
'&');
352 while(pos != std::string::npos)
354 output = output.replace(pos, 1,
"&");
355 pos = output.find(
'&', pos + 2);
358 pos = output.find(
'"');
359 while(pos != std::string::npos)
361 output = output.replace(pos, 1,
""");
362 pos = output.find(
'"', pos + 1);
365 pos = output.find(
'\'');
366 while(pos != std::string::npos)
368 output = output.replace(pos, 1,
"'");
369 pos = output.find(
'\'', pos + 1);
372 pos = output.find(
'<');
373 while(pos != std::string::npos)
375 output = output.replace(pos, 1,
"<");
376 pos = output.find(
'<', pos + 1);
379 pos = output.find(
'>');
380 while(pos != std::string::npos)
382 output = output.replace(pos, 1,
">");
383 pos = output.find(
'>', pos + 1);