Blame view

src/observation_manager/tasks.py 7.31 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
c53a13e0   Jeremy   Updating a lot of...
6
7
8
9
10
11
12
13
from analyzer.tasks import analysis
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
66
67
    def launchAnalysis(self):
        analysis.apply_async((self.plan_id, settings.OUTPUT_FOLDER))
        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
112
113
114
        self.log("Sleeping for " + str(int(self.duration)))
        time.sleep(int(self.duration))
        self.launchCalibration()
        return 0
aed99094   haribo   Debug routine MGR...
115

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

c53a13e0   Jeremy   Updating a lot of...
125
126
    def run(self, plan_id: int, countdown: float) -> int:
        self.setCamera()
945b36d2   haribo   Généricité des ca...
127
        super().run(plan_id, countdown, "NIR")
c53a13e0   Jeremy   Updating a lot of...
128
129
130
131
132
        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...
133
        self.nir_camera.do("CAPTURE "+self.dir_name+" "+str(self.plan.nb_images)+" "+str(self.plan.duration))
c53a13e0   Jeremy   Updating a lot of...
134
135
136
137
        self.log("Sleeping for " + str(int(self.duration)))
        time.sleep(int(self.duration))
        self.launchCalibration()
        return 0
ad85da7c   haribo   Date: 28/06/2016
138

ad85da7c   haribo   Date: 28/06/2016
139

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

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

c53a13e0   Jeremy   Updating a lot of...
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
    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...
168
            self.log("not found calibrations")
c53a13e0   Jeremy   Updating a lot of...
169
170
171
172
            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...
173
            self.log("err dir calibrations")
c53a13e0   Jeremy   Updating a lot of...
174
175
            self.logDB(message="Could not create folder for calibrations")
            return 1
3224f14a   Jeremy   Fixed some simula...
176
        self.log("start calibrations")
c53a13e0   Jeremy   Updating a lot of...
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
        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...
193
        return self.execProcess("./calibrator '"+str(self.folder)+"' '"+self.plan_folder+"' '"+str(self.image_number) + "'")
c53a13e0   Jeremy   Updating a lot of...
194
195
196
197

    def end(self) -> int:
        self.process.wait()
        if self.process.returncode == 0:
3224f14a   Jeremy   Fixed some simula...
198
            self.log("executed calibrations")
c53a13e0   Jeremy   Updating a lot of...
199
200
            self.logDB(message="Calibration executed successfully for image " + str(self.image_number))
        else:
3224f14a   Jeremy   Fixed some simula...
201
            self.log("failed calibrations")
c53a13e0   Jeremy   Updating a lot of...
202
203
204
205
206
207
208
209
            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()