#!/bin/python
import Basda
import BasdaMoccaX
import time
import Nice
import numpy.random as rnd

srvName = 'INBEAM_TT_CTRL'      # to be used when server controlling 2 services
tcpStr = 'localhost:10005'


# srvName = 'INBEAM_TT_XCTRL'
# tcpStr = 'localhost:10002'

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

# srvName = 'INBEAM_TT_YCTRL'
# tcpStr = 'localhost:10003'

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

print 'inbeamTTX State: {:s}'.format(str(inbeamTTX.state())) 
print 'inbeamTTY State: {:s}'.format(str(inbeamTTY.state())) 


for i in range(1000):
    stepX = rnd.random_integers(-1000,1000)
    stepY = rnd.random_integers(-1000,1000)
    try:
        # if i > 100:
        #     stepX = 20 if (inbeamTTX.getPosition() < 0) else -20
        #     stepY = 20 if (inbeamTTY.getPosition() < 0) else -20
        # else:
        #     stepX = 10 if (inbeamTTX.getPosition() < 0) else -10
        #     stepY = 10 if (inbeamTTY.getPosition() < 0) else -10

        print 'State X: {:s} | State Y: {:s}'.format(str(inbeamTTX.state()), str(inbeamTTY.state()))
        BasdaMoccaX.U9_LOG('Moving X by {:d}, moving Y by {:d} ...'.format(stepX, stepY))
        inbeamTTX.moveRelativeStart(stepX)
        inbeamTTY.moveRelativeStart(stepY)
 
    except Basda.ServiceIsBusyException as e:
        print '{:s}: inbeamTT'.format(e)

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

    try:
        # BasdaMoccaX.U9_LOG('Wait for ONLINE state from both services ...')
        # inbeamTTX.waitForState(Basda.ServiceState.ONLINE)
        # inbeamTTY.waitForState(Basda.ServiceState.ONLINE)
        # BasdaMoccaX.U9_LOG('ONLINE!')
        inbeamTTX.moveRelativeWait()
        inbeamTTY.moveRelativeWait()
        BasdaMoccaX.U9_LOG('Done'.format(stepX, stepY))
        print 'State X: {:s} | State Y: {:s}'.format(str(inbeamTTX.state()), str(inbeamTTY.state()))
    except Basda.ServiceException as e:
        print '{:s}: inbeamTT moveRelativeWait'.format(e)

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

stepX = 100
stepY = 100
inbeamTTX.moveRelativeStart(stepX)
inbeamTTY.moveRelativeStart(stepY)
inbeamTTX.moveRelativeWait()
inbeamTTY.moveRelativeWait()
inbeamTTX.moveRelativeStart(-stepX)
inbeamTTY.moveRelativeStart(-stepY)
inbeamTTX.moveRelativeWait()
inbeamTTY.moveRelativeWait()

print 'inbeamTTX State: {:s}'.format(str(inbeamTTX.state())) 
print 'inbeamTTY State: {:s}'.format(str(inbeamTTY.state())) 

# for i in range(100):
#     if inbeamTTX.getPosition() < 0:
#         stepX = 1000
#     else:
#         stepX = -1000
#     try:
#         if inbeamTTY.getPosition() < 0:
#             stepY = 1000
#         else:
#             stepY = -1000
#     except Exception as e:
#         print e
#     try:
#         inbeamTTX.moveRelativeStart(stepX)
#         time.sleep(0.5)
#     except KeyboardInterrupt:
#         raise(KeyboardInterrupt)
#     except ::Basda::MPIA::NoResponseException as e:
#         print e

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

#     try:
#         inbeamTTY.moveRelativeStart(stepY)
#         time.sleep(0.5)
#     except KeyboardInterrupt:
#         raise(KeyboardInterrupt)
#     except Exception as e:
#         print e


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

#     # inbeamTTX.
#     while not (inbeamTTX.waitForState(BasdaServiceState.ONLINE) and 
#            inbeamTTY.waitForState(BasdaServiceState.ONLINE)):
        
#     # time.sleep(10)
#     print 'Cicle {:d}'.format(i)

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