
###### Init - DO NOT MODIFY THE CODE BELOW ######
global sys, inspect, os, traceback
import sys, inspect, os, traceback
    
def startTest(testName, DEVICE, params):
    #by default, assume the test failed 
    ret = 'Test failed'
    success = False
    #plot name to be given to the scan. Use: scan.setPlotTitle(plotName)
    plotName = DEVICE + ' - ' + testName
    #put the whole custom code under try/catch
    try:
        #get the path of this script
        testPath = inspect.getfile(inspect.currentframe()) 
        #init the testing tool class. It can be sued in the following ways:
        test = TestingTool(testName, testPath, DEVICE, params)
    
######### WRITE YOUR CODE HERE BELOW #############
        #get parameters from the calling interface
        try:
            #test.log( "with the following parameters:\n" + str(params))
            #test.log( 'testpath: ' + testPath )
            #test.log( 'parameters:' + str( params) )
            #test.log( 'device: ' + DEVICE )
            monitorTime = float(test.getParam("monitorTime"))
            samplePeriod = 0.05 #seconds
        except:
            ret = 'Could not retrieve testing parameters - ' + traceback.format_exc()
            success = False
            test.sendFeedback( ret, success)
            return

        #get parameters from the calling interface
        scan = ManualScan(['time [s]'],
                          ['Motor Pos (IST3:2)', 'Poti Position (IST2:1)', 'Motor Pos - Poti Pos'] )
        scan.setPlotTitle(plotName)
        scan.start()

        #Creating channels: dimension 1
        try:
            idCom               = Channel(DEVICE+':COM:2' , type = 'd') #current position as from motor step counter [mm]
            idMotorPosition     = Channel(DEVICE+':IST3:2', type = 'd') #current position as from motor step counter [mm]
            #idPotiPosFromBeam   = Channel(DEVICE+':IST1:2', type = 'd') #current position from beam as from potentiometer [mm]
            idPotiPosition      = Channel(DEVICE+':IST2:1', type = 'd') #current position as from potentiometer [mm]
            #idPotiRef1Position  = Channel(DEVICE+':REF1:1', type = 'd') #R1 position as from potentiometer [mm]
            #idPotiRef2Position  = Channel(DEVICE+':REF2:1', type = 'd') #R2 position as from potentiometer [mm]

        except:
            test.sendFeedback( 'Unable to create channel - ' + traceback.format_exc(), False)
            return

        r1 = tscan((idMotorPosition, idPotiPosition), int(monitorTime/0.02), 0.02)


        test.log(  'Monitoring movement for ' + str(monitorTime) + 's')
        #scan quickly the output during some seconds
        timeElapsed=0
        startTime=float(java.lang.System.currentTimeMillis())
        currentTime = float(java.lang.System.currentTimeMillis())
        timeElapsed=currentTime-startTime
        while timeElapsed<(monitorTime*1000): 
            #Detector time
            currentTime = float(java.lang.System.currentTimeMillis())
            timeElapsed=currentTime-startTime
            motorPosition    = idMotorPosition.get()
            #potiPosFromBeam  = idPotiPosFromBeam.get()
            potiPosition     = idPotiPosition.get()
            #potiRef1Position = idPotiRef1Position.get()
            #potiRef2Position = idPotiRef2Position.get()
            diff1 = motorPosition-potiPosition
            scan.append ([timeElapsed/1000], [timeElapsed/1000], [motorPosition, potiPosition, diff1])
            sleep( samplePeriod ) # Settling time
            #timeElapsed=timeElapsed+1
            
        #Closing channels
        idCom.close()
        idMotorPosition.close()
        #idPotiPosFromBeam.close()
        idPotiPosition.close()
        #idPotiRef1Position.close()
        #idPotiRef2Position.close()
        ret = 'End of Monitoring'
        success = True    
      
################ END OF YOUR CODE ################
###### Final - DO NOT MODIFY THE CODE BELOW ######
        
        #just in case the feedback was forgotten
        test.sendFeedback(ret, success)
    except:
        #generic error handler
        ret = traceback.format_exc()
        success = False
        test.sendFeedback(ret, success)

#launch the test
startTest(test, device, parameters)