
###### DO NOT MODIFY THE CODE BELOW ######
def startTest(testName, DEVICE, params):
    #get the path of this script
    testPath = inspect.getfile(inspect.currentframe()) 
    #by default, failed 
    ret = 'Test failed'
    success = False   
    #plot name to be given to the scan. Use: scan.setPlotTitle(plotName)
    plotName = DEVICE + ' - ' + testName    
###### WRITE YOUR CODE HERE BELOW #######   
    #get parameters from the calling interface
    print_log(testName, DEVICE,  'testpath: ' + testPath )
    print_log(testName, DEVICE, 'parameters:' + str( params) )
    print_log(testName, DEVICE, 'device: ' + DEVICE )
    scan = ManualScan(['time'], ['idMotorStep', 'idPotiPosition', 'idPotiRef1Position','idMotorStep-idPotiPosition'] , [0.0], [30.0], [20])
    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]
        idMotorStep = 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:
        sendFeedback(testPath, testName, DEVICE, 'Unable to create channel - ' + traceback.format_exc(), False)
        #raise Exception('Unable to create channel - ' + traceback.format_exc())   
        return

    idCom.put('3', timeout=None) # go to R1
    print_log(testName, DEVICE,  'Moving to reference point')
    #scan quickly the output during some seconds
    detector4 = idPotiPosition.get()
    detector6 = idPotiRef2Position.get()
    timeElapsed=0
    while detector4>detector6 and timeElapsed<600: 
        #Detector time
        detector1 = float(java.lang.System.currentTimeMillis())

        detector2 = idMotorStep.get()
        detector3 = idPotiPosFromBeam.get()
        detector4 = idPotiPosition.get()
        detector5 = idPotiRef1Position.get()
        detector6 = idPotiRef2Position.get()
        diff1 = detector2-detector4
        scan.append ([detector1], [detector1], [detector2, detector4, detector5, diff1]) 
        sleep( 0.1 ) # Settling time        
        timeElapsed=timeElapsed+1
        
    #Closing channels
    idCom.close()
    idMotorStep.close()
    idPotiPosFromBeam.close()
    idPotiPosition.close()
    idPotiRef1Position.close()
    idPotiRef2Position.close()
    print_log(testName, DEVICE,  ' Reference point reached')
    
########## END OF YOUR CODE ###########

###### DO NOT MODIFY THE CODE BELOW ######        
    sendFeedback(testPath, testName, DEVICE, ret, success)

#prepare and send feedback to calling tool   
def sendFeedback(testPath, testName, DEVICE, returnString, testPassed):
    print_log(testName, DEVICE, 'End of test. Result:')
    print_log(testName, DEVICE,  'Test path: ' + testPath)
    print_log(testName, DEVICE,  'Test name: ' + testName    )
    print_log(testName, DEVICE,  'Device: ' + DEVICE)
    print_log(testName, DEVICE,  'Test passed: ' + str(testPassed))
    print_log(testName, DEVICE,  'Return string: ' + returnString)
    ret = [testPath, DEVICE, returnString, testPassed]
    set_return(ret)
    
def print_log(testName, DEVICE, text):
    time.ctime()
    now = time.strftime('%Y.%m.%d %H:%M:%S') 
    print now + ' ' + DEVICE + ' - ' + testName + ': ' + text
       
import sys, inspect, os, traceback
#get test arguments
DEVICE = device
testName = test
params = parameters
#launch the test
startTest(testName, DEVICE, params)
