# $Header: https://oberon.oapd.inaf.it/shins/trunk/shins/src/scripts/templates/tpl/testOB_tcs_PresetTelescopeNoAO.py 3808 2022-01-25 12:43:04Z depa $

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 testOB_tcs_PresetTelescopeNoAO(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 #
        ################################
        
        # tel_s["OBJECTNAME"] = "M31"
        # objra_degree = 15*(0 + 42/60. + 44.05/3600.)
        # tel_s["OBJRA"] = str(objra_degree*3.141592653/180)
        # objdec_degree = 41 + 15/60. + 54.5/3600.
        # tel_s["OBJDEC"] = str(objdec_degree*3.141592653/180)

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


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

            time.sleep(1)
            
        ##############
        ### SETUP  ###
        ##############        
        if self.paragraph("INSTRUMENT SETUP"):
        
            logger.info("Running: Moving drot")
            self.status = obsController.setupInstrument(ins_s)
            logger.info("Running: Moving drot... DONE")

        
        ##############
        ### TRACK  ###
        ##############        
        if self.paragraph("TRACK"):

            # derot_duration = 60 # integer seconds
            # obsController.setDerotationParameters(derot_duration, 1, 1, 2, 300, 100)

            logger.info("Running: trackingStartHere")
            async = obsController.begin_trackingStart()

            # status = obsController.end_trackingStart(async)
            # logger.info("trackingStartHere returned : %d" % status)
