#!/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 apsttk 1
APSDebugPath

APSApplication . \
    -name BTS-DCPS \
    -overview "BTS-DCPS provides convenience controls for BTS DC power supply convertors." 

set DcpsStatus "Ready..."
APSScrolledStatus .status \
    -parent .userFrame \
    -width 80 \
    -packOption "-fill both -expand true" \
    -textVariable DcpsStatus 

proc SetDcpsStatus {text} {
    global DcpsStatus
    set DcpsStatus $text
    update
}

proc MakeActionWidget {widget args} {
    set parent ""
    APSParseArguments {parent}
    set w $parent$widget
    frame $w -bd 4 -relief raised
    pack $w -side top -fill x 

    APSButton .execute \
        -width "" \
        -text EXECUTE \
        -command "ExecuteBtsDcpsCommands ; APSDisableButton $w.execute.button" \
        -parent $w \
        -contextHelp "Executes the command configured by the various settings.  It must be enabled by the ENABLE button and may be disabled by the DISABLE button"
    APSDisableButton $w.execute.button
    APSButton .enableExec \
        -width "" \
        -text ENABLE \
        -command "APSEnableButton $w.execute.button" \
        -parent $w \
        -contextHelp "Enables the EXECUTE button."
    APSButton .disableExec \
        -width "" \
        -text DISABLE \
        -command "APSDisableButton $w.execute.button" \
        -parent $w \
        -contextHelp "Disables the EXECUTE button."
}

set currentlyRunning 0
proc ExecuteBtsDcpsCommands {} {
    global SetValue ConfigFile2 ConfigFile3 ConfigFile4 CommandChoice
    global SectorSelection FamilyName DetailedSelection currentlyRunning
    if {$CommandChoice==""} {
        SetDcpsStatus "You must select one of the command choices before pressing EXECUTE"
        return
    }
    if {$currentlyRunning} {
        SetDcpsStatus "Please wait for current operation to finish."
        return 
    }
    set names ""
    foreach name [array names DetailedSelection] {
        if {$DetailedSelection($name)} {
            lappend names $name
        }
    }
    if {[llength $names]} {
        set currentlyRunning 1
        SetDcpsStatus "Starting action=$CommandChoice for selected BTS PS"
        if [catch {APSUBtsDcpsAction -names $names \
                       -action $CommandChoice \
                       -value $SetValue \
                       -statusCallback SetDcpsStatus} result] {
            SetDcpsStatus "Error: $result"
            set currentlyRunning 0
        } else {
            if {$result != ""} {
                SetDcpsStatus "$result"
            }
            if {($CommandChoice == "on") || ($CommandChoice == "off")} {
                APSWaitWithUpdate -waitSeconds 5
            }
            SetDcpsStatus "Done with action=$CommandChoice for selected BTS PS"
            set currentlyRunning 0
        }
    }
}


proc MakeCommandChoiceWidget {widget args} {
    global SetValue ConfigFile2 ConfigFile3 ConfigFile4 CommandChoice
    set parent ""
    APSParseArguments {parent}
    
    set CommandChoice ""

    set w $parent$widget
    frame $w -bd 4 -relief raised
    pack $w -side top -fill x

    label $w.title -text "DC Power Supply Commands" -relief ridge
    pack $w.title -side top -fill x
    
    frame $w.cb
    pack $w.cb -side top -fill x
    radiobutton $w.cb.on -value on -variable CommandChoice -relief ridge -text ON
    radiobutton $w.cb.off -value off -variable CommandChoice -relief ridge -text OFF
    radiobutton $w.cb.ccmode -value ccmode -variable CommandChoice -relief ridge -text CCMODE
    radiobutton $w.cb.set -value set -variable CommandChoice -relief ridge -text SET
    radiobutton $w.cb.read -value read -variable CommandChoice -relief ridge -text READ
    radiobutton $w.cb.config -value configure -variable CommandChoice -relief ridge -text CONFIGURE
    radiobutton $w.cb.cond -value forcecondition -variable CommandChoice -relief ridge -text CONDITION
    radiobutton $w.cb.stopcond -value stopcond -variable CommandChoice -relief ridge -text "STOP COND."
    radiobutton $w.cb.listcond -value listcond -variable CommandChoice -relief ridge -text "LIST COND."
    radiobutton $w.cb.cycle -value setcycles -variable CommandChoice -relief ridge -text "SET CYCLES"
    #radiobutton $w.cb.validate -value validateconnections -variable CommandChoice -relief ridge -text "VALIDATE CONNECTIONS"
    #pack $w.cb.on $w.cb.off $w.cb.set $w.cb.config $w.cb.cond $w.cb.stopcond $w.cb.listcond $w.cb.cycle $w.cb.validate -side left -fill x 
    pack $w.cb.on $w.cb.off $w.cb.ccmode $w.cb.set $w.cb.read $w.cb.config $w.cb.cond $w.cb.stopcond $w.cb.listcond $w.cb.cycle -side left -fill x 

    set SetValue 0
    #set ConfigFile1 "/home/helios/oagData/bts/magnetConditioning/standardize.config"
    set ConfigFile2 "/home/helios/oagData/bts/magnetConditioning/standardizeDwell.config"
    set ConfigFile3 "/home/helios/oagData/bts/magnetConditioning/degauss.config"
    set ConfigFile4 "/home/helios/oagData/bts/magnetConditioning/triDegauss.config"
    frame $w.entries
    pack $w.entries -side top
    APSLabeledEntry .val \
        -parent $w.entries \
        -label "Value: " \
        -textVariable SetValue \
        -width 60 \
        -contextHelp "A setpoint value that will be sent to all selected power supplies if the SET or SET TOLER. command is used."
    APSLabeledOutput .cfile2 \
        -parent $w.entries \
        -label "Config file \#1: " \
        -textVariable ConfigFile2 \
        -width 60
    APSLabeledOutput .cfile3 \
        -parent $w.entries \
        -label "Config file \#2: " \
        -textVariable ConfigFile3 \
        -width 60
    APSLabeledOutput .cfile4 \
        -parent $w.entries \
        -label "Config file \#3: " \
        -textVariable ConfigFile4 \
        -width 60
}


proc MakeDcpsWidget {widget args} {
    global FamilyName
    set parent ""
    APSParseArguments {parent}
    set w $parent$widget
    frame $w -bd 4 -relief raised
    pack $w -side top -fill x

    label $w.title -text "DC Power Supply Family Selection" -relief ridge
    pack $w.title -side top -fill x

    frame $w.ops -bd 2 
    pack $w.ops -side top -fill x
    APSButton .setAll -width "" -text "SELECT ALL" -command "SetSelections -selectAll 1" -parent $w.ops 
    APSButton .setBTSside -width "" -text "SELECT BTS SIDE " -command "SetSelections -selectBTS 1" -parent $w.ops 
    APSButton .setSRside -width "" -text "SELECT SR SIDE " -command "SetSelections -selectSR 1" -parent $w.ops 
    APSButton .clearBTSside -width "" -text "CLEAR BTS SIDE" -command "SetSelections -clearBTS 1" -parent $w.ops 
    APSButton .clearSRside -width "" -text "CLEAR SR SIDE" -command "SetSelections -clearSR 1" -parent $w.ops 
    APSButton .clearAll -width "" -text "CLEAR ALL" -command "SetSelections -clearAll 1" -parent $w.ops 
    
    frame $w.check -bd 2 
    pack $w.check -side top -fill x
    set choices "CORRs QUADS SKEW_QUADS DIPOLES SEPTUMS KICKERS"
    set FamilyName(CORRs) HV
    set FamilyName(QUADS) Q 
    set FamilyName(SKEW_QUADS) SQ
    set FamilyName(DIPOLES) BM
    set FamilyName(SEPTUMS) S
    set FamilyName(KICKERS) K
    foreach choice $choices {
        APSButton .b$choice -width "" -text $choice -command "SetSelections -category $choice" -parent $w.check
    }
    SetSelections -clearAll 1
}

proc MakeDetailedWidget {widget args} {
    global DetailedSelection
    set parent ""
    APSParseArguments {parent}
    set w $parent$widget
    frame $w -bd 4 -relief raised
    pack $w -side top -fill x

    label $w.title -text "BTS Detailed Selection" -relief ridge
    pack $w.title -side top -fill x

    frame $w.check -bd 2 
    pack $w.check -side top -fill x

    set n 1
    frame $w.check.line$n
    pack $w.check.line$n -side left -anchor nw
    set i 1
    foreach name "BTS:AH1 BTS:AV1 BTS:AH2 BTS:AV2 BTS:AH3 BTS:AV3" {
        set DetailedSelection($name) 0
        checkbutton $w.check.line$n.ps$i -text $name \
            -variable DetailedSelection($name) -relief ridge -width 11 -anchor w
        pack $w.check.line$n.ps$i -side top -fill x
        incr i
    }
    incr n
    frame $w.check.line$n
    pack $w.check.line$n -side left -anchor nw
    set i 1
    foreach name "BTS:BH1 BTS:BV1 BTS:BH2 BTS:BV2 BTS:BV3" {
        set DetailedSelection($name) 0
        checkbutton $w.check.line$n.ps$i -text $name \
            -variable DetailedSelection($name) -relief ridge -width 11 -anchor w
        pack $w.check.line$n.ps$i -side top -fill x
        incr i
    }
    incr n
    frame $w.check.line$n
    pack $w.check.line$n -side left -anchor nw
    set i 1
    foreach name "BTS:BSQ1H BTS:BSQ2V BTS:BSQ5H BTS:BSQ6V" {
        set DetailedSelection($name) 0
        checkbutton $w.check.line$n.ps$i -text $name \
            -variable DetailedSelection($name) -relief ridge -width 11 -anchor w
        pack $w.check.line$n.ps$i -side top -fill x
        incr i
    }
    incr n
    frame $w.check.line$n
    pack $w.check.line$n -side left -anchor nw
    set i 1
    foreach name "BTS:CQ1H BTS:CQ2V BTS:CQ3H BTS:DQ1H BTS:DQ2V" {
        set DetailedSelection($name) 0
        checkbutton $w.check.line$n.ps$i -text $name \
            -variable DetailedSelection($name) -relief ridge -width 11 -anchor w
        pack $w.check.line$n.ps$i -side top -fill x
        incr i
    }
    incr n
    frame $w.check.line$n
    pack $w.check.line$n -side left -anchor nw
    set i 1
    foreach name "BTS:AQ1 BTS:AQ2 BTS:AQ3 BTS:AQ4 BTS:AQ5" {
        set DetailedSelection($name) 0
        checkbutton $w.check.line$n.ps$i -text $name \
            -variable DetailedSelection($name) -relief ridge -width 11 -anchor w
        pack $w.check.line$n.ps$i -side top -fill x
        incr i
    }
    incr n
    frame $w.check.line$n
    pack $w.check.line$n -side left -anchor nw
    set i 1
    foreach name "BTS:BQ1 BTS:BQ2 BTS:BQ3 BTS:BQ4 BTS:BQ5" {
        set DetailedSelection($name) 0
        checkbutton $w.check.line$n.ps$i -text $name \
            -variable DetailedSelection($name) -relief ridge -width 11 -anchor w
        pack $w.check.line$n.ps$i -side top -fill x
        incr i
    }
    incr n
    frame $w.check.line$n
    pack $w.check.line$n -side left -anchor nw
    set i 1
    foreach name "BTS:CQ1 BTS:CQ2 BTS:CQ3 BTS:DQ1 BTS:DQ2" {
        set DetailedSelection($name) 0
        checkbutton $w.check.line$n.ps$i -text $name \
            -variable DetailedSelection($name) -relief ridge -width 11 -anchor w
        pack $w.check.line$n.ps$i -side top -fill x
        incr i
    }
    incr n
    frame $w.check.line$n
    pack $w.check.line$n -side left -anchor nw
    set i 1
    foreach name "BTS:BSQ1 BTS:BSQ2 BTS:BSQ3 BTS:BSQ4 BTS:BSQ5 BTS:BSQ6" {
        set DetailedSelection($name) 0
        checkbutton $w.check.line$n.ps$i -text $name \
            -variable DetailedSelection($name) -relief ridge -width 11 -anchor w
        pack $w.check.line$n.ps$i -side top -fill x
        incr i
    }
    incr n
    frame $w.check.line$n
    pack $w.check.line$n -side left -anchor nw
    set i 1
    foreach name "B:EK B:ES1 B:ES2 BTS:AB BTS:BX BTS:BB1 BTS:CB1" {
        set DetailedSelection($name) 0
        checkbutton $w.check.line$n.ps$i -text $name \
            -variable DetailedSelection($name) -relief ridge -width 11 -anchor w
        pack $w.check.line$n.ps$i -side top -fill x
        incr i
    }
    incr n
    frame $w.check.line$n
    pack $w.check.line$n -side left -anchor nw
    set i 1
    foreach name "S38-IES:DK0 S39-IES:IS1 S39-IES:IK1 S39-IES:IK2 S39-IES:IK3" {
        set DetailedSelection($name) 0
        checkbutton $w.check.line$n.ps$i -text $name \
            -variable DetailedSelection($name) -relief ridge -width 11 -anchor w
        pack $w.check.line$n.ps$i -side top -fill x
        incr i
    }
}

proc SetSelections {args} {
    set selectAll 0
    set selectBTS 0
    set selectSR 0
    set clearAll 0
    set clearBTS 0
    set clearSR 0
    set category ""
    APSParseArguments {selectAll selectBTS selectSR clearAll category clearBTS clearSR}
    global DetailedSelection
    set BTSside "BTS:AH1 BTS:AV1 BTS:AH2 BTS:AV2 BTS:AH3 BTS:AV3 BTS:AQ1 BTS:AQ2 BTS:AQ3 BTS:AQ4 BTS:AQ5 B:ES1 B:ES2 B:EK BTS:AB BTS:BX"
    if {$selectAll} {
        foreach name [array names DetailedSelection] {
            set DetailedSelection($name) 1
        }
    } elseif {$selectBTS} {
        foreach name [array names DetailedSelection] {
            if {[lsearch -exact $BTSside $name] != -1} {
                set DetailedSelection($name) 1
            }
        }
    } elseif {$selectSR} {
        foreach name [array names DetailedSelection] {
            if {[lsearch -exact $BTSside $name] == -1} {
                set DetailedSelection($name) 1
            }
        }
    } elseif {$clearBTS} {
        foreach name [array names DetailedSelection] {
            if {[lsearch -exact $BTSside $name] != -1} {
                set DetailedSelection($name) 0
            }
        }
    } elseif {$clearSR} {
        foreach name [array names DetailedSelection] {
            if {[lsearch -exact $BTSside $name] == -1} {
                set DetailedSelection($name) 0
            }
        }
    } elseif {$clearAll} {
        foreach name [array names DetailedSelection] {
            set DetailedSelection($name) 0
        }
    } elseif {$category != ""} {
        if {$category == "CORRs"} {
            foreach name [array names DetailedSelection] {
                if {([string index $name end-1] == "H") || ([string index $name end-1] == "V") || \
                        ([string index $name end] == "H") || ([string index $name end] == "V")} {
                    set DetailedSelection($name) 1
                }
            }
        } elseif {$category == "QUADS"} {
            foreach name [array names DetailedSelection] {
                if {([string index $name end-1] == "Q") && ([string index $name end-2] != "S")} {
                    set DetailedSelection($name) 1
                }
            }
        } elseif {$category == "SKEW_QUADS"} {
            foreach name [array names DetailedSelection] {
                if {([string index $name end-1] == "Q") && ([string index $name end-2] == "S")} {
                    set DetailedSelection($name) 1
                }
            }
        } elseif {$category == "DIPOLES"} {
            foreach name [array names DetailedSelection] {
                if {([string index $name end-1] == "B") || ([string index $name end] == "B")} {
                    set DetailedSelection($name) 1
                }
            }
        } elseif {$category == "SEPTUMS"} {
            foreach name [array names DetailedSelection] {
                if {([string range $name 2 3] == "ES") || ([string range $name 4 9] == "IES:IS")} {
                    set DetailedSelection($name) 1
                }
            }
        } elseif {$category == "KICKERS"} {
            foreach name [array names DetailedSelection] {
                if {([string range $name 4 9] == "IES:IK") || ([string range $name 4 9] == "IES:DK") || ([string range $name 2 3] == "EK")} {
                    set DetailedSelection($name) 1
                }
            }
        }
    }
}

MakeDcpsWidget .dcps -parent .userFrame
MakeDetailedWidget .detailed -parent .userFrame
MakeCommandChoiceWidget .coms -parent .userFrame
MakeActionWidget .ops -parent .userFrame
