#!/bin/sh  
# \
exec oagwish "$0" "$@"



set auto_path [linsert $auto_path 0  /usr/local/oag/apps/lib/$env(HOST_ARCH)]
set auto_path [linsert $auto_path 0  /usr/local/oag/lib_patch/$env(HOST_ARCH)]
set CVSRevisionAuthor "\$Revision: 1.5 $ \$Author: soliday $"

APSApplication . -name RF1Limit -version $CVSRevisionAuthor \
  -overview {Imposes limits on gap voltage and reflected power for PAR fundamental RF cavity.}

set Variables {rf1Voltage  rf1Voltage1 rf1RefPower1 rf1RefPower2 rf1RefPower3 rf1RefPower4}
set PVs       {PAR:RF1:gapVoltage PAR:RF1:gapVoltage1 PAR:RF1:revPower1 \
                 PAR:RF1:revPower2 PAR:RF1:revPower3 PAR:RF1:revPower4}
set LimitVariables {voltageLimit voltageLimit refPowerLimit refPowerLimit refPowerLimit \
                 refPowerLimit }

set count [llength $PVs]
for {set i 0} {$i<$count} {incr i} {
    if {[pv linkw [lindex $Variables $i] [lindex $PVs $i]] !=0 } {
        puts $errorCode
        exit
    }
}
if {[pv mon $Variables EvaluateValues]!=0} {
    puts $errorCode
    exit
}
if {[pv linkw rfState PAR:RF1:abBoA0C7S0]!=0 || [pv mon rfState EvaluateValues]!=0} {
    puts $errorCode
    exit
}


set mainStatus "Ready."
APSStatusLine .status -parent .userFrame -width 60 -variable mainStatus
proc SetMainStatus {text} {
    global mainStatus
    set mainStatus $text
    update
}

set ActivityRecord ""
APSLabeledOutput .record -parent .userFrame -width 60 \
  -textVariable ActivityRecord \
  -label "Last activity: " \
  -contextHelp "Shows the last action taken and the time it was taken."
APSLabeledOutput .rfState  -parent .userFrame -width 60 \
  -textVariable rfState \
  -label "RF state: " 

set tcl_precision 4

APSFrame .limits -parent .userFrame -label "Readback Limits"
set limitsFrame .userFrame.limits.frame
set voltageLimit 25
set refPowerLimit 30
APSLabeledEntry .voltage -parent $limitsFrame \
        -textVariable voltageLimit \
        -label "Gap voltage (kV): " \
        -contextHelp "Enter the fundamental cavity voltage limit in kV.  Entry is disabled when running."
APSLabeledEntry .refPower -parent $limitsFrame \
        -textVariable refPowerLimit \
        -label "Reflected power (W): " \
        -contextHelp "Enter the fundamental cavity reflected power limit in W.  Entry is disabled when running."

APSButton .run -parent .userFrame -text "RUN" \
        -command StartLimiting \
        -contextHelp "Initiate limiting of readbacks within parameters given above."
APSButton .stop -parent .userFrame -text "STOP" \
        -command StopLimiting \
        -contextHelp "Stop limiting readbacks."
APSDisableButton .userFrame.run.button
APSDisableButton .userFrame.stop.button

set limitingOn 0
proc StartLimiting {} {
    global limitingOn limitsFrame
    set limitingOn 1
    APSDisableButton .userFrame.run.button
    APSEnableButton .userFrame.stop.button
    EvaluateValues
    SetMainStatus "Limiting activated [exec date]"
    $limitsFrame.voltage.entry configure -state disabled
    $limitsFrame.refPower.entry configure -state disabled
}

proc StopLimiting {} {
    global limitingOn limitsFrame
    set limitingOn 0
    APSEnableButton .userFrame.run.button
    APSDisableButton .userFrame.stop.button
    EvaluateValues
    SetMainStatus "Limiting deactivated [exec date]"
    $limitsFrame.voltage.entry configure -state normal
    $limitsFrame.refPower.entry configure -state normal
}

set InEvaluateValues 0
proc EvaluateValues {} {
    global limitingOn Variables LimitVariables rfState
    global InEvaluateValues 

    set rfState $rfState
    if !$limitingOn return
    if $InEvaluateValues return

    set InEvaluateValues 1
    set count [llength $Variables]
    for {set i 0} {$i<$count} {incr i} {
        set variableName [lindex $Variables $i]
        global $variableName
        set value [subst \$$variableName]
        set limitName [lindex $LimitVariables $i]
        global $limitName
        set limit [subst \$$limitName]
        if {$value>$limit && [string compare $rfState Off]!=0} {
            global ActivityRecord
            set rfState Off
            pv putw rfState
#            catch {exec caput -t PAR:RF1:abBoA0C7S0 Off}
            set ActivityRecord "RF turned off [exec date] ($limitName)"
        }
    }
    set InEvaluateValues 0
}


APSEnableButton .userFrame.run.button
APSDisableButton .userFrame.stop.button
