tasks.py 3.77 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
import os

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

    def run(self, plan_id, countdown, type):
        '''
            :param type: VIS or NIR
            :param countdown: Time to wait before execution start
        '''

        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)

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

        # 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)


    def wait_camera_ready(self, cam):
        '''
            Loop to wait for the configuration to be done
        '''
        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


    def wait_camera_finished(self, cam):
        '''
            Loop to wait for the observation to be finished
        '''

        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)




class execute_plan_vis(execute_plan):
    '''
        Gives the orders to the instruments to retrieve the image(s) of a plan VIS.
        Send the images to the analyzer
    '''

    def run(self, plan_id, countdown):
        super().run(plan_id, countdown, "VIS")


class execute_plan_nir(execute_plan):
    '''
        Gives the orders to the instruments to retrieve the image(s) of a plan NIR.
        Send the images to the analyzer
    '''

    def run(self, plan_id, countdown):
        super().run(plan_id, countdown, "NIR")



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