#!/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 CVSRevisionAuthor "\$Revision: 1.6 $ \$Author: borland $"

#

set ControlStatus "Ready."

#Make application and status window widgets.

proc MakeSRBPMCheckoutStatusWidget {widget args} {

        global ControlStatus
        set parent ""
        APSParseArguments {parent}
        APSApplication . -name "SR BPM Checkout" \
                -overview "SR BPM Checkout Procedure Interface."

        set w $parent$widget
        APSScrolledStatus .status -parent $w -textVariable ControlStatus -width 75 -packOption {-side top}
}

#Make BPM local bus address checkout widget.

proc MakeSRBPMLocalBusAddressCheckoutWidget {widget args} {
        
        global ControlStatus
        set parent ""
        APSParseArguments {parent}
        set w $parent$widget

        APSButton .checkBPMLocalBusFrame -parent $w \
                -text "Check SR BPM Local Bus Addresses" \
                -command {CheckSRBPMLocalBusAddresses -statusCallback SetStatus} \
                -packOption {-side top} \
                -contextHelp "This button invokes a procedure that reads the SR BPM local bus addresses and produces a list of BPMs that have do not have the correct local bus address.  The IOC(s) and VXI crate(s) for the BPM(s) that have incorrect local bus addresses need to be re-booted."
}

#Sets the status variable used by the status widget.

proc SetStatus {text} {
        global ControlStatus
        set ControlStatus $text
        update
        }

#Disable buttons once one is pressed.

proc DisableMachineChoiceButtons {} {
        APSDisableButton .userFrame.checkBPMLocalBusFrame.button
}

#Enable buttons once one is pressed and its associated procedure finishes.

proc EnableMachineChoiceButtons {} {
        APSEnableButton .userFrame.checkBPMLocalBusFrame.button
}

# Checks the SR BPM local bus addresses for errors.  If errors are detected, an sddsfile is made in /tmp 
# and sddsprintout is used to display the specific BPMs with errors, sector number, and IOC number.  Returns 0
# if there are errors; 1 if there are no errors.

proc CheckSRBPMLocalBusAddresses {args} {
        
        global ControlStatus

        DisableMachineChoiceButtons
        APSParseArguments {statusCallback}

        if {$statusCallback!=""} {
                $statusCallback "Checking SR BPM Local Bus Addresses..."
        }
        
        set BPMLabelList [list {A:P1} {A:P2} {A:P3} {A:P4} {B:P5} {B:P4} {B:P3} {B:P2} {B:P1}]
        set BPMLabelCommand "-list=A:P1,A:P2,A:P3,A:P4,B:P5,B:P4,B:P3,B:P2,B:P1"
        set BPMcmd "exec cavget -list=S -range=beg=1,end=40 $BPMLabelCommand -list=:raddr_li.VAL -label -pend=10 \"-delim= \""
        catch $BPMcmd SRBPMBusAddressesAndValue
        set BPMLocalBusErrorList ""
        set listIndex 0
        set badBPMCount 0
        for {set sector 1} {$sector <= 40} {incr sector 1} {
                set bpmAddressIndex 0
                foreach bpmLabel $BPMLabelList {
                        set SRBPMName [join "S $sector $bpmLabel" ""]
                        set SRBPMAddressValue [lindex $SRBPMBusAddressesAndValue [expr $listIndex + 1]]
                        if {$SRBPMAddressValue!=$bpmAddressIndex} {
                                incr badBPMCount 1
                                if {$badBPMCount=="1"} {
                                        set badBPMFile "/tmp/SRBadBPM[APSTmpString].sdds"
                                        set badBPMFileID [open $badBPMFile w+]
                                        CreateBadSRBPMSDDSFileHeader $badBPMFileID
                                }
                                if {$sector<=34} {
                                        if {fmod($sector,2)=="1" && fmod([expr $sector + 1],2)=="0" && $sector<=34} {
                                                set IOCNumber $sector
                                        } else {set IOCNumber [expr $sector - 1]}
                                        set SRBPMIOCName "iocs${IOCNumber}bpm"
                                } elseif {$sector>34 && $sector<=41} {
                                        set SRBPMIOCName "iocs${sector}bpm"
                                }
                                puts $badBPMFileID "$sector $SRBPMName $SRBPMIOCName $bpmAddressIndex $SRBPMAddressValue"
                        }
                incr bpmAddressIndex
                incr listIndex 2
                }
        }
        if {$badBPMCount!=0} {       
                close $badBPMFileID
                set createBadBPMFileCmd "sddsprocess -pipe=out ${badBPMFile} \"-define=param,BadBPMCount,$badBPMCount,type=long\" | sddsprintout -pipe=in -param -col > /tmp/SRBPMDisplayFile.txt"
                if [catch {eval exec $createBadBPMFileCmd} result] {APSAlertBox .alert -errorMessage "$result"}
                
                if {$statusCallback!=""} {
                        $statusCallback "SR BPM local bus address errors occurred.\nRe-boot the IOC(s) and VXI crate(s) for the listed BPM(s)."
                }
                APSFileDisplayWindow .badBPMdisplay -comment "SR BPMs With Local Bus Errors" \
                                                    -deleteOnClose 1 \
                                                    -fileName "/tmp/SRBPMDisplayFile.txt"
                APSAlertBox .alert -errorMessage "SR BPM local bus address error(s) occurred.  Re-boot the IOC(s) and 
VXI crate(s) for the listed BPM(s)."
                after 1000 "exec rm ${badBPMFile}"
                EnableMachineChoiceButtons
                return 0
        } else {
                if {$statusCallback!=""} {
                        $statusCallback "SR BPM local bus addresses verified and correct."
                }
                EnableMachineChoiceButtons
                return 1
        }
}

# This procedure creates the SDDS file header for SR BPMs that had bus errors.  Used in the procedure 
# proc CheckSRBPMLocalBusAddresses.  Returns 1 when finished

proc CreateBadSRBPMSDDSFileHeader {badBPMFileID} {

puts $badBPMFileID "SDDS1\n&description\ntext = \"SR Bad BPM List\",  contents = \"SR Bad BPMs vs Sector\", &end\n&column\nname = SRSector,  symbol = SRSector,  description = SRSector,  type = string, &end\n&column\nname = BPM, symbol = BPM, description = SRBPM, type=string, &end\n&column\nname = BPMIOCName, symbol = BPMIOCName, description = BPMIOCName, type=string, &end\n&column\nname = BusAddress, symbol = BusAddress, description = BusAddress, type=string, &end\n&column\nname = DetectedBusAddress, symbol = DetectedBusAddress, description = DetectedBusAddress, type=string, &end\n&data\nmode = ascii, no_row_counts=1 &end"

return 1
}

MakeSRBPMCheckoutStatusWidget .userFrame .
MakeSRBPMLocalBusAddressCheckoutWidget .userFrame .
