# $Header: https://oberon.oapd.inaf.it/shins/trunk/shins/test/end_2_end/end_2_end.py 435 2018-02-09 08:16:29Z depa $

import numpy.random as npr

import telnet_connection
import telnet_motion
import Nice
import Basda
import BasdaMoccaWheel as moccaW
import BasdaMoccaX as moccaX
import BasdaMoccaXY as moccaXY
import test_single_TANservice_connection as TANcon
import test_multi_TANservice_connection as TANmultiCon
import timer

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

if __name__ == "__main__":
    # hosts dictionary contains tuple with ip address and flag standing for host is online 
    hosts = {
        "host0": ["192.168.1.51",   0], # test MoCon in office
        "host1": ["192.168.103.50", 0], # MoCon1 Stepper motors: 2 ADC, 1 rotator 
        "host2": ["192.168.103.51", 1], # MoCon2 DC motors (deployers + actuators)
        "host3": ["192.168.103.52", 1]  # MoCon3 DC motors (wheels + shutter)
    }

    modules = {
        "host0": [1,2],
        "host1": [1,2,3,5,6],
        "host2": [5,6], #[1,2,3,4,5,6],
        "host3": [1,2,3,4,5,6,7] #,8]
    }
    onlineFlag = 0

    # TEST TELNET CONNECTIONS 

    print ("> ---------------------------------")
    print ("> START TESTS OVER TELNET CONNETION")
    print ("> ---------------------------------")


    for key in hosts.keys():
        if (hosts[key][1] != 0):
            onlineFlag = telnet_connection.test_connection(hosts[key][0])
            hosts[key][1] = onlineFlag

            try:
                for key in modules.keys():
                    if hosts[key][1] == 1:
                        print key, modules[key]
                        telnet_motion.test_motion(modules[key])
            except Exception as error:
                print "Telnet motion test failed on " + key + "for: ", error

    print ("> -------------------------------")
    print ("> START BASDARD DAEMONS VIA MONIT")
    print ("> -------------------------------")
    raw_input("Press key ...")

    portMotion  = 50200
    portTrack   = 50300
    portCalUnit = 50400

    dictServiceNamePorts = {
        "host0": [("TEST_MOTION", "WHEELS.APO_W", 10300, "wheel"),
                  ("TEST_MOTION", "WHEELS.ND_W" , 10300, "wheel")],

        "host1": [("TRACK_CTRL", "ADC.PRISMS", portTrack, "xy"),
                  ("TRACK_CTRL", "DROT.DEROTATOR.1", portTrack, "wheel"), # these 2 will not work
                  ("TRACK_CTRL", "DROT.DEROTATOR.2", portTrack, "wheel")],# service names have to be better checked

        "host2": [("MOTION_CTRL"  , "BEAM.INBEAM_DEP" , portMotion, "x"),
                  ("MOTION_CTRL"  , "BEAM.INBEAM_TT"  , portMotion, "xy"),
                  ("MOTION_CTRL"  , "BEAM.LENS_DEP"   , portMotion, "x"),
                  ("CALUNIT_CTRL" , "CAL_MIRROR_DEP"  , portTrack, "x"),
                  ("CALUNIT_CTRL" , "FIBER_DEP"       , portTrack, "x")],

        "host3": [("MOTION_CTRL", "BEAM.SHUTTER", portMotion, "x"),
                  ("MOTION_CTRL", "WHEELS.APO_W", portMotion, "wheel"),
                  ("MOTION_CTRL", "WHEELS.ND_W", portMotion, "wheel"),
                  ("MOTION_CTRL", "WHEELS.CORO_W", portMotion, "wheel"),
                  ("MOTION_CTRL", "WHEELS.LYOT_W", portMotion, "wheel"),
                  ("MOTION_CTRL", "WHEELS.SCI1_W", portMotion, "wheel"),
                  ("MOTION_CTRL", "WHEELS.SCI1_W", portMotion, "wheel"),
                  ("MOTION_CTRL", "WHEELS.DB_W", portMotion, "wheel")]
    }
    dictServiceNamePortsDUMMY = {
        "host0": [("TEST_MOTION", "WHEELS.APO_W", portTrack, "wheel"),
                  ("TEST_MOTION", "WHEELS.ND_W" , portTrack, "wheel")],

        "host1": [("TRACK_CTRL-DUMMY", "ADC-DUMMY.PRISMS-DUMMY", portTrack, "xy"),
                  ("TRACK_CTRL-DUMMY", "DROT-DUMMY.DEROTATOR-DUMMY.1", portTrack, "wheel"), # these 2 will not work
                  ("TRACK_CTRL-DUMMY", "DROT-DUMMY.DEROTATOR-DUMMY.2", portTrack, "wheel")],# service names have to be better checked

        "host2": [("MOTION_CTRL-DUMMY"  , "BEAM-DUMMY.INBEAM_DEP-DUMMY" , portMotion, "x"),
                  ("MOTION_CTRL-DUMMY"  , "BEAM-DUMMY.INBEAM_TT-DUMMY"  , portMotion, "xy"), # the service receives as input a Nice::Point
                  ("MOTION_CTRL-DUMMY"  , "BEAM-DUMMY.LENS_DEP-DUMMY"   , portMotion, "x"),
                  ("CALUNIT_CTRL-DUMMY" , "CAL_MIRROR_DEP-DUMMY"  , portCalUnit, "x"),
                  ("CALUNIT_CTRL-DUMMY" , "FIBER_DEP-DUMMY"       , portCalUnit, "x")],

        "host3": [("MOTION_CTRL-DUMMY", "WHEELS-DUMMY.APO_W-DUMMY", portMotion, "wheel"),
                  ("MOTION_CTRL-DUMMY", "WHEELS-DUMMY.ND_W-DUMMY", portMotion, "wheel"),
                  ("MOTION_CTRL-DUMMY", "WHEELS-DUMMY.CORO_W-DUMMY", portMotion, "wheel"),
                  ("MOTION_CTRL-DUMMY", "WHEELS-DUMMY.LYOT_W-DUMMY", portMotion, "wheel"),
                  ("MOTION_CTRL-DUMMY", "WHEELS-DUMMY.SCI1_W-DUMMY", portMotion, "wheel"),
                  ("MOTION_CTRL-DUMMY", "WHEELS-DUMMY.SCI2_W-DUMMY", portMotion, "wheel"),
                  ("MOTION_CTRL-DUMMY", "WHEELS-DUMMY.DB_W-DUMMY", portMotion, "wheel")]#,
                  # ("MOTION_CTRL-DUMMY", "BEAM-DUMMY.SHUTTER-DUMMY", portMotion, "x")] 
    }
    T = timer.timer() # initilize timer T

    isDummy = True

    # test single connection
    hostPort = 10300
    for key in hosts.keys():
        hostIP = hosts[key][0]
        onlineFlag = hosts[key][1]
        if  (onlineFlag == 1):
            print "> Testing single TaN connection, moving motors to random position ..."

            if isDummy:
                dictJolly = dictServiceNamePortsDUMMY
            else:
                dictJolly = dictServiceNamePorts

            for svc in dictJolly[key]:
                # print dictJolly[key]
                serverName = svc[0]
                hostPort = svc[2]
                serviceName = "{}.{}".format(serverName,svc[1])
                serverName = serverName.lower()
                serverName = serverName.replace("-dummy", "-svr-DUMMY")
                # print serverName
                devType = svc[3]
                #raw_input("> Press key to connect to {} TAN service ...".format(svc[1]))
                device = TANcon.TAN_connect(serverName, hostIP, hostPort, serviceName, svc[1], devType)
                if (devType == "wheel"):
                    steps = npr.randint(0,1985482)
                if ((svc[1] == "CAL_MIRROR_DEP-DUMMY") or (svc[1] == "FIBER_DEP-DUMMY")):
                    steps = npr.randint(-3640883, 0)
                    # steps = npr.randint(-3407172, 401742)
                if (svc[1] == "BEAM-DUMMY.INBEAM_DEP-DUMMY"):
                    steps = npr.randint(-4292060, 4292060)
                if (svc[1] == "BEAM-DUMMY.LENS_DEP-DUMMY"):
                    steps = npr.randint(0, 3605244);
                if (svc[1] == "BEAM-DUMMY.INBEAM_TT-DUMMY"):
                    steps = Nice.Point(npr.randint(0, 2475804), npr.randint(0, 2475804))
                T.begin()
                TANcon.TAN_service_move_absolute(device, steps)
                print "> {} - Elapsed time: {}".format(svc[1], T.elapsed())

    # test parallel multiple connection
    T.begin()
    TANmultiCon.TAN_multi_connect(hosts, dictJolly)
    print "{}> Test elapsed time: {} {}".format(bcolors.OKGREEN , T.elapsed(), bcolors.ENDC)
