1 #include "otsdaq-utilities/ECLWriter/ECLSupervisor.h"
2 #include "otsdaq-core/MessageFacility/MessageFacility.h"
3 #include "otsdaq-core/Macros/CoutMacros.h"
4 #include "otsdaq-core/CgiDataUtilities/CgiDataUtilities.h"
5 #include "otsdaq-core/XmlUtilities/HttpXmlDocument.h"
6 #include "otsdaq-core/SOAPUtilities/SOAPUtilities.h"
7 #include "otsdaq-core/SOAPUtilities/SOAPParameters.h"
8 #include "otsdaq-core/SOAPUtilities/SOAPCommand.h"
9 #include "otsdaq-core/ConfigurationInterface/ConfigurationManager.h"
10 #include "otsdaq-core/ConfigurationPluginDataFormats/XDAQContextConfiguration.h"
11 #include "otsdaq-core/ConfigurationDataFormats/ConfigurationGroupKey.h"
13 #include "otsdaq-utilities/ECLWriter/ECLConnection.h"
18 #include <xdaq/NamespaceURI.h>
26 #define __MF_SUBJECT__ "ECL"
32 : CoreSupervisorBase(s)
33 , theConfigurationManager_(new ConfigurationManager)
34 , supervisorContextUID_(theConfigurationManager_->__GET_CONFIG__(XDAQContextConfiguration)->getContextUID(getApplicationContext()->getContextDescriptor()->getURL()))
35 , supervisorApplicationUID_(theConfigurationManager_->__GET_CONFIG__(XDAQContextConfiguration)->getApplicationUID
37 getApplicationContext()->getContextDescriptor()->getURL(),
38 getApplicationDescriptor()->getLocalId()
40 , supervisorConfigurationPath_("/" + supervisorContextUID_ + "/LinkToApplicationTable/" + supervisorApplicationUID_ + "/LinkToSupervisorTable")
42 INIT_MF(
"ECLSupervisor");
43 __COUT__ << __PRETTY_FUNCTION__ << std::endl;
44 __COUT__ << __PRETTY_FUNCTION__ << std::endl;
45 __COUT__ << __PRETTY_FUNCTION__ << std::endl;
46 __COUT__ << __PRETTY_FUNCTION__ << std::endl;
47 __COUT__ << __PRETTY_FUNCTION__ << std::endl;
48 __COUT__ << __PRETTY_FUNCTION__ << std::endl;
49 __COUT__ << __PRETTY_FUNCTION__ << std::endl;
50 __COUT__ << __PRETTY_FUNCTION__ << std::endl;
51 __COUT__ << __PRETTY_FUNCTION__ << std::endl;
52 __COUT__ << __PRETTY_FUNCTION__ << std::endl;
55 __COUT__ << __PRETTY_FUNCTION__ <<
"done data manager" << std::endl;
61 ECLSupervisor::~ECLSupervisor(
void)
66 void ECLSupervisor::init(
void)
73 void ECLSupervisor::destroy(
void)
76 delete theConfigurationManager_;
80 void ECLSupervisor::defaultPage(xgi::Input * in, xgi::Output * out)
85 *out <<
"<!DOCTYPE HTML><html lang='en'><frameset col='100%' row='100%'><frame src='/WebPath/html/ECL.html?urn=" <<
86 this->getApplicationDescriptor()->getLocalId() <<
"'></frameset></html>";
91 void ECLSupervisor::transitionConfiguring(toolbox::Event::Reference e)
100 std::pair<std::string , ConfigurationGroupKey> theGroup(
101 SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).
102 getParameters().getValue(
"ConfigurationGroupName"),
103 ConfigurationGroupKey(SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).
104 getParameters().getValue(
"ConfigurationGroupKey")));
106 __COUT__ <<
"Configuration group name: " << theGroup.first <<
" key: " <<
107 theGroup.second << std::endl;
109 theConfigurationManager_->loadConfigurationGroup(
111 theGroup.second,
true);
114 ConfigurationTree configLinkNode = theConfigurationManager_->getSupervisorConfigurationNode(
115 supervisorContextUID_, supervisorApplicationUID_);
117 ECLUser = configLinkNode.getNode(
"ECLUserName").getValue<std::string>();
118 ECLHost = configLinkNode.getNode(
"ECLInstanceURL").getValue<std::string>();
119 ECLPwd = configLinkNode.getNode(
"ECLPassword").getValue<std::string>();
120 ExperimentName = configLinkNode.getNode(
"ExperimentName").getValue<std::string>();
130 void ECLSupervisor::transitionStarting(toolbox::Event::Reference e)
135 __COUT_INFO__ <<
"ECLSupervisor sending Start Run log message to ECL" << std::endl;
136 run = SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).getParameters().getValue(
"RunNumber");
137 run_start = std::chrono::steady_clock::now();
139 Write(WriteState::kStart);
143 __COUT_INFO__ <<
"ERROR! Couldn't Start the ECLSupervisor" << std::endl;
148 void ECLSupervisor::transitionStopping(toolbox::Event::Reference e)
153 __COUT_INFO__ <<
"ECLSupervisor sending Stop Run log message to ECL" << std::endl;
154 Write(WriteState::kStop);
158 __COUT_INFO__ <<
"ERROR! Couldn't Stop the ECLSupervisor" << std::endl;
164 void ECLSupervisor::transitionPausing(toolbox::Event::Reference e)
169 __COUT_INFO__ <<
"ECLSupervisor sending Pause Run log message to ECL" << std::endl;
170 Write(WriteState::kPause);
171 duration_ms += std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - run_start).count();
175 __COUT_INFO__ <<
"ERROR! Couldn't Pause the ECLSupervisor" << std::endl;
180 void ECLSupervisor::transitionResuming(toolbox::Event::Reference e)
185 __COUT_INFO__ <<
"ECLSupervisor sending Resume Run log message to ECL" << std::endl;
186 run_start = std::chrono::steady_clock::now();
187 Write(WriteState::kResume);
191 __COUT_INFO__ <<
"ERROR! Couldn't Resume the ECLSupervisor" << std::endl;
195 void ECLSupervisor::enteringError(toolbox::Event::Reference e)
199 __COUT_INFO__ <<
"ECLSupervisor sending Error log message to ECL" << std::endl;
200 Write(WriteState::kError);
204 __COUT_INFO__ <<
"ERROR! Couldn't Error the ECLSupervisor" << std::endl;
212 xoap::MessageReference ECLSupervisor::MakeSystemLogbookEntry(xoap::MessageReference msg)
215 SOAPParameters parameters(
"EntryText");
218 SOAPUtilities::receive(msg, parameters);
219 std::string EntryText = parameters.getValue(
"EntryText");
221 __COUT__ <<
"Received External Supervisor System Entry " << EntryText << std::endl;
223 std::string retStr =
"Success";
226 eclEntry.author(ECLUser);
227 eclEntry.category(
"Facility/DAQ");
230 Form_t::field_sequence fields;
231 std::string users = theRemoteWebUsers_.getActiveUserList(allSupervisorInfo_.getGatewayDescriptor());
233 form.name(
"OTSDAQ System Logbook Entry");
235 field =
Field_t(EscapeECLString(ExperimentName),
"Experiment");
236 fields.push_back(field);
238 field =
Field_t(EscapeECLString(run),
"RunNumber");
239 fields.push_back(field);
241 field =
Field_t(EscapeECLString(users),
"ActiveUsers");
242 fields.push_back(field);
244 field =
Field_t(EscapeECLString(EntryText),
"Entry");
245 fields.push_back(field);
248 if (!eclConn.Post(eclEntry)) {
253 SOAPParameters retParameters(
"Status", retStr);
255 return SOAPUtilities::makeSOAPMessageReference(
"LogbookEntryStatusResponse", retParameters);
259 int ECLSupervisor::Write(WriteState state)
262 eclEntry.author(ECLUser);
263 eclEntry.category(
"Facility/DAQ");
266 Form_t::field_sequence fields;
267 std::string users = theRemoteWebUsers_.getActiveUserList(allSupervisorInfo_.getGatewayDescriptor());
271 case WriteState::kStart: form.name(
"OTSDAQ Start Run");
break;
272 case WriteState::kStop: form.name(
"OTSDAQ Stop Run");
break;
273 case WriteState::kResume: form.name(
"OTSDAQ Resume Run");
break;
274 case WriteState::kPause: form.name(
"OTSDAQ Pause Run");
break;
275 case WriteState::kError: form.name(
"OTSDAQ Run Error");
break;
278 field =
Field_t(EscapeECLString(ExperimentName),
"Experiment");
279 fields.push_back(field);
281 field =
Field_t(EscapeECLString(run),
"RunNumber");
282 fields.push_back(field);
284 field =
Field_t(EscapeECLString(users),
"ActiveUsers");
285 fields.push_back(field);
288 if(state != WriteState::kStart && state != WriteState::kResume) {
289 int dur = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - run_start).count() + duration_ms;
290 int dur_s = dur / 1000;
292 int dur_m = dur_s / 60;
294 int dur_h = dur_m / 60;
297 std::ostringstream dur_ss;
298 dur_ss << std::setw(2) << std::setfill(
'0') << dur_h
299 <<
":" << std::setw(2) << std::setfill(
'0') << dur_m
300 <<
":" << std::setw(2) << std::setfill(
'0') << dur_s
303 field =
Field_t(EscapeECLString(dur_ss.str()),
"Duration");
304 fields.push_back(field);
312 if (!eclConn.Post(eclEntry)) {
321 std::string ECLSupervisor::EscapeECLString(std::string input)
323 std::string output = input;
324 size_t pos = output.find(
'&');
325 while (pos != std::string::npos)
327 output = output.replace(pos, 1,
"&");
328 pos = output.find(
'&', pos + 2);
331 pos = output.find(
'"');
332 while (pos != std::string::npos)
334 output = output.replace(pos, 1,
""");
335 pos = output.find(
'"', pos + 1);
338 pos = output.find(
'\'');
339 while (pos != std::string::npos)
341 output = output.replace(pos, 1,
"'");
342 pos = output.find(
'\'', pos + 1);
345 pos = output.find(
'<');
346 while (pos != std::string::npos)
348 output = output.replace(pos, 1,
"<");
349 pos = output.find(
'<', pos + 1);
352 pos = output.find(
'>');
353 while (pos != std::string::npos)
355 output = output.replace(pos, 1,
">");
356 pos = output.find(
'>', pos + 1);