#!/bin/python
import Basda
import BasdaMoccaXY
import Nice
import time

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


svcPath = 'INBEAM_TT_XY.SVC'
endPoint = '[NAME='+srvName+',TCP='+tcpStr+',PATH='+svcPath+']'
inbeamTTXY = BasdaMoccaXY.XY.connectWithEndpoint('TTXY', endPoint)


print 'inbeamTTX State: ', 
print(inbeamTTXY.state())
nicePoint = Nice.Point()

for i in range(1000):
    try:
        nicePoint=inbeamTTXY.getPosition()
        if nicePoint.x() < 0:
            nicePoint.setX(10)
        else:
            nicePoint.setX(-10)
        
        if nicePoint.y() < 0:
            nicePoint.setY(10)
        else:
            nicePoint.setY(-10)

        
        print 'inbeamTTXY: {:s}'.format(str(inbeamTTXY.state()))
        
        c = inbeamTTXY.moveRelativeStart(nicePoint)

    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)
        
    inbeamTTXY.moveRelativeWait()
    
    print 'Cicle {:d}'.format(i)


nicePoint.setX(100)
nicePoint.setY(-100)

inbeamTTXY.moveRelativeStart(nicePoint)
inbeamTTXY.moveRelativeWait()
nicePoint.setX(-100)
nicePoint.setY(+100)
inbeamTTXY.moveRelativeStart(nicePoint)
inbeamTTXY.moveRelativeWait()

print 'inbeamTTXY State: ', 
print(inbeamTTXY.state()) 
