# $Header: https://oberon.oapd.inaf.it/shins/trunk/shins/src/scripts/templates/SHINS_SW_TEST/SHINS_TEST_MOVE_RELATIVE_CAL_FIBER_DEP.py 3724 2021-10-15 14:26:48Z laud $

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 Basda
import Ice
import pyds9

# cp /home/shinsmgr/shins/src/scripts/templates/SHINS_SW_TEST/SHINS_TEST_MOVE_RELATIVE_CAL_FIBER_DEP.py /opt/tan/lib/python/site-packages/shins/templates/SHINS_SW_TEST

class SHINS_TEST_MOVE_RELATIVE_CAL_FIBER_DEP_App(SHINSApp):
    def run(self, ins_s=None, det_s=None, tel_s=None, rtc_s=None, **kwargs):
        '''template for log INDI messages
        '''
        #logger.info(__name__)

        self.status = uvars.EXIT_SUCCESS
        lsFitsFilename = []
        units_s = {}
        det_s = {}
        ErrorCounter = 0
        try:
            obsController = self.obsController

            ############################################################
            ######################## Debug Test ########################
            ############################################################
            obsController.sashaGetIndiMsges({"FTEST" : "3", "SEPARATOR" : "Debug Test"})
            logger.info("##############################################################")
            logger.info("######################### DEBUG TEST #########################")
            logger.info("##############################################################")

            ######### TEST 5: CAL FIBER Safe moving to Danger #########
            logger.info("##############################################################")
            logger.info("########## TEST 5: CAL FIBER Safe moving to Danger ###########")
            logger.info("##############################################################")
            logger.info("####################### TEST 5: Preset #######################")
            obsController.sashaGetIndiMsges({"FTEST" : "3", "SEPARATOR" : "TEST 5: CAL FIBER Safe moving to Danger"})
            det_s.clear()
            det_s["CORO_SLIT_W"] = "GAUSS"
            det_s["APODIZER_W"] = "SP1_APO"
            det_s["CAL_FIBER_DEP"] = "-1595000"
            logger.info("Moving CORO Wheel to %s" % det_s["CORO_SLIT_W"])
            logger.info("Moving APODIZER Wheel to %s" % det_s["APODIZER_W"])
            logger.info("Moving CAL_FIBER_DEP to %s" % det_s["CAL_FIBER_DEP"])
            var = input("Press 1 and Enter To Continue...")
            if var == 1:
                async = obsController.begin_setupInstrument(det_s)
                self.status = obsController.end_setupInstrument(async)
                if(self.status == uvars.EXIT_FAILURE):
                    logger.error("Error in TEST 4")
                ufun.error_handler(self.status, "setupInstrument")


            logger.info("######################## TEST 5: Run #########################")
            det_s.clear()
            det_s["CORO_SLIT_W"] = "SLIT2"
            det_s["APODIZER_W"] = "R@-8000"
            det_s["CAL_FIBER_DEP"] = "R#-10000"
            logger.info("Moving CORO Wheel to %s" % det_s["CORO_SLIT_W"])
            logger.info("Moving APODIZER Wheel to %s" % det_s["APODIZER_W"])
            logger.info("Moving CAL_FIBER_DEP to %s" % det_s["CAL_FIBER_DEP"])
            var = input("Press 1 and Enter To Continue...")
            if var == 1:
                async = obsController.begin_setupInstrument(det_s)
                self.status = obsController.end_setupInstrument(async)
                if(self.status == uvars.EXIT_FAILURE):
                    logger.error("Error in TEST 5")
                ufun.error_handler(self.status, "setupInstrument")

            ######### TEST 7: CAL FIBER In danger moving to Safe #########
            logger.info("##############################################################")
            logger.info("######### TEST 7: CAL FIBER In danger moving to Safe #########")
            logger.info("##############################################################")
            logger.info("####################### TEST 7: Preset #######################")
            obsController.sashaGetIndiMsges({"FTEST" : "3", "SEPARATOR" : "TEST 7: CAL FIBER In danger moving to Safe"})
            det_s.clear()
            det_s["CORO_SLIT_W"] = "GAUSS"
            det_s["APODIZER_W"] = "SP1_APO"
            det_s["CAL_FIBER_DEP"] = "-1605000"
            logger.info("Moving CORO Wheel to %s" % det_s["CORO_SLIT_W"])
            logger.info("Moving APODIZER Wheel to %s" % det_s["APODIZER_W"])
            logger.info("Moving CAL_FIBER_DEP to %s" % det_s["CAL_FIBER_DEP"])
            var = input("Press 1 and Enter To Continue...")
            if var == 1:
                async = obsController.begin_setupInstrument(det_s)
                self.status = obsController.end_setupInstrument(async)
                if(self.status == uvars.EXIT_FAILURE):
                    logger.error("Error in TEST 4")
                ufun.error_handler(self.status, "setupInstrument")


            logger.info("######################## TEST 7: Run #########################")
            det_s.clear()
            det_s["CORO_SLIT_W"] = "SLIT2"
            det_s["APODIZER_W"] = "R@-8000"
            det_s["CAL_FIBER_DEP"] = "R#10000"
            logger.info("Moving CORO Wheel to %s" % det_s["CORO_SLIT_W"])
            logger.info("Moving APODIZER Wheel to %s" % det_s["APODIZER_W"])
            logger.info("Moving CAL_FIBER_DEP to %s" % det_s["CAL_FIBER_DEP"])
            var = input("Press 1 and Enter To Continue...")
            if var == 1:
                async = obsController.begin_setupInstrument(det_s)
                self.status = obsController.end_setupInstrument(async)
                if(self.status == uvars.EXIT_FAILURE):
                    logger.error("Error in TEST 5")
                ufun.error_handler(self.status, "setupInstrument")

            ######### TEST 8: CAL FIBER in Danger moving to Danger #########
            logger.info("##############################################################")
            logger.info("########## TEST 8: CAL FIBER in Danger moving to Danger ###########")
            logger.info("##############################################################")
            logger.info("####################### TEST 8: Preset #######################")
            obsController.sashaGetIndiMsges({"FTEST" : "3", "SEPARATOR" : "TEST 8: CAL FIBER in Danger moving to Danger"})
            det_s.clear()
            det_s["CORO_SLIT_W"] = "GAUSS"
            det_s["APODIZER_W"] = "SP1_APO"
            det_s["CAL_FIBER_DEP"] = "-1605000"
            logger.info("Moving CORO Wheel to %s" % det_s["CORO_SLIT_W"])
            logger.info("Moving APODIZER Wheel to %s" % det_s["APODIZER_W"])
            logger.info("Moving CAL_FIBER_DEP to %s" % det_s["CAL_FIBER_DEP"])
            var = input("Press 1 and Enter To Continue...")
            if var == 1:
                async = obsController.begin_setupInstrument(det_s)
                self.status = obsController.end_setupInstrument(async)
                if(self.status == uvars.EXIT_FAILURE):
                    logger.error("Error in TEST 4")
                ufun.error_handler(self.status, "setupInstrument")


            logger.info("######################## TEST 5: Run #########################")
            det_s.clear()
            det_s["CORO_SLIT_W"] = "SLIT2"
            det_s["APODIZER_W"] = "R@-8000"
            det_s["CAL_FIBER_DEP"] = "R#-10000"
            logger.info("Moving CORO Wheel to %s" % det_s["CORO_SLIT_W"])
            logger.info("Moving APODIZER Wheel to %s" % det_s["APODIZER_W"])
            logger.info("Moving CAL_FIBER_DEP to %s" % det_s["CAL_FIBER_DEP"])
            var = input("Press 1 and Enter To Continue...")
            if var == 1:
                async = obsController.begin_setupInstrument(det_s)
                self.status = obsController.end_setupInstrument(async)
                if(self.status == uvars.EXIT_FAILURE):
                    logger.error("Error in TEST 5")
                ufun.error_handler(self.status, "setupInstrument")

        except Basda.ServiceException as e:
            logger.error("Service Exception was thrown during insturment setup {}".format(e.errmsg))
            self.status = uvars.EXIT_FAILURE
        except exception.TemplateException as e:
            logger.error(e)
            raise e
        except exception as e:
            logger.error(e)
            raise e
        except StandardError as e:
            logger.error(e)
            raise e

        return self.status



def SHINS_TEST_MOVE_RELATIVE_CAL_FIBER_DEP(ins_s=None, det_s=None, tel_s=None, rtc_s=None, **kwargs):
    """
    Test template, listening on Indi In channel

    Parameters not used
    ----------
    ins_s : instument setup dictionary; keys are tags from OB file
    det_s : detector setup dictionary; keys are tags from OB file
    tel_s : telescope setup dictionary; keys are tags from OB file
    rtc_s : real-time control system dictonary; keys are tags from OB file
    **kwargs : optional dictionary with additional control keywords.
               E.g. {"EXIT":False} is used to prevent template to call
               sys.exit() method

    Returns
    -------
    Integer value with status of operation:
    0 for success
    1 for failure
    """

    logger.info('SHINS_TEST_MOVE_RELATIVE_CAL_FIBER_DEP: begin')
    
    app = SHINS_TEST_MOVE_RELATIVE_CAL_FIBER_DEP_App()
    if not (uvars.filenameSHINS in kwargs.keys()):
        kwargs[uvars.filenameSHINS] = SHINS_TEST_MOVE_RELATIVE_CAL_FIBER_DEP.__name__
    status = app.main(ins_s, det_s, tel_s, rtc_s, **kwargs)

    logger.info('SHINS_TEST_MOVE_RELATIVE_CAL_FIBER_DEP: end')
    return status


if __name__ == "__main__":
    SHINS_TEST_MOVE_RELATIVE_CAL_FIBER_DEP(ins_s=None, det_s=None, tel_s=None, rtc_s=None, **kwargs)
