Blame view

src/monitoring/tasks.py 4.22 KB
5b5566ab   haribo   added celery
1
from __future__ import absolute_import
a190892c   haribo   Date: 05/07/2016
2
from django.conf import settings
ddf59dd4   haribo   Remaniement :
3
from common.models import *
5b5566ab   haribo   added celery
4
from celery.task import Task
c5a3b4a0   haribo   Date: 05/07/2016
5
6
7
8
9
import scheduler.tasks
import alert_manager.tasks
import observation_manager.tasks
import time

ddf59dd4   haribo   Remaniement :
10
from devices.PLC import PLCController
5b5566ab   haribo   added celery
11
12
13

import time

c5a3b4a0   haribo   Date: 05/07/2016
14
15
TIMER_CHECK = 10  # in seconds

65149de7   Jeremy   Update
16
17
18
'''
    Infinite task created at the program's start.
    It initilize all the external connections, and starts the alert_listener.
0afcefa9   Jeremy   Reworked simulato...
19

65149de7   Jeremy   Update
20
    This is the place to put the starting configurations.
9b5bad52   haribo   Commented all the...
21

65149de7   Jeremy   Update
22
23
24
25
    Once the starting configurations are done, it becomes a loop that checks the PLC and instruments status.
    It also handles the beginning and the end of the night, recalculating them at each end of night.
'''
class monitoring(Task):
5b5566ab   haribo   added celery
26
27

    def run(self):
65149de7   Jeremy   Update
28
        self.setContext()
c5a3b4a0   haribo   Date: 05/07/2016
29
        self.get_night_start_end()
c5a3b4a0   haribo   Date: 05/07/2016
30

c5a3b4a0   haribo   Date: 05/07/2016
31
        alert_manager.tasks.alert_listener.delay()
a190892c   haribo   Date: 05/07/2016
32

c5a3b4a0   haribo   Date: 05/07/2016
33
34
        self.timers_loop()

65149de7   Jeremy   Update
35
36
37
38
    '''
        Creates the communication objects for each instrument, and give them the basic configurations.
    '''
    def setContext(self):
ddf59dd4   haribo   Remaniement :
39
        self.plc = PLCController()
65149de7   Jeremy   Update
40
        return (0)
c5a3b4a0   haribo   Date: 05/07/2016
41
42
43
44
45
46

    def timers_loop(self):
        '''
            Infinite loop for the different timers :
                - Every TIMER_CHECK seconds, check PLC and instruments status (+ analyse them and send them to the IC)
                - 2 minutes before the night start, make a scheduling
0afcefa9   Jeremy   Reworked simulato...
47
                - At the end of the night, do calibration files and computes the next night limits + make a scheduling with the new schedule
c5a3b4a0   haribo   Date: 05/07/2016
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
        '''

        timer_status = TIMER_CHECK

        ''' Set night start timer to 1 day, then compute the real ones if the current time isn't during the night '''
        timer_night_start = 86400

        night_start_seconds = self.night_start * 3600 * 24
        night_end_seconds = self.night_end * 3600 * 24

        if night_start_seconds - 120 > time.time():
            timer_night_start = night_start_seconds - 120 - time.time()

        timer_night_end = night_end_seconds - time.time()

        timers = {"status": timer_status, "night_start": timer_night_start, "night_end": timer_night_end}

        while True:
            minimal_timer = min(timers, key=timers.get)
            ''' Wait for the nearest timer '''
            time.sleep(timers[minimal_timer])
            ''' Update the timers '''
            timers = {key: value - timers[minimal_timer] for key, value in timers.items()}

            ''' Then check what timers are <= 0 '''
            for timer_name, timer_value in timers.items():
                if timer_value <= 0:
                    if timer_name == "status":
ac26ad2b   haribo   Date: 22/07/2016
76

00fa2456   haribo   Debug adaption code
77
78
79
                        status_tel = self.tel.get("STATUS")
                        status_nir = self.nir_camera.get("STATUS")
                        status_vis = self.vis_camera.get("STATUS")
21598bc6   haribo   Date: 01/08/2016
80
                        status_plc = self.plc.get("STATUS")
ac26ad2b   haribo   Date: 22/07/2016
81

c5a3b4a0   haribo   Date: 05/07/2016
82
                        # TODO: stocker les statuts & les envoyer à l'IC
21598bc6   haribo   Date: 01/08/2016
83

2ceea892   haribo   Date: 19/07/2016
84
                        timers["status"] = TIMER_CHECK
21598bc6   haribo   Date: 01/08/2016
85
86
87

                        self.analyze_plc_status()

c5a3b4a0   haribo   Date: 05/07/2016
88
89
90
91
                    elif timer_name == "night_start":
                        scheduler.tasks.scheduling.delay(first_schedule=False, alert=False)
                        timers["night_start"] = 86400
                    elif timer_name == "night_end":
ddf59dd4   haribo   Remaniement :
92
                        # TODO: faire un majordome.system_pause (fin de nuit)
164eebbd   haribo   UML and comments
93
                        observation_manager.tasks.create_calibrations.delay()
c5a3b4a0   haribo   Date: 05/07/2016
94
                        self.get_night_start_end()
2ceea892   haribo   Date: 19/07/2016
95
96
                        scheduler.tasks.scheduling(first_schedule=True, alert=False, night_start=self.night_start, night_end=self.night_end)
                        timers["night_start"] = self.night_start * 3600 * 24 - time.time() - 120
ac26ad2b   haribo   Date: 22/07/2016
97
                        timers["night_end"] = self.night_end * 3600 * 24 - time.time()
21598bc6   haribo   Date: 01/08/2016
98
99
100
101
102
103
104
105
106
107

    def analyze_plc_status(self):
        '''
           Reads the status in DB, and fill missing fields (maybe ?)
           Determines the obs conditions and compare them with the previous ones to know if they changed
           Create a task to stop the system if there is a security problem
        '''

        pass
        # TODO: toute la fct
1a317dbd   haribo   TODOs et modifs m...
108
        # On calcule le nouveau seeing, et si il y a eu du changement, on crée une tâche de majordome.change_obs_conditions