#!/usr/bin/python

import time
import sys
import subprocess

#
# Some EPICS PVs need to be simulation mode when we test code without PXI board.
# When user uses sim mode, new value must be written into PV.SVAL and PV itselfself.
#
print '### Start State Machine Sequence Test ###'

def test_setup():
    """
    Setup EPICS PVs for testing.
    Turn simulation mode ON for unavailable device
    """
    print '---------- Start setup for the test ----------'

    print '1. set PVs to simulation mode'
    #res = subprocess.call('caput EC-GN-P01-GAF:STAT-AI-SIMM YES', shell=True)
    #res = subprocess.call('caput EC-GN-P01-GBF:STAT-AI-SIMM YES', shell=True)
    #res = subprocess.call('caput EC-GN-P01-GAF:STAT-DI-SIMM YES', shell=True)
    #res = subprocess.call('caput EC-GN-P01-GBF:STAT-DI-SIMM YES', shell=True)
    #temporary commentout 2 lines
    #res = subprocess.call('caput EC-GN-P01-GAF-MCPS:STAT-SIMM YES', shell=True) 
    #res = subprocess.call('caput EC-GN-P01-GBF-MCPS:STAT-SIMM YES', shell=True) 
    #res = subprocess.call('caput EC-GN-P01-GAF-GCPS:STAT-SIMM YES', shell=True)
    #temporary commentout following line
    #res = subprocess.call('caput EC-GN-P01-GBF-GCPS:STAT-SIMM YES', shell=True) 
    #res = subprocess.call('caput EC-GN-P01-GAF-CCPS:STAT-SIMM YES', shell=True)
    #temporary commentout following line
    #res = subprocess.call('caput EC-GN-P01-GBF-CCPS:STAT-SIMM YES', shell=True)
    #res = subprocess.call('caput EC-GN-P01-GAF-FHPS:STAT-SIMM YES', shell=True)
    #temporary commentout following line
    #res = subprocess.call('caput EC-GN-P01-GBF-FHPS:STAT-SIMM YES', shell=True)
    time.sleep(1)
    print '2. Change SCAN mode from I/O Inter to Passive to write sumulated values.'
    #res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-CON-OPGY1.SCAN Passive', shell=True)#SELECT
    #res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-CON-OPGY2.SCAN Passive', shell=True)#SELECT
    #res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-ST1R.SCAN Passive', shell=True) #STANDBY
    #res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-ST2R.SCAN Passive', shell=True) #READY
    #res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-ST3R.SCAN Passive', shell=True) #ON
    #res = subprocess.call('caput EC-GN-P01-PMF:PSU0000-TYSTA.SCAN Passive', shell=True)
    #res = subprocess.call('caput EC-GN-P01-PA1F:PSU3000-YSTA.SCAN Passive', shell=True)
    #res = subprocess.call('caput EC-GN-P01-PB1F:PSU1000-YSTA.SCAN Passive', shell=True)
    #res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-CON-GY1PRM.SCAN Passive', shell=True) #PERMIT
    #res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-CON-GY2PRM.SCAN Passive', shell=True) #PERMIT
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-MD1.SCAN Passive', shell = True)
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-MD2.SCAN Passive', shell = True)
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-MD3.SCAN Passive', shell = True)
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-MD4.SCAN Passive', shell = True)
    #res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YSTA-MPSS.SCAN Passive', shell = True) #SYNC/ASYNC
    #res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YON-CCPS1.SCAN Passive', shell=True) #CCPS ON REQUEST
    time.sleep(1)
    print '3. Set pulse length limit mode value and state.'
    res = subprocess.call('caput EC-GN-P01-GPF:STAT-MD1-LIM 10000', shell = True)
    res = subprocess.call('caput EC-GN-P01-GPF:STAT-MD2-LIM 100000', shell = True)
    res = subprocess.call('caput EC-GN-P01-GPF:STAT-MD3-LIM 1000000', shell = True)
    res = subprocess.call('caput EC-GN-P01-GPF:STAT-MD4-LIM 10000000', shell = True)
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-MD1.SVAL 0', shell = True) #Pulse lenght mode
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-MD1 0', shell = True)      #Pulse lenght mode
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-MD2.SVAL 0', shell = True) #Pulse lenght mode
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-MD2 0', shell = True)      #Pulse lenght mode
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-MD3.SVAL 0', shell = True) #Pulse lenght mode
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-MD3 0', shell = True)      #Pulse lenght mode
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-MD4.SVAL 0', shell = True) #Pulse lenght mode
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-MD4 0', shell = True)      #Pulse lenght mode
    print '4. Set other operation modes'
    res = subprocess.call('caput EC-GN-P01-GAF:STAT-PREP-MODE 0', shell = True)         #Pre-pro mode
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YSTA-MPSS.SVAL 0', shell = True) #SYNC/ASYNC mode
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YSTA-MPSS 0', shell = True)      #SYNC/ASYNC mode
    res = subprocess.call('caput EC-GN-P01-GAF:STAT-SHORT-PULSE 0',shell = True)        #Short pulse mode
    time.sleep(1)
    print '5. Set a beam-on schedule(10ms diff + 100ms pulse)'
    res = subprocess.call('caput EC-GN-P01-PA1F:STAT-DT-HVON 10000', shell=True)
    res = subprocess.call('caput EC-GN-P01-PB1F:STAT-DT-HVON 20000', shell=True)
    res = subprocess.call('caput EC-GN-P01-PMF:STAT-DT-HVON 30000', shell=True)
    res = subprocess.call('caput EC-GN-P01-PB1F:STAT-DT-SWON 40000', shell=True)
    res = subprocess.call('caput EC-GN-P01-PA1F:STAT-DT-SWON 50000', shell=True)
    res = subprocess.call('caput EC-GN-P01-GAF:STAT-DT-SHOTLEN 100000', shell=True)
    time.sleep(1)
    print '6. Reset PLC INTERLOCK'
    #res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTRP.SIMM 1', shell=True) #PLC Interlock
    #res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTRP.SCAN 0', shell=True)
    #res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTRP.SVAL 0', shell=True) 
    #res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTRP 0', shell=True) #CCPS_IS_OPERATION
    #res = subprocess.call('caput EC-GN-P01-GAFP:FMC4310-YTRP.SIMM 1', shell=True) #cRIO Interlock
    #res = subprocess.call('caput EC-GN-P01-GAFP:FMC4310-YTRP.SCAN 0', shell=True)
    #res = subprocess.call('caput EC-GN-P01-GAFP:FMC4310-YTRP.SVAL 0', shell=True) 
    #res = subprocess.call('caput EC-GN-P01-GAFP:FMC4310-YTRP 0', shell=True)   
    #res = subprocess.call('caput EC-GN-P01-GPF:STAT-RST-FLT 1', shell=True) 
    print '---------- END setup for the test ----------'
