# $Header: https://oberon.oapd.inaf.it/shins/trunk/shins/src/scripts/templates/tpl/testOB_tiptilt_timehistory.py 3822 2022-02-08 16:50:16Z depa $

import time

from util.shinsLogger import logger
from util import variables as uvars
from util import functions as ufun
from util import exception
from util import defaults
from util import masterListenerRTC as mlRTC
from shinsapp import SHINSApp

class testOB_tiptilt_timehistory_App(SHINSApp):
    
    @exception.handler_for_run
    def run(self, ins_s=None, det_s=None, tel_s=None, rtc_s=None, **kwargs):
        logger.info(__name__)
        obsController = self.obsController
        self.status = uvars.EXIT_SUCCESS

        logger.info("get test duration")
        if "TTTESTTIME" in rtc_s.keys():
            duration = int(rtc_s["TTTESTTIME"])
        else:
            duration = dafaults.ttTestTime

        ############################
        # INITIALIZE RTC SUBSYSTEM #
        ############################
        logger.info("init RTC")
        print("init")
        self.status = obsController.RTCTTInit(rtc_s)
        ufun.error_handler(self.status, "RTCTTInitRTC")

        #######################
        # START RTC SUBSYSTEM #
        #######################
        logger.info("start RTC")
        print("start")
        self.status = obsController.RTCTTStart(rtc_s)
        ufun.error_handler(self.status, "RTCTTStartRTC")

        ##########################
        # CONFIGURE CRED2 CAMERA #
        ##########################
        logger.info("configure CRED2 camera")
        print ("conf camera")
        self.status = obsController.RTCTTConfigCamera(rtc_s)
        ufun.error_handler(self.status, "RTCTTConfigCamera")

        #####################
        # ENABLE DIAGNOSTIC #
        #####################
        logger.info("enable bcu diagnostic")
        print("enable bcu diagnostic")
        self.status = obsController.RTCTTEnableDiagnostic(rtc_s)
        ufun.error_handler(self.status, "RTCTTEnableDiagnostic")

        #############################
        # WRITE TIME HISTORY TO BCU #
        #############################
        logger.info("write time history to bcu")
        print("write time history to bcu")
        self.status = obsController.RTCTTWriteTimeHistory(rtc_s)
        ufun.error_handler(self.status, "RTCTTWriteTimeHistory")

        #######################
        # ENABLE TIME HISTORY #
        #######################
        logger.info("start time history")
        print("start time history")
        self.status = obsController.RTCTTEnableTimeHistory(rtc_s)
        ufun.error_handler(self.status, "RTCTTEnableTimeHistory")

        ##############
        # CLOSE LOOP #
        ##############
        logger.info("close loop")
        print("close loop")
        self.status = obsController.RTCTTEnableLoop(rtc_s)
        ufun.error_handler(self.status, "RTCTTEnableLoop")

        #############################
        # RUN MASTER LISTNER THREAD #
        #############################
        # logger.info("run master listner thread")
        # endTime = time.clock() + duration
        # mlRTC.run()


        # run script for TTTESTTIME
        startTime = time.clock()
        et = 1
        while(time.clock() <= endTime):
            if int(time.clock()-startTime) == et:
                print ("{} sec".format(et))
                et+=1
            pass

        rtc_s_stop = rtc_s.copy()
        rtc_s_stop["TTLOOPENABLED"]    = "false"
        rtc_s_stop["TTHISTORYENABLED"] = "false"

        ##################################
        # DISABLE TIME HISTORY EXECUTION #
        ##################################
        logger.info("stop time history execution")
        print("stop time history execution")
        self.status = obsController.RTCTTEnableTimeHistory(rtc_s_stop)
        ufun.error_handler(self.status, "RTCTTEnableTimeHistory")

        #############
        # OPEN LOOP #
        #############
        logger.info("open loop")
        print("open loop")
        self.status = obsController.RTCTTEnableLoop(rtc_s_stop)
        ufun.error_handler(self.status, "RTCTTEnableLoop")

