otsdaq_utilities  v2_02_00
ECLSupervisor.cc
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"
12 
13 #include "otsdaq-utilities/ECLWriter/ECLConnection.h"
14 
15 #include <dirent.h> /*DIR and dirent*/
16 #include <sys/stat.h> /*mkdir*/
17 
18 #include <xdaq/NamespaceURI.h>
19 
20 #include <iostream>
21 #include <iomanip>
22 
23 using namespace ots;
24 
25 #undef __MF_SUBJECT__
26 #define __MF_SUBJECT__ "ECL"
27 
28 XDAQ_INSTANTIATOR_IMPL(ECLSupervisor)
29 
30 //========================================================================================================================
31 ECLSupervisor::ECLSupervisor(xdaq::ApplicationStub * s)
32 : CoreSupervisorBase(s)
33  , theConfigurationManager_(new ConfigurationManager)//(Singleton<ConfigurationManager>::getInstance()) //I always load the full config but if I want to load a partial configuration (new ConfigurationManager)
34  , supervisorContextUID_(theConfigurationManager_->__GET_CONFIG__(XDAQContextConfiguration)->getContextUID(getApplicationContext()->getContextDescriptor()->getURL()))
35  , supervisorApplicationUID_(theConfigurationManager_->__GET_CONFIG__(XDAQContextConfiguration)->getApplicationUID
36  (
37  getApplicationContext()->getContextDescriptor()->getURL(),
38  getApplicationDescriptor()->getLocalId()
39  ))
40  , supervisorConfigurationPath_("/" + supervisorContextUID_ + "/LinkToApplicationTable/" + supervisorApplicationUID_ + "/LinkToSupervisorTable")
41 {
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;
53 
54 
55  __COUT__ << __PRETTY_FUNCTION__ << "done data manager" << std::endl;
56 
57  init();
58 }
59 
60 //========================================================================================================================
61 ECLSupervisor::~ECLSupervisor(void)
62 {
63  destroy();
64 }
65 //========================================================================================================================
66 void ECLSupervisor::init(void)
67 {
68  //called by constructor
69  //allSupervisorInfo_.init(getApplicationContext());
70 }
71 
72 //========================================================================================================================
73 void ECLSupervisor::destroy(void)
74 {
75  //called by destructor
76  delete theConfigurationManager_;
77 }
78 
79 //========================================================================================================================
80 void ECLSupervisor::defaultPage(xgi::Input * in, xgi::Output * out)
81 
82 {
83  //__COUT__ << this->getApplicationContext()->getURL() << __E__;
84 
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>";
87 
88 }
89 
90 //========================================================================================================================
91 void ECLSupervisor::transitionConfiguring(toolbox::Event::Reference e)
92 
93 {
94 
95  //try
96  {
97  //theConfigurationGroupKey_ = theConfigurationManager_->makeTheConfigurationGroupKey(atoi(SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).getParameters().getValue("ConfigurationGroupKey").c_str()));
98  //theConfigurationManager_->activateConfigurationGroupKey(theConfigurationGroupKey_,0);
99 
100  std::pair<std::string /*group name*/, ConfigurationGroupKey> theGroup(
101  SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).
102  getParameters().getValue("ConfigurationGroupName"),
103  ConfigurationGroupKey(SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).
104  getParameters().getValue("ConfigurationGroupKey")));
105 
106  __COUT__ << "Configuration group name: " << theGroup.first << " key: " <<
107  theGroup.second << std::endl;
108 
109  theConfigurationManager_->loadConfigurationGroup(
110  theGroup.first,
111  theGroup.second, true);
112 
113 
114  ConfigurationTree configLinkNode = theConfigurationManager_->getSupervisorConfigurationNode(
115  supervisorContextUID_, supervisorApplicationUID_);
116 
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>();
121 
122  }
123  //catch(...)
124  //{
125  // {__SS__;__THROW__(ss.str()+"Error configuring the visual supervisor most likely a plugin name is wrong or your configuration table is outdated and doesn't match the new plugin definition!");}
126  //}
127 }
128 
129 //========================================================================================================================
130 void ECLSupervisor::transitionStarting(toolbox::Event::Reference e)
131 
132 {
133  try
134  {
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();
138  duration_ms = 0;
139  Write(WriteState::kStart);
140  }
141  catch (...)
142  {
143  __COUT_INFO__ << "ERROR! Couldn't Start the ECLSupervisor" << std::endl;
144  }
145 }
146 
147 //========================================================================================================================
148 void ECLSupervisor::transitionStopping(toolbox::Event::Reference e)
149 
150 {
151  try
152  {
153  __COUT_INFO__ << "ECLSupervisor sending Stop Run log message to ECL" << std::endl;
154  Write(WriteState::kStop);
155  }
156  catch (...)
157  {
158  __COUT_INFO__ << "ERROR! Couldn't Stop the ECLSupervisor" << std::endl;
159  }
160 }
161 
162 
163 //========================================================================================================================
164 void ECLSupervisor::transitionPausing(toolbox::Event::Reference e)
165 
166 {
167  try
168  {
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();
172  }
173  catch (...)
174  {
175  __COUT_INFO__ << "ERROR! Couldn't Pause the ECLSupervisor" << std::endl;
176  }
177 }
178 
179 //========================================================================================================================
180 void ECLSupervisor::transitionResuming(toolbox::Event::Reference e)
181 
182 {
183  try
184  {
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);
188  }
189  catch (...)
190  {
191  __COUT_INFO__ << "ERROR! Couldn't Resume the ECLSupervisor" << std::endl;
192  }
193 }
194 
195 void ECLSupervisor::enteringError(toolbox::Event::Reference e)
196 {
197  try
198  {
199  __COUT_INFO__ << "ECLSupervisor sending Error log message to ECL" << std::endl;
200  Write(WriteState::kError);
201  }
202  catch (...)
203  {
204  __COUT_INFO__ << "ERROR! Couldn't Error the ECLSupervisor" << std::endl;
205  }
206 }
207 
208 //========================================================================================================================
209 //xoap::MakeSystemLogbookEntry
210 // make a system logbook entry into active experiment's logbook from Supervisor only
211 // TODO: (how to enforce?)
212 xoap::MessageReference ECLSupervisor::MakeSystemLogbookEntry(xoap::MessageReference msg)
213 
214 {
215  SOAPParameters parameters("EntryText");
216  // SOAPParametersV parameters(1);
217  // parameters[0].setName("EntryText");
218  SOAPUtilities::receive(msg, parameters);
219  std::string EntryText = parameters.getValue("EntryText");
220 
221  __COUT__ << "Received External Supervisor System Entry " << EntryText << std::endl;
222 
223  std::string retStr = "Success";
224 
225  ECLEntry_t eclEntry;
226  eclEntry.author(ECLUser);
227  eclEntry.category("Facility/DAQ");
228  Form_t form;
229  Field_t field;
230  Form_t::field_sequence fields;
231  std::string users = theRemoteWebUsers_.getActiveUserList(allSupervisorInfo_.getGatewayDescriptor());
232 
233  form.name("OTSDAQ System Logbook Entry");
234 
235  field = Field_t(EscapeECLString(ExperimentName), "Experiment");
236  fields.push_back(field);
237 
238  field = Field_t(EscapeECLString(run), "RunNumber");
239  fields.push_back(field);
240 
241  field = Field_t(EscapeECLString(users), "ActiveUsers");
242  fields.push_back(field);
243 
244  field = Field_t(EscapeECLString(EntryText), "Entry");
245  fields.push_back(field);
246 
247  ECLConnection eclConn(ECLUser, ECLPwd, ECLHost);
248  if (!eclConn.Post(eclEntry)) {
249  retStr = "Failure";
250  }
251 
252  //fill return parameters
253  SOAPParameters retParameters("Status", retStr);
254 
255  return SOAPUtilities::makeSOAPMessageReference("LogbookEntryStatusResponse", retParameters);
256 }
257 
258 
259 int ECLSupervisor::Write(WriteState state)
260 {
261  ECLEntry_t eclEntry;
262  eclEntry.author(ECLUser);
263  eclEntry.category("Facility/DAQ");
264  Form_t form;
265  Field_t field;
266  Form_t::field_sequence fields;
267  std::string users = theRemoteWebUsers_.getActiveUserList(allSupervisorInfo_.getGatewayDescriptor());
268 
269  switch(state)
270  {
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;
276  }
277 
278  field = Field_t(EscapeECLString(ExperimentName), "Experiment");
279  fields.push_back(field);
280 
281  field = Field_t(EscapeECLString(run), "RunNumber");
282  fields.push_back(field);
283 
284  field = Field_t(EscapeECLString(users), "ActiveUsers");
285  fields.push_back(field);
286 
287 
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;
291  dur = dur % 1000;
292  int dur_m = dur_s / 60;
293  dur_s = dur_s % 60;
294  int dur_h = dur_m / 60;
295  dur_m = dur_m % 60;
296 
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
301  << "." << dur;
302 
303  field = Field_t(EscapeECLString(dur_ss.str()), "Duration");
304  fields.push_back(field);
305  }
306 
307  form.field(fields);
308 
309  eclEntry.form(form);
310 
311  ECLConnection eclConn(ECLUser, ECLPwd, ECLHost);
312  if (!eclConn.Post(eclEntry)) {
313  return -1;
314  }
315 
316  return 0;
317 }
318 
319 //************************************************************
320 
321 std::string ECLSupervisor::EscapeECLString(std::string input)
322 {
323  std::string output = input;
324  size_t pos = output.find('&');
325  while (pos != std::string::npos)
326  {
327  output = output.replace(pos, 1, "&amp;");
328  pos = output.find('&', pos + 2);
329  }
330 
331  pos = output.find('"');
332  while (pos != std::string::npos)
333  {
334  output = output.replace(pos, 1, "&quot;");
335  pos = output.find('"', pos + 1);
336  }
337 
338  pos = output.find('\'');
339  while (pos != std::string::npos)
340  {
341  output = output.replace(pos, 1, "&apos;");
342  pos = output.find('\'', pos + 1);
343  }
344 
345  pos = output.find('<');
346  while (pos != std::string::npos)
347  {
348  output = output.replace(pos, 1, "&lt;");
349  pos = output.find('<', pos + 1);
350  }
351 
352  pos = output.find('>');
353  while (pos != std::string::npos)
354  {
355  output = output.replace(pos, 1, "&gt;");
356  pos = output.find('>', pos + 1);
357  }
358 
359  return output;
360 }
361 
362 
363 
Definition: ECL.hxx:547
Definition: ECL.hxx:410
Definition: ECL.hxx:474