ICP  1
ICPRequestHandlerFactory.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
3 #include "isiscrpt.h"
4 #include "../svn-revision.h"
5 
6 class RootHandler : public Poco::Net::HTTPRequestHandler
7 {
8 private:
10 
11 public:
12  RootHandler(const ISISCRPT_STRUCT* crpt) : m_crpt(crpt) { }
13  void handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response);
14 };
15 
16 class DataHandler : public Poco::Net::HTTPRequestHandler
17 {
18 private:
20 public:
21  DataHandler(const ISISCRPT_STRUCT* crpt) : m_crpt(crpt) { }
22  void handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response);
23 };
24 
25 
27 {
28 }
29 
30 Poco::Net::HTTPRequestHandler* ICPRequestHandlerFactory::createRequestHandler(const Poco::Net::HTTPServerRequest& request)
31 {
32  if (request.getURI() == "/")
33  {
34  return new RootHandler(m_crpt);
35  }
36  else
37  {
38  return new DataHandler(m_crpt);
39  }
40 }
41 
42 static void outputValue(std::ostream& os, const std::string& name, const std::string& units, double value)
43 {
44  os << Poco::format("<%s units=\"%s\">%f</%s>\n", name, units, value, name);
45 }
46 
47 static void outputValue(std::ostream& os, const std::string& name, const std::string& units, int value)
48 {
49  os << Poco::format("<%s units=\"%s\">%d</%s>\n", name, units, value, name);
50 }
51 
52 static void outputValue(std::ostream& os, const std::string& name, const std::string& units, unsigned int value)
53 {
54  os << Poco::format("<%s units=\"%s\">%u</%s>\n", name, units, value, name);
55 }
56 
57 static void outputValue(std::ostream& os, const std::string& name, const std::string& units, const std::string& value)
58 {
59  os << Poco::format("<%s units=\"%s\">%s</%s>\n", name, units, value, name);
60 }
61 
62 static void outputTimeValue(std::ostream& os, const std::string& name, time_t value)
63 {
64  char time_buffer[128];
65  strftime(time_buffer, sizeof(time_buffer), "%H:%M, %a %d %b", localtime(&value));
66  os << Poco::format("<%s units=\"\" fromepoch=\"%lu\" iso=\"%s\">%s</%s>\n", name, (unsigned long)value,
67  Poco::DateTimeFormatter::format(Poco::Timestamp::fromEpochTime(value), "%Y-%m-%dT%H:%M:%S%z"),
68  std::string(time_buffer), name);
69 }
70 
71 static const char* nagios_status[] = { "OK", "WARNING", "CRITICAL", "UNKNOWN" };
72 
73 static const char* nagios_shutter_val[] = { "CLOSED and RUNNING", "CLOSED", "OPEN" };
74 
75 void RootHandler::handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response)
76 {
77  Poco::Util::Application& app = Poco::Util::Application::instance();
78 // app.logger().information("Request from " + request.clientAddress().toString());
79  response.setChunkedTransferEncoding(true);
80  response.setContentType("text/xml");
81  std::ostream& ostr = response.send();
82  const nagios_t& nagios = m_crpt->nagios;
83  ostr << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
84  ostr << "<values>\n";
85  outputValue(ostr, "VeroPercent", "%", nagios.veto_percent);
86  outputValue(ostr, "BeamCurrent", "uA", m_crpt->beam_current);
87  outputValue(ostr, "TotalUAmps", "uAh", m_crpt->update_good_uamph);
88  outputValue(ostr, "GoodFramesTotal", "frames", m_crpt->update_good_frames);
89  outputValue(ostr, "RawFramesTotal", "frames", m_crpt->update_raw_frames);
90  outputValue(ostr, "RunNumber", "", m_crpt->run_number);
91  outputValue(ostr, "RunStatus", "", runstatus_names[m_crpt->run_status]);
92  outputValue(ostr, "SEStatus", "", sestatus_names[m_crpt->se_status]);
93  outputValue(ostr, "CurrentPeriod", "", m_crpt->update_period + 1);
94  outputValue(ostr, "RunDurationTotal", "seconds", m_crpt->update_duration);
95  outputValue(ostr, "WaitTime", "minutes", nagios.wait_mins);
96  outputTimeValue(ostr, "StartTime", m_crpt->start_time);
97  outputTimeValue(ostr, "StopTime", m_crpt->stop_time);
98  outputTimeValue(ostr, "ResumeTime", m_crpt->resume_time);
99  outputTimeValue(ostr, "ICPStartTime", m_crpt->icp_start_time);
100  outputValue(ostr, "CountRate", "MEv /s", m_crpt->count_rate);
101  outputValue(ostr, "InstName", "", m_crpt->inst_name);
102  outputValue(ostr, "TotalDAECounts", "MEv", (float)m_crpt->update_total_counts / (float)1e6);
103  outputValue(ostr, "MonitorCounts", "counts", m_crpt->monitor_sum[0]);
104  outputValue(ostr, "RunTitle", "", m_crpt->long_title);
105  outputValue(ostr, "RBNumber", "", m_crpt->rb_number);
106  outputValue(ostr, "EventModeCardFraction", "", m_crpt->eventModeCardFraction());
107  outputValue(ostr, "BeamlineStatus", "", nagios_status[nagios.shutter_status]);
108  outputValue(ostr, "DAEStatus", "", nagios_status[nagios.dae_status]);
109  outputValue(ostr, "WaitStatus", "", nagios_status[nagios.wait_status]);
110  outputValue(ostr, "ShutterVal", "", nagios_shutter_val[nagios.shutter_val]);
111  outputValue(ostr, "NumberOfPeriods", "", m_crpt->nper);
112  outputValue(ostr, "UserName", "", m_crpt->user_name);
113  outputValue(ostr, "NDataDae", "", m_crpt->n_data_dae);
114  outputValue(ostr, "DAEType", "", ISISDAE::DAEType_desc[m_crpt->dae_type]);
115  outputValue(ostr, "ICPSVNRevision", "", ICPSVN_Revision);
116  outputValue(ostr, "ICPFullVersion", "",
117  Poco::format("SVN revision %s (%s, %s %s, %s)", ICPSVN_Revision, ICPSVN_Date, ICPSVN_Mixed, ICPSVN_Range, ICPSVN_Modified));
118  ostr << "</values>\n";
119 }
120 
121 void DataHandler::handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response)
122 {
123  Poco::Util::Application& app = Poco::Util::Application::instance();
124 // app.logger().information("Request from " + request.clientAddress().toString());
125  response.setChunkedTransferEncoding(true);
126  response.setContentType("text/plain");
127  std::ostream& ostr = response.send();
128  const nagios_t& nagios = m_crpt->nagios;
129  const std::string& item = request.getURI();
130  if ("/VeroPercent" == item)
131  {
132  ostr << nagios.veto_percent;
133  }
134  else if ("/ShutterStatus" == item)
135  {
136  ostr << nagios.shutter_status;
137  }
138  else if ("/ShutterVal" == item)
139  {
140  ostr << nagios.shutter_val;
141  }
142  else if ("/DAEStatus" == item)
143  {
144  ostr << nagios.dae_status;
145  }
146 }
const ISISCRPT_STRUCT * m_crpt
static const char * sestatus_names[]
Definition: isiscrpt.h:65
static const char * nagios_status[]
int64_t update_total_counts
Definition: isiscrpt.h:375
float update_good_uamph
Definition: isiscrpt.h:372
double veto_percent
Definition: isiscrpt.h:208
static void outputTimeValue(std::ostream &os, const std::string &name, time_t value)
time_t resume_time
Definition: isiscrpt.h:255
time_t start_time
Definition: isiscrpt.h:254
void handleRequest(Poco::Net::HTTPServerRequest &request, Poco::Net::HTTPServerResponse &response)
RootHandler(const ISISCRPT_STRUCT *crpt)
static const char * nagios_shutter_val[]
long update_period
Definition: isiscrpt.h:378
int dae_status
Definition: isiscrpt.h:212
long update_raw_frames
Definition: isiscrpt.h:377
long update_duration
Definition: isiscrpt.h:382
static const char * DAEType_desc[]
Definition: isisdae.h:66
float beam_current
Definition: isiscrpt.h:374
int dae_type
time CRPT unloaded (may not be set if program crashed)
Definition: isiscrpt.h:247
char long_title[ISISCRPT_TITLE_LEN+1]
Definition: isiscrpt.h:350
int shutter_val
Definition: isiscrpt.h:210
long update_good_frames
Definition: isiscrpt.h:376
boost::array< unsigned, ISISCRPT_MAX_MONITOR > monitor_sum
Definition: isiscrpt.h:300
time_t stop_time
Definition: isiscrpt.h:256
static const char * runstatus_names[]
Definition: isiscrpt.h:57
void handleRequest(Poco::Net::HTTPServerRequest &request, Poco::Net::HTTPServerResponse &response)
int wait_status
Definition: isiscrpt.h:211
const ISISCRPT_STRUCT * m_crpt
int shutter_status
Definition: isiscrpt.h:213
double eventModeCardFraction() const
Definition: isiscrpt.h:844
double wait_mins
Definition: isiscrpt.h:209
time_t icp_start_time
Definition: isiscrpt.h:244
float count_rate
Definition: isiscrpt.h:385
char user_name[ISISCRPT_USER_NAME_LEN+1]
Definition: isiscrpt.h:353
char inst_name[ISISCRPT_INST_LEN+1]
Definition: isiscrpt.h:351
Poco::Net::HTTPRequestHandler * createRequestHandler(const Poco::Net::HTTPServerRequest &request)
const ISISCRPT_STRUCT * m_crpt
DataHandler(const ISISCRPT_STRUCT *crpt)
static void outputValue(std::ostream &os, const std::string &name, const std::string &units, double value)
ICPRequestHandlerFactory(const ISISCRPT_STRUCT *crpt)
nagios_t nagios
Definition: isiscrpt.h:491