Blame view

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

from common.Telescope import TelescopeObj
from common.VISCamera import VISCameraObj
from common.NIRCamera import NIRCameraObj
5b5566ab   haribo   added celery
13
14
15

import time

c5a3b4a0   haribo   Date: 05/07/2016
16
17
TIMER_CHECK = 10  # in seconds

5b5566ab   haribo   added celery
18
class monitoring(Task):
9b5bad52   haribo   Commented all the...
19
20
21
22
23
24
25
26
27
28
    '''
        Infinite task created at the program's start.
        It initilize all the external connections, and starts the alert_listener.
        
        This is the place to put the starting configurations.
        
        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.
    '''

5b5566ab   haribo   added celery
29
30

    def run(self):
c5a3b4a0   haribo   Date: 05/07/2016
31
32
        self.configure_instruments()
        self.connect_to_plc()
a190892c   haribo   Date: 05/07/2016
33
        self.update_software_versions()
c5a3b4a0   haribo   Date: 05/07/2016
34
35
36
37
38
39
        self.get_night_start_end()
        self.wait_devices_ready()

        # TODO: décommenter pour lancer un scheduling
        # scheduler.tasks.scheduling(first_schedule=True, alert=False, night_start=self.night_start, night_end=self.night_end)
        alert_manager.tasks.alert_listener.delay()
a190892c   haribo   Date: 05/07/2016
40

5b5566ab   haribo   added celery
41
42
        time.sleep(5)
        print("monitoring")
a190892c   haribo   Date: 05/07/2016
43

c5a3b4a0   haribo   Date: 05/07/2016
44
45
46
        self.timers_loop()

    def configure_instruments(self):
9b5bad52   haribo   Commented all the...
47
48
49
        '''
            Creates the communication objects for each instrument, and give them the basic configurations.
        '''
c5a3b4a0   haribo   Date: 05/07/2016
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
        self.tel = TelescopeObj()
        self.vis_camera = VISCameraObj()
        self.nir_camera = NIRCameraObj()

        # TODO: ici, on initialise les connections

        self.vis_camera.do("TEMPERATURE", 1.0, -150.0)
        self.nir_camera.do("TEMPERATURE", 1.0, -150.0)
        self.tel.do("HOMING")
        self.tel.do("DOORS", True)

        # Souci : gestion des instruments en global
        # Cette fonction dépendra du moyen de partager les connections des instruments

    def connect_to_plc(self):
9b5bad52   haribo   Commented all the...
65
66
67
        '''
            Initialize the connection to the PLC
        '''
c5a3b4a0   haribo   Date: 05/07/2016
68
69
70
        # TODO: initialiser la co avec le plc et stocker les identifiants de la co dans self
        pass

a190892c   haribo   Date: 05/07/2016
71
    def update_software_versions(self):
9b5bad52   haribo   Commented all the...
72
73
74
        '''
            Reads the softwares versions in the settings.py, store them in the DB and send them to the IC.
        '''
a190892c   haribo   Date: 05/07/2016
75
76
77
78
79
80
81
82
        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)
c5a3b4a0   haribo   Date: 05/07/2016
83
84
85
86

        # TODO: envoyer les versions à l'IC

    def get_night_start_end(self):
9b5bad52   haribo   Commented all the...
87
88
89
        '''
            Computes the beginning and the end of the following (or current) night
        '''
c5a3b4a0   haribo   Date: 05/07/2016
90
91
92
93
94
95
        # TODO: utiliser un logiciel by AK pour stocker en local le début et la fin de la nuit (on est peut-être dedans)
        self.night_start = time.time() + 180 / 86400
        self.night_end = time.time() + 360 / 86400
        pass

    def wait_devices_ready(self):
9b5bad52   haribo   Commented all the...
96
97
98
        '''
            Loop to wait for the device to be idle avec the starting configurations.
        '''
c5a3b4a0   haribo   Date: 05/07/2016
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
        # TODO: faire une boucle pour attendre que les devices soient prêts
        pass

    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
                - At the end of the night, do calibration files and computes the next night limits + make a scheduling with the new schedule  
        '''

        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":
                        # TODO: get PLC status
                        status_tel = self.tel.get("STATUS")
                        status_nir = self.nir_camera.get("STATUS")
                        status_vis = self.vis_camera.get("STATUS")
                        # TODO: stocker les statuts & les envoyer à l'IC
                        # TODO: analyser le retour du PLC
                    elif timer_name == "night_start":
                        scheduler.tasks.scheduling.delay(first_schedule=False, alert=False)
                        timers["night_start"] = 86400
                    elif timer_name == "night_end":
164eebbd   haribo   UML and comments
146
                        observation_manager.tasks.create_calibrations.delay()
c5a3b4a0   haribo   Date: 05/07/2016
147
148
                        self.get_night_start_end()
                        scheduler.tasks.scheduling(first_schedule=True, alert=False, night_start=self.night_start, night_end=self.night_end)