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

# $Log: not supported by cvs2svn $
# interface for running sddsfeedforward
#

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)]
APSDebugPath

set CVSRevisionAuthor "\$Revision: 1.1 $ \$Author: shang $"

APSApplication . -name quickSDDSFeedforward -version $CVSRevisionAuthor \
  -overview {This is the quickSDDSFeedforward utility.  It provides a simple interface to the sddsfeedforward program.}

set monitors 0
set monitorLines 0

set mainStatus "ready."

proc SetMainStatus {text} {
    global mainStatus
    set mainStatus $text
    update
}

APSScrolledStatus .status -parent .userFrame -textVariable mainStatus -width 90
 
frame .userFrame.settings -bd 4 -relief raised
pack .userFrame.settings -side top -fill x
set Interval 1
set inputFile /home/helios/PAR/daily/0503/14/2/ParTuning/parRFBeamLoadComp.sdds
set testFile /home/helios/PAR/daily/0503/14/2/ParTuning/feedforwardTest
set runControlPV APS:RunControlSlot4RC
set runControlDesc "par feedforward"
set runControlPingTimeout 30
APSLabeledEntry .settings.runcontrol -parent .userFrame -label "Run control pv:" \
    -textVariable runControlPV -width 80 \
    -contextHelp "enter the run control pv for running feedforward."
APSLabeledEntry .settings.desc -parent .userFrame -label "Run control description:" \
    -textVariable runControlDesc -width 80 \
    -contextHelp "run control description."
APSLabeledEntry .settings.time -parent .userFrame -label "Run control ping timeout (s):" \
    -textVariable runControlPingTimeout -width 80 
APSLabeledEntry .settings.interval -parent .userFrame -label "Interval (s): " \
  -textVariable Interval -width 80 \
  -contextHelp "Enter the time between samples in seconds in this field."
APSLabeledEntry .settings.input -parent .userFrame -label "Feedforwad input file: " \
  -textVariable inputFile -width 80 -commandButton 1 -buttonsOnLeft 1 \
  -contextHelp "Enter a name for the input file of running feedforward."
APSLabeledEntry .settings.test -parent .userFrame -label "Feedforwad test file: " \
  -textVariable testFile -width 80 -commandButton 1 -buttonsOnLeft 1 \
  -contextHelp "the test file of testValues for running feedforward"

frame .userFrame.ops -bd 4 -relief raised
pack .userFrame.ops -side top -fill x
APSButton .ops.run -parent .userFrame -text RUN -command RunFeedforward \
  -contextHelp "start feedforward."
APSButton .ops.stop -parent .userFrame -text "STOP" -command "AbortFeedforward" \
  -contextHelp "abort feedforward"
APSButton .ops.info -parent .userFrame -text "INFO" -command "FeedforwardInfo" \
  -contextHelp "bring up medm screed for feedforward"
set fileSelListDir $env(HOME)

proc RunFeedforward {} {
    global inputFile runControlPV runControlDesc testFile runControlPingTimeout Interval

    if ![string length $runControlPV] {
	SetMainStatus "run control pv is not provided.!"
	return
    }
    if ![string length $runControlDesc] {
	set runControlDesc "par feedforward"
    }
    if {![string length $inputFile]} {
	SetMainStatus "input file is not provided for running feedforward!"
	return
    }
    if ![file exist $inputFile] {
	SetMainStatus "$inputFile does not exist"
	return
    }
    if {[string length $testFile] && ![file exist $testFile]} {
	SetMainStatus "test file $testFile does not exist"
	return
    }
    if [file exist $testFile] {
	set option "-testValues=$testFile"
    } else {
	set option ""
    }
    exec medm -x -attach -macro RCPV=$runControlPV ./sr/psApp/APSRunControlSingle.adl &
    APSExecLog .parfeedforward -width 80 -unixCommand \
      "sddsfeedforward $inputFile -interval=$Interval -verbose -infiniteLoop -runControlPV=string=$runControlPV,pingTimeout=$runControlPingTimeout \"-runControlDescription=$runControlDesc\" $option"
}

proc AbortFeedforward {} {
    global runControlPV
    if [catch {exec cavput -list=$runControlPV.ABRT=1 -pend=30} result] {
	SetMainStatus "Error in aborting feedforward: $result"
	return
    }
    catch {exec cawait -waitfor=$runControlPV.RUN,equal=0}
}

proc FeedforwardInfo {args} {
    global runControlPV
    exec medm -x -attach -macro RCPV=$runControlPV ./sr/psApp/APSRunControlSingle.adl &
}
