#!/bin/python
import Basda
import BasdaMoccaX
import time

srvName = 'INBEAM_TT_CTRL'
tcpStr = 'localhost:10002'

svcPath = 'INBEAM_TT_X.SVC'
endPoint = '[NAME='+srvName+',TCP='+tcpStr+',PATH='+svcPath+']'
inbeamTTX = BasdaMoccaX.X.connectWithEndpoint('TTX', endPoint)

svcPath = 'INBEAM_TT_Y.SVC'
endPoint = '[NAME='+srvName+',TCP='+tcpStr+',PATH='+svcPath+']'
inbeamTTY = BasdaMoccaX.X.connectWithEndpoint('TTY', endPoint)


for i in range(300):
    try:
        if inbeamTTX.getPosition() < 0:
            step1 = 10
        else:
            step1 = -10
        if inbeamTTY.getPosition() < 0:
            step2 = 10
        else:
            step2 = -10

        print 'inbeamTT: {:s} {:s}'.format(str(inbeamTTX.state()), str(inbeamTTY.state()))
        c = inbeamTTX.moveRelativeStart(step1)
        c = inbeamTTY.moveRelativeStart(step2)

    except Basda.ServiceIsBusyException as e:
        print '{:s}: inbeamTT'.format(e)

    except Basda.ServiceException as e:
        print '{:s}: inbeamTT'.format(e)
    except Basda.NoResponseException as e:
        print '{:s}: inbeamTT'.format(e)

    try:
        print 'inbeamTT: {:s} {:s}'.format(str(inbeamTTX.state()), str(inbeamTTY.state()))

        c = inbeamTTX.moveRelativeWait()
        c = inbeamTTY.moveRelativeWait()

    except Basda.ServiceException as e:
        print '{:s}: inbeamTT'.format(e)
    except Basda.NoResponseException as e:
        print '{:s}: inbeamTT'.format(e)

    
    print 'Cicle {:d}'.format(i)


step1 = 100
step2 = 100
c = inbeamTTX.moveRelativeStart(step1)
c = inbeamTTY.moveRelativeStart(step2)
c = inbeamTTX.moveRelativeWait()
c = inbeamTTY.moveRelativeWait()
c = inbeamTTX.moveRelativeStart(-step1)
c = inbeamTTY.moveRelativeStart(-step2)
c = inbeamTTX.moveRelativeWait()
c = inbeamTTY.moveRelativeWait()

print 'inbeamTTX State: ', 
print(inbeamTTX.state()) 
print 'inbeamTTY State: ', 
print(inbeamTTY.state())

