
from util.shinsLogger import logger
from util import exception
from util import variables as uvars
from util import functions as ufun
from shinsapp import SHINSApp
import time

class SHARKNIR_gen_Preset(SHINSApp):

    @exception.handler_for_run
    def run(self, ins_s=None, det_s=None, tel_s=None, rtc_s=None, **kwargs):
        '''Try some tcs command
        '''
        
        obsController = self.obsController
        self.status = uvars.EXIT_SUCCESS            
        logger.info(__name__)
        
        ################################
        # TRANSFORM COORDINATES TO RAD #
        ################################

        logger.debug("Enumerating telescope setup")
        for element, value in tel_s.items():
            logger.debug("Element : % 15s  -  Value % 15s    " % (element, value))

        tel_s = ufun.to_radians(tel_s)

        logger.debug("Enumerating telescope setup after conversion")
        for element, value in tel_s.items():
            logger.debug("Element : % 15s  -  Value % 15s    " % (element, value))
            
        ##############
        ### SETUP  ###
        ##############        
        if self.paragraph("INSTRUMENT SETUP"):
        
            logger.info("Running: Moving drot")
            self.status = obsController.setupInstrument(ins_s)
            logger.info("Running: Moving drot... DONE")

        ###################################
        ### STOP ANY PREVIOUS TRACKING  ###
        ###################################
        if self.paragraph("STOP TRACKING"):
        
            logger.info("Running: trackingStop")
            status = obsController.trackingStop()
            logger.info("trackingStop returned : %d" % status)

        ##########################
        # OPEN REAL-TIME TT LOOP #
        ##########################
        if self.paragraph("OPEN REAL-TIME TT LOOP"):

            logger.info("TT open loop")
            self.status = obsController.RTCTTOpenLoop()
            ufun.error_handler(self.status, "RTCTTOpenLoop")
            logger.debug(self.status)
        
            logger.info("Tip tilt loop open on TECCAM")
            
        ###################################
        ### PRESET TELESCOPE AND TRACK  ###
        ###################################
        if self.paragraph("PRESET AND TRACK"):
            
            logger.info("Begin PresetTelescope")
            async_preset = obsController.begin_PresetTelescope(tel_s)
            logger.info("Begin PresetTelescope... DONE" )

            if ("DROT_Mode" in ins_s and (ins_s["DROT_Mode"]=="FIELD_FIXED" or ins_s["DROT_Mode"]=="FIELD_FREE")):
                logger.info("Set derotation parameters...")
                derot_duration = 60*60 # integer seconds
                self.status = obsController.setDerotationParameters1(derot_duration, 1, 1)
                logger.info("Set derotation parameters... DONE")

                # Never ends. It is stopped by obsController.trackingStop()
                logger.info("Start tracking....")
                async = obsController.begin_trackingStart()
                logger.info("Start tracking... DONE")
            else:
                logger.info("DROT_Mode is PUPIL Mode: NO Derotation will begin")
            
            logger.info("Wait PresetTelescope")
            self.status = obsController.end_PresetTelescope(async_preset)
            logger.info("Wait PresetTelescope... DONE")
