tasks.py 5.51 KB
from __future__ import absolute_import

from celery.task import Task
from analyzer.tasks import analysis
from pyrosapp.models import *

from common import VISCamera as VIS
from common import NIRCamera as NIR
from common import FilterWheel as FilterW

import time
import os

IMAGES_FOLDER = 'simulation_images'

class execute_plan_vis(Task):

    def run(self, plan_id, countdown):
#         print("ex plan : ", self.request.id)
        if countdown > 0:
            time.sleep(countdown)
        TaskId.objects.filter(task_id=self.request.id).delete()

        message = 'Start plan ' + str(plan_id) + ' execution'
        Log.objects.create(agent='Observation manager', message=message)
        print("execute_plan VIS : ", plan_id)

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

        # TODO: pour l'instant je crée une fake camera temporaire, mais à l'avenir
        # j'aurais un objet global
        cam = VIS.VISCameraObj()

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

        cam.do("START")

# TODO: décommenter quand on aura un simulateur
#         st = self.wait_camera_finished(cam)

        # TODO: récupérer les vraies images ? je fais quoi ?
        time.sleep(1)
        with open(os.path.join(IMAGES_FOLDER, str(plan_id)), 'w'):
            pass
        analysis.delay(plan_id)
        message = 'Finished plan ' + str(plan_id) + ' execution'
        Log.objects.create(agent='Observation manager', message=message)

    def set_camera(self, cam, plan):

        cam.filter_wheel.set("FILTER", FilterW.FilterEnum.h)
        cam.filter_wheel.do("CHANGE_FILTER")

        cam.set("WINDOW", 0, 100, 10, 100)
        cam.set("READMODE", VIS.ReadmodeEnum.ramp)
        cam.set("FILENAME", "toto_a_la_campagne")
        cam.set("HEADER", {})
        cam.set("READOUT_FREQUENCY", 20.0)

        cam.set("EXPOSURE", 180)
        cam.set("BINNING", 300, 300)

    def wait_camera_ready(self, cam):

        st = 0
        while st == 0:
            # TODO: uncomment quand on caura la télescope en global
#             st_tel = tel.get("STATUS")
            st_cam = cam.get("STATUS")
            st_fw = cam.filter_wheel.get("STATUS")
            # TODO: uncomment quand on aura le plc en global
#             st_plc = plc.get("STATUS")

            st = 1
            # TODO: checker les statuts comme il faut, et repasser à 0 si on a des statuts pas bons


    def wait_camera_finished(self, cam):

#         countdown = cam.get("TIMER")
#         while countdown > 5:
#             time.sleep(5)
#             countdown = cam.get("TIMER")

        st = 0
        while st == 0:
            timer = cam.get("TIMER")
#             st_plc = pls.get("STATUS") # TODO: quand le plc sera fait ...
            # TODO: checks PLC, mais c'est peut-être pas nécessaire, si c'est le PLC qui abort direct la camera
            if timer == -1:
                st = 1

        if st > 1:
            cam.do("ABORT")  # normalement ça ne devrait jamais arriver


class execute_plan_nir(Task):

    def run(self, plan_id, countdown):
#         print("ex plan : ", self.request.id)
        if countdown > 0:
            time.sleep(countdown)
        TaskId.objects.filter(task_id=self.request.id).delete()

        message = 'Start plan ' + str(plan_id) + ' execution'
        Log.objects.create(agent='Observation manager', message=message)
        print("execute_plan NIR : ", plan_id)

        plan = Plan.objects.get(pk=plan_id)

        # TODO: pour l'instant je crée une fake camera temporaire, mais à l'avenir
        # j'aurais un objet global
        cam = NIR.NIRCameraObj()

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

        cam.do("START")

# TODO: décommenter quand on aura un simulateur
#         st = self.wait_camera_finished(cam)

        # TODO: récupérer les vraies images ? je fais quoi ?
        time.sleep(1)
        with open(os.path.join(IMAGES_FOLDER, str(plan_id)), 'w'):
            pass
        analysis.delay(plan_id)
        message = 'Finished plan ' + str(plan_id) + ' execution'
        Log.objects.create(agent='Observation manager', message=message)

    def set_camera(self, cam, plan):

        cam.filter_wheel.set("FILTER", FilterW.FilterEnum.h)
        cam.filter_wheel.do("CHANGE_FILTER")

        cam.set("WINDOW", 0, 100, 10, 100)
        cam.set("READMODE", NIR.ReadmodeEnum.ramp)
        cam.set("FILENAME", "toto_a_la_campagne")
        cam.set("HEADER", {})
        cam.set("READOUT_FREQUENCY", 20.0)

        cam.set("NB_IMAGES", 28)

    def wait_camera_ready(self, cam):

        st = 0
        while st == 0:
            # TODO: uncomment quand on caura la télescope en global
#             st_tel = tel.get("STATUS")
            st_cam = cam.get("STATUS")
            st_fw = cam.filter_wheel.get("STATUS")
            # TODO: uncomment quand on aura le plc en global
#             st_plc = plc.get("STATUS")

            st = 1
            # TODO: checker les statuts comme il faut, et repasser à 0 si on a des statuts pas bons


    def wait_camera_finished(self, cam):

#         countdown = cam.get("TIMER")
#         while countdown > 5:
#             time.sleep(5)
#             countdown = cam.get("TIMER")
        st = 0
        while st == 0:
            timer = cam.get("TIMER")
#             st_plc = pls.get("STATUS") # TODO: quand le plc sera fait ...
            # TODO: checks PLC, mais c'est peut-être pas nécessaire, si c'est le PLC qui abort direct la camera
            if timer == -1:
                st = 1

        if st > 1:
            cam.do("ABORT")  # normalement ça ne devrait jamais arriver