Blame view

src/observation_manager/tasks.py 7.59 KB
5b5566ab   haribo   added celery
1
from __future__ import absolute_import
5b5566ab   haribo   added celery
2
from celery.task import Task
c53a13e0   Jeremy   Updating a lot of...
3
from django.conf import settings
ddf59dd4   haribo   Remaniement :
4
from common.models import *
ff448d43   Jeremy   Update
5
from utils.Logger import setupLogger
ee2c4543   Etienne Pallier   Bugfix Monitoring...
6
from analyzer.tasks import Analysis
c53a13e0   Jeremy   Updating a lot of...
7
8
9
10
11
12
13
from utils.JDManipulator import *
from devices.Telescope import TelescopeController
from devices.CameraNIR import NIRCameraController
from devices.CameraVIS import VISCameraController
import time
import subprocess
import os
77816f10   haribo   Workflow implemen...
14

605b65e5   Jeremy   Update simulators
15
16
17
'''
    Super class for execute_plan_vis / _nir
'''
945b36d2   haribo   Généricité des ca...
18
class execute_plan(Task):
c53a13e0   Jeremy   Updating a lot of...
19
20
21
22
23
24
25
26
    def setTelescope(self):
        self.tel = TelescopeController()
        return 0

    def start(self) -> int:
        self.setTelescope()
        self.logger = setupLogger("Observation"+self.type, "Observation"+self.type)
        self.image_count = 0
3224f14a   Jeremy   Fixed some simula...
27
        # JB TODO REMOVE : is it still useful ?
c53a13e0   Jeremy   Updating a lot of...
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
        # TaskId.objects.filter(task_id=self.request.id).delete()
        if self.countdown > 0:
            time.sleep(self.countdown)
        try:
            self.plan = Plan.objects.get(id=self.plan_id)
            self.duration = julianSecondsToSeconds(self.plan.duration)
        except:
            self.logDB(message="Plan with id %d not found" % (self.plan_id))
            return 1
        self.plan_dir = self.plan.name + "_" + str(self.plan_id)
        self.dir_name = settings.OUTPUT_FOLDER + os.sep + self.plan_dir
        if self.createDirs():
            self.log("Could not create dirs")
            self.logDB(message="Could not create directory " + self.dir_name + " for image storage")
            return 1
        self.launchAnalysis()
        self.log("Starting execution " + self.type)
        return self.logDB('Start plan %s observation from camera %s'%(self.plan.name, str(self.type)))

    def createDirs(self):
        try:
            if not os.path.isdir(settings.OUTPUT_FOLDER):
                os.makedirs(settings.OUTPUT_FOLDER)
            if not os.path.isdir(self.dir_name):
                os.makedirs(self.dir_name)
        except:
            return 1
        return 0

    def log(self, message: str) -> int:
        self.logger.info(self.type + ' -> '+ message)
        return 0

    def logDB(self, message: str) -> int:
945b36d2   haribo   Généricité des ca...
62
        Log.objects.create(agent='Observation manager', message=message)
c53a13e0   Jeremy   Updating a lot of...
63
        return 0
ad85da7c   haribo   Date: 28/06/2016
64

c53a13e0   Jeremy   Updating a lot of...
65
    def launchAnalysis(self):
ee2c4543   Etienne Pallier   Bugfix Monitoring...
66
        Analysis.apply_async((self.plan_id, settings.OUTPUT_FOLDER))
c53a13e0   Jeremy   Updating a lot of...
67
        return 0
ad85da7c   haribo   Date: 28/06/2016
68

c53a13e0   Jeremy   Updating a lot of...
69
70
71
    def launchCalibration(self) -> int:
        create_calibrations.apply_async((self.plan_id, settings.OUTPUT_FOLDER, self.image_count))
        return 0
ad85da7c   haribo   Date: 28/06/2016
72

c53a13e0   Jeremy   Updating a lot of...
73
74
75
    def end(self) -> int:
        self.log("Finished plan observation")
        return self.logDB('Finished plan observation ' + self.plan.name + ' from camera ' + str(self.type))
ad85da7c   haribo   Date: 28/06/2016
76

c53a13e0   Jeremy   Updating a lot of...
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
    def run(self, plan_id: int, countdown: float, type: str) -> int:
        self.plan_id = plan_id
        self.countdown = countdown
        self.type = type
        if self.start():
            self.log("fail exec task -> leaving")
            return 1
        self.execute()
        return self.end()

    def execute(self) -> int:
        time = julianSecondsToSeconds(self.plan.duration)
        self.log("Sleeping for " + str(int(self.duration)))
        time.sleep(int(self.duration))
        return 0
9774228b   haribo   Date: 22/06/2016
92

605b65e5   Jeremy   Update simulators
93
94
'''
    Gives the orders to the instruments to retrieve the image(s) of a plan VIS.
c53a13e0   Jeremy   Updating a lot of...
95
    Send the images to the calibrator
605b65e5   Jeremy   Update simulators
96
'''
945b36d2   haribo   Généricité des ca...
97
class execute_plan_vis(execute_plan):
c53a13e0   Jeremy   Updating a lot of...
98
99
100
    def setCamera(self):
        self.vis_camera = VISCameraController()
        return 0
ff448d43   Jeremy   Update
101

c53a13e0   Jeremy   Updating a lot of...
102
103
    def run(self, plan_id: int, countdown: float) -> int:
        self.setCamera()
945b36d2   haribo   Généricité des ca...
104
        super().run(plan_id, countdown, "VIS")
c53a13e0   Jeremy   Updating a lot of...
105
        return 0
ad85da7c   haribo   Date: 28/06/2016
106

c53a13e0   Jeremy   Updating a lot of...
107
108
109
    def execute(self) -> int:
        # TODO All the comunication protocol with the device
        self.tel.do("GOTO " + str(self.plan.position))
3224f14a   Jeremy   Fixed some simula...
110
        self.vis_camera.do("CAPTURE "+self.dir_name+" "+str(self.plan.nb_images)+" "+str(self.plan.duration))
c53a13e0   Jeremy   Updating a lot of...
111
        self.log("Sleeping for " + str(int(self.duration)))
dbacdf02   Etienne Pallier   commentaires
112
        #TODO: faire un boucle infinie de recup image + lancement calib pour chaque image des que disponible
c53a13e0   Jeremy   Updating a lot of...
113
114
115
        time.sleep(int(self.duration))
        self.launchCalibration()
        return 0
aed99094   haribo   Debug routine MGR...
116

605b65e5   Jeremy   Update simulators
117
118
'''
    Gives the orders to the instruments to retrieve the image(s) of a plan NIR.
c53a13e0   Jeremy   Updating a lot of...
119
    Send the images to the calibrator
605b65e5   Jeremy   Update simulators
120
'''
945b36d2   haribo   Généricité des ca...
121
class execute_plan_nir(execute_plan):
c53a13e0   Jeremy   Updating a lot of...
122
123
124
    def setCamera(self):
        self.nir_camera = NIRCameraController()
        return 0
ff448d43   Jeremy   Update
125

c53a13e0   Jeremy   Updating a lot of...
126
127
    def run(self, plan_id: int, countdown: float) -> int:
        self.setCamera()
945b36d2   haribo   Généricité des ca...
128
        super().run(plan_id, countdown, "NIR")
c53a13e0   Jeremy   Updating a lot of...
129
130
131
132
133
        return 0

    def execute(self) -> int:
        # TODO All the comunication protocol with the device
        self.tel.do("GOTO " + str(self.plan.position))
3224f14a   Jeremy   Fixed some simula...
134
        self.nir_camera.do("CAPTURE "+self.dir_name+" "+str(self.plan.nb_images)+" "+str(self.plan.duration))
c53a13e0   Jeremy   Updating a lot of...
135
        self.log("Sleeping for " + str(int(self.duration)))
dbacdf02   Etienne Pallier   commentaires
136
        #TODO: faire un boucle infinie de recup image + lancement calib pour chaque image des que disponible
c53a13e0   Jeremy   Updating a lot of...
137
138
139
        time.sleep(int(self.duration))
        self.launchCalibration()
        return 0
ad85da7c   haribo   Date: 28/06/2016
140

ad85da7c   haribo   Date: 28/06/2016
141

605b65e5   Jeremy   Update simulators
142
'''
c53a13e0   Jeremy   Updating a lot of...
143
144
    Call a process with a folder and an image number as parameter who will create the
    calibration for an image
605b65e5   Jeremy   Update simulators
145
'''
164eebbd   haribo   UML and comments
146
class create_calibrations(Task):
c53a13e0   Jeremy   Updating a lot of...
147
148
149
150
151
    logger = setupLogger("calibrations", "calibrations")

    def log(self, message: str) -> int:
        self.logger.info(message)
        return 0
ff448d43   Jeremy   Update
152

c53a13e0   Jeremy   Updating a lot of...
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
    def logDB(self, message: str) -> int:
        Log.objects.create(agent='Observation manager', message=message)
        return 0

    def execProcess(self, command: str) -> int:
        self.process = subprocess.Popen(command, shell=True)
        return 0

    def start(self, plan_id: int, folder: str, image_number: int) -> int:
        self.plan_id = plan_id
        self.folder = folder
        self.image_number = image_number
        self.path_dir_file = os.path.dirname(os.path.realpath(__file__))
        try:
            self.plan = Plan.objects.get(id=self.plan_id)
            self.plan_folder = self.plan.name + "_" + str(self.plan_id)
        except:
3224f14a   Jeremy   Fixed some simula...
170
            self.log("not found calibrations")
c53a13e0   Jeremy   Updating a lot of...
171
172
173
174
            self.logDB(message="Plan with id %d not found"%(self.plan_id))
            return 1
        self.dir_name = self.folder + os.sep + self.plan.name + "_" + str(self.plan_id) + os.sep + "calibrations"
        if self.createDirectory():
3224f14a   Jeremy   Fixed some simula...
175
            self.log("err dir calibrations")
c53a13e0   Jeremy   Updating a lot of...
176
177
            self.logDB(message="Could not create folder for calibrations")
            return 1
3224f14a   Jeremy   Fixed some simula...
178
        self.log("start calibrations")
c53a13e0   Jeremy   Updating a lot of...
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
        return self.logDB("Starting calibration for image " + str(self.image_number))

    def createDirectory(self):
        try:
            if not os.path.isdir(self.dir_name):
                os.makedirs(self.dir_name)
        except:
            return 1
        return 0

    def changeDirectory(self, path: str):
        os.chdir(path)
        return 0

    def execute(self) -> int:
        self.changeDirectory(self.path_dir_file)
3224f14a   Jeremy   Fixed some simula...
195
        return self.execProcess("./calibrator '"+str(self.folder)+"' '"+self.plan_folder+"' '"+str(self.image_number) + "'")
c53a13e0   Jeremy   Updating a lot of...
196
197
198
199

    def end(self) -> int:
        self.process.wait()
        if self.process.returncode == 0:
3224f14a   Jeremy   Fixed some simula...
200
            self.log("executed calibrations")
c53a13e0   Jeremy   Updating a lot of...
201
202
            self.logDB(message="Calibration executed successfully for image " + str(self.image_number))
        else:
3224f14a   Jeremy   Fixed some simula...
203
            self.log("failed calibrations")
c53a13e0   Jeremy   Updating a lot of...
204
205
206
207
208
209
210
211
            self.logDB(message="Could not calibrate image " + str(self.image_number))
        return self.process.returncode

    def run(self, plan_id: int, folder: str, image_number: int) -> int:
        if self.start(plan_id, folder, image_number):
            return 1
        self.execute()
        return self.end()
c72eb17a   Jeremy   Update celery task
212
213
214
215

class night_calibrations(Task):
    def run(self):
        return 0