tasks.py 3.53 KB
from __future__ import absolute_import
from celery.task import Task
from analyzer.tasks import analysis
from common.models import *
from devices import CameraVIS as VIS
from devices import CameraNIR as NIR
from devices import Telescope
import time

'''
    Super class for execute_plan_vis / _nir
'''
class execute_plan(Task):

    def run(self, plan_id, countdown, type):
        if countdown > 0:
            time.sleep(countdown)
        TaskId.objects.filter(task_id=self.request.id).delete()

        self.type = type
        plan = Plan.objects.get(id=plan_id)

        message = 'Start plan ' + plan.name + ' execution'
        Log.objects.create(agent='Observation manager', message=message)
        print("execute_plan " + self.type + " : ", plan.name)

        self.tel = Telescope.TelescopeController()
        if self.type == "VIS":
            cam = VIS.VISCameraController()
        else:
            cam = NIR.NIRCameraController()

        self.set_camera(cam, plan)
        self.wait_camera_ready(cam)

        cam.do("START")

        st = self.wait_camera_finished(cam)

        # TODO: récupérer les vraies images ? je fais quoi ?
        time.sleep(1)
        # Penser qu'un plan peut créer plusieurs images (et analyser image par image)
        analysis.delay(plan_id)
        message = 'Finished plan ' + plan.name + ' execution'
        Log.objects.create(agent='Observation manager', message=message)

    '''
        Set the camera configuration
    '''
    def set_camera(self, cam, plan):

        # TODO: mettre les vraies configurations en fct du plan
        cam.set("WINDOW", 0, 100, 10, 100)
        cam.set("READMODE", "Ramp")
        cam.set("FILENAME", plan.name)
        cam.set("HEADER", {})
        cam.set("READOUT_FREQUENCY", 20.0)
        cam.set("FILTER", "H")

        if self.type == "VIS":
            cam.set("EXPOSURE", 180)
            cam.set("BINNING", 300, 300)
        else:
            cam.set("NB_IMAGES", 28)

    '''
        Loop to wait for the configuration to be done
    '''
    def wait_camera_ready(self, cam):
        st = 0
        while st == 0:
            st_tel = self.tel.get("STATUS")

            st_cam = cam.get("STATUS")

            st = 1

            # TODO: checker les statuts comme il faut, et repasser à 0 si on a des statuts pas bons
            if st_tel != "IDLE" or st_cam != "IDLE":
                st = 0

    '''
        Loop to wait for the observation to be finished
    '''
    def wait_camera_finished(self, cam):
        countdown = int(cam.get("TIMER"))
        while countdown > 5:
            time.sleep(5)
            countdown = int(cam.get("TIMER"))

        st = 0
        while st == 0:
            timer = int(cam.get("TIMER"))
            if timer == -1:
                st = 1
            else:
                time.sleep(1)


'''
    Gives the orders to the instruments to retrieve the image(s) of a plan VIS.
    Send the images to the analyzer
'''
class execute_plan_vis(execute_plan):
    def run(self, plan_id, countdown):
        super().run(plan_id, countdown, "VIS")


'''
    Gives the orders to the instruments to retrieve the image(s) of a plan NIR.
    Send the images to the analyzer
'''
class execute_plan_nir(execute_plan):
    def run(self, plan_id, countdown):
        super().run(plan_id, countdown, "NIR")


'''
    Directly make the right calls to the instruments to create the calibration files.
    When they are all finished, it creates the 'super' calibration files.
'''
class create_calibrations(Task):
     def run(self):
         # TODO: attendre que tout soit idle
         pass