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

class testOB_both_scicam_and_teccam(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

        ###############
        # SETUP SASHA #
        ###############
        if self.paragraph("SASHA SETUP"): 
            
            # if det_s:
            #     logger.info("Detector Setup")
            #     for element, value in det_s.items():
            #         logger.debug("{:>20} : {:<20}".format(element, str(value)))
            
            logger.info("Setting up SASHA ...")
            status = obsController.sashaSetup(det_s)
            logger.info("Setting up SASHA ... DONE")
            
            logger.debug(status)
            if (status == uvars.EXIT_FAILURE):
                raise exception.TemplateException
    
        ########################
        # BEGIN EXPOSING SASHA #
        ########################
        if self.paragraph("BEGINNING EXPOSING SASHA"): 
    
            logger.info("Waiting camera exposure to complete ...")
            asyncSashaExpose = obsController.begin_sashaExpose()
            

        ########################
        # Take an image on RTC #
        ########################

        repetitions = int(rtc_s["REPEAT"])
        for repetition in range(1, repetitions+1):

            if self.paragraph("EXPOSING TECCAM"): 
                
                logger.info("TT take image {}/{}".format(repetition, repetitions))
                asyncRtc = obsController.begin_RTCTTTeccamExpose(rtc_s)
                
                self.status, rtc_fits_file = obsController.end_RTCTTTeccamExpose(asyncRtc)
                ufun.error_handler(self.status, "RTCTTTeccamExpose")
                logger.info("TT take image: fits cube is {}".format(rtc_fits_file))
                
                
        #######################
        # WAIT EXPOSING SASHA #
        #######################
        if self.paragraph("WAITING EXPOSING SASHA"): 
                
            self.status, self.fitsfilenames = obsController.end_sashaExpose(asyncSashaExpose)
            logger.info("FITS files are "+str(self.fitsfilenames))
            
