#!/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)]

APSStandardSetup
set status ""
proc SetStatus {text} {
    global status
    set status "$text"
    update
}

proc StartHoldVCPosition {args} {
    set sector ""
    APSParseArguments {sector}
    
    global onTime waitInterval posPV posSetpoint onActPV offActPV abort
    global irm iterations logInterval minWaitTime directory monFile outFile
     
    set waitIntervalNew $waitInterval($sector)
    
    SetStatus "Starting hold VS position for $sector  ( pv=$posPV($sector) setpoint=$posSetpoint($sector) )..."
    
    set i 0
    while {1} {
        incr i
        if $abort {
            SetStatus "aborted."
            set abort 0
            return
        }
        
        update
        if [catch {exec cavget -list=$posPV($sector) -pend=20} pos] {
            return -code error "Error reading $posPV : $pos"
        }
        
        if {$pos>$posSetpoint($sector)} {
            SetStatus "$sector, The setpoint ($posSetpoint($sector)) is lower than current position ($pos), wait for the position goes below the setpoint!"
            while {1} {
                if $abort {
                    SetStatus "aborted."
                    set abort 0
                    return
                }
                update
                after 1000
                if [catch {exec cavget -list=$posPV($sector) -pend=20} pos] {
                    return -code error "Error reading $posPV($sector) : $pos"
                }
                if {$pos<$posSetpoint($sector)} {
                    break
                }
            }
            #return
        }
        SetStatus "$sector Working on iteration ${i} and turning on the heater..."
        if [catch {exec cavput -list=$onActPV($sector)=1 -pend=20} result] {
            return -code error "Error turn on the heater: $result"
        }
        SetStatus "$sector Waiting for $onTime($sector) seconds after heater is turned on ..."
        #APSWaitWithUpdate -waitSeconds $onTime -updateInterval 1
        set timeout [expr [clock seconds] + $onTime($sector)]
        while {[clock seconds]<$timeout} {
            if $abort {
                SetStatus "$sector aborted."
                set abort 0
                return
            }
            update
            after 1000
        }
        SetStatus "$sector, Turning off the heater and waiting ${waitIntervalNew} seconds..."
        if [catch {exec cavput -list=$offActPV($sector)=1 -pend=20} result] {
            return -code error "Error turn off the heater: $result"
        }
        # APSWaitWithUpdate -waitSeconds $waitIntervalNew -updateInterval 1
        set timeout [expr [clock seconds] + $waitIntervalNew]
        while {[clock seconds]<$timeout} {
            if $abort {
                set abort 0
                SetStatus "aborted."
                return
            }
            update
            after 1000
        }
        if [catch {exec cavget -list=$posPV($sector) -pend=20} pos] {
            return -code error "Error reading $posPV : $pos"
        }
        if {$pos>$posSetpoint($sector)} {
            SetStatus "$sector Overshoot, wait until it goes under the setpoint..."
            while {1} {
                if $abort {
                    set abort 0
                    SetStatus "Aborted."
                    return
                }
                update
                after 1000
                if [catch {exec cavget -list=$posPV($sector) -pend=20} pos] {
                    return -code error "Error reading $posPV : $pos"
                }
                if {$pos<$posSetpoint($sector)} {
                    break
                }
            }
        }
    }
}


proc MakeSectorWidget {args} {
    set parent ""
    set sector ""
    APSParseArguments {parent sector}
    
    global onTime irm posSetpoint iterations waitInterval minWaitTime posPV 
    APSFrameGrid .grid -parent $parent -xList {x1 x2}
    set w1 $parent.grid.x1
    set w2 $parent.grid.x2
    set width 30
    
    APSLabeledEntry .ontime -parent $w1 -label "Heater on time (seconds):" -textVariable onTime($sector) -width $width
    #APSLabeledEntry .irm -parent $w1 -label "irm factor:" -textVariable irm($sector) -width $width
    APSLabeledEntry .possetpoint -parent $w1 -label "Position setpoint:" -textVariable posSetpoint($sector) -width $width
    #APSLabeledEntry .iterations -parent $w1 -label "Iterations:" -textVariable iterations($sector) -width $width
    
    APSLabeledEntry .waitint -parent $w2 -label "Wait interval (seconds):" -textVariable waitInterval($sector) -width $width
    APSLabeledEntry .waitlimit -parent $w2 -label "Minimum wait time (seconds):" -textVariable minWaitTime($sector) -width $width
    APSLabeledEntry .pospv -parent $w1 -label "Postion PV:" -textVariable posPV($sector) -width $width

  #  APSButton .start -parent $parent -text "Start HoldVCPostion" -command "StartHoldVCPosition -sector $sector"
  #  APSButton .abort -parent $parent -text "Abort" -command "set abort($sector) 1"
    
}

set monFile  /home/helios/SR/MMS/MMS.mon
set args $argv
set onTime(S27) 3
set waitInterval(S27) 10.0
set posPV(S27) CAP:S27:BP0:y
set posSetpoint(S27) 526.0
set onActPV(S27) MMS:S27:POWR:1:outlet1onC.PROC
set offActPV(S27) MMS:S27:POWR:1:outlet1offC.PROC
set irm(S27) -1500.0
set gain(S27) 1.0
set iterations(S27) 10000
set minWaitTime(S27) 10

set onTime(S28) 3
set waitInterval(S28) 10.0
set posPV(S28) CAP:S28:AP0:y
set posSetpoint(S28) 449
set onActPV(S28) MMS:S27:POWR:1:outlet2onC.PROC
set offActPV(S28) MMS:S27:POWR:1:outlet2offC.PROC
set irm(S28) -1500.0
set gain(S28) 1.0
set iterations(S28) 10000
set minWaitTime(S28) 10
set sector S27
APSStrictParseArguments {sector}

set CVSRevisionAuthor "\$Revision: 1.0 $ \$Author: shang $"
APSApplication . -name holdVCPosition-$sector -version $CVSRevisionAuthor \
  -overview {This interface provides a simple interface to hold the VC position and log data for review.}
APSScrolledStatus .status  -parent .userFrame  -textVariable status -width 100 \
  -height 4 -withButtons 1 -packOption "-fill x"



MakeSectorWidget -parent .userFrame  -sector $sector
set abort 0

APSButton .start -parent .userFrame -text "Start HoldVCPosition" -command "StartHoldVCPosition -sector $sector"
APSButton .abort -parent .userFrame -text "Abort" -command "set abort 1"

