

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

from SHARKNIR_gen_base import SHARKNIR_gen_base

class SHARKNIR_gen_tiptilt_dark(SHINSApp):

    @exception.handler_for_run
    def run(self, ins_s=None, det_s=None, tel_s=None, rtc_s=None, **kwargs):
        '''Close tiptilt loop
        '''

        logger.info(__name__)
        logger.info('Taking dark for TT ... ')

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

        self.gen_base = SHARKNIR_gen_base();        gen_base = self.gen_base

        ###################################
        # START AND WAIT INSTRUMENT SETUP #
        ###################################

        logger.info("Waiting setup ...")
        self.status, _, __ = gen_base.main(ins_s, **kwargs)
        logger.info("Waiting setup ... DONE")
        
        ########################
        # Take an image on RTC #
        ########################
        if self.paragraph("Take an image on RTC"):
        
            logger.debug("Enumerating RTC setup")
            for element, value in rtc_s.items():
                logger.debug("Element : % 20s  -  Value % 20s    " % (element, value))
            
            logger.info("TT update dark")
            self.status, self.filenames = obsController.RTCTTTeccamExpose(rtc_s)
            ufun.error_handler(self.status, "RTCTTTeccamExpose")
            logger.info("TT update dark... DONE. fits cube is {}".format(self.filenames))
