-
Notifications
You must be signed in to change notification settings - Fork 34
/
dome.cpp
521 lines (468 loc) · 14 KB
/
dome.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
* dome driver skeleton.
* Copyright (C) 2005-2008 Petr Kubanek <petr@kubanek.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "dome.h"
using namespace rts2dome;
using namespace rts2core;
#define OPT_WEATHER_OPENS OPT_LOCAL + 200
#define OPT_STATE_MASTER OPT_LOCAL + 201
#define OPT_DONOTCLOSE OPT_LOCAL + 202
#define OPT_IGNORETIMEOUT OPT_LOCAL + 203
Dome::Dome (int in_argc, char **in_argv, int in_device_type, bool inhibit_auto_close):Device (in_argc, in_argv, in_device_type, "DOME")
{
stateMaster = NULL;
closeFailReported = false;
createValue (weatherOpensDome, "weather_open", "if true, good weather can open dome", false, RTS2_VALUE_WRITABLE | RTS2_VALUE_AUTOSAVE);
weatherOpensDome->setValueBool (false);
if (inhibit_auto_close)
{
createValue (domeAutoClose, "auto_close", "if false, ignore commands to close dome from centrald", false, RTS2_VALUE_WRITABLE);
domeAutoClose->setValueBool (false);
}
else
{
domeAutoClose = NULL;
}
createValue (ignoreTimeout, "ignore_time", "ignore weather state until given time", false, RTS2_VALUE_WRITABLE);
ignoreTimeout->setValueDouble (0);
createValue (nextGoodWeather, "next_open", "next possible opening of the dome", false);
nextGoodWeather->setValueDouble (getNow () + DEF_WEATHER_TIMEOUT);
addOption (OPT_WEATHER_OPENS, "weather-can-open", 0, "specified that option if weather signal is allowed to open dome");
addOption (OPT_STATE_MASTER, "state-master", 1, "state master - server which guverns opening and closing of dome for evening and morning");
addOption (OPT_DONOTCLOSE, "notclose", 0, "do not close (switch to bad weather) on startup");
addOption (OPT_IGNORETIMEOUT, "ignore-timeout", 1, "set initial ignore timeout to now + value in seconds");
createValue (closeRepeatTimeAfterPark, "close_repeat_time", "time [s] after which to retry the close (when equipment repositioning was necessary)", false, RTS2_VALUE_WRITABLE);
closeRepeatTimeAfterPark->setValueDouble (3);
}
Dome::~Dome ()
{
}
int Dome::processOption (int in_opt)
{
switch (in_opt)
{
case OPT_WEATHER_OPENS:
weatherOpensDome->setValueBool (true);
break;
case OPT_STATE_MASTER:
createValue (stateMaster, "state_master", "server governing dome evening and morning states", false);
updateMetaInformations (stateMaster);
stateMaster->setValueCharArr (optarg);
break;
case OPT_IGNORETIMEOUT:
ignoreTimeout->setValueDouble (getNow () + atoi (optarg));
break;
case OPT_DONOTCLOSE:
nextGoodWeather->setValueDouble (getNow () - 300);
break;
default:
return Device::processOption (in_opt);
}
return 0;
}
int Dome::init ()
{
int ret;
ret = Device::init ();
if (ret)
return ret;
// check for presense of state-master
if (getCentraldConns ()->size () > 1)
{
if (stateMaster == NULL || stateMaster->getValueString ().length () == 0)
{
logStream (MESSAGE_ERROR) << "multiple central server specified, but none identified as master - please add state-master option" << sendLog;
return -1;
}
rts2core::Connection *conn = getCentraldConn (stateMaster->getValue ());
if (conn == NULL)
{
logStream (MESSAGE_ERROR) << "cannot find state-master " << stateMaster->getValue () << " in list of central connections" << sendLog;
return -1;
}
setMasterConn (conn);
}
centraldLastContactTime = getNow ();
initialSettleTime = getNow () + DEF_INITIAL_SETTLE_TIME;
setTimeout (5 * USEC_SEC);
setIdleInfoInterval (5);
return 0;
}
int Dome::domeOpenStart ()
{
deleteTimers (EVENT_DOMECLOSE_RETRY);
if (isOpened () == -2)
{
maskState (DOME_DOME_MASK | BOP_EXPOSURE | DEVICE_BLOCK_OPEN | DEVICE_BLOCK_CLOSE, DOME_OPENED | DEVICE_BLOCK_CLOSE, "dome opened");
logStream (MESSAGE_REPORTIT | MESSAGE_INFO) << "dome (already) opened" << sendLog;
return 0;
}
if (isGoodWeather () == false)
return -1;
if (startOpen ())
{
logStream (MESSAGE_ERROR) << "opening of the dome failed" << sendLog;
return -1;
}
closeFailReported = false;
maskState (DOME_DOME_MASK | BOP_EXPOSURE | DEVICE_BLOCK_OPEN | DEVICE_BLOCK_CLOSE, DOME_OPENING | BOP_EXPOSURE | DEVICE_BLOCK_OPEN | DEVICE_BLOCK_CLOSE, "opening dome");
logStream (MESSAGE_REPORTIT | MESSAGE_INFO) << "starting to open the dome" << sendLog;
return 0;
}
int Dome::domeCloseStart ()
{
deleteTimers (EVENT_DOMECLOSE_RETRY);
if (isClosed () == -2)
{
maskState (DOME_DOME_MASK | DEVICE_BLOCK_OPEN | DEVICE_BLOCK_CLOSE, DOME_CLOSED | DEVICE_BLOCK_OPEN, "dome closed");
//logStream (MESSAGE_INFO) << "dome (already) closed" << sendLog;
return 0;
}
int ret = startClose ();
if (ret < 0)
{
if (closeFailReported == false)
logStream (MESSAGE_REPORTIT | MESSAGE_ERROR) << "cannot start closing of the dome" << sendLog;
closeFailReported = true;
return -1;
}
else if (ret > 0)
{
if ((getState () & DOME_DOME_MASK) != DOME_WAIT_CLOSING)
{
logStream (MESSAGE_REPORTIT | MESSAGE_INFO) << "waiting for devices to move into a position safe for dome to close" << sendLog;
}
maskState (DOME_DOME_MASK | BOP_EXPOSURE, DOME_WAIT_CLOSING, "waiting for devices to move into a position safe for dome to close");
addTimer (closeRepeatTimeAfterPark->getValueDouble (), new rts2core::Event (EVENT_DOMECLOSE_RETRY));
return 0;
}
closeFailReported = false;
if ((getState () & DOME_DOME_MASK) != DOME_CLOSING)
{
logStream (MESSAGE_REPORTIT | MESSAGE_INFO) << "starting to close dome" << sendLog;
}
maskState (DOME_DOME_MASK | BOP_EXPOSURE | DEVICE_BLOCK_OPEN | DEVICE_BLOCK_CLOSE, DOME_CLOSING | DEVICE_BLOCK_OPEN | DEVICE_BLOCK_CLOSE, "starting to close dome");
return 0;
}
int Dome::domeStop ()
{
deleteTimers (EVENT_DOMECLOSE_RETRY);
int ret = stop ();
if (ret < 0)
{
logStream (MESSAGE_ERROR) << "stopping dome movement failed" << sendLog;
return -1;
}
maskState (DOME_STOPPED_MASK, DOME_STOPPED, "dome movement stopped");
return 0;
}
bool Dome::isGoodWeather ()
{
if (getIgnoreMeteo () == true)
return true;
if (Device::isGoodWeather () == false)
return false;
if (getNextOpen () > getNow ())
return false;
return true;
}
int Dome::checkOpening ()
{
if ((getState () & DOME_DOME_MASK) == DOME_OPENING)
{
long ret;
ret = isOpened ();
if (ret >= 0)
{
setTimeout (ret);
return 0;
}
if (ret == -1)
{
setTimeout (2 * USEC_SEC); // this can be "overloaded" by a value in local endOpen ();
endOpen ();
infoAll ();
logStream (MESSAGE_CRITICAL | MESSAGE_REPORTIT) << "dome opening interrupted" << sendLog;
maskState (DOME_DOME_MASK | BOP_EXPOSURE | DEVICE_BLOCK_OPEN | DEVICE_BLOCK_CLOSE, DOME_OPENED | DEVICE_BLOCK_CLOSE, "dome opening interrupted");
}
if (ret == -2)
{
setTimeout (2 * USEC_SEC); // this can be "overloaded" by a value in local endOpen ();
ret = endOpen ();
infoAll ();
if (ret)
{
logStream (MESSAGE_CRITICAL | MESSAGE_REPORTIT) << "dome did not open propely" << sendLog;
maskState (DOME_DOME_MASK | BOP_EXPOSURE | DEVICE_BLOCK_OPEN | DEVICE_BLOCK_CLOSE, DOME_OPENED | DEVICE_BLOCK_CLOSE, "dome did not open properly");
}
else
{
maskState (DOME_DOME_MASK | BOP_EXPOSURE | DEVICE_BLOCK_OPEN | DEVICE_BLOCK_CLOSE, DOME_OPENED | DEVICE_BLOCK_CLOSE, "dome opened");
logStream (MESSAGE_REPORTIT | MESSAGE_INFO) << "dome opened" << sendLog;
}
}
}
else if ((getState () & DOME_DOME_MASK) == DOME_CLOSING)
{
long ret;
ret = isClosed ();
if (ret >= 0)
{
setTimeout (ret);
return 0;
}
if (ret == -1)
{
setTimeout (10 * USEC_SEC); // this can be "overloaded" by a value in local endClose ();
endClose ();
infoAll ();
logStream (MESSAGE_CRITICAL | MESSAGE_REPORTIT) << "dome closing interrupted" << sendLog;
maskState (DOME_DOME_MASK | DEVICE_BLOCK_OPEN | DEVICE_BLOCK_CLOSE, DOME_CLOSED | DEVICE_BLOCK_OPEN, "closing finished with error");
}
if (ret == -2)
{
setTimeout (5 * USEC_SEC); // this can be "overloaded" by a value in local endClose ();
ret = endClose ();
infoAll ();
if (ret)
{
logStream (MESSAGE_CRITICAL | MESSAGE_REPORTIT) << "dome did not close properly" << sendLog;
maskState (DOME_DOME_MASK | DEVICE_BLOCK_OPEN | DEVICE_BLOCK_CLOSE, DOME_CLOSED | DEVICE_BLOCK_OPEN, "dome did not close propely");
}
else
{
maskState (DOME_DOME_MASK | DEVICE_BLOCK_OPEN | DEVICE_BLOCK_CLOSE, DOME_CLOSED | DEVICE_BLOCK_OPEN, "dome closed");
logStream (MESSAGE_INFO) << "dome closed" << sendLog;
}
}
}
// if we are back in idle state..beware of copula state (bit non-structural, but I
// cannot find better solution)
if ((getState () & DOME_CUP_MASK_MOVE) == DOME_CUP_NOT_MOVE)
setTimeout (10 * USEC_SEC);
return 0;
}
int Dome::idle ()
{
if (getNow () < initialSettleTime) // give connections time to settle down
return Device::idle ();
checkOpening ();
if (isGoodWeather ())
{
if (weatherOpensDome->getValueBool () == true && (getMasterState () & SERVERD_ONOFF_MASK) == SERVERD_STANDBY)
{
setMasterOn ();
}
}
else if (isClosed () != -2)
{
int ret;
ret = closeDomeWeather ();
if (ret == -1)
{
setTimeout (10 * USEC_SEC);
}
}
// update our own weather state..
bool allCen = allCentraldRunning ();
if (allCen)
centraldLastContactTime = getNow ();
if ((allCen || centraldLastContactTime + DEF_CENTRALD_CONTACT_TIMEOUT > getNow ()) && getNextOpen () < getNow ())
{
valueGood (nextGoodWeather);
setWeatherState (true, "can open dome");
}
else
{
if (allCen)
setWeatherState (false, "waiting for next_open");
else
setWeatherState (false, "some centrald are not connected");
}
return Device::idle ();
}
void Dome::postEvent (rts2core::Event * event)
{
switch (event->getType ())
{
case EVENT_DOMECLOSE_RETRY:
logStream (MESSAGE_DEBUG) << "postEvent: EVENT_DOMECLOSE_RETRY" << sendLog;
domeCloseStart ();
break;
}
rts2core::Device::postEvent (event);
}
int Dome::closeDomeWeather ()
{
int ret = 0;
if (getIgnoreMeteo () == false)
{
if (domeAutoClose == NULL || domeAutoClose->getValueBool () == true)
{
logStream (MESSAGE_DEBUG) << "closeDomeWeather: domeCloseStart" << sendLog;
ret = domeCloseStart ();
}
setMasterStandby ();
return ret;
}
return 0;
}
int Dome::observing ()
{
if (domeAutoClose == NULL || domeAutoClose->getValueBool () == true)
return domeOpenStart ();
return 0;
}
int Dome::standby ()
{
ignoreTimeout->setValueDouble (getNow () - 1);
if (domeAutoClose == NULL || domeAutoClose->getValueBool () == true)
{
logStream (MESSAGE_DEBUG) << "standby: domeCloseStart" << sendLog;
return domeCloseStart ();
}
return 0;
}
int Dome::off ()
{
ignoreTimeout->setValueDouble (getNow () - 1);
if (domeAutoClose == NULL || domeAutoClose->getValueBool () == true)
{
logStream (MESSAGE_DEBUG) << "off: domeCloseStart" << sendLog;
return domeCloseStart ();
}
return 0;
}
int Dome::setMasterStandby ()
{
if (!(getMasterState () & SERVERD_ONOFF_MASK) && ((getMasterState () & SERVERD_STATUS_MASK) != SERVERD_UNKNOW))
{
return sendMasters ("standby");
}
return 0;
}
int Dome::setMasterOn ()
{
if ((getMasterState () & SERVERD_ONOFF_MASK) == SERVERD_STANDBY)
{
return sendMasters ("on");
}
return 0;
}
void Dome::changeMasterState (rts2_status_t old_state, rts2_status_t new_state)
{
// detect state changes triggered by bad weather
if ((old_state & WEATHER_MASK) == GOOD_WEATHER && (new_state & WEATHER_MASK) == BAD_WEATHER && getIgnoreMeteo () == true)
{
logStream (MESSAGE_INFO) << "ignoring bad weather trigerred state change" << sendLog;
}
else if ((new_state & SERVERD_ONOFF_MASK) == SERVERD_STANDBY)
{
switch (new_state & SERVERD_STATUS_MASK)
{
case SERVERD_EVENING:
case SERVERD_DUSK:
case SERVERD_NIGHT:
case SERVERD_DAWN:
standby ();
break;
default:
off ();
}
}
// HARD_OFF or SOFT_OFF states - STANDBY is above
else if (new_state & SERVERD_ONOFF_MASK)
{
off ();
}
else
{
switch (new_state & SERVERD_STATUS_MASK)
{
case SERVERD_DUSK:
case SERVERD_NIGHT:
case SERVERD_DAWN:
observing ();
break;
case SERVERD_EVENING:
case SERVERD_MORNING:
standby ();
break;
default:
off ();
}
}
Device::changeMasterState (old_state, new_state);
}
void Dome::setIgnoreTimeout (time_t _ignore_time)
{
time_t now;
time (&now);
ignoreTimeout->setValueDouble (getNow () + _ignore_time);
}
bool Dome::getIgnoreMeteo ()
{
return ignoreTimeout->getValueDouble () > getNow ();
}
void Dome::setWeatherTimeout (time_t wait_time, const char *msg)
{
time_t next;
time (&next);
next += wait_time;
if (next > nextGoodWeather->getValueInteger ())
{
valueError (nextGoodWeather);
setWeatherState (false, msg);
nextGoodWeather->setValueInteger (next);
sendValueAll (nextGoodWeather);
}
}
int Dome::commandAuthorized (rts2core::Connection * conn)
{
if (conn->isCommand (COMMAND_OPEN))
{
return (domeOpenStart () == 0 ? 0 : -2);
}
else if (conn->isCommand (COMMAND_CLOSE))
{
logStream (MESSAGE_DEBUG) << "commandAuthorized: COMMAND_CLOSE" << sendLog;
return (domeCloseStart () == 0 ? 0 : -2);
}
else if (conn->isCommand (COMMAND_STOP))
{
return (domeStop () == 0 ? 0 : -2);
}
else if (conn->isCommand ("close_for"))
{
double next_good;
if (conn->paramNextDouble (&next_good) || !conn->paramEnd())
return -2;
if (next_good < 0)
return -2;
nextGoodWeather->setValueDouble (getNow () + next_good);
valueError (nextGoodWeather);
return (domeCloseStart () == 0 ? 0 : -2);
}
else if (conn->isCommand ("reset_next"))
{
nextGoodWeather->setValueDouble (getNow () - 1);
valueGood (nextGoodWeather);
return 0;
}
return Device::commandAuthorized (conn);
}