Blame view

src/majordome/tasks.py 39.1 KB
5b5566ab   haribo   added celery
1
from __future__ import absolute_import
ce470283   Jeremy   Plc simulator fin...
2

5b5566ab   haribo   added celery
3
from celery.task import Task
ce470283   Jeremy   Plc simulator fin...
4
5
6
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import Q

9774228b   haribo   Date: 22/06/2016
7
import observation_manager
abfb02e2   Jeremy   Device Model is n...
8
import observation_manager.tasks
ce470283   Jeremy   Plc simulator fin...
9
10
11
import scheduler
import scheduler.tasks as sched_task
import utils.Logger as L
c72eb17a   Jeremy   Update celery task
12
13
import monitoring.tasks
import alert_manager.tasks
ce470283   Jeremy   Plc simulator fin...
14
from common.models import *
6aec6155   theopuhl   Majordome algorit...
15
16
17
from dashboard.views import get_sunelev
from devices.TelescopeRemoteControlDefault import TelescopeRemoteControlDefault
from django.shortcuts import get_object_or_404
65149de7   Jeremy   Update
18
from devices.CameraNIR import NIRCameraController
ce470283   Jeremy   Plc simulator fin...
19
from devices.CameraVIS import VISCameraController
cfc9d09c   Jeremy   Added dome simula...
20
from devices.Dome import DomeController
65149de7   Jeremy   Update
21
from devices.PLC import PLCController
ce470283   Jeremy   Plc simulator fin...
22
23
from devices.Telescope import TelescopeController
from majordome.MajordomeDecorators import *
65149de7   Jeremy   Update
24
from utils.JDManipulator import *
6aec6155   theopuhl   Majordome algorit...
25
import time
d51386bf   Etienne Pallier   new file majordom...
26
27
from django.conf import settings as djangosettings

cfc9d09c   Jeremy   Added dome simula...
28

03814884   Etienne Pallier   launch majordome ...
29
30
from threading import Thread

ff448d43   Jeremy   Update
31
DEBUG_FILE = False
65149de7   Jeremy   Update
32
log = L.setupLogger("MajordomeTaskLogger", "Majordome")
5b5566ab   haribo   added celery
33

65149de7   Jeremy   Update
34
'''
05038bc8   Jeremy   Majordome logic i...
35
    Task to handle the execution of the program
65149de7   Jeremy   Update
36

4cb0ff36   Jeremy   Update
37
    check the environment status in database
05038bc8   Jeremy   Majordome logic i...
38
39
40
    check the devices status (telescope / cameras)
    check if the last schedule made has to be planned
    launch schedule's sequences
65149de7   Jeremy   Update
41
'''
3001ec20   Etienne Pallier   majordome small fix
42
43
44
45
46


class Majordome(Task):
# (EP) do this so that Majordome can be run from a thread, and called with thread.start():
#class Majordome(Task, Thread):
0997e1c3   Etienne Pallier   moved main run() ...
47
    
65149de7   Jeremy   Update
48
49
    loop_speed = 1
    julian_div = 86400
05038bc8   Jeremy   Majordome logic i...
50
51
52
53
54
    executing_sequence = None
    next_sequence = None
    status_tel = ""
    status_nir = ""
    status_vis = ""
cfc9d09c   Jeremy   Added dome simula...
55
    status_dom = ""
ce470283   Jeremy   Plc simulator fin...
56
57
    site_status = "OK"
    weather_status = "OK"
ef60c3ec   Jeremy   Majordome and mon...
58
59
60
    timers = {}
    functions = {}
    schedule = None
c53a13e0   Jeremy   Updating a lot of...
61
    available_status = []
678838ed   Jeremy   Weather ans insid...
62
    alarm_list = []
468a79dc   Etienne Pallier   added comments
63
64

    # New variables from TP:
d0b663be   Etienne Pallier   refactorized majo...
65
    
6aec6155   theopuhl   Majordome algorit...
66
    config = None
468a79dc   Etienne Pallier   added comments
67
    plc_status = None
0ca00ebd   Etienne Pallier   refactorized and ...
68
    current_state = "Starting"
d0b663be   Etienne Pallier   refactorized majo...
69
70
71
72
    
    # OCS-RESTART, OCS-SHUTDOWN, or NONE (default)
    closing_mode = "NONE"
    
0ca00ebd   Etienne Pallier   refactorized and ...
73
74
75
76
    NEED_TO_CLOSE = False
    PLC_IS_SAFE = False

    
6aec6155   theopuhl   Majordome algorit...
77
    '''
0997e1c3   Etienne Pallier   moved main run() ...
78
79
80
81
82
83
84
85
86
87
        OLD //// Function called by celery task
        Behavior:
            Init telescope / cameras
            set night limits
            check the software version
            launch the majordome loop ///// OLD

        Main loop of Majordome
        startup everything if needed
        Loop over Majordome state
d0b663be   Etienne Pallier   refactorized majo...
88
        Change behavior with the pyros states
0997e1c3   Etienne Pallier   moved main run() ...
89
90
91
92
93
94
95
96
97
98
99
        Take suitable decision thanks to states in database
    '''
    def run(self):
        #self.createTask()
        #self.updateSoftware()
        #self.setContext()
        #self.setTime()                     /// OLD Majordome
        #self.setTasks()
        #self.loop()


d51386bf   Etienne Pallier   new file majordom...
100
101
        print("DB3 used is:", djangosettings.DATABASES['default']['NAME'])

0ca00ebd   Etienne Pallier   refactorized and ...
102
        while True:
d0b663be   Etienne Pallier   refactorized majo...
103
104
            
            # SETUP
1179175f   Etienne Pallier   Majordome agent c...
105
            try :
d0b663be   Etienne Pallier   refactorized majo...
106
                self.config = get_object_or_404(Config, id=1)
03814884   Etienne Pallier   launch majordome ...
107
108
109
110
111
112
113
114
                #self.config = Config.objects.get(pk=1)
                #self.config = Config.objects.get()[0]

                #print("maj config id is", self.config.id)

            #except Config.ObjectDoesNotExist:
            except Exception as e:
                print("Config read exception", str(e))
d0b663be   Etienne Pallier   refactorized majo...
115
116
                return -1
            
0ca00ebd   Etienne Pallier   refactorized and ...
117
            #self.config.ntc = False
d0b663be   Etienne Pallier   refactorized majo...
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
            
            # Set STATE to STARTING
            self.changeState("STARTING")
            
            '''
            if not self.config.majordome_restarted:
                #in order to start other agents etc....
                self.setup()
            
            # Set STATE to PASSIVE (NO PLC)
            self.changeState("Passive No PLC")
            print("Waiting for PLC connection")
            # Wait until PCL is connected
            while not self.plc_is_connected():                  
                pass           
            print("PLC is connected")
                    
            # Set STATE to PASSIVE (with PLC)
            self.changeState("Passive")
            self.config.majordome_state = "RUNNING"
    
            # MAIN LOOP: iterate on current state
            #while self.plc_is_connected() != False and self.closing_mode == "RUNNING" :          
0ca00ebd   Etienne Pallier   refactorized and ...
141
            while self.plc_is_connected() and not self.is_shuttingdown_or_restarting():          
d0b663be   Etienne Pallier   refactorized majo...
142
143
144
145
146
147
                # Call behavior for the current state and set new current state (can stay the same if no state change)
                #self.behavior[self.current_state](self)
                self.current_state = self.behavior[self.current_state](self)
                time.sleep(2)
            '''              
            while True:
1179175f   Etienne Pallier   Majordome agent c...
148
149
                self.reload_config()
                if self.config.majordome_state == "STOP": break
0ca00ebd   Etienne Pallier   refactorized and ...
150
151
152
                if self.current_state in ['STARTING', 'PASSIVE_NO_PLC', 'PASSIVE']:
                    if self.is_shuttingdown_or_restarting() and not self.NEED_TO_CLOSE: break
                '''
d0b663be   Etienne Pallier   refactorized majo...
153
                if self.current_state not in ['STARTING', 'PASSIVE_NO_PLC']:
0ca00ebd   Etienne Pallier   refactorized and ...
154
155
                    if self.plc_is_not_auto(): self.changeState("PASSIVE")
                '''        
03814884   Etienne Pallier   launch majordome ...
156
                self.current_state = self.do_behavior_for_current_state()
1179175f   Etienne Pallier   Majordome agent c...
157
                    
d0b663be   Etienne Pallier   refactorized majo...
158
            # Shutdown options change by the main program
1179175f   Etienne Pallier   Majordome agent c...
159
160
            if self.config.majordome_state == "STOP" or self.closing_mode == "OCS-SHUTDOWN":
                if self.config.majordome_state == "STOP":
d51386bf   Etienne Pallier   new file majordom...
161
                    print("STOP Majordome agent from DB")
1179175f   Etienne Pallier   Majordome agent c...
162
163
164
                    self.config.majordome_state = "RUNNING"
                    self.config.save()
                #self.shutdown()
d0b663be   Etienne Pallier   refactorized majo...
165
                #self.closeBehaviour(self)
0ca00ebd   Etienne Pallier   refactorized and ...
166
167
                #self.sub_behavior_closing()
                break
d0b663be   Etienne Pallier   refactorized majo...
168
            
0ca00ebd   Etienne Pallier   refactorized and ...
169
            if self.closing_mode == "OCS-RESTART":
d0b663be   Etienne Pallier   refactorized majo...
170
171
172
                self.config.majordome_restarted = True
                self.config.save()
                #TODO: self.kill_all_agents()
0ca00ebd   Etienne Pallier   refactorized and ...
173
                #self.send_alarm_if_not_closed()
d0b663be   Etienne Pallier   refactorized majo...
174
                #self.run()
0ca00ebd   Etienne Pallier   refactorized and ...
175
176

            '''    
d0b663be   Etienne Pallier   refactorized majo...
177
178
179
180
181
182
            elif not self.plc_is_connected():
                #self.config.majordome_restarted = True
                #self.config.save()
                self.changeState("Passive No PLC")
                self.send_alarm_if_not_closed()
                #self.run()
0ca00ebd   Etienne Pallier   refactorized and ...
183
184
185
186
            '''
        
        self.shutdown()
 
0997e1c3   Etienne Pallier   moved main run() ...
187
 
d0b663be   Etienne Pallier   refactorized majo...
188
 
0ca00ebd   Etienne Pallier   refactorized and ...
189
190
191
192
193
194
195
196
197
    def shutdown(self):
        #TODO: write shutdown code (kill agents...)
        print("OCS SHUTDOWN")
    
    def is_restarting(self):
        return self.closing_mode == "OCS-RESTART"
    def is_shuttingdown(self):
        return self.closing_mode == "OCS-SHUTDOWN"
    def is_shuttingdown_or_restarting(self):
d0b663be   Etienne Pallier   refactorized majo...
198
199
        return self.closing_mode != "NONE"
    
0997e1c3   Etienne Pallier   moved main run() ...
200
201

    '''
6aec6155   theopuhl   Majordome algorit...
202
203
204
        Function called to change and save current state of pyros.
    '''
    def changeState(self, state):
468a79dc   Etienne Pallier   added comments
205
        # (local variable) idem self.config.pyros_state
1b9ba1f6   Quentin Durand   Little update on ...
206
        Log.objects.create(message="Go from " + self.current_state + " state to " + state + " state", agent="Majordome")
6aec6155   theopuhl   Majordome algorit...
207
        self.current_state = state
468a79dc   Etienne Pallier   added comments
208
209

        # (in DB) PASSIVE, STANDBY, REMOTE, OPERATING
6aec6155   theopuhl   Majordome algorit...
210
211
        self.config.pyros_state = state
        self.config.save()
d0b663be   Etienne Pallier   refactorized majo...
212
        
0ca00ebd   Etienne Pallier   refactorized and ...
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
    def _plc_is_not_auto(self):
        if not self.plc_is_connected(): return True;
        # now, self.plc_status has been updated, so check it:
        return self.plc_status.plc_mode != "AUTO"
    def plc_is_auto(self):
        return not self._plc_is_not_auto()
    
    def plc_is_safe(self):
        if not self.plc_is_connected(): return False;
        # now, self.plc_status has been updated, so check it:
        return self.plc_status.is_safe
        
    
    def is_night(self):
        return get_sunelev() < -10
d0b663be   Etienne Pallier   refactorized majo...
228
        
0ca00ebd   Etienne Pallier   refactorized and ...
229

6aec6155   theopuhl   Majordome algorit...
230
    '''
d0b663be   Etienne Pallier   refactorized majo...
231
232
        Each function with behavior describes the behavior of a state,
        they are all contained in a dictionary called in run()
6aec6155   theopuhl   Majordome algorit...
233
    '''
0ca00ebd   Etienne Pallier   refactorized and ...
234

d0b663be   Etienne Pallier   refactorized majo...
235
236
    def behavior_starting(self):
        #print("STARTING")
0ca00ebd   Etienne Pallier   refactorized and ...
237
        #if self.is_shuttingdown_or_restarting(): return
d0b663be   Etienne Pallier   refactorized majo...
238
239
240
241
242
243
244
245
246
247
248
249
250
251
        if not self.config.majordome_restarted:
            #TODO: Do setup things... (start agents...)
            time.sleep(2)
        '''
        Cette variable sert à indiquer l'état du majordome dans la DB.
        Elle va aussi être changée par le panneau Simulator pour passer à l'état SHUTDOWN ou RESTART.
        Elle est récupérée à chaque itération par la fonction plcConnection()
        '''
        self.config.majordome_state = "RUNNING"
        #self.config.majordome_state = "NONE"

        self.changeState("PASSIVE_NO_PLC")
        #return self.current_state

0ca00ebd   Etienne Pallier   refactorized and ...
252

d0b663be   Etienne Pallier   refactorized majo...
253
254
    def behavior_passive_no_plc(self):
        #print("PASSIVE_NO_PLC")
0ca00ebd   Etienne Pallier   refactorized and ...
255
        #if self.is_shuttingdown_or_restarting(): return
d0b663be   Etienne Pallier   refactorized majo...
256
257
258
259
        #self.changeState("Passive No PLC")
        print("Waiting for PLC connection")
        # Wait until PCL is connected
        while not self.plc_is_connected():
03814884   Etienne Pallier   launch majordome ...
260
            time.sleep(3)
0ca00ebd   Etienne Pallier   refactorized and ...
261
262
263
264
265
            '''
            if self.is_restarting():
                self.changeState("STARTING")
                return
            '''
1179175f   Etienne Pallier   Majordome agent c...
266
267
            self.reload_config()
            if self.config.majordome_state == "STOP" or self.is_shuttingdown_or_restarting(): return
d0b663be   Etienne Pallier   refactorized majo...
268
269
        print("PLC is connected")
        # Set STATE to PASSIVE (with PLC)
d51386bf   Etienne Pallier   new file majordom...
270
        self.changeState("PASSIVE")
d0b663be   Etienne Pallier   refactorized majo...
271
272
        #return self.current_state
        
0ca00ebd   Etienne Pallier   refactorized and ...
273
        
d0b663be   Etienne Pallier   refactorized majo...
274
275
    def behavior_passive(self):
        #print("Passive")
0ca00ebd   Etienne Pallier   refactorized and ...
276
277
278
279
280
281
282
283
284
        #if self.is_shuttingdown_or_restarting(): return
        #if self.plc_status.plc_mode == "AUTO":
        if not self.plc_is_connected(): 
            self.changeState("PASSIVE_NO_PLC")
            return
        
        if self.plc_is_auto():
            #if not self.config.majordome_restarted: self.config.ntc = True
            if not self.config.majordome_restarted: self.NEED_TO_CLOSE=True
6aec6155   theopuhl   Majordome algorit...
285
            self.changeState("Standby")
d0b663be   Etienne Pallier   refactorized majo...
286
        #return self.current_state
6aec6155   theopuhl   Majordome algorit...
287
            
0ca00ebd   Etienne Pallier   refactorized and ...
288
            
d0b663be   Etienne Pallier   refactorized majo...
289
290
    def behavior_standby(self):
        #print("Standby")
0ca00ebd   Etienne Pallier   refactorized and ...
291
292
293
294
295
        #if self.config.ntc:
        if self.is_shuttingdown() and not self.NEED_TO_CLOSE:
            self.NEED_TO_CLOSE = True
            
        if self.NEED_TO_CLOSE:
6aec6155   theopuhl   Majordome algorit...
296
            self.changeState("Closing")
0ca00ebd   Etienne Pallier   refactorized and ...
297
            #self.sub_behavior_closing()
03814884   Etienne Pallier   launch majordome ...
298
            self.do_behavior_for_current_state()
0ca00ebd   Etienne Pallier   refactorized and ...
299
300
301
302
            self.changeState("Standby")
            
        #elif self.plc_status.plc_mode != "AUTO" or self.is_shuttingdown_or_restarting():
        if not self.plc_is_auto() or self.is_shuttingdown_or_restarting():
d51386bf   Etienne Pallier   new file majordom...
303
            self.changeState("PASSIVE")
0ca00ebd   Etienne Pallier   refactorized and ...
304
        elif not self.config.global_mode and not self.config.lock:
6aec6155   theopuhl   Majordome algorit...
305
            self.changeState("Remote")
0ca00ebd   Etienne Pallier   refactorized and ...
306
307
            
        elif self.is_night() and self.plc_is_safe() and self.config.ack and not self.config.lock :
6aec6155   theopuhl   Majordome algorit...
308
            self.changeState("Startup")
d0b663be   Etienne Pallier   refactorized majo...
309
        #return self.current_state
6aec6155   theopuhl   Majordome algorit...
310

0ca00ebd   Etienne Pallier   refactorized and ...
311

d0b663be   Etienne Pallier   refactorized majo...
312
313
    def behavior_remote(self):
        #print("Remote")
0ca00ebd   Etienne Pallier   refactorized and ...
314
315
316
        if self.config.global_mode or self.is_shuttingdown_or_restarting() or not self.plc_is_auto() or self.config.lock:
            if not self.plc_is_auto() or self.config.lock or self.is_shuttingdown():
                self.NEED_TO_CLOSE=True
6aec6155   theopuhl   Majordome algorit...
317
            self.changeState("Standby")
0ca00ebd   Etienne Pallier   refactorized and ...
318
319
320
321
322
323
324
325
326
            return
        
        #TODO: get shutter state from db status (then wait until close/open)
        if not self.plc_is_safe() and self.PLC_IS_SAFE:
            PLC_IS_SAFE = False
            response = TelescopeRemoteControlDefault("DOME SHUTTER CLOSE", expert_mode=True).exec_command()
        if self.plc_is_safe() and not self.PLC_IS_SAFE:
            PLC_IS_SAFE = True
            response = TelescopeRemoteControlDefault("DOME SHUTTER OPEN", expert_mode=True).exec_command()
d0b663be   Etienne Pallier   refactorized majo...
327
        #return self.current_state
6aec6155   theopuhl   Majordome algorit...
328

0ca00ebd   Etienne Pallier   refactorized and ...
329

d0b663be   Etienne Pallier   refactorized majo...
330
    def behavior_startup(self):
d0b663be   Etienne Pallier   refactorized majo...
331
        #print("Startup")
0ca00ebd   Etienne Pallier   refactorized and ...
332
333
        if self.config.lock or not self.plc_is_auto():
            self.NEED_TO_CLOSE=True
6aec6155   theopuhl   Majordome algorit...
334
            self.changeState("Standby")
0ca00ebd   Etienne Pallier   refactorized and ...
335
336
337
338
339
340
341
342
343
344
            return
        
        #TODO: get shutter and telescope from db status
        time.sleep(5)
        '''
        if not (dome_shutter == Open and telescope == Ready)
            TelescopeRemoteControlDefault("DO DOME SHUTTER OPEN", expert_mode=True).exec_command()
            TelescopeRemoteControlDefault("DO START", expert_mode=True).exec_command()
        '''
        self.changeState("Scheduler")
d0b663be   Etienne Pallier   refactorized majo...
345
        #return self.current_state
6aec6155   theopuhl   Majordome algorit...
346
347
        

d0b663be   Etienne Pallier   refactorized majo...
348
349
    def behavior_scheduler(self):
        #print("Scheduler")
0ca00ebd   Etienne Pallier   refactorized and ...
350
351
        if not self.is_night() or not self.plc_status.is_safe or self.config.lock or not self.plc_is_auto() or not self.config.global_mode or self.is_shuttingdown_or_restarting():
            if not ( not self.config.global_mode or self.is_restarting() ): self.NEED_TO_CLOSE = True
6aec6155   theopuhl   Majordome algorit...
352
            self.changeState("Standby")
d0b663be   Etienne Pallier   refactorized majo...
353
        #return self.current_state
6aec6155   theopuhl   Majordome algorit...
354

0ca00ebd   Etienne Pallier   refactorized and ...
355
356
357

    def sub_behavior_closing(self):
        #print("CURRENT OCS (MAJORDOME) STATE: "+ self.current_state)
d0b663be   Etienne Pallier   refactorized majo...
358
        #print("Closing")
0ca00ebd   Etienne Pallier   refactorized and ...
359
360
361
362
363
364
365
366
367
368
369
370
371
        #self.config.save()
        if not self.plc_is_auto():
            # PLC not AUTO => we can do nothing, so only send email if dome not closed...
            self.send_alarm_if_not_closed()
        else:
            # These commands should do nothing if instruments are already closed/parked...
            response = TelescopeRemoteControlDefault("DO DOME SHUTTER CLOSE", expert_mode=True).exec_command()
            response = TelescopeRemoteControlDefault("DO DOME PARK", expert_mode=True).exec_command()
            response = TelescopeRemoteControlDefault("DO TELESCOPE PARK", expert_mode=True).exec_command()

        self.NEED_TO_CLOSE = False

        #self.changeState("Standby")
d0b663be   Etienne Pallier   refactorized majo...
372
373
374
375
376
        #return self.current_state

    behavior = {
        "STARTING": behavior_starting,
        "PASSIVE_NO_PLC": behavior_passive_no_plc,
d51386bf   Etienne Pallier   new file majordom...
377
        "PASSIVE": behavior_passive,
d0b663be   Etienne Pallier   refactorized majo...
378
379
380
381
        "Standby": behavior_standby,
        "Remote": behavior_remote,
        "Startup": behavior_startup,
        "Scheduler": behavior_scheduler,
0ca00ebd   Etienne Pallier   refactorized and ...
382
        #"Closing": sub_behavior_closing,
d0b663be   Etienne Pallier   refactorized majo...
383
    }
03814884   Etienne Pallier   launch majordome ...
384
385
    def do_behavior_for_current_state(self) -> str:
        print("CURRENT OCS (MAJORDOME) STATE: " + self.current_state)
0ca00ebd   Etienne Pallier   refactorized and ...
386
        time.sleep(2)
d0b663be   Etienne Pallier   refactorized majo...
387
388
389
        # EXIT if PLC not connected
        #if not self.plc_is_connected(): return                  
        # EXIT if closing or restarting
0ca00ebd   Etienne Pallier   refactorized and ...
390
        #if self.is_shuttingdown_or_restarting(): return
d0b663be   Etienne Pallier   refactorized majo...
391
392
        # run behavior for this state
        #self.behavior[current_state](self)
03814884   Etienne Pallier   launch majordome ...
393
        if self.current_state == "Closing": 
0ca00ebd   Etienne Pallier   refactorized and ...
394
395
            self.sub_behavior_closing()
        else:
03814884   Etienne Pallier   launch majordome ...
396
397
            self.behavior[self.current_state](self)
        return self.current_state
468a79dc   Etienne Pallier   added comments
398

0997e1c3   Etienne Pallier   moved main run() ...
399
           
6aec6155   theopuhl   Majordome algorit...
400
    '''
6aec6155   theopuhl   Majordome algorit...
401
402
        Function called to send alarms is the site isn't closed (Empty for the moment)
    '''
d0b663be   Etienne Pallier   refactorized majo...
403
    def send_alarm_if_not_closed(self):
6aec6155   theopuhl   Majordome algorit...
404
405
        pass

0997e1c3   Etienne Pallier   moved main run() ...
406

6aec6155   theopuhl   Majordome algorit...
407
408
409
410
    '''
        Function called by run to wait for PLC connection before and during the loop
        (also gathering needed info)
    '''
d0b663be   Etienne Pallier   refactorized majo...
411
    def plc_is_connected(self):
6aec6155   theopuhl   Majordome algorit...
412
413
414
        try :
            self.config = Config.objects.get(pk=1)
            self.plc_status = PlcDeviceStatus.objects.exclude(plc_mode=None).latest('created')
6aec6155   theopuhl   Majordome algorit...
415
            timeout = (datetime.datetime.now() - self.plc_status.created).total_seconds()
d0b663be   Etienne Pallier   refactorized majo...
416
            if timeout >= self.config.plc_timeout_seconds:
6aec6155   theopuhl   Majordome algorit...
417
418
                return (False)
            return (True)
d0b663be   Etienne Pallier   refactorized majo...
419
        
6aec6155   theopuhl   Majordome algorit...
420
421
422
423
        except Config.DoesNotExist or PlcDeviceStatus.DoesNotExist:
            return (False)


1179175f   Etienne Pallier   Majordome agent c...
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
    def reload_config(self):
        self.config = Config.objects.get(pk=1)
        '''
        try :
            self.config = Config.objects.get(pk=1)
        except Config.DoesNotExist:
            return (False)
        return self.config
        '''
            






""" OLD MAJORDOME CODE
6aec6155   theopuhl   Majordome algorit...
441
442


e8e6f017   Jeremy   Reworked devices ...
443
444
445
446
447
448
449
450
    '''
        Check if the instrument status is valid
    '''
    def isValidStatus(self, status):
        # TODO REMOVE COMMENT AND CHANGE WHEN DEFINED
        # if (status == "" or status == "ERROR" or status == "FAILED" or status == "NOT_SET"):
        #     return (False)
        return (True)
65149de7   Jeremy   Update
451
452
453
454
455
456

    def setContext(self):
        self.tel = TelescopeController()
        self.vis_camera = VISCameraController()
        self.nir_camera = NIRCameraController()
        self.plc = PLCController()
cfc9d09c   Jeremy   Added dome simula...
457
        self.dom = DomeController()
65149de7   Jeremy   Update
458
        return (0)
65149de7   Jeremy   Update
459

c72eb17a   Jeremy   Update celery task
460
461
    def createTask(self):
        try:
1a2dc19f   Etienne Pallier   nombreux bugfixes...
462
463
464
            # (EP) NO find() method available from Django ORM !!!! 
            #TaskId.objects.find(task="majordome").delete()
            TaskId.objects.filter(task="majordome").delete()
c72eb17a   Jeremy   Update celery task
465
466
467
468
469
470
471
472
473
474
475
476
477
478
        except Exception as e:
            log.info(str(e))
            return 1
        TaskId.objects.create(task_id=self.request.id, task="majordome")
        return 0

    def setTasks(self):
        try:
            self.monitoring_task = TaskId.objects.get(task="monitoring")
            self.alert_task = TaskId.objects.get(task="alert_manager")
        except Exception as e:
            self.monitoring_task = None
            self.alert_task = None
        return 0
65149de7   Jeremy   Update
479
480
481
482
483
484
485
486
487
488
489
490
491
492
    '''
        Reads the softwares versions in the settings.py, store them in the DB and send them to the IC.
    '''
    def updateSoftware(self):
        versions = settings.MODULES_VERSIONS
        for module, version in versions.items():
            same_module_versions = Version.objects.filter(module_name=module)
            if same_module_versions.count() == 0:
                Version.objects.create(module_name=module, version=version)
            elif same_module_versions.order_by("-created")[0].version != version:
                Version.objects.create(module_name=module, version=version)
        return (0)

    '''
ce470283   Jeremy   Plc simulator fin...
493
        Loop to wait for the device to be idle with the starting configurations.
65149de7   Jeremy   Update
494
495
    '''
    def waitDevices(self):
ce470283   Jeremy   Plc simulator fin...
496
497
498
499
500
        while self.status_vis == "" and self.status_tel == "" and self.status_nir == "" and self.status_dom == "":
            self.status_vis = self.vis_camera.getStatus()
            self.status_nir = self.nir_camera.getStatus()
            self.status_tel = self.tel.getStatus()
            self.status_dom = self.dom.getStatus()
65149de7   Jeremy   Update
501
502
503
504
        return (0)

    '''
        Computes the beginning and the end of the following (or current) night
257abe9b   Jeremy   Added comments
505
        set the timers -> maybe put timers in a config file ?
65149de7   Jeremy   Update
506
507
508
509
510
511
    '''
    def setTime(self):
        self.night_start = getNightStart()
        self.night_end = getNightEnd()
        self.night_start_jd = secondsToJulianDate(getNightStart())
        self.night_end_jd = secondsToJulianDate(getNightEnd())
ef60c3ec   Jeremy   Majordome and mon...
512
513
        self.timer_night_start = self.night_start - getCurrentTime()
        self.timer_night_end = self.night_end - getCurrentTime()
65149de7   Jeremy   Update
514
        self.timer_status = 5
c72eb17a   Jeremy   Update celery task
515
        self.tasks_timer = 5
65149de7   Jeremy   Update
516
        self.timer_plc = 2
1aed430d   jeremy   Alert handled + k...
517
        self.alert_timer = 1
65149de7   Jeremy   Update
518
        self.timer_schedule = 1
05038bc8   Jeremy   Majordome logic i...
519
        self.timer_sequence = 1
65149de7   Jeremy   Update
520
521
522
523

        if (self.night_start - 120 > getCurrentTime()):
            self.timer_night_start = self.night_start - 120 - getCurrentTime()
        self.timer_night_end = self.night_end - getCurrentTime()
4cb0ff36   Jeremy   Update
524
525
        if (getCurrentTime() > self.night_start):
            self.adaptTimers()
65149de7   Jeremy   Update
526
527

        self.timers = {
4cb0ff36   Jeremy   Update
528
529
530
531
532
            "status": self.timer_status,
            "environment": self.timer_plc,
            "night_start": self.timer_night_start,
            "night_end": self.timer_night_end,
            "schedule": self.timer_schedule,
c72eb17a   Jeremy   Update celery task
533
534
            "sequence": self.timer_sequence,
            "tasks": self.tasks_timer
4cb0ff36   Jeremy   Update
535
        }
ff448d43   Jeremy   Update
536
        if (settings.DEBUG and DEBUG_FILE):
e8e6f017   Jeremy   Reworked devices ...
537
538
            log.info("Majordome started with timers : " + str(self.timers))
        # Functions called during the loop
4cb0ff36   Jeremy   Update
539
540
541
542
543
544
        self.functions = {
            "status": self.handleStatusTimer,
            "environment": self.handleEnvironmentTimer,
            "night_start": self.handleNightStartTimer,
            "night_end": self.handleNightEndTimer,
            "schedule": self.handleScheduleTimer,
c72eb17a   Jeremy   Update celery task
545
546
            "sequence": self.handleSequenceTimer,
            "tasks": self.handleTasks
65149de7   Jeremy   Update
547
        }
65149de7   Jeremy   Update
548
549
        return (0)

257abe9b   Jeremy   Added comments
550
551
552
    '''
        Function called by the main loop to handle the task event (check monitoring and alert_manager)
    '''
c72eb17a   Jeremy   Update celery task
553
    def handleTasks(self):
05bdcc44   Etienne Pallier   BIG DEMO tests (s...
554
555
        if not settings.USE_CELERY: return 0

c72eb17a   Jeremy   Update celery task
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
        self.timers["tasks"] = self.tasks_timer
        if self.monitoring_task is None:
            try:
                self.monitoring_task = TaskId.objects.get(task="monitoring")
            except Exception as e:
                monitoring.tasks.Monitoring.apply_async()
                if settings.DEBUG and DEBUG_FILE:
                    log.info(str(e))
        if self.alert_task is None:
            try:
                self.alert_task = TaskId.objects.get(task="alert_manager")
            except Exception as e:
                alert_manager.tasks.AlertListener.apply_async()
                if settings.DEBUG and DEBUG_FILE:
                    log.info(str(e))
        return 0

    # TODO adapt timers if the majordome is started during the night or not ?
4cb0ff36   Jeremy   Update
574
575
576
    def adaptTimers(self):
        pass

bca9a283   Jeremy   Reworked the sche...
577
578
579
    def logDB(self, message: str):
        Log.objects.create(agent="Majordome", message=message)

65149de7   Jeremy   Update
580
581
    '''
        Infinite loop according to the majordome behavior
65149de7   Jeremy   Update
582
583
    '''
    def loop(self):
d0b663be   Etienne Pallier   refactorized majo...
584
        while (self.closing_mode != "OCS-SHUTDOWN"):
b4e8963c   Etienne Pallier   Each agent (envmo...
585
            print("(MAJOR): start new iteration")
65149de7   Jeremy   Update
586
587
588
589
            minimal_timer = min(self.timers, key=self.timers.get)
            if (self.timers[minimal_timer] > 0):
                time.sleep(self.timers[minimal_timer])
                self.timers = {key: value - self.timers[minimal_timer] for key, value in self.timers.items()}
65149de7   Jeremy   Update
590
591
            for timer_name, timer_value in self.timers.items():
                if (timer_value <= 0):
4cb0ff36   Jeremy   Update
592
                    if timer_name in self.functions:
bca9a283   Jeremy   Reworked the sche...
593
                        self.logDB("Executing timer " + str(timer_name))
4cb0ff36   Jeremy   Update
594
                        self.functions[timer_name]()
65149de7   Jeremy   Update
595
                    else:
ff448d43   Jeremy   Update
596
                        if (settings.DEBUG and DEBUG_FILE):
4cb0ff36   Jeremy   Update
597
                            log.info("Timer : " + str(timer_name) + "is not known by the Majordome")
bca9a283   Jeremy   Reworked the sche...
598
                        self.logDB("Timer " + str(timer_name) + " unknown")
ff448d43   Jeremy   Update
599
                    if (settings.DEBUG and DEBUG_FILE):
e8e6f017   Jeremy   Reworked devices ...
600
                        log.info("Timer : " + str(timer_name) + " executed")
075082e1   Etienne Pallier   added start of ag...
601
602
            # EP added because loop is too quick (without CELERY)
            if not settings.USE_CELERY: time.sleep(2)
05038bc8   Jeremy   Majordome logic i...
603
604
        return (0)

257abe9b   Jeremy   Added comments
605
606
607
    '''
        Function called by the main loop to handle environment event (PLC info)
    '''
4cb0ff36   Jeremy   Update
608
609
    def handleEnvironmentTimer(self):
        self.timers["environment"] = self.timer_plc
ce470283   Jeremy   Plc simulator fin...
610
        self.handlePLC()
4cb0ff36   Jeremy   Update
611
612
        return (0)

257abe9b   Jeremy   Added comments
613
614
615
    '''
        Function called by the main loop to handle the devices status
    '''
4cb0ff36   Jeremy   Update
616
617
    def handleStatusTimer(self):
        self.timers["status"] = self.timer_status
ce470283   Jeremy   Plc simulator fin...
618
619
620
621
        self.status_tel = self.tel.getStatus()
        self.status_nir = self.nir_camera.getStatus()
        self.status_vis = self.vis_camera.getStatus()
        self.status_dom = self.dom.getStatus()
4cb0ff36   Jeremy   Update
622
        self.handleStatus()
c72eb17a   Jeremy   Update celery task
623
        return 0
4cb0ff36   Jeremy   Update
624

257abe9b   Jeremy   Added comments
625
626
627
    '''
        Function called by the main loop to check if the executing sequence is finished
    '''
4cb0ff36   Jeremy   Update
628
629
    def handleSequenceTimer(self):
        self.timers["sequence"] = self.timer_sequence
ff448d43   Jeremy   Update
630
631
632
        if (self.executing_sequence):
            self.handleSequence(self.executing_sequence[0],
                                self.executing_sequence[1], self.executing_sequence[2])
4cb0ff36   Jeremy   Update
633
634
        return (0)

257abe9b   Jeremy   Added comments
635
636
637
    '''
        Function called by the main loop to check if there is a new schedule and to execute its sequences
    '''
4cb0ff36   Jeremy   Update
638
    def handleScheduleTimer(self):
ef60c3ec   Jeremy   Majordome and mon...
639
        self.timers["schedule"] = self.timer_schedule
4cb0ff36   Jeremy   Update
640
        if (self.isValidStatus(self.status_tel)):
ff448d43   Jeremy   Update
641
            if (self.schedule is None):
ef60c3ec   Jeremy   Majordome and mon...
642
643
644
                try:
                    self.schedule = Schedule.objects.latest('created')
                except ObjectDoesNotExist:
ff448d43   Jeremy   Update
645
                    if (settings.DEBUG and DEBUG_FILE):
ef60c3ec   Jeremy   Majordome and mon...
646
647
                        log.info("No schedule found in database")
                    return (1)
4cb0ff36   Jeremy   Update
648
            else:
ef60c3ec   Jeremy   Majordome and mon...
649
650
651
                try:
                    schedule = Schedule.objects.latest('created')
                except ObjectDoesNotExist:
ff448d43   Jeremy   Update
652
                    if (settings.DEBUG and DEBUG_FILE):
ef60c3ec   Jeremy   Majordome and mon...
653
654
                        log.info("No schedule found in database")
                    return (1)
4cb0ff36   Jeremy   Update
655
656
657
                if (schedule.created != self.schedule.created):
                    self.next_sequence = None
                    self.schedule = schedule
1aed430d   jeremy   Alert handled + k...
658
                    self.firstSequenceIsAlert()
4cb0ff36   Jeremy   Update
659
            if (self.schedule):
c53a13e0   Jeremy   Updating a lot of...
660
661
                shs_list = self.schedule.shs.filter(Q(status=Sequence.PLANNED) |
                                                    Q(status=Sequence.PENDING)).order_by('tsp')
4cb0ff36   Jeremy   Update
662
663
664
665
666
                self.executeSchedule(shs_list)
        else:
            self.notifyTelescopeStatus("scheduler")
        return (0)

257abe9b   Jeremy   Added comments
667
    '''
1aed430d   jeremy   Alert handled + k...
668
669
670
671
672
673
674
675
676
677
678
679
680
681
        Function called by handleScheduleTimer, the purpose is to kill the executing sequence if the
        first sequence in the new planning is from an alert
    '''
    @executingSequenceExist
    def firstSequenceIsAlert(self):
        shs_list = self.schedule.shs.filter(Q(status=Sequence.PLANNED) |
                                            Q(status=Sequence.PENDING)).order_by('tsp')
        if shs_list and shs_list[0].sequence.is_alert:
            if shs_list[0].sequence.created.date() >= (datetime.datetime.now() - datetime.timedelta(seconds=10)).date():
                self.killExecutingSequence()
                return 1
        return 0

    '''
257abe9b   Jeremy   Added comments
682
683
        Function called by the main loop to handle the end of a night
    '''
4cb0ff36   Jeremy   Update
684
    def handleNightEndTimer(self):
ce470283   Jeremy   Plc simulator fin...
685
        self.timers["night_end"] = getNightEnd()
4cb0ff36   Jeremy   Update
686
        if (self.isValidStatus(self.status_tel)):
05bdcc44   Etienne Pallier   BIG DEMO tests (s...
687
688
689
690
691
692
693
694
            #observation_manager.tasks.night_calibrations.apply_async()
            if settings.USE_CELERY:
                print("MJ: call observation_manager WITH CELERY")
                observation_manager.tasks.night_calibrations.apply_async()
            else:
                print("MJ: call observation_manager WITHOUT CELERY")
                observation_manager.tasks.night_calibrations().run()

4cb0ff36   Jeremy   Update
695
696
697
698
        else:
            self.notifyTelescopeStatus("night_end")
        return (0)

257abe9b   Jeremy   Added comments
699
    '''
ed1bf194   Etienne Pallier   bugfix task major...
700
        Function called by the main loop to handle the beginning of a night
257abe9b   Jeremy   Added comments
701
    '''
4cb0ff36   Jeremy   Update
702
703
    def handleNightStartTimer(self):
        self.timers["night_start"] = getNextNightStart()
c72eb17a   Jeremy   Update celery task
704
705
706
707
        if self.isOutsideOk():
            self.dom.open()
            self.vis_camera.open_shutter()
            self.nir_camera.open_shutter()
05bdcc44   Etienne Pallier   BIG DEMO tests (s...
708
709
710
711
712
713
714
715
        #scheduler.tasks.scheduling.apply_async((False, False))
        if settings.USE_CELERY:
            print("MJ: call schedule WITH CELERY")
            scheduler.tasks.scheduling.apply_async((False, False))
        else:
            print("MJ: call schedule WITHOUT CELERY")
            scheduler.tasks.scheduling().run((False, False))

4cb0ff36   Jeremy   Update
716
717
        return (0)

ef60c3ec   Jeremy   Majordome and mon...
718
719
720
    def notifyTelescopeStatus(self, timer_name):
        return (self.notifyDeviceStatus("telescope", timer_name, self.status_tel))

05038bc8   Jeremy   Majordome logic i...
721
    def notifyDeviceStatus(self, device_name, timer_name, status):
ef60c3ec   Jeremy   Majordome and mon...
722
723
        Log.objects.create(agent=device_name, created=datetime.datetime.now(),
                           message="The action : " + str(timer_name) + " has been canceled : Telescope status : " + str(status))
ce470283   Jeremy   Plc simulator fin...
724
        # TODO MAYBE reset some variables and do a scheduling
65149de7   Jeremy   Update
725
726
727
        return (0)

    '''
257abe9b   Jeremy   Added comments
728
        Execute a schedule
65149de7   Jeremy   Update
729
730
    '''
    def executeSchedule(self, shs_list):
c72eb17a   Jeremy   Update celery task
731
        for shs in shs_list:  # shs_list is sorted by tsp
c53a13e0   Jeremy   Updating a lot of...
732
733
734
            if (self.executableSequence(shs.sequence.status) and self.observable(shs.sequence)):
                if self.next_sequence is None:
                    self.setNextSequence(shs, shs.sequence)
678838ed   Jeremy   Weather ans insid...
735
                if self.isExecutable() and self.executing_sequence is None:
c53a13e0   Jeremy   Updating a lot of...
736
737
738
739
740
                    if self.isValidTimer(self.next_sequence[0]):
                        if self.executeSequence(self.next_sequence[0], self.next_sequence[1]) == -1:
                            return -1
                        if self.next_sequence[0] != shs and self.next_sequence[1] != shs.sequence:
                            self.setNextSequence(shs, shs.sequence)
ff448d43   Jeremy   Update
741
                        else:
c53a13e0   Jeremy   Updating a lot of...
742
                            self.next_sequence = None
ff448d43   Jeremy   Update
743
744
                    else:
                        return 0
bca9a283   Jeremy   Reworked the sche...
745
            else:
ff448d43   Jeremy   Update
746
                if (settings.DEBUG and DEBUG_FILE):
bca9a283   Jeremy   Reworked the sche...
747
                    log.info("Sequence cannot be executed : Not observable")
ff448d43   Jeremy   Update
748
749
                self.logDB("Sequence "+shs.sequence.name+" cannot be executed : Not observable")
        return 0
05038bc8   Jeremy   Majordome logic i...
750

4cb0ff36   Jeremy   Update
751
    def observable(self, sequence):
ff448d43   Jeremy   Update
752
753
754
        if (sequence.jd2 - sequence.duration - secondsToPreciseJulianDate(getPreciseCurrentTime()) <= 0):
            return 0
        return 1
4cb0ff36   Jeremy   Update
755

c53a13e0   Jeremy   Updating a lot of...
756
757
758
759
760
    def executableSequence(self, status):
        if status == Sequence.PLANNED or status == Sequence.PENDING:
            return 1
        return 0

257abe9b   Jeremy   Added comments
761
762
763
    '''
        Kill the executing sequence and set its state to cancelled
    '''
678838ed   Jeremy   Weather ans insid...
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
    @executingSequenceExist
    def killExecutingSequence(self):
        shs = self.executing_sequence[0]
        sequence = self.executing_sequence[1]
        executing_plans = self.executing_sequence[2]

        shs.status = Sequence.CANCELLED
        sequence.status = Sequence.CANCELLED
        shs.save()
        sequence.save()
        for rev in executing_plans:
            if (not rev.failed() and not rev.successful()):
                rev.revoke(terminate=True)
        self.executing_sequence = None
        return 0

    def reset(self, type):
        if type == "WEATHER":
            self.dom.open()
05bdcc44   Etienne Pallier   BIG DEMO tests (s...
783
784
785
786
787
788
789
790
            #scheduler.tasks.scheduling.delay((False, False))
            if settings.USE_CELERY:
                print("MJ: call schedule WITH CELERY")
                scheduler.tasks.scheduling.delay((False, False))
            else:
                print("MJ: call schedule WITHOUT CELERY")
                scheduler.tasks.scheduling().run((False, False))

678838ed   Jeremy   Weather ans insid...
791
        elif type == "INSIDE":
05bdcc44   Etienne Pallier   BIG DEMO tests (s...
792
793
794
795
796
797
798
            #scheduler.tasks.scheduling.delay((False, False))
            if settings.USE_CELERY:
                print("MJ: call schedule WITH CELERY")
                scheduler.tasks.scheduling.delay((False, False))
            else:
                print("MJ: call schedule WITHOUT CELERY")
                scheduler.tasks.scheduling().run((False, False))
678838ed   Jeremy   Weather ans insid...
799

257abe9b   Jeremy   Added comments
800
801
802
    '''
        Handle a new alarm (called by isInsideOk or isWeatherOk)
    '''
678838ed   Jeremy   Weather ans insid...
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
    @SameAlarmCheck
    def handleAlarm(self, type, pos=-1):
        if type == "WEATHER":
            #TODO send email
            self.dom.close()
            self.killExecutingSequence()
            self.vis_camera.park()
            self.nir_camera.park()
        elif type == "INSIDE":
            #TODO send email
            self.killExecutingSequence()
            self.vis_camera.park()
            self.nir_camera.park()
        elif type == "ENDED":
            if len(self.alarm_list) > 0 and pos != -1:
                ended = self.alarm_list[pos]
                del self.alarm_list[pos]
                self.reset(ended)
            return 0
        else:
            return 1
        self.alarm_list.append(type)
        return 0

    '''
        for now weather_status and site_status contains something different than OK if the status is critical
        Later we may have other states to handle
    '''
ce470283   Jeremy   Plc simulator fin...
831
832
833
    def isOutsideOk(self) -> bool:
        self.handlePLC()
        if self.weather_status == "OK":
678838ed   Jeremy   Weather ans insid...
834
835
            if "WEATHER" in self.alarm_list:
                self.handleAlarm("ENDED", self.alarm_list.index("WEATHER"))
ce470283   Jeremy   Plc simulator fin...
836
            return True
678838ed   Jeremy   Weather ans insid...
837
        self.handleAlarm("WEATHER")
ce470283   Jeremy   Plc simulator fin...
838
839
        return False

257abe9b   Jeremy   Added comments
840
841
842
    '''
        Check the telescope inside status
    '''
ce470283   Jeremy   Plc simulator fin...
843
844
845
    def isInsideOk(self) -> bool:
        self.handlePLC()
        if self.site_status == "OK":
678838ed   Jeremy   Weather ans insid...
846
847
            if "INSIDE" in self.alarm_list:
                self.handleAlarm("ENDED", self.alarm_list.index("INSIDE"))
ce470283   Jeremy   Plc simulator fin...
848
            return True
678838ed   Jeremy   Weather ans insid...
849
        self.handleAlarm("INSIDE")
ce470283   Jeremy   Plc simulator fin...
850
851
852
853
854
855
856
857
858
859
860
861
862
863
        return False

    def isDevicesOk(self) -> bool:
        if self.isValidStatus(self.status_tel) and self.isValidStatus(self.status_dom)\
            and self.isValidStatus(self.status_vis) and self.isValidStatus(self.status_nir):
            return True
        return False

    def isExecutable(self) -> bool:
        if self.isValidStatus(self.status_tel) and self.isValidStatus(self.status_dom)\
                and self.isOutsideOk() and self.isInsideOk():
            return True
        return False

257abe9b   Jeremy   Added comments
864
865
866
    '''
        check if the sequence timer is valid for execution, also check if there is a scheduling task running
    '''
ce470283   Jeremy   Plc simulator fin...
867
    def isValidTimer(self, shs) -> bool:
c53a13e0   Jeremy   Updating a lot of...
868
869
        current_countdown = self.getCountdown(shs)
        if (current_countdown <= JulianSeconds(5)):
c53a13e0   Jeremy   Updating a lot of...
870
871
872
            try:
                task = TaskId.objects.filter(task="scheduling")
                if not task:
ce470283   Jeremy   Plc simulator fin...
873
874
                    return True
                return False
c53a13e0   Jeremy   Updating a lot of...
875
            except:
ce470283   Jeremy   Plc simulator fin...
876
877
                return True
        return False
c53a13e0   Jeremy   Updating a lot of...
878

05038bc8   Jeremy   Majordome logic i...
879
    '''
257abe9b   Jeremy   Added comments
880
        Launch the observation tasks NIR and VIS associated to a sequence
05038bc8   Jeremy   Majordome logic i...
881
    '''
c53a13e0   Jeremy   Updating a lot of...
882
    def executeSequence(self, shs, sequence):
3224f14a   Jeremy   Fixed some simula...
883
884
885
886
        shs.status = Sequence.EXECUTING
        sequence.status = Sequence.EXECUTING
        shs.save()
        sequence.save()
c53a13e0   Jeremy   Updating a lot of...
887
        log.info("Executing sequence id = " + str(sequence.pk))
bca9a283   Jeremy   Reworked the sche...
888
        self.logDB("Executing sequence")
05038bc8   Jeremy   Majordome logic i...
889
        plans_results = []
05038bc8   Jeremy   Majordome logic i...
890
891
892
        if sequence.albums.filter(detector__name="Cagire").exists():
            if (self.isValidStatus(self.status_nir)):
                for plan in sequence.albums.get(detector__name="Cagire").plans.all():
05bdcc44   Etienne Pallier   BIG DEMO tests (s...
893
894
895
896
897
898
899
900
901
902
                    #res = observation_manager.tasks.execute_plan_nir.apply_async((plan.id, float(self.getCountdown(shs))))
                    if settings.USE_CELERY:
                        print("MJ: call observation_manager WITH CELERY")
                        res = observation_manager.tasks.execute_plan_nir.apply_async(
                            (plan.id, float(self.getCountdown(shs))))
                    else:
                        print("MJ: call observation_manager WITHOUT CELERY")
                        res = observation_manager.tasks.execute_plan_nir().run(
                            (plan.id, float(self.getCountdown(shs))))

c53a13e0   Jeremy   Updating a lot of...
903
904
                    # JB TODO : is it still usefull ?
                    # TaskId.objects.create(task_id=res.id, task="execute_plan")
05038bc8   Jeremy   Majordome logic i...
905
                    plans_results.append(res)
ff448d43   Jeremy   Update
906
907
            else:
                self.notifyDeviceStatus("Cagire", "Sequence execution", self.status_nir)
c53a13e0   Jeremy   Updating a lot of...
908
909
910
                sequence.status = Sequence.CANCELLED
                shs.status = Sequence.CANCELLED
                shs.save()
ff448d43   Jeremy   Update
911
912
                sequence.save()
                return (1)
05038bc8   Jeremy   Majordome logic i...
913
914
915
        if sequence.albums.filter(detector__name="Visible camera").exists():
            if (self.isValidStatus(self.status_vis)):
                for plan in sequence.albums.get(detector__name="Visible camera").plans.all():
05bdcc44   Etienne Pallier   BIG DEMO tests (s...
916
917
918
919
920
921
922
923
                    #res = observation_manager.tasks.execute_plan_vis.apply_async((plan.id, float(self.getCountdown(shs))))
                    if settings.USE_CELERY:
                        print("MJ: call observation_manager WITH CELERY")
                        res = observation_manager.tasks.execute_plan_vis.apply_async((plan.id, float(self.getCountdown(shs))))
                    else:
                        print("MJ: call observation_manager WITHOUT CELERY")
                        res = observation_manager.tasks.execute_plan_vis().run((plan.id, float(self.getCountdown(shs))))

05038bc8   Jeremy   Majordome logic i...
924
925
926
                    plans_results.append(res)
            else:
                self.notifyDeviceStatus("Camera visible", "Sequence execution", self.status_vis)
c53a13e0   Jeremy   Updating a lot of...
927
928
929
                sequence.status = Sequence.CANCELLED
                shs.status = Sequence.CANCELLED
                shs.save()
05038bc8   Jeremy   Majordome logic i...
930
931
                sequence.save()
                return (1)
05038bc8   Jeremy   Majordome logic i...
932
        self.executing_sequence = [shs, sequence, plans_results]
05038bc8   Jeremy   Majordome logic i...
933
934
935
936
937
        return (0)

    '''
        Set the next sequence
    '''
c53a13e0   Jeremy   Updating a lot of...
938
    def setNextSequence(self, shs, sequence):
ff448d43   Jeremy   Update
939
        sequence.status = Sequence.PENDING
c53a13e0   Jeremy   Updating a lot of...
940
941
        shs.status = Sequence.PENDING
        self.next_sequence = [shs, sequence]
ff448d43   Jeremy   Update
942
        sequence.save()
c53a13e0   Jeremy   Updating a lot of...
943
        shs.save()
05038bc8   Jeremy   Majordome logic i...
944
945
946
947
948
949
        return (0)

    '''
        Check if the current sequence is finished
    '''
    def handleSequence(self, shs, sequence, executing_plans):
c53a13e0   Jeremy   Updating a lot of...
950
        count = 0
c53a13e0   Jeremy   Updating a lot of...
951
        for res in executing_plans:
05038bc8   Jeremy   Majordome logic i...
952
            try:
c53a13e0   Jeremy   Updating a lot of...
953
954
                if res.successful() or res.failed():
                    count += 1
05038bc8   Jeremy   Majordome logic i...
955
            except Exception as e:
678838ed   Jeremy   Weather ans insid...
956
957
                if DEBUG_FILE and settings.DEBUG:
                    log.info(str(e))
05038bc8   Jeremy   Majordome logic i...
958
959
960
961
962
                shs.status = Sequence.CANCELLED
                sequence.status = Sequence.CANCELLED
                shs.save()
                sequence.save()
                for rev in executing_plans:
c53a13e0   Jeremy   Updating a lot of...
963
964
965
                    if (not rev.failed() and not rev.successful()):
                        rev.revoke(terminate=True)
                self.executing_sequence = None
05038bc8   Jeremy   Majordome logic i...
966
                return (-1)
c53a13e0   Jeremy   Updating a lot of...
967
        if count >= len(executing_plans):
05038bc8   Jeremy   Majordome logic i...
968
969
970
971
972
            sequence.status = Sequence.EXECUTED
            shs.status = Sequence.EXECUTED
            sequence.save()
            shs.save()
            message = "Finished sequence " + str(sequence.pk) + " execution"
05038bc8   Jeremy   Majordome logic i...
973
            Log.objects.create(agent="Majordome", message=message)
c53a13e0   Jeremy   Updating a lot of...
974
            self.executing_sequence = None
05038bc8   Jeremy   Majordome logic i...
975
        return (0)
65149de7   Jeremy   Update
976
977
978
979

    '''
        Function called to do an action with the devices status
    '''
05038bc8   Jeremy   Majordome logic i...
980
    def handleStatus(self):
05038bc8   Jeremy   Majordome logic i...
981
982
983
        telescope = Telescope.objects.first()
        camera_nir = Detector.objects.get(name="Cagire")
        camera_vis = Detector.objects.get(name="Visible camera")
ce470283   Jeremy   Plc simulator fin...
984
985
986
        dome = Dome.objects.get(name="Dome")

        dome.status = self.status_dom
05038bc8   Jeremy   Majordome logic i...
987
988
989
        telescope.status = self.status_tel
        camera_nir.status = self.status_nir
        camera_vis.status = self.status_vis
ce470283   Jeremy   Plc simulator fin...
990
991
992
993
994

        dome.save()
        telescope.save()
        camera_nir.save()
        camera_vis.save()
bca9a283   Jeremy   Reworked the sche...
995
996
        self.logDB("Instrument status : dome  = " + str(self.status_dom) + ", telescope = " + str(self.status_tel)
                   + ", camera_nir = " + str(self.status_nir) + ", camera_vis" + str(self.status_vis))
65149de7   Jeremy   Update
997
998
999
        return (0)

    '''
3224f14a   Jeremy   Fixed some simula...
1000
        Put the majordome in pause
4cb0ff36   Jeremy   Update
1001
    '''
4cb0ff36   Jeremy   Update
1002
    def systemPause(self, duration, cause: str):
bca9a283   Jeremy   Reworked the sche...
1003
        self.logDB("System in pause for " + str(duration))
4cb0ff36   Jeremy   Update
1004
        time.sleep(duration)
05bdcc44   Etienne Pallier   BIG DEMO tests (s...
1005
1006
1007
1008
1009
1010
1011
1012
        #scheduler.tasks.scheduling.apply_async(first_schedule=False, alert=False)
        if settings.USE_CELERY:
            print("MJ: call schedule WITH CELERY")
            scheduler.tasks.scheduling.apply_async(first_schedule=False, alert=False)
        else:
            print("MJ: call schedule WITHOUT CELERY")
            scheduler.tasks.scheduling().run(first_schedule=False, alert=False)

4cb0ff36   Jeremy   Update
1013
1014
1015
1016
1017
        self.setTime()
        print("system has been paused. Cause : " + cause)
        return (0)

    '''
65149de7   Jeremy   Update
1018
1019
        Function called to do an action with the site status and the wheather status
    '''
ce470283   Jeremy   Plc simulator fin...
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
    def handlePLC(self):
        try:
            site_status = SiteWatch.objects.latest('updated')
            weather_status = WeatherWatch.objects.latest('updated')
            self.weather_status = weather_status.global_status
            self.site_status = site_status.global_status
        except ObjectDoesNotExist as e:
            if (settings.DEBUG and DEBUG_FILE):
                log.info("No site_status or weather_status found in database : " + str(e))
            # TODO shutdown everything
            return 1
3224f14a   Jeremy   Fixed some simula...
1031
        return 0
5b5566ab   haribo   added celery
1032

4cb0ff36   Jeremy   Update
1033
    '''
05038bc8   Jeremy   Majordome logic i...
1034
        Gets the time before the expected start of the execution.
4cb0ff36   Jeremy   Update
1035
    '''
05038bc8   Jeremy   Majordome logic i...
1036
1037
    def getCountdown(self, shs):
        # TODO start sequence as soon as possible (a lot of verifications must be done there)
ff448d43   Jeremy   Update
1038
        current_time = secondsToPreciseJulianDate(getPreciseCurrentTime())
05038bc8   Jeremy   Majordome logic i...
1039
        countdown = shs.tsp - current_time
9774228b   haribo   Date: 22/06/2016
1040
        return countdown
7a79e25b   haribo   Date: 19/05/2016
1041

4cb0ff36   Jeremy   Update
1042
1043
1044
1045
    '''
        Change observation conditions
    '''
    def changeObsConditions(self):
5b5566ab   haribo   added celery
1046
        print("change_obs_conditions")
1179175f   Etienne Pallier   Majordome agent c...
1047
1048
1049
        pass

"""