The VisAO Camera
PwrMon.cpp
1 /************************************************************
2 * PwrMon.h
3 *
4 * Author: Jared R. Males (jrmales@email.arizona.edu)
5 *
6 * Declarations for the PwrMon class, which monitors the VisAOPwrCtrl
7 * application. VisAO power control is managed from the supervisor computer
8 * in the interest of reliability, but the VisAO system needs access to it.
9 *
10 * Developed as part of the Magellan Adaptive Optics system.
11 ************************************************************/
12 
13 /** \file PwrMon.h
14  * \author Jared R. Males
15  * \brief Declarations for the VisAOPwrMon class
16  *
17  * VisAOPwrMon monitors the VisAOPwrCtrl
18  * application. VisAO power control is managed from the supervisor computer
19  * in the interest of reliability, but the VisAO system needs access to it.
20  *
21 */
22 
23 //#define _debug
24 
25 #include "PwrMon.h"
26 
27 
28 
29 namespace VisAO
30 {
31 
32 PwrMon::PwrMon( std::string name, const std::string &conffile) throw (AOException) : AOApp(name, conffile)
33 {
34  setupVisAOApp();
35 }
36 
37 PwrMon::PwrMon( int argc, char **argv) throw (AOException) : AOApp(argc, argv)
38 {
39  setupVisAOApp();
40 }
41 
43 {
44  //Init the status board
45  statusboard_shmemkey = 13000;
46  if(create_statusboard(sizeof(power_status_board)) != 0)
47  {
49  _logger->log(Logger::LOG_LEV_ERROR, "Could not create status board.");
50  }
51  else
52  {
54  strncpy(bsb->appname, MyFullName().c_str(), 25);
55  bsb->max_update_interval = pause_time;
56  }
57 
58 }
59 
60 
62 {
63  Config_File *cfg = &ConfigDictionary();
64  std::string pwrctrl_name, pwrconf_name, pwr_full_name;
65  Config_File * pwr_conf;
66 
67  try
68  {
69  pwrctrl_name = (std::string)(*cfg)["pwrCtrlName"];
70  }
71  catch (Config_File_Exception &e)
72  {
73  Logger::get()->log( Logger::LOG_LEV_FATAL, e.what().c_str());
74  throw(e);
75  }
76 
77  pwr_full_name = pwrctrl_name + ".L";
78 
79  pwrconf_name = Utils::getConffile(pwrctrl_name);
80 
81  pwr_conf = new Config_File(pwrconf_name);
82 
83  std::vector<std::string> item_codes(8);
84  std::vector<std::string> item_names(8);
85  char item_code[32];
86  char item_name[32];
87 
88  for(int i=0; i< 8; i++)
89  {
90 
91  snprintf( item_code, 32, "item%d.code", i);
92  snprintf( item_name, 32, "item%d.name", i);
93 
94  try
95  {
96  item_codes[i] = (std::string)(*pwr_conf)[item_code];
97  }
98  catch (Config_File_Exception &e)
99  {
100  snprintf( item_code, 32, "OUTLET%d", i+1);
101  item_codes[i] = item_code;
102  }
103 
104  try
105  {
106  item_names[i] = (std::string)(*pwr_conf)[item_name];
107  }
108  catch (Config_File_Exception &e)
109  {
110  snprintf( item_name, 32, "OUTLET%d", i+1);
111  item_names[i] = item_name;
112  }
113  }
114 
115  try
116  {
117 
118  var_outlet1_cur = RTDBvar( pwr_full_name, item_codes[0], CUR_VAR, INT_VARIABLE, 1,1);
119  var_outlet1_req = RTDBvar( pwr_full_name, item_codes[0], REQ_VAR, INT_VARIABLE, 1,1);
120  var_outlet1_name = RTDBvar( pwr_full_name, item_codes[0]+".NAME" , NO_DIR, CHAR_VARIABLE, item_names[0].size());
121  Notify(var_outlet1_cur, outlet1CurChanged);
122 
123  var_outlet2_cur = RTDBvar( pwr_full_name, item_codes[1], CUR_VAR, INT_VARIABLE, 1,1);
124  var_outlet2_req = RTDBvar( pwr_full_name, item_codes[1], REQ_VAR, INT_VARIABLE, 1,1);
125  var_outlet2_name = RTDBvar( pwr_full_name, item_codes[1]+".NAME" , NO_DIR, CHAR_VARIABLE, item_names[1].size());
126  Notify(var_outlet2_cur, outlet2CurChanged);
127 
128  var_outlet3_cur = RTDBvar( pwr_full_name, item_codes[2], CUR_VAR, INT_VARIABLE, 1,1);
129  var_outlet3_req = RTDBvar( pwr_full_name, item_codes[2], REQ_VAR, INT_VARIABLE, 1,1);
130  var_outlet3_name = RTDBvar( pwr_full_name, item_codes[2]+".NAME" , NO_DIR, CHAR_VARIABLE, item_names[2].size());
131  Notify(var_outlet3_cur, outlet3CurChanged);
132 
133  var_outlet4_cur = RTDBvar( pwr_full_name, item_codes[3], CUR_VAR, INT_VARIABLE, 1,1);
134  var_outlet4_req = RTDBvar( pwr_full_name, item_codes[3], REQ_VAR, INT_VARIABLE, 1,1);
135  var_outlet4_name = RTDBvar( pwr_full_name, item_codes[3]+".NAME" , NO_DIR, CHAR_VARIABLE, item_names[3].size());
136  Notify(var_outlet4_cur, outlet4CurChanged);
137 
138  var_outlet5_cur = RTDBvar( pwr_full_name, item_codes[4], CUR_VAR, INT_VARIABLE, 1,1);
139  var_outlet5_req = RTDBvar( pwr_full_name, item_codes[4], REQ_VAR, INT_VARIABLE, 1,1);
140  var_outlet5_name = RTDBvar( pwr_full_name, item_codes[4]+".NAME" , NO_DIR, CHAR_VARIABLE, item_names[4].size());
141  Notify(var_outlet5_cur, outlet5CurChanged);
142 
143  var_outlet6_cur = RTDBvar( pwr_full_name, item_codes[5], CUR_VAR, INT_VARIABLE, 1,1);
144  var_outlet6_req = RTDBvar( pwr_full_name, item_codes[5], REQ_VAR, INT_VARIABLE, 1,1);
145  var_outlet6_name = RTDBvar( pwr_full_name, item_codes[5]+".NAME" , NO_DIR, CHAR_VARIABLE, item_names[5].size());
146  Notify(var_outlet6_cur, outlet6CurChanged);
147 
148  var_outlet7_cur = RTDBvar( pwr_full_name, item_codes[6], CUR_VAR, INT_VARIABLE, 1,1);
149  var_outlet7_req = RTDBvar( pwr_full_name, item_codes[6], REQ_VAR, INT_VARIABLE, 1,1);
150  var_outlet7_name = RTDBvar( pwr_full_name, item_codes[6]+".NAME" , NO_DIR, CHAR_VARIABLE, item_names[6].size());
151  Notify(var_outlet7_cur, outlet7CurChanged);
152 
153  var_outlet8_cur = RTDBvar( pwr_full_name, item_codes[7], CUR_VAR, INT_VARIABLE, 1,1);
154  var_outlet8_req = RTDBvar( pwr_full_name, item_codes[7], REQ_VAR, INT_VARIABLE, 1,1);
155  var_outlet8_name = RTDBvar( pwr_full_name, item_codes[7]+".NAME" , NO_DIR, CHAR_VARIABLE, item_names[7].size());
156  Notify(var_outlet8_cur, outlet8CurChanged);
157 
158  }
159  catch (AOVarException &e)
160  {
161  Logger::get()->log(Logger::LOG_LEV_FATAL, "%s:%s: %s", __FILE__, __LINE__, e.what().c_str());
162  throw AOException("Error creating RTDB variables");
163  }
164 }
165 
166 int PwrMon::outlet1CurChanged(void *pt, Variable *msgb)
167 {
168  PwrMon * pm = (PwrMon *) pt;
169 
170  pm->var_outlet1_cur.Update();
171 
172  pm->update_statusboard();
173 
174  return 0;
175 }
176 
177 int PwrMon::outlet2CurChanged(void *pt, Variable *msgb)
178 {
179  PwrMon * pm = (PwrMon *) pt;
180 
181  pm->var_outlet2_cur.Update();
182 
183  pm->update_statusboard();
184 
185  return 0;
186 }
187 
188 int PwrMon::outlet3CurChanged(void *pt, Variable *msgb)
189 {
190  PwrMon * pm = (PwrMon *) pt;
191 
192  pm->var_outlet3_cur.Update();
193 
194  pm->update_statusboard();
195 
196  return 0;
197 }
198 
199 int PwrMon::outlet4CurChanged(void *pt, Variable *msgb)
200 {
201  PwrMon * pm = (PwrMon *) pt;
202 
203  pm->var_outlet4_cur.Update();
204 
205  pm->update_statusboard();
206 
207  return 0;
208 }
209 
210 int PwrMon::outlet5CurChanged(void *pt, Variable *msgb)
211 {
212  PwrMon * pm = (PwrMon *) pt;
213 
214  pm->var_outlet5_cur.Update();
215 
216  pm->update_statusboard();
217 
218  return 0;
219 
220 }
221 
222 int PwrMon::outlet6CurChanged(void *pt, Variable *msgb)
223 {
224  PwrMon * pm = (PwrMon *) pt;
225 
226  pm->var_outlet6_cur.Update();
227 
228  pm->update_statusboard();
229 
230  return 0;
231 }
232 
233 int PwrMon::outlet7CurChanged(void *pt, Variable *msgb)
234 {
235  PwrMon * pm = (PwrMon *) pt;
236 
237  pm->var_outlet7_cur.Update();
238 
239  pm->update_statusboard();
240 
241  return 0;
242 }
243 
244 int PwrMon::outlet8CurChanged(void *pt, Variable *msgb)
245 {
246  PwrMon * pm = (PwrMon *) pt;
247 
248  pm->var_outlet8_cur.Update();
249 
250  pm->update_statusboard();
251 
252  return 0;
253 }
254 
255 
256 void PwrMon::Run()
257 {
258  init_statusboard();
259 
260  _logger->log( Logger::LOG_LEV_INFO, "Running...");
261 
262  while(!TimeToDie())
263  {
264  try
265  {
266  usleep((int) pause_time*1000000.);
268  }
269  catch (AOException &e)
270  {
271  _logger->log( Logger::LOG_LEV_ERROR, "Caught exception (at %s:%d): %s", __FILE__, __LINE__, e.what().c_str());
272  }
273  }
274 }
275 
276 
277 int PwrMon::init_statusboard()
278 {
279  std::string tmpstr;
281  {
283 
284  tmpstr = var_outlet1_name.Get();
285  strncpy(psb->outlet1_name, tmpstr.c_str(), 50);
286 
287  tmpstr = var_outlet2_name.Get();
288  strncpy(psb->outlet2_name, tmpstr.c_str(), 50);
289 
290  tmpstr = var_outlet3_name.Get();
291  strncpy(psb->outlet3_name, tmpstr.c_str(), 50);
292 
293  tmpstr = var_outlet4_name.Get();
294  strncpy(psb->outlet4_name, tmpstr.c_str(), 50);
295 
296  tmpstr = var_outlet5_name.Get();
297  strncpy(psb->outlet5_name, tmpstr.c_str(), 50);
298 
299  tmpstr = var_outlet6_name.Get();
300  strncpy(psb->outlet6_name, tmpstr.c_str(), 50);
301 
302  tmpstr = var_outlet7_name.Get();
303  strncpy(psb->outlet7_name, tmpstr.c_str(), 50);
304 
305  tmpstr = var_outlet8_name.Get();
306  strncpy(psb->outlet8_name, tmpstr.c_str(), 50);
307  }
308 
309  return 0;
310 }
311 
313 {
315  {
317 
319 
320 /* var_outlet1_cur.Update();
321  var_outlet2_cur.Update();
322  var_outlet3_cur.Update();
323  var_outlet4_cur.Update();
324  var_outlet5_cur.Update();
325  var_outlet6_cur.Update();
326  var_outlet7_cur.Update();
327  var_outlet8_cur.Update();*/
328 
329  var_outlet1_cur.Get(&psb->outlet1_state);
330  var_outlet2_cur.Get(&psb->outlet2_state);
331  var_outlet3_cur.Get(&psb->outlet3_state);
332  var_outlet4_cur.Get(&psb->outlet4_state);
333  var_outlet5_cur.Get(&psb->outlet5_state);
334  var_outlet6_cur.Get(&psb->outlet6_state);
335  var_outlet7_cur.Get(&psb->outlet7_state);
336  var_outlet8_cur.Get(&psb->outlet8_state);
337 
338  #ifdef _debug
339  std::cout << psb->outlet1_name << " " << psb->outlet1_state << "\n";
340  std::cout << psb->outlet2_name << " " << psb->outlet2_state << "\n";
341  std::cout << psb->outlet3_name << " " << psb->outlet3_state << "\n";
342  std::cout << psb->outlet4_name << " " << psb->outlet4_state << "\n";
343  std::cout << psb->outlet5_name << " " << psb->outlet5_state << "\n";
344  std::cout << psb->outlet6_name << " " << psb->outlet6_state << "\n";
345  std::cout << psb->outlet7_name << " " << psb->outlet7_state << "\n";
346  std::cout << psb->outlet8_name << " " << psb->outlet8_state << "\n";
347  std::cout << "\n";
348  #endif
349 
350  }
351  return 0;
352 }//int PwrMon::update_statusboard()
353 
354 } //namespace VisAO
355 
356 
A class to monitor the VisAOPwrCtrl via the RTDB.
Definition: PwrMon.h:36
void SetupVars()
Virtual function to setup variables in RTDB.
Definition: PwrMon.cpp:61
Declarations for the VisAOPwrMon class.
void * statusboard_shmemptr
The pointer to the shared memory block for the statusboard.
virtual int update_statusboard()
Update the status board.
void setupVisAOApp()
Basic setup of the class, called by both constructors.
Definition: PwrMon.cpp:42
key_t statusboard_shmemkey
The key used to lookup the shared memory.
PwrMon(std::string name, const std::string &conffile)
Standard adopt style config file constructor.
Definition: PwrMon.cpp:32
int TimeToDie
Global set by SIGTERM.
double pause_time
Time to pause during application main loop.
Definition: VisAOApp_base.h:84
int create_statusboard(size_t sz)
Creates and attaches to the statusboard shared memory.
virtual int update_statusboard()
Update the status board.
Definition: PwrMon.cpp:312
The namespace of VisAO software.