MIDAS
Loading...
Searching...
No Matches
tmfe_example_everything.cxx
Go to the documentation of this file.
1/*******************************************************************\
2
3 Name: tmfe_example_everything.cxx
4 Created by: K.Olchanski
5
6 Contents: Example Front end to demonstrate all functions of TMFE class
7
8\********************************************************************/
9
10#undef NDEBUG // midas required assert() to be always enabled
11
12#include <stdio.h>
13#include <math.h> // M_PI
14
15#include "tmfe.h"
16
18 public TMFeEquipment
19{
20public:
21 EqEverything(const char* eqname, const char* eqfilename) // ctor
23 {
24 printf("EqEverything::ctor!\n");
25
26 // configure the equipment here:
27
28 //fEqConfReadConfigFromOdb = false;
33 fEqConfEnablePoll = true; // enable polled equipment
34 //fEqConfPollSleepSec = 0; // to create a "100% CPU busy" polling loop, set poll sleep time to zero
35 }
36
37 ~EqEverything() // dtor
38 {
39 printf("EqEverything::dtor!\n");
40 }
41
43 {
44 printf("EqEverything::HandleUsage!\n");
45 }
46
47 TMFeResult HandleInit(const std::vector<std::string>& args)
48 {
49 printf("EqEverything::HandleInit!\n");
51 fEqConfReadOnlyWhenRunning = false; // overwrite ODB Common RO_RUNNING to false
52 fEqConfWriteEventsToOdb = true; // overwrite ODB Common RO_ODB to true
53 EqSetStatus("Started...", "white");
54 //EqStartPollThread();
55 return TMFeOk();
56 }
57
58 TMFeResult HandleRpc(const char* cmd, const char* args, std::string& response)
59 {
60 fMfe->Msg(MINFO, "HandleRpc", "RPC cmd [%s], args [%s]", cmd, args);
61
62 // RPC handler
63
64 char tmp[256];
65 time_t now = time(NULL);
66 snprintf(tmp, sizeof(tmp), "{ \"current_time\" : [ %d, \"%s\"] }", (int)now, ctime(&now));
67
68 response = tmp;
69
70 return TMFeOk();
71 }
72
74 {
75 fMfe->Msg(MINFO, "HandleBeginRun", "Begin run %d!", run_number);
76 EqSetStatus("Running", "#00FF00");
77 return TMFeOk();
78 }
79
81 {
82 fMfe->Msg(MINFO, "HandleEndRun", "End run %d!", run_number);
83 EqSetStatus("Stopped", "#FFFFFF");
84 return TMFeOk();
85 }
86
88 {
89 fMfe->Msg(MINFO, "HandlePauseRun", "Pause run %d!", run_number);
90 EqSetStatus("Paused", "#FFFF00");
91 return TMFeOk();
92 }
93
95 {
96 fMfe->Msg(MINFO, "HandleResumeRun", "Resume run %d!", run_number);
97 EqSetStatus("Running", "#00FF00");
98 return TMFeOk();
99 }
100
102 {
103 fMfe->Msg(MINFO, "HandleStartAbortRun", "Begin run %d aborted!", run_number);
104 EqSetStatus("Stopped", "#FFFFFF");
105 return TMFeOk();
106 }
107
108 void SendData(double dvalue)
109 {
110 char buf[1024];
111 ComposeEvent(buf, sizeof(buf));
112 BkInit(buf, sizeof(buf));
113
114 double* ptr = (double*)BkOpen(buf, "data", TID_DOUBLE);
115 *ptr++ = dvalue;
116 BkClose(buf, ptr);
117
118 EqSendEvent(buf);
119 }
120
122 {
123 printf("EqEverything::HandlePeriodic!\n");
124 double t = TMFE::GetTime();
125 double data = 100.0*sin(0*M_PI/2.0+M_PI*t/40);
126 SendData(data);
127 char status_buf[256];
128 snprintf(status_buf, sizeof(status_buf), "value %.1f", data);
129 EqSetStatus(status_buf, "#00FF00");
130 }
131
133 {
134 //printf("EqEverything::HandlePoll!\n");
135
136 if (!fMfe->fStateRunning) // only poll when running
137 return false;
138
139 double r = drand48();
140 if (r > 0.999) {
141 // return successful poll rarely
142 printf("EqEverything::HandlePoll!\n");
143 return true;
144 }
145
146 return false;
147 }
148
150 {
151 printf("EqEverything::HandlePollRead!\n");
152
153 char buf[1024];
154
155 ComposeEvent(buf, sizeof(buf));
156 BkInit(buf, sizeof(buf));
157
158 uint32_t* ptr = (uint32_t*)BkOpen(buf, "poll", TID_UINT32);
159 for (int i=0; i<16; i++) {
160 *ptr++ = lrand48();
161 }
162 BkClose(buf, ptr);
163
164 EqSendEvent(buf, false); // do not write polled data to ODB and history
165 }
166};
167
168// example frontend
169
171{
172public:
173 FeEverything() // ctor
174 {
175 printf("FeEverything::ctor!\n");
176 FeSetName("tmfe_example_everything");
177 FeAddEquipment(new EqEverything("tmfe_example_everything", __FILE__));
178 }
179
181 {
182 printf("FeEverything::HandleUsage!\n");
183 };
184
185 TMFeResult HandleArguments(const std::vector<std::string>& args)
186 {
187 printf("FeEverything::HandleArguments!\n");
188 return TMFeOk();
189 };
190
191 TMFeResult HandleFrontendInit(const std::vector<std::string>& args)
192 {
193 printf("FeEverything::HandleFrontendInit!\n");
194 return TMFeOk();
195 };
196
197 TMFeResult HandleFrontendReady(const std::vector<std::string>& args)
198 {
199 printf("FeEverything::HandleFrontendReady!\n");
200 //FeStartPeriodicThread();
201 //fMfe->StartRpcThread();
202 return TMFeOk();
203 };
204
206 {
207 printf("FeEverything::HandleFrontendExit!\n");
208 };
209};
210
211// boilerplate main function
212
213int main(int argc, char* argv[])
214{
216 return fe_everything.FeMain(argc, argv);
217}
218
219/* emacs
220 * Local Variables:
221 * tab-width: 8
222 * c-basic-offset: 3
223 * indent-tabs-mode: nil
224 * End:
225 */
TMFeResult HandleInit(const std::vector< std::string > &args)
EqEverything(const char *eqname, const char *eqfilename)
TMFeResult HandleRpc(const char *cmd, const char *args, std::string &response)
TMFeResult HandleBeginRun(int run_number)
TMFeResult HandleResumeRun(int run_number)
void SendData(double dvalue)
TMFeResult HandlePauseRun(int run_number)
TMFeResult HandleEndRun(int run_number)
TMFeResult HandleStartAbortRun(int run_number)
TMFeResult HandleArguments(const std::vector< std::string > &args)
TMFeResult HandleFrontendInit(const std::vector< std::string > &args)
TMFeResult HandleFrontendReady(const std::vector< std::string > &args)
bool fStateRunning
run state is running or paused
Definition tmfe.h:402
static double GetTime()
return current time in seconds, with micro-second precision
Definition tmfe.cxx:1011
void Msg(int message_type, const char *filename, int line, const char *routine, const char *format,...) MATTRPRINTF(6
Definition tmfe.cxx:991
void RegisterTransitionStartAbort()
Definition tmfe.cxx:1507
void * BkOpen(char *pevent, const char *bank_name, int bank_type) const
Definition tmfe.cxx:2201
TMFeResult BkInit(char *pevent, size_t size) const
Definition tmfe.cxx:2195
TMFE * fMfe
Definition tmfe.h:207
TMFeResult EqSetStatus(const char *status, const char *color)
Definition tmfe.cxx:2215
bool fEqConfWriteEventsToOdb
Definition tmfe.h:197
uint16_t fEqConfEventID
Definition tmfe.h:171
int fEqConfPeriodMilliSec
Definition tmfe.h:178
TMFeResult BkClose(char *pevent, void *ptr) const
Definition tmfe.cxx:2208
int fEqConfLogHistory
Definition tmfe.h:181
TMFeResult ComposeEvent(char *pevent, size_t size) const
Definition tmfe.cxx:1995
TMFeResult EqSendEvent(const char *pevent, bool write_to_odb=true)
Definition tmfe.cxx:2006
bool fEqConfEnablePoll
Definition tmfe.h:166
bool fEqConfReadOnlyWhenRunning
Definition tmfe.h:196
void FeSetName(const char *program_name)
Definition tmfe.cxx:1647
TMFeResult FeAddEquipment(TMFeEquipment *eq)
Definition tmfe.cxx:1600
int FeMain(int argc, char *argv[])
Definition tmfe.cxx:2279
#define TID_DOUBLE
Definition midas.h:343
#define MINFO
Definition midas.h:560
#define TID_UINT32
Definition midas.h:337
int main()
Definition hwtest.cxx:23
INT run_number[2]
Definition mana.cxx:246
void * data
Definition mana.cxx:268
INT i
Definition mdump.cxx:32
char response[10000]
Definition melog.cxx:90
#define ctime
Definition msystem.h:264
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)
Definition rmidas.h:24
TMFeResult TMFeOk()
Definition tmfe.h:106