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

#
# $Log: not supported by cvs2svn $
# Revision 1.6  2002/03/26 17:01:47  emery
# Added button for gun kicker. Changed other buttons to include
# gun kicker actions.
#
# Revision 1.5  2001/05/11 16:39:07  emery
# Use more conventional error catching and error message passing methods.
#
# Revision 1.4  1999/02/25 17:44:03  soliday
# Replaced exec date commands with clock commands
#
# Revision 1.3  1996/09/19 21:51:39  borland
# Changed from using "exec wish" to "exec oagwish".
#
# Revision 1.2  1996/03/22 04:19:27  borland
# Added buttons to disable groups of PM charges.
#
# Revision 1.1  1996/03/18  23:28:44  borland
# First version.
#
#

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.7 $ \$Author: emery $"
APSDebugPath

proc SetMainStatus {text} {
    global mainStatus
    set mainStatus $text
    update
}
set GunCount 0
set PARCount 0
set BoosterInjCount 0
set BoosterExtCount 0

proc DoGunPMReset {} {
    global GunCount
    if [catch {TogglePulsedMagnetEnables -location Gun \
             } result] {
        SetMainStatus "DoGunPMReset: $result"
        return -code error "DoGunPMReset: $result"
    }
    incr GunCount
    SetMainStatus "Gun PMs reset [clock format [clock seconds] -format %H:%M] ($GunCount)"
}

proc DoPARPMReset {} {
    global PARCount
    if [catch {TogglePulsedMagnetEnables -location PAR \
             } result] {
        SetMainStatus "DoPARPMReset: $result"
        return -code error "DoPARPMReset: $result"
    }
    incr PARCount
    SetMainStatus "PAR PMs reset [clock format [clock seconds] -format %H:%M] ($PARCount)"
}

proc DoBoosterInjPMReset {} {
    global BoosterInjCount PARCount
    if [catch {TogglePulsedMagnetEnables -location GuntoBoosterInj \
             } result] {
        SetMainStatus "DoBoosterInjPMReset: $result"
        return -code error "DoBoosterInjPMReset: $result"
    }
    incr PARCount
    SetMainStatus "PAR PMs reset [clock format [clock seconds] -format %H%M] ($PARCount)"
    incr BoosterInjCount
    SetMainStatus "Booster injection PMs reset [clock format [clock seconds] -format %H%M] ($BoosterInjCount)"
}

proc DoBoosterExtPMReset {} {
    global BoosterInjCount PARCount BoosterExtCount
    if [catch {TogglePulsedMagnetEnables -location GuntoBoosterExt \
             } result] {
        SetMainStatus "DoBoosterExtPMReset: $result"
        return -code error "DoBoosterExtPMReset: $result"
    }
    incr PARCount
    SetMainStatus "PAR PMs reset [clock format [clock seconds] -format %H%M] ($PARCount)"
    incr BoosterInjCount
    SetMainStatus "Booster injection PMs reset [clock format [clock seconds] -format %H%M] ($BoosterInjCount)"
    incr BoosterExtCount
    SetMainStatus "Booster extraction PMs reset [clock format [clock seconds] -format %H%M] ($BoosterExtCount)"
}


proc DoGunPMOff {} {
    global GunCount
    if [catch {SetPulsedMagnetEnables -location Gun -state 0 \
             } result] {
        SetMainStatus "DoGunPMOff: $result"
        return -code error "DoGunPMOff: $result"
    }
    incr GunCount
    SetMainStatus "Gun PMs off [clock format [clock seconds] -format %H:%M] ($GunCount)"
}

proc DoPARPMOff {} {
    global PARCount
    if [catch {SetPulsedMagnetEnables -location PAR -state 0\
             } result] {
        SetMainStatus "DoPARPMOff: $result"
        return -code error "DoPARPMOff: $result"
    }
    incr PARCount
    SetMainStatus "PAR PMs off [clock format [clock seconds] -format %H:%M] ($PARCount)"
}

proc DoBoosterInjPMOff {} {
    global BoosterInjCount PARCount
    if [catch {SetPulsedMagnetEnables -location GuntoBoosterInj -state 0 \
             } result] {
        SetMainStatus "DoBoosterInjPMOff: $result"
        return -code error "DoBoosterInjPMOff: $result"
    }
    incr PARCount
    SetMainStatus "PAR PMs off [clock format [clock seconds] -format %H%M] ($PARCount)"
    incr BoosterInjCount
    SetMainStatus "Booster injection PMs off [clock format [clock seconds] -format %H%M] ($BoosterInjCount)"
}

proc DoBoosterExtPMOff {} {
    global BoosterInjCount PARCount BoosterExtCount 
    if [catch {SetPulsedMagnetEnables -location GuntoBoosterExt  -state 0 \
             } result] {
        SetMainStatus "DoBoosterExtPMOff: $result"
        return -code error "DoBoosterExtPMOff: $result"
    }
    incr PARCount
    SetMainStatus "PAR PMs off [clock format [clock seconds] -format %H%M] ($PARCount)"
    incr BoosterInjCount
    SetMainStatus "Booster injection PMs off [clock format [clock seconds] -format %H%M] ($BoosterInjCount)"
    incr BoosterExtCount
    SetMainStatus "Booster extraction PMs off [clock format [clock seconds] -format %H%M] ($BoosterExtCount)"
}

proc DoBoosterInjOnlyPMOff {} {
    global BoosterInjCount PARCount
    if [catch {SetPulsedMagnetEnables -location BoosterInj  -state 0 \
             } result] {
        SetMainStatus "DoBoosterInjOnlyPMOff: $result"
        return -code error "DoBoosterInjOnlyPMOff: $result"
    }
    incr BoosterInjCount
    SetMainStatus "Booster injection PMs off [clock format [clock seconds] -format %H%M] ($BoosterInjCount)"
}

proc DoBoosterExtOnlyPMOff {} {
    global BoosterInjCount PARCount BoosterExtCount
    if [catch {SetPulsedMagnetEnables -location BoosterExt  -state 0 \
             } result] {
        SetMainStatus "DoBoosterExtOnlyPMOff: $result"
        return -code error "DoBoosterExtOnlyPMOff: $result"
    }
    incr BoosterExtCount
    SetMainStatus "Booster extraction PMs off [clock format [clock seconds] -format %H%M] ($BoosterExtCount)"
}


proc MakePMResetWidget {widget args} {
    set parent ""
    if [APSStrictParseArguments {parent}]==-1 {
        return -code error
    }
    APSFrame $widget -parent $parent -label "Reset Pulsed Magnet Timeouts" \
      -contextHelp "Buttons for pulsed magnet timeout resets.  Use only if beam is needed to the system in question!"

    set w $parent$widget.frame
    APSButton .gun -parent $w -text Gun \
      -command DoGunPMReset \
      -contextHelp \
      "Resets Gun pulsing timeouts. Use only if beam is needed in the linac." 
    APSButton .par -parent $w -text "Gun to PAR" \
      -command DoPARPMReset \
      -contextHelp \
      "Resets Gun PAR injection and extraction timeouts.  Use only if beam is needed in the PAR or PTB." 
    APSButton .booInj -parent $w -text "Gun to Booster Injection" \
      -command "DoBoosterInjPMReset" \
      -contextHelp \
      "Resets PAR injection and extraction timeouts and booster injection timeouts.  Use only if beam is needed in the Booster"
    APSButton .booExtr -parent $w -text "Gun to Booster Extraction" \
      -command "DoBoosterExtPMReset" \
      -contextHelp \
      "Resets PAR and Booster injection and extraction timeouts.  Use only if beam is needed in the BTS line or storage ring."

}


proc MakePMOffWidget {widget args} {
    set parent ""
    if [APSStrictParseArguments {parent}]==-1 {
        return -code error
    }
    APSFrame $widget -parent $parent -label "Disable Pulsed Magnets" \
      -contextHelp "Buttons for pulsed magnet charge disabling."
    APSFrameGrid .fg -parent $parent$widget.frame -yList {f1 f2}

    set w $parent$widget.frame.fg.f1
    APSButton .gun -parent $w -text Gun \
      -command DoGunPMOff \
      -contextHelp \
      "Turns off pulsed magnet charge signals for Gun."
    APSButton .par -parent $w -text "Gun to PAR" \
      -command DoPARPMOff \
      -contextHelp \
      "Turns off pulsed magnet charge signals for PAR injection and extraction."
    APSButton .booInj -parent $w -text "Gun to Booster Injection" \
      -command "DoBoosterInjPMOff" \
      -contextHelp \
      "Turns off pulsed magnet charge signals for PAR injection and extraction, and Booster injection."
    APSButton .booExtr -parent $w -text "Gun to Booster Extraction" \
      -command "DoBoosterExtPMOff" \
      -contextHelp \
      "Turns off pulsed magnet charge signals for PAR and Booster injection and extraction."


    set w $parent$widget.frame.fg.f2
    APSButton .booInj -parent $w -text "Booster Injection" \
      -command "DoBoosterInjOnlyPMOff" \
      -contextHelp \
      "Turns off pulsed magnet charge signals for Booster injection."
    APSButton .booExtr -parent $w -text "Booster Extraction" \
      -command "DoBoosterExtOnlyPMOff" \
      -contextHelp \
      "Turns off pulsed magnet charge signals for Booster extraction."
}


APSApplication . -name timingResets -version $CVSRevisionAuthor \
  -overview "Provides convenient buttons for resetting pulsed magnet timeouts.  The pulsed magnet timing resets are used to restore beam after a magnet-protection timeout, and should be used only when beam is required.\nDO NOT USE THESE BUTTONS JUST TO SILENCE ALARMS."

set mainStatus "DO NOT USE THESE BUTTONS JUST TO SILENCE ALARMS."
APSScrolledStatus .status -parent .userFrame -textVariable mainStatus -width 60 \
  -contextHelp "Provides a scrolling status display telling you what resets have been done, when, and the cumulative number of times they've been done."
SetMainStatus "Use only to restore beam."

MakePMResetWidget .pmreset -parent .userFrame
MakePMOffWidget .pmoff -parent .userFrame
