#!/usr/bin/python

import time
import sys
import subprocess

def test_sync():
    """Test Sync Mode"""
    print '1. Set beam-on schedule'
    res = subprocess.call('caput EC-GN-P01-PA1F:STAT-DT-HVON 100000', shell=True)
    res = subprocess.call('caput EC-GN-P01-PB1F:STAT-DT-HVON 200000', shell=True)
    res = subprocess.call('caput EC-GN-P01-PMF:STAT-DT-HVON 300000', shell=True)
    res = subprocess.call('caput EC-GN-P01-PB1F:STAT-DT-SWON 400000', shell=True)
    res = subprocess.call('caput EC-GN-P01-PA1F:STAT-DT-SWON 500000', shell=True)

    res = subprocess.call('caput EC-GN-P01-PA2F:STAT-DT-HVON 100000', shell=True)
    res = subprocess.call('caput EC-GN-P01-PB2F:STAT-DT-HVON 200000', shell=True)
    res = subprocess.call('caput EC-GN-P01-PB2F:STAT-DT-SWON 400000', shell=True)
    res = subprocess.call('caput EC-GN-P01-PA2F:STAT-DT-SWON 500000', shell=True)
    res = subprocess.call('caput EC-GN-P01-GAF:STAT-DT-SHOTLEN 300000000', shell=True)
    res = subprocess.call('caput EC-GN-P01-GBF:STAT-DT-SHOTLEN 300000000', shell=True)
    print '2. Set SYNC flag'
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YSTA-MPSS.SVAL 1', shell = True)
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YSTA-MPSS 1', shell = True)
    print '3. Set Mode 1 flag'
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-MD1.SVAL 1', shell = True)
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-MD1 1', shell = True)
    res = subprocess.call('caput EC-GN-P01-GPF:STAT-MD1-LIM 600000000', shell = True)
    print '4. Write PERMIT'
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-CON-GY1PRM.SVAL 1', shell=True)
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-CON-GY1PRM 1', shell=True)
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-CON-GY2PRM.SVAL 1', shell=True)
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-CON-GY2PRM 1', shell=True)
    time.sleep(1)
    print '5. Write HVON'
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-ST3R.SVAL 1', shell=True)
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-ST3R 1', shell=True)
    print '6. Wait SDN commands.'
    print 'Enter to stop operation(Trun off PLC_HVON)'
    inpval = raw_input()
    print '7. Write 0 to HVON'
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-ST3R.SVAL 0', shell=True)
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-ST3R 0', shell=True)
    time.sleep(2)
    print '8. Reset PERMIT'
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-CON-GY1PRM.SVAL 0', shell=True)
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-CON-GY1PRM 0', shell=True)
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-CON-GY2PRM.SVAL 0', shell=True)
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-CON-GY2PRM 0', shell=True)
    print '9. Reset Mode 1 flag'
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-MD1.SVAL 0', shell = True)
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-MD1 0', shell = True)
    print 'end of sequence.'

def test_sync_prepro():
    """Test Sync PrePro Mode"""
    print '1. Set beam-on schedule'
    res = subprocess.call('caput EC-GN-P01-PA1F:STAT-DT-HVON 100000', shell=True)
    res = subprocess.call('caput EC-GN-P01-PB1F:STAT-DT-HVON 200000', shell=True)
    res = subprocess.call('caput EC-GN-P01-PMF:STAT-DT-HVON 300000', shell=True)
    res = subprocess.call('caput EC-GN-P01-PB1F:STAT-DT-SWON 400000', shell=True)
    res = subprocess.call('caput EC-GN-P01-PA1F:STAT-DT-SWON 500000', shell=True)
    res = subprocess.call('caput EC-GN-P01-GAF:STAT-DT-SHOTLEN 300000000', shell=True)
    print '2. Set SYNC flag'
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YSTA-MPSS.SVAL 1', shell = True)
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YSTA-MPSS 1', shell = True)
    print '3. Set Mode 1 flag'
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-MD1.SVAL 1', shell = True)
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-MD1 1', shell = True)
    res = subprocess.call('caput EC-GN-P01-GPF:STAT-MD1-LIM 600000000', shell = True)
    print '4.Set Pre-Pro flag'
    res = subprocess.call('caput EC-GN-P01-GAF:STAT-PREP-MODE 1', shell = True)
    time.sleep(1)
    print '5. Write PERMIT'
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-CON-GY1PRM.SVAL 1', shell=True)
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-CON-GY1PRM 1', shell=True)
    time.sleep(1)
    print '6. Write HVON'
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-ST3R.SVAL 1', shell=True)
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-ST3R 1', shell=True)
    print '7. Wait SDN commands.'
    print 'Enter to stop operation(Trun off PLC_HVON)'
    inpval = raw_input()
    print '8. Write 0 to HVON'
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-ST3R.SVAL 0', shell=True)
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-ST3R 0', shell=True)
    time.sleep(2)
    print '9. Reset PERMIT'
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-CON-GY1PRM.SVAL 0', shell=True)
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-CON-GY1PRM 0', shell=True)
    print '10.Reset Pre-Pro flag'
    res = subprocess.call('caput EC-GN-P01-GAF:STAT-PREP-MODE 0', shell = True)
    time.sleep(1)
    print '11. Reset Mode 1 flag'
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-MD1.SVAL 0', shell = True)
    res = subprocess.call('caput EC-GN-P01-GPS:PLC4110-YTS-MD1 0', shell = True)
    print 'end of sequence.'