


###### 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 #######  

        #get parameters from the calling interface
        try:
            print_log(testName, DEVICE,  "Running test Initialise with the following parameters:")
            print_log(testName, DEVICE,  params )
            samplingTimeWindow = int(params["timeWindowS"]["value"])
            samplingTime = float(params["samplingTimeS"]["value"])
            if samplingTime<0.001:
                samplingTime=0.001
        except:
            ret = 'Could not retrieve testing parameters - ' + traceback.format_exc()
            success = False
            sendFeedback(testPath, testName, DEVICE, ret, success)
            return
        scan = ManualScan(['id000000'], ['Motor Status (MSTA)', 'Motor Step Count (RVAL)', 'Motor Position (VAL)', 'Motor Home Switch (ATHM)', 'Encoder Count (ENCODERraw)', 'Encoder Position (ENCODER)', 'Motor/Encoder Diff', 'Drive Ready (RDY)', 'Drive interlock (ILK)', 'CAD_VALA', 'MOTOR_ATHM', 'MOTOR_LLS', 'MOTOR_HLS', 'MOTOR_DMOV', 'CAD_ODIR', 'MOTOR_HOMF', 'MOTOR_RLV', 'MOTOR_STOP', 'MOTOR_SET', 'MOTOR_OFF', 'MOTOR_VAL', 'MOTOR_DVAL', 'MOTOR_DLLM', 'MOTOR_DHLM', 'CAD_VALB', 'CAR_IVAL', 'CAR_IERR', 'SIR_VAL', 'ENCODERraw', 'ENCODERscale', 'ENCODER_oEN', 'ENCODER_HFF', 'FIRST_INIT'] )
        scan.setPlotTitle(plotName)
        scan.start()

        #Creating channels: dimension 1
        try:
            #ScalarDetector id000001
            id000001 = Channel(DEVICE+':MOTOR.MSTA', type = 'd')
            #ScalarDetector id000002
            id000002 = Channel(DEVICE+':MOTOR.RVAL', type = 'd')
            #ScalarDetector id000003
            id000003 = Channel(DEVICE+':MOTOR.VAL', type = 'd')
            #ScalarDetector id000004
            id000004 = Channel(DEVICE+':MOTOR.ATHM', type = 'd')
            #ScalarDetector id000005
            id000005 = Channel(DEVICE+':ENCODERraw', type = 'd')
            #ScalarDetector id000006
            id000006 = Channel(DEVICE+':ENCODER', type = 'd')
            #ScalarDetector id000007
            id000007 = Channel(DEVICE+':RDY', type = 'd')
            #ScalarDetector id000008
            id000008 = Channel(DEVICE+':ILK', type = 'd')

            pV_CAD_VALA =       Channel(DEVICE+':CAD.VALA',     type = 'i');
            pV_MOTOR_ATHM =     Channel(DEVICE+':MOTOR.ATHM',   type = 'i');
            pV_MOTOR_LLS =      Channel(DEVICE+':MOTOR.LLS',    type = 'i');
            pV_MOTOR_HLS =      Channel(DEVICE+':MOTOR.HLS',    type = 'i');
            pV_MOTOR_DMOV =     Channel(DEVICE+':MOTOR.DMOV',   type = 'i');
            pV_CAD_ODIR =       Channel(DEVICE+':CAD.ODIR',     type = 'i');
            pV_MOTOR_HOMF =     Channel(DEVICE+':MOTOR.HOMF',   type = 'i');
            pV_MOTOR_RLV =      Channel(DEVICE+':MOTOR.RLV',    type = 'l');
            pV_MOTOR_STOP =     Channel(DEVICE+':MOTOR.STOP',   type = 'i');
            pV_MOTOR_SET =      Channel(DEVICE+':MOTOR.SET',    type = 'i');
            pV_MOTOR_OFF =      Channel(DEVICE+':MOTOR.OFF',    type = 'i');
            pV_MOTOR_VAL =      Channel(DEVICE+':MOTOR.VAL',    type = 'l');
            pV_MOTOR_DVAL =     Channel(DEVICE+':MOTOR.DVAL',   type = 'l');
            pV_MOTOR_DLLM =     Channel(DEVICE+':MOTOR.DLLM',   type = 'd');
            pV_MOTOR_DHLM =     Channel(DEVICE+':MOTOR.DHLM',   type = 'd');   
            pV_CAD_VALB =       Channel(DEVICE+':CAD.VALB',     type = 'l');
            pV_CAR_IVAL =       Channel(DEVICE+':CAR.IVAL',     type = 'i');
            pV_CAR_IERR =       Channel(DEVICE+':CAR.IERR',     type = 'i');
            pV_SIR_VAL =        Channel(DEVICE+':SIR.VAL',      type = 'i');
            pV_ENCODERraw =     Channel(DEVICE+':ENCODERraw',   type = 'l'); 
            pV_ENCODERscale =   Channel(DEVICE+':ENCODERscale', type = 'l'); 
            pV_ENCODER_oEN =    Channel(DEVICE+':ENCODER_oEN',  type = 'i'); 
            pV_ENCODER_HFF =    Channel(DEVICE+':ENCODER_HFF',  type = 'i'); 
            pV_FIRST_INIT =     Channel(DEVICE+':FIRST_INIT',   type = 'i');              
        except:
            ret = 'Unable to create channel - ' + traceback.format_exc()
            success = False
            sendFeedback(testPath, testName, DEVICE, ret, success)
            return
        #Dimension 1
        #PseudoPositioner id000000
        samplingRange = int(float(samplingTimeWindow) / samplingTime)
        print_log(testName, DEVICE,  'Start monitoring during ' + samplingTimeWindow + 's')
        for setpoint1 in range(0, samplingRange):
            readback1 = setpoint1
            sleep( samplingTime ) # Settling time
            detector1 = id000001.get()
            detector2 = id000002.get()
            detector3 = id000003.get()
            detector4 = id000004.get()
            detector5 = id000005.get()
            detector6 = id000006.get()
            detector7 = id000007.get()
            detector8 = id000008.get()
            CAD_VALA = pV_CAD_VALA.get()
            MOTOR_ATHM = pV_MOTOR_ATHM.get()
            MOTOR_LLS = pV_MOTOR_LLS.get()
            MOTOR_HLS = pV_MOTOR_HLS.get()
            MOTOR_DMOV = pV_MOTOR_DMOV.get()
            CAD_ODIR = pV_CAD_ODIR.get()
            MOTOR_HOMF = pV_MOTOR_HOMF.get()
            MOTOR_RLV = pV_MOTOR_RLV.get()
            MOTOR_STOP = pV_MOTOR_STOP.get()
            MOTOR_SET = pV_MOTOR_SET.get()
            MOTOR_OFF = pV_MOTOR_OFF.get()
            MOTOR_VAL = pV_MOTOR_VAL.get()
            MOTOR_DVAL = pV_MOTOR_DVAL.get()
            MOTOR_DLLM = pV_MOTOR_DLLM.get()
            MOTOR_DHLM = pV_MOTOR_DHLM.get()
            CAD_VALB = pV_CAD_VALB.get()
            CAR_IVAL = pV_CAR_IVAL.get()
            CAR_IERR = pV_CAR_IERR.get()
            SIR_VAL = pV_SIR_VAL.get()
            ENCODERraw = pV_ENCODERraw.get()
            ENCODERscale = pV_ENCODERscale.get()
            ENCODER_oEN = pV_ENCODER_oEN.get()
            ENCODER_HFF = pV_ENCODER_HFF.get()
            FIRST_INIT = pV_FIRST_INIT.get()     
            #Variable Mappings
            a =  detector6
            b =  detector3
            id000009 = a-b
            scan.append ([setpoint1], [readback1], [detector1, detector2, detector3, detector4, detector5, detector6, detector7, detector8, id000009, CAD_VALA, MOTOR_ATHM, MOTOR_LLS, MOTOR_HLS, MOTOR_DMOV, CAD_ODIR, MOTOR_HOMF, MOTOR_RLV, MOTOR_STOP, MOTOR_SET, MOTOR_OFF, MOTOR_VAL, MOTOR_DVAL, MOTOR_DLLM, MOTOR_DHLM, CAD_VALB, CAR_IVAL, CAR_IERR, SIR_VAL, ENCODERraw, ENCODERscale, ENCODER_oEN, ENCODER_HFF, FIRST_INIT] )

        #Closing channels
        id000001.close()
        id000002.close()
        id000003.close()
        id000004.close()
        id000005.close()
        id000006.close()
        id000007.close()
        id000008.close()
        pV_CAD_VALA.close()
        pV_MOTOR_ATHM.close()
        pV_MOTOR_LLS.close()
        pV_MOTOR_HLS.close()
        pV_MOTOR_DMOV.close()
        pV_CAD_ODIR.close()
        pV_MOTOR_HOMF.close()
        pV_MOTOR_RLV.close()
        pV_MOTOR_STOP.close()
        pV_MOTOR_SET.close()
        pV_MOTOR_OFF.close()
        pV_MOTOR_VAL.close()
        pV_MOTOR_DVAL.close()
        pV_MOTOR_DLLM.close()
        pV_MOTOR_DHLM.close()
        pV_CAD_VALB.close()
        pV_CAR_IVAL.close()
        pV_CAR_IERR.close()
        pV_SIR_VAL.close()
        pV_ENCODERraw.close()
        pV_ENCODERscale.close()
        pV_ENCODER_oEN.close()
        pV_ENCODER_HFF.close()
        pV_FIRST_INIT.close()

        scan.end()
        
        success = True
        ret = 'Monitoring completed'
        print_log(testName, DEVICE,  ret)
############# 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)