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

#read warn and stop from PV
pv linkw {WarnValue StopValue AveValue} \
    {B:BM:CurrentWFAveWarn B:BM:CurrentWFAveStop B:BM:CurrentWFAve}
pv getw WarnValue
pv getw StopValue
pv getw AveValue
pv umon AveValue

#set WarnValue 32010
#set StopValue 33535
#set aveValue 0
set status ""

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

APSApplication . -name BMRampMonAndStop  \
  -overview "This utility provides convenience controls for monitoring and stopping BM ramp when it is out of range."
APSScrolledStatus .status -parent .userFrame -width 80 \
        -textVariable status

set width 25
set filename [APSGoToDailyDirectory -subdirectory BMRampMon]/BMcurrentMon-[clock format [clock seconds] -format %Y-%m%d].sdds
APSLabeledEntry .file -parent .userFrame -width 80 -label "Monitor file:" -textVariable filename -commandButton 1
APSLabeledEntry .ave -parent .userFrame -width $width -label "B:BM:CurrentWF average:" -textVariable AveValue
APSLabeledEntry .warn -parent .userFrame -width $width -label "Warning value for B:BM:CurrentWF average:" -textVariable WarnValue
bind .userFrame.warn.entry <Return> "ChangeValue -type Warn"
APSLabeledEntry .stop -parent .userFrame -width $width -label "Stop value for stopping BM ramp:" -textVariable StopValue
bind .userFrame.stop.entry <Return> "ChangeValue -type Stop"

set runControlPV B:BM:RampMonitorRC
APSButton .info -parent .userFrame -text "Info" -command "exec medm -x -attach -macro RCPV=$runControlPV ./sr/psApp/APSRunControlSingle.adl &"
APSButton .start -parent .userFrame -text "Start" -command StartBMRampMon
APSButton .stop1 -parent .userFrame -text "Stop" -command "StopBMRampMon"
APSButton .plot -parent .userFrame -text "Plot" -command Plot
######################
proc StartBMRampMon {args} {
    global WarnValue StopValue AveValue filename runControlPV
    
    ChangeValue -type Warn
    ChangeValue -type Stop
    after 1000
    if [catch {exec cavput -list=B:BM:CurrentWFAve.HIHI=$StopValue,B:BM:CurrentWFAve.HIGH=$WarnValue -pend=10} result] {
	return -code error "Error setting B:BM:CurrentWFAve.HIHI/B:BM:CurrentWFAve.HIGH: $result"
    }
    set command "BMRampMon -filename $filename -runControlPV $runControlPV"
    APSExecLog .bmMon -width 100 -lineLimit 1048 -name "BM Ramp Monitor and Stop" -unixCommand "$command"
}

proc StopBMRampMon {args} {
    global runControlPV 
    if [catch {exec cavget -list=$runControlPV.RUN -pend=10 -printErrors} running] {
	return -code error "Error reading $runControlPV.RUN: $running"
    }
    if $running {
	if [catch {exec cavput -list=$runControlPV.ABRT=1 -pend=10} result] {
	    return -code error "Error abort $runControlPV:  result"
	}
	after 3000
	exec cavput -list=$runControlPV.CLR=1 -pend=10
    }
    SetStatus "BMRampMonitorAndStop stopped."
}

proc Plot {args} {
    global filename
    if [file exist $filename] {
	exec sddsplot -col=BM,B:BM* -sep $filename &
    }
}

proc ChangeValue {args} {
    set type Warn
    APSParseArguments {type}
    global WarnValue StopValue
    if [catch {exec cavput -list=B:BM:CurrentWFAve$type=[set ${type}Value] -pend=10} result] {
	return -code error "error change $type value: $result"
    }
    
    SetStatus "$type value was changed to [set ${type}Value]"
}
