#!/usr/local/lcls/package/python/current/bin/python

from __future__ import print_function
import epics
import time
import sys 
import getopt 
import os

# Author: K.Lauer
# Created: 09/12/2017
# Modified: 
#           11/2/2017 N.Balakrishnan (namrata) 
#           Add XLEAP enable and disable 
#           Add WS32 back to the list
#           10/27/2017 N.Balakrishnan (namrata) 
#           Add Maxv FWS support 
#           10/26/2017 N.Balakrishnan (namrata) 
#           Update to add argument 
#
# Usage: ./enableFWS.py [OPTION] [STRING]
# Description: Enables the requested FWS and disables the rest

fws_name = None

def usage(prog_name):
    print('\n%s: Enable the requested FWS and disable the rest' % prog_name)
    print('Usage:')
    print('   %s -h,--help  Shows this usage message' % prog_name)
    print('   %s -f,--fws   The FWS to be enabled. Use MAD name' % prog_name)

try:
    opts, args = getopt.getopt(sys.argv[1:],'hf:', ['help','fws='])
except getopt.GetoptError, err:
    print(str(err))
    usage(sys.argv[0])
    sys.exit(2)
for opt,arg in opts:
    if opt in ('-h','--help'):
        usage(sys.argv[0])
        sys.exit()
    elif opt in ('-f', '--fws'):
        fws_name= arg 
    else:
        print('arg given is' + arg )
        fws_name = arg
        usage(sys.argv[0])
        sys.exit(1)

# Include FWS name to be default
# When wirescanner name is entered as an argument
arglen = len(sys.argv)
if arglen == 2:
    fws_name = sys.argv[1] 


linmot = {
'WS02':'WIRE:IN20:561:' ,
'WS03':'WIRE:IN20:611:' ,
'WS11':'WIRE:LI21:285:' ,
'WS12':'WIRE:LI21:293:' ,
'WS13':'WIRE:LI21:301:' ,
'WS644':'WIRE:LI27:644:' ,
'WS144':'WIRE:LI28:144:' ,
'WS444':'WIRE:LI28:444:' ,
'WS744':'WIRE:LI28:744:' ,
'WSVM2':'WIRE:LTU0:122:' ,
'WS31':'WIRE:LTU1:715:' ,
'WS32':'WIRE:LTU1:735:' ,
'WS33':'WIRE:LTU1:755:' ,
'WS34':'WIRE:LTU1:775:'
}

XLEAP = '''
WIGG:LTU1:849:USMotor
WIGG:LTU1:849:DSMotor
'''.strip().split()

if fws_name is None:
    print('\nNo FWS specified. All will be disabled.\n')
else: 
    if fws_name not in linmot.keys():
       if fws_name not in maxv.keys():
           print(fws_name,' not a FWS or a working FWS')
           sys.exit()

# Turning off XLEAP
for prefix in XLEAP:
    enable_pv = ''.join((prefix, '.CNEN'))
    if epics.caget(enable_pv):
        print(prefix, 'is enabled. Turning it off...')
        epics.caput('{}'.format(enable_pv), 0)
        time.sleep(1)
        print(prefix, 'is', epics.caget(enable_pv, as_string=True))
    else:
        print(prefix, 'is', epics.caget(enable_pv, as_string=True))    
        
# Loop for linmot FWS
for key,value in linmot.items():
    enabled_status = ''.join((value, 'MOTR_ENABLED_STS'))

    # If FWS is selected : initialize it and print enable status
    if key == fws_name:
        if epics.caget(enabled_status):
            print(key, 'is enabled')
        else:
            enable_pv = ''.join((value, 'MOTR_INIT'))
            epics.caput('{}'.format(enable_pv), 1)
            time.sleep(3)
            print(key, 'is', epics.caget(enabled_status, as_string=True))

    # If FWS is not selected : retract it and print the enable status
    else:
        if epics.caget(enabled_status):
            retract_pv = ''.join((value, 'RETRACT_SEQ'))
            print(key, 'is enabled')
            print('Turning it off..')
            epics.caput('{}'.format(retract_pv), 1)
            time.sleep(2)
        print(key, 'is', epics.caget(enabled_status, as_string=True))

print('NOTE: XLEAP motors are disabled so that the NOISE is minimised. Please turn ON XLEAP motors after the test. Use enableXLEAP.py ')

