


###### DO NOT MODIFY THE CODE BELOW ######
global print_log, sendFeedback, inspect, log, sys, inspect, os, traceback
import sys, inspect, os, traceback

def print_log(testName, DEVICE, text):
    time.ctime()
    now = time.strftime('%Y.%m.%d %H:%M:%S') 
    print now + ' ' + DEVICE + ' - ' + testName + ': ' + str(text)

#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,  'Device:        ' + DEVICE)
    print_log(testName, DEVICE,  'Test name:     ' + testName)
    print_log(testName, DEVICE,  'Test path:     ' + testPath)
    print_log(testName, DEVICE,  'Test passed:   ' + str(testPassed))
    print_log(testName, DEVICE,  'Return string: ' + returnString)
    ret = [testPath, DEVICE, returnString, testPassed]
    set_return(ret)

def startTest(testName, DEVICE, params):
    try:
        import traceback
        #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 #######   

        # caput(DEVICE+':INIT.PROC', '1')
         
        #get parameters from the calling interface
        try:
            print_log(testName, DEVICE,  "Running test Initialise with the following parameters:")
            print_log(testName, DEVICE,  params )
            loopTimes = int(params["repeatTimes"]["value"])
            delaySeconds = int(params["delayS"]["value"])
        except:
            ret = 'Could not retrieve testing parameters - ' + traceback.format_exc()
            success = False
            sendFeedback(testPath, testName, DEVICE, ret, success)
            return
            
        scan = ManualScan(['idX'], ['idPositioner'] )
        scan.setPlotTitle(plotName)
        scan.start()
        try:
            idInit = Channel(DEVICE+':DEMAND', type = 'l')
        except:
            ret = 'Unable to create channel - ' + traceback.format_exc()
            success = False
            sendFeedback(testPath, testName, DEVICE, ret, success)
            return

        for count in range(1, loopTimes+1):
            print_log(testName, DEVICE,  'Positioning sequence #' + str(count) + '/' + str(loopTimes))
            timeStampStart = float(java.lang.System.currentTimeMillis())
            for positioner in range(1, 143):
                #Detector time
                idInit.put(positioner, timeout=1000) # TODO: Set appropriate timeout
                timeStamp = float(java.lang.System.currentTimeMillis())
                sleep( 0.01 )
                detector = idInit.get()
                scan.append ([timeStamp],[timeStamp], [detector])
            if(count < loopTimes):
                print_log(testName, DEVICE,  'Next sequence starting in ' + str(delaySeconds) + 's')
                sleep( delaySeconds ) # pause between two sequences
               
        idInit.close()
        scan.end()     

############# END OF YOUR CODE ###########
###### DO NOT MODIFY THE CODE BELOW ######        
        sendFeedback(testPath, testName, DEVICE, ret, success)
    except:
        ret = traceback.format_exc()
        success = False
        sendFeedback(testPath, testName, DEVICE, ret, success)
        return 

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