#This is a temporarty script to allow for setting the hard and soft line
#mps crate timing

import sys
import time
import argparse
import epics

def lineMode(PVs):
    lineMode = epics.caget(PVs[0] +"TIMING_MODE_RBV")
    for pv in PVs:
        mode = epics.caget(pv +"TIMING_MODE_RBV")
        if((mode == 2) or (mode != lineMode)):
            print("Configuration Error with: " + pv +"TIMING_MODE_RBV")
            lineMode = 2
            return lineMode
    #0 NC 1 SC 2 line misconfigured        
    return lineMode
    

def setLineMode(PVs,mode):
    for pv in PVs:
        epics.caput(pv +"TIMING_MODE", mode)
    time.sleep(3)
    actualMode = lineMode(PVs)
    if(actualMode != mode):
        actualMode = 2
    #0 NC 1 SC 2 line misconfigured 
    return actualMode
    
def process_args(argv):
     parser=argparse.ArgumentParser(description='This script is used to set the timing for the hard or softline MPS nodes')
     parser.add_argument('-LINE', required=True, type = str, choices = ['Hard','Soft'], help = 'Hard or Soft')
     parser.add_argument('-SET', required=True, type = str, choices = ['Yes','No'], help='If set to no this script will only report line status')
     parser.add_argument('-DESIRED', required=False, type = str, choices = ['SC','NC'], help='SC or NC')
     parser=parser.parse_args()
     
     return parser


def main(argv):
    softLinePVs = ["MPLN:BSYS:MP01:1:",
        "MPLN:BSYS:MP02:5:",
        "MPLN:BSYS:MP03:6:",
        "MPLN:BSYS:MP04:1:",
        "MPLN:LTUS:MP01:1:",
        "MPLN:LTUS:MP02:1:",
        "MPLN:LTUS:MP03:1:",
        "MPLN:LTUS:MP04:1:",
        "MPLN:LTUS:MP05:6:",
        "MPLN:LTUS:MP06:1:",
        "MPLN:UNDS:MP01:1:",
        "MPLN:UNDS:MP02:1:",
        "MPLN:UNDS:MP03:6:",
        "MPLN:UNDS:MP04:7:",
        "MPLN:UNDS:MP05:1:",
        "MPLN:UNDS:MP06:1:",
        "MPLN:UNDS:MP07:1:",
        "MPLN:FEES:MP01:1:"]
        
    hardLinePVs = ["MPLN:LTUH:MP01:1:",
        "MPLN:LTUH:MP02:1:",
        "MPLN:LTUH:MP03:1:",
        "MPLN:LTUH:MP04:1:",
        "MPLN:LTUH:MP05:1:",
        "MPLN:LTUH:MP06:6:",
        "MPLN:UNDH:MP01:1:",
        "MPLN:UNDH:MP02:7:",
        "MPLN:UNDH:MP03:1:",
        "MPLN:UNDH:MP04:1:",
        "MPLN:UNDH:MP05:1:",
        "MPLN:UNDH:MP06:1:",
        "MPLN:UNDH:MP07:6:",
        "MPLN:UNDH:MP08:1:",
        "MPLN:UNDH:MP09:1:",
        "MPLN:FEEH:MP01:1:"]
        
        #Additional hard line pvs
        #"MPLN:BSYH:MP01:1:",
        #"MPLN:BSYH:MP02:6:",
        #"MPLN:BSYH:MP03:1:",
        
#    devPVs = ["MPLN:UNDH:MP01:1:"]
    controls =  process_args(argv)
    if(controls.DESIRED == "NC"):
        desiredInt = 0
    else:
        desiredInt = 1   
        
    if(controls.LINE == "Hard"):
        mode = lineMode(hardLinePVs)
    else:
        mode = lineMode(softLinePVs)
    if(mode == 0):
        print("The " + controls.LINE + " line is in NC mode")
    elif(mode == 1):
        print("The " + controls.LINE + " line is in SC mode")
    else:
        print("The " + controls.LINE + " line is not correctly configured")
    
    if(controls.SET == "No"):
        return
    else:
        print("Setting the " + controls.LINE + " to " + controls.DESIRED + " mode")
        if(controls.LINE == "Hard"):
            newMode = setLineMode(hardLinePVs, desiredInt) 
        else:
            newMode = setLineMode(softLinePVs, desiredInt)
    
    if(newMode == desiredInt):
        print("Successfully set the " + controls.LINE + " line to " + controls.DESIRED + " mode")
    else:
        print("Error in setting the " + controls.LINE + " line to " + controls.DESIRED + " mode")                        
        


if __name__ == "__main__":
    main(sys.argv[1:])
