# $Header: https://oberon.oapd.inaf.it/shins/trunk/shins/test/obs_ctrl/py/obsControlClient.py 262 2017-10-25 16:15:38Z depa $

# obsControlClient is actually the Sequencer component.
import sys, traceback, Ice
import argparse
import sharknir

if __name__ == "__main__":

    parser = argparse.ArgumentParser()
    parser.add_argument("--inbeam_dep"  , type=int,
                        help="Commanded position for input beam deployable arm (in steps)")
    parser.add_argument("--inbeam_tt_x" , type=int,
                        help="Commanded X position for input beam \"tip/tilt\" mirror (in steps)")
    parser.add_argument("--inbeam_tt_y" , type=int,
                        help="Commanded Y position for input beam \"tip/tilt\" mirror (in steps)")
    parser.add_argument("--shutter"     , type=int,
                        help="Commanded X position for entrance shutter (in steps)")
    #parser.add_argument("adc1", type=int, help="...")
    #parser.add_argument("adc2", type=int, help="...")
    parser.add_argument("--apo"         , type=int,
                        help="Commanded named position for Apodizer wheel")
    parser.add_argument("--nd"          , type=int,
                        help="Commanded named position for  wheel ND wheel")
    parser.add_argument("--coro"        , type=int,
                        help="Commanded named position for Coro-Slit wheel")
    parser.add_argument("--lyot"        , type=int,
                        help="Commanded named position for Lyot-Grism wheel")
    parser.add_argument("--sci1"        , type=int,
                        help="Commanded named position for Scientific wheel 1")
    parser.add_argument("--sci2"        , type=int,
                        help="Commanded named position for Scientific wheel 2")
    parser.add_argument("--lens_dep"    , type=int,
                        help="Commanded named position for pupil lens deployer")
    parser.add_argument("--db"          , type=int,
                        help="Commanded named position for Dual Band wheel")
    #parser.add_argument("derot", type=int, help="...")
    # NOTE: all missing devices will be added once code to control them had been developed

    args = parser.parse_args()

    status = 0
    ic = None # Ice Communicator
    
    dictNonSci = {
        1 : sharknir.NonSciAssembly("Direct Imaging Field Stabilized", 1, 1, 1, True),
        2 : sharknir.NonSciAssembly("Direct Imaging Pupil Stabilized", 1, 1, 1, False),
        3 : sharknir.NonSciAssembly("Gaussian Lyot Coronograph", 1, 2, 2, True)
    }



    dictSetup = {
        "INBEAM_DEP"  : args.inbeam_dep,
        "INBEAM_TT_X" : args.inbeam_tt_x,
        "INBEAM_TT_Y" : args.inbeam_tt_y,
        "SHUTTER"     : args.shutter,
        "APO_W"       : args.apo,
        "ND_W"        : args.nd,
        "CORO_W"      : args.coro,
        "LYOT_W"      : args.lyot,
        "SCI1_W"      : args.sci1,
        "SCI2_W"      : args.sci2,
        "LENS_DEP"    : args.lens_dep,
        "DB_W"        : args.db
    }

    dictSetupTest = {}

    for key in dictSetup.keys():
        if dictSetup[key] != None:
            dictSetupTest[key] = dictSetup[key];




    strSep = " "
    strSetupMotion = "APO_W={}".format(args.apo) +\
                     strSep +\
                     "ND_W={}".format(args.nd) +\
                     strSep +\
                     "CORO_W={}".format(args.coro) +\
                     strSep +\
                     "LYOT_W={}".format(args.lyot) +\
                     strSep +\
                     "SCI1_W={}".format(args.sci1) +\
                     strSep +\
                     "SCI2_W={}".format(args.sci2) +\
                     strSep +\
                     "DB_W={}".format(args.db)
    # NOTE: all missing devices will be added once code to control them had been developed
    strSetupTracking = ""
    strSetupCalibration = ""



    try:
        ic = Ice.initialize(sys.argv)
        base = ic.stringToProxy("SimpleObsControlAdapter:default -p 10000")
        obsController = sharknir.obsControl.SequencerPrx.checkedCast(base)
        if not obsController:
            raise RuntimeError("Invalid Proxy")

        # status = obsController.setupMotion(dictNonSci, strSetupMotion)
        # print "Wheel configuration string is: {:s}".format(strSetupMotion)
        status = obsController.setupMotionDict(dictSetupTest)
    except:
        traceback.print_exc()
        status = 1

    if ic:
    # Clean up
        try:
            ic.destroy()
        except:
            traceback.print_exc()
            status = 1

    sys.exit(status)
