# $Header: https://oberon.oapd.inaf.it/shins/trunk/shins/src/scripts/templates/tpl/SHARKNIR_gen_tiptilt_init.py 3952 2022-05-19 08:30:53Z depa $

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

class SHARKNIR_gen_tiptilt_setup_and_start(SHINSApp):
    
    @exception.handler_for_run
    def run(self, ins_s=None, det_s=None, tel_s=None, rtc_s=None, **kwargs):
        '''Setup and start the RTC BCU, not the real-time tiptilt loop
        input parameter is rtc_s
        '''
        
        logger.info(__name__)
        logger.info('Test tiptilt system setup and start ... ')

        self.status = uvars.EXIT_SUCCESS
        obsController = self.obsController

        # ########################
        # # REAL-TIME RTCTT INIT #
        # ########################
        # if self.paragraph("RTC INIT"):
        
        #     logger.debug("Enumerating RTC setup")
        #     for element, value in rtc_s.items():
        #         logger.debug("Element : % 20s  -  Value % 20s    " % (element, value))
            
        #     ########################
        #     # REAL-TIME RTCTT INIT #
        #     ########################
            
        #     # if not(rtcInit):
        #     logger.warning("RTCTT is not initialized. RTCTT init. wait ~30s")
        #     self.status = obsController.RTCTTInit(rtc_s)
        #     logger.debug(self.status)
        #     ufun.error_handler(self.status, "RTCTTInit")

        #########################
        # SETUP REAL-TIME RTCTT #
        #########################
        if self.paragraph("RTC SETUP AND START"):

            logger.info("RTCTT setup")
            self.status = obsController.RTCTTSetup()
            logger.debug(self.status)
            ufun.error_handler(self.status, "RTCTTSetup")
            
            #########################
            # START REAL-TIME RTCTT #
            #########################
           
            time.sleep(1) 
            logger.info("RTCTT start")
            self.status = obsController.RTCTTStart()
            logger.debug(self.status)
            ufun.error_handler(self.status, "RTCTTStart")
