#
# $Log: not supported by cvs2svn $
# Revision 1.4  2007/03/01 16:04:50  soliday
# Added missing close brace.
#
# Revision 1.3  2007/03/01 15:56:27  soliday
# Added calls to APSLogScriptAction to store initialization values
# for APSMpOpenCloseGateValves.
#
# Revision 1.2  2006/09/05 20:24:52  soliday
# Updated to remove the VM:LP:VAL2 gate valve because this no longer exists.
#
# Revision 1.1  2003/11/25 15:59:55  soliday
# Added procedures to open and close all the different gate valves.
#
#
#

proc APSMpOpenCloseGateValvesInfo {} {
    return "This procedure is used to open or close the selected gate valves."
}

proc APSMpOpenCloseGateValvesInitDialog {frame} {
    global APSMpOpenCloseGateValves operation
    pack [label $frame.label -text "Select Gate Valves to $operation"]
    pack [frame $frame.f1] -side left -anchor n -padx .25i
    pack [frame $frame.f2] -side left -anchor n -padx .25i
    APSCheckButtonFrame .gatevalves1 -parent $frame.f1 -allNone 1 \
      -label "Linac" \
      -buttonList "L1:PC1:GV1 L1:PC1:GV2 L1:RG1:GV1 L1:RG2:GV1 L1:GV1 L1:GV2 L2:GV1 L3:GV1 L4:GV1 L5:GV1" \
      -variableList "APSMpOpenCloseGateValves(L1:PC1:GV1) APSMpOpenCloseGateValves(L1:PC1:GV2) APSMpOpenCloseGateValves(L1:RG1:GV1) APSMpOpenCloseGateValves(L1:RG2:GV1) APSMpOpenCloseGateValves(L1:GV1) APSMpOpenCloseGateValves(L1:GV2) APSMpOpenCloseGateValves(L2:GV1) APSMpOpenCloseGateValves(L3:GV1) APSMpOpenCloseGateValves(L4:GV1) APSMpOpenCloseGateValves(L5:GV1)"
    APSCheckButtonFrame .gatevalves2 -parent $frame.f1 -allNone 1 \
      -label "LTP/PB/PTB" \
      -packOption "-pady .25i" \
      -buttonList "VM:LTP:GV1 VM:LP:VAL1 VM:PBY:VAL1 VM:PBY:VAL2 VM:PB:VAL1 VM:PB:VAL2" \
      -variableList "APSMpOpenCloseGateValves(VM:LTP:GV1) APSMpOpenCloseGateValves(VM:LP:VAL1) APSMpOpenCloseGateValves(VM:PBY:VAL1) APSMpOpenCloseGateValves(VM:PBY:VAL2) APSMpOpenCloseGateValves(VM:PB:VAL1) APSMpOpenCloseGateValves(VM:PB:VAL2)"
    APSCheckButtonFrame .gatevalves3 -parent $frame.f1 -allNone 1 \
      -label "Booster/BTS" \
      -buttonList "VM:BV:0001 VM:BV:0001A VM:BV:0001B VM:BV:0002 VM:BV:0003 VM:BV:0004 VM:BV:0005 VM:BV:0006 VM:BV:0007 VM:BV:0008 VM:BV:0009 VM:BS:HBSTP VM:BS:VAL1 VM:BS:VAL2 VM:BTX:VV01" \
      -variableList "APSMpOpenCloseGateValves(VM:BV:0001) APSMpOpenCloseGateValves(VM:BV:0001A) APSMpOpenCloseGateValves(VM:BV:0001B) APSMpOpenCloseGateValves(VM:BV:0002) APSMpOpenCloseGateValves(VM:BV:0003) APSMpOpenCloseGateValves(VM:BV:0004) APSMpOpenCloseGateValves(VM:BV:0005) APSMpOpenCloseGateValves(VM:BV:0006) APSMpOpenCloseGateValves(VM:BV:0007) APSMpOpenCloseGateValves(VM:BV:0008) APSMpOpenCloseGateValves(VM:BV:0009) APSMpOpenCloseGateValves(VM:BS:HBSTP) APSMpOpenCloseGateValves(VM:BS:VAL1) APSMpOpenCloseGateValves(VM:BS:VAL2) APSMpOpenCloseGateValves(VM:BTX:VV01)"
    set SRvalves ""
    set SRvar ""
    for {set i 1} {$i <= 40} {incr i} {
        #append SRvalves "VM:[format %02d $i]:VV01 VM:[format %02d $i]:VV02 "
        #append SRvar "APSMpOpenCloseGateValves(VM:[format %02d $i]:VV01) APSMpOpenCloseGateValves(VM:[format %02d $i]:VV02) "
        if {$i != 38} {
            append SRvalves "S[format %02d $i]A:GV1 S[format %02d $i]B:GV1 "
            append SRvar "APSMpOpenCloseGateValves(S[format %02d $i]A:GV1) APSMpOpenCloseGateValves(S[format %02d $i]B:GV1) "
        }
        if {($i == 36) || ($i == 37) || ($i == 39) || ($i == 40)} {
            append SRvalves "S[format %02d $i]C:GV1 "
            append SRvar "APSMpOpenCloseGateValves(S[format %02d $i]C:GV1) "
        }
        if {$i == 38} {
            append SRvalves "S[format %02d $i]A:GV1 S[format %02d $i]-BLS:GV1 S[format %02d $i]-BLS:GV2 "
            append SRvar "APSMpOpenCloseGateValves(S[format %02d $i]A:GV1) APSMpOpenCloseGateValves(S[format %02d $i]-BLS:GV1) APSMpOpenCloseGateValves(S[format %02d $i]-BLS:GV2) "
        }
    }
    APSCheckButtonFrame .gatevalves \
      -parent $frame.f2 \
      -allNone 1 \
      -limitPerRow 34 \
      -label "Storage Ring" \
      -buttonList "$SRvalves" \
      -variableList "$SRvar"

}

proc APSMpCloseGateValvesInfo {} {
    return "This procedure is used to close the selected gate valves."
}
proc APSMpCloseGateValves {args} {
    if {[catch {APSMpOpenCloseGateValves -operation close} results]} {
        APSMpReturn error "APSMpOpenCloseGateValves: $results"
    }
    APSMpReturn ok
}

proc APSMpOpenGateValvesInfo {} {
    return "This procedure is used to open the selected gate valves."
}
proc APSMpOpenGateValves {args} {
    if {[catch {APSMpOpenCloseGateValves -operation open} results]} {
        APSMpReturn error "APSMpOpenCloseGateValves: $results"
    }
    APSMpReturn ok
}

proc APSMpCloseInjectorGateValvesInfo {} {
    return "This procedure is used to close the selected gate valves."
}
proc APSMpCloseInjectorGateValves {args} {
    if {[catch {APSMpOpenCloseGateValves -guns 1 -operation close} results]} {
        APSMpReturn error "APSMpCloseInjectorGateValves: $results"
    }
    APSMpReturn ok
}

proc APSMpCloseLinacGateValvesInfo {} {
    return "This procedure is used to close the selected gate valves."
}
proc APSMpCloseLinacGateValves {args} {
    if {[catch {APSMpOpenCloseGateValves -linac 1 -operation close} results]} {
        APSMpReturn error "APSMpCloseLinacGateValves: $results"
    }
    APSMpReturn ok
}

proc APSMpOpenCloseGateValves {args} {
    set gateValves "L1:PC1:GV1 L1:PC1:GV2 L1:RG1:GV1 L1:RG2:GV1 L1:GV1 L1:GV2 L2:GV1 L3:GV1 L4:GV1 L5:GV1 VM:LTP:GV1 VM:LP:VAL1 VM:PBY:VAL1 VM:PBY:VAL2 VM:PB:VAL1 VM:PB:VAL2 VM:BV:0001 VM:BV:0001A VM:BV:0001B VM:BV:0002 VM:BV:0003 VM:BV:0004 VM:BV:0005 VM:BV:0006 VM:BV:0007 VM:BV:0008 VM:BV:0009 VM:BS:HBSTP  VM:BS:VAL1 VM:BS:VAL2 VM:BTX:VV01 "

    for {set i 1} {$i <= 40} {incr i} {
        #append gateValves "VM:[format %02d $i]:VV01 VM:[format %02d $i]:VV02 "
        if {$i != 38} {
            append gateValves "S[format %02d $i]A:GV1 S[format %02d $i]B:GV1 "
        }
        if {($i == 36) || ($i == 37) || ($i == 39) || ($i == 40)} {
            append gateValves "S[format %02d $i]C:GV1 "
        }
        if {$i == 38} {
            append gateValves "S[format %02d $i]A:GV1 S[format %02d $i]-BLS:GV1 S[format %02d $i]-BLS:GV2 "
        }
    }

    foreach gv $gateValves {
        set $gv 0
    }
    set all 0
    set guns 0
    set linac 0
    global operation
    set operation close
    APSStrictParseArguments {all guns linac operation}
    if {$all} {
        foreach gv $gateValves {
            set $gv 1
        }
    }
    if {$linac} {
        foreach gv "L1:GV1 L1:GV2 L2:GV1 L3:GV1 L4:GV1 L5:GV1" {
            set $gv 1
        }
        set guns 1
    }
    if {$guns} {
        foreach gv "L1:PC1:GV1 L1:PC1:GV2 L1:RG1:GV1 L1:RG2:GV1" {
            set $gv 1
        }
    }

    eval APSMpStep init -promptDialog APSMpOpenCloseGateValvesInitDialog -vars {$gateValves}
    
    #Call APSLogScriptAction three times because there are so many arguments
    set paramString ""
    foreach param "L1:PC1:GV1 L1:PC1:GV2 L1:RG1:GV1 L1:RG2:GV1 L1:GV1 L1:GV2 L2:GV1 L3:GV1 L4:GV1 L5:GV1 VM:LTP:GV1 VM:LP:VAL1 VM:PBY:VAL1 VM:PBY:VAL2 VM:PB:VAL1 VM:PB:VAL2 VM:BV:0001 VM:BV:0001A VM:BV:0001B VM:BV:0002 VM:BV:0003 VM:BV:0004 VM:BV:0005 VM:BV:0006 VM:BV:0007 VM:BV:0008 VM:BV:0009 VM:BS:HBSTP VM:BS:VAL1 VM:BS:VAL2 VM:BTX:VV01 " {
        append paramString "$param=\"[set $param]\" "
    }
    catch {APSLogScriptAction -procedure APSMpOpenCloseGateValves -action initialize -state ok -parameters $paramString}
    
    set gvList ""
    set paramString ""
    for {set i 1} {$i <= 10} {incr i} {
        append gvList "S[format %02d $i]A:GV1 S[format %02d $i]B:GV1 "
    }
    foreach param $gvList {
        append paramString "$param=\"[set $param]\" "
    }
    catch {APSLogScriptAction -procedure APSMpOpenCloseGateValves -action initialize -state ok -parameters $paramString}

    set gvList ""
    set paramString ""
    for {set i 11} {$i <= 20} {incr i} {
        append gvList "S[format %02d $i]A:GV1 S[format %02d $i]B:GV1 "
    }
    foreach param $gvList {
        append paramString "$param=\"[set $param]\" "
    }
    catch {APSLogScriptAction -procedure APSMpOpenCloseGateValves -action initialize -state ok -parameters $paramString}

    set gvList ""
    set paramString ""
    for {set i 21} {$i <= 30} {incr i} {
        append gvList "S[format %02d $i]A:GV1 S[format %02d $i]B:GV1 "
    }
    foreach param $gvList {
        append paramString "$param=\"[set $param]\" "
    }
    catch {APSLogScriptAction -procedure APSMpOpenCloseGateValves -action initialize -state ok -parameters $paramString}

    set gvList ""
    set paramString ""
    for {set i 31} {$i <= 35} {incr i} {
        append gvList "S[format %02d $i]A:GV1 S[format %02d $i]B:GV1 "
    }
    for {set i 36} {$i <= 37} {incr i} {
        append gvList "S[format %02d $i]A:GV1 S[format %02d $i]B:GV1 S[format %02d $i]C:GV1 "
    }
    for {set i 38} {$i <= 38} {incr i} {
        append gvList "S[format %02d $i]A:GV1 S[format %02d $i]-BLS:GV1 S[format %02d $i]-BLS:GV2 "
    }
    for {set i 39} {$i <= 40} {incr i} {
        append gvList "S[format %02d $i]A:GV1 S[format %02d $i]B:GV1 S[format %02d $i]C:GV1 "
    }
    foreach param $gvList {
        append paramString "$param=\"[set $param]\" "
    }
    catch {APSLogScriptAction -procedure APSMpOpenCloseGateValves -action initialize -state ok -parameters $paramString}


    set selectedValves ""
    set selectedValvesCheck1 ""
    set selectedValvesCheck2 ""
    foreach gv $gateValves {
        if {[set $gv]} {
            if {[lsearch "L1:PC1:GV1 L1:PC1:GV2 L1:RG1:GV1 L1:RG2:GV1 L1:GV1 L1:GV2 L2:GV1 L3:GV1 L4:GV1 L5:GV1 VM:LTP:GV1 VM:LP:VAL1 VM:PBY:VAL1 VM:PBY:VAL2 VM:PB:VAL1 VM:PB:VAL2" $gv] != -1} {
                append selectedValves "${gv}:${operation}CmdBO=1 "
                append selectedValvesCheck1 "${gv}:positionMI "
            } elseif {[string index $gv 0] == "S"} {
                if {$operation == "close"} {
                    append selectedValves "${gv}:CloseC=1 "
                } else {
                    append selectedValves "${gv}:OpenC=1 "
                }
                append selectedValvesCheck1 "${gv}:PositionM "
            } else {
                append selectedValves "${gv}:${operation}BO=1 "
                append selectedValvesCheck2 "${gv}:openBI "
            }
        }
    }
    if {[llength $selectedValves] == 0} {
        APSMpReturn ok
    }
    if {[APSPEMTKDialog .warningMessage \
           -text "Are you sure you wish to $operation these gate valves?" \
           -appendtext 0] == 1} {
        APSMpReturn error "APSMpOpenCloseGateValves: User aborted"
    }
    if {$operation == "close"} {
        set invOperation "open"
        set target1 2
        set target2 0
    } else {
        set invOperation "close"
        set target1 4
        set target2 1
    }

    set try 1
    set done 0
    while {$try < 8} {
        APSMpStep "Checking for $invOperation selected gate valves"
        if {[llength $selectedValvesCheck1]} {
            if {[catch {exec cavget -list=[join $selectedValvesCheck1 ,] -num -pend=20} results1]} {
                APSMpReturn error "APSMpCloseGateValves: $results1"
            }
        }
        if {[llength $selectedValvesCheck2]} {
            if {[catch {exec cavget -list=[join $selectedValvesCheck2 ,] -num -pend=20} results2]} {
                APSMpReturn error "APSMpCloseGateValves: $results2"
            }
        }
        set done 1
        set detectedGVs ""
        if {[llength $selectedValvesCheck1]} {
            foreach value $results1 sgv $selectedValvesCheck1 {
                if {$value != $target1} {
                    set done 0
                    append detectedGVs "$sgv "
                }
            }
        }
        if {[llength $selectedValvesCheck2]} {
            foreach value $results2 sgv $selectedValvesCheck2 {
                if {$value != $target2} {
                    set done 0
                    append detectedGVs "$sgv "
                }
            }
        }
        if {$done} {
            break
        }

        APSMpStep "[llength $detectedGVs] selected gate valves need to be switched"
        APSMpStep "Attempting to $operation selected gate valves"
        if {[catch {exec cavput -list=[join $selectedValves ,] -num -pend=20} results]} {
            APSMpReturn error "APSMpCloseGateValves: $results"
        }
        APSWaitWithUpdate -waitSeconds 10
        incr try
    }
    if {$done == 0} {
        if {$operation == "open"} {
            if {[APSPEMTKDialog .warningMessage \
                   -text "Unable to $operation all the selected gate valves. Some take a long time to open. Check: $detectedGVs" \
                   -appendtext 0] == 1} {
                APSMpReturn error "APSMpCloseGateValves: Unable to $operation all the selected gate valves"
            }
        } else {
            if {[APSPEMTKDialog .warningMessage \
                   -text "Unable to $operation all the selected gate valves. Check: $detectedGVs" \
                   -appendtext 0] == 1} {
                APSMpReturn error "APSMpCloseGateValves: Unable to $operation all the selected gate valves"
            }
        }
    }
    APSMpReturn ok
}
