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

# $Log: not supported by cvs2svn $
# Revision 1.4  2004/02/19 17:31:37  soliday
# Changed PV names once again.
#
# Revision 1.3  2004/02/19 16:24:58  soliday
# Updated to reflect updated PV names that where changed to conform with the
# naming conventions at the beamlines.
#
# Revision 1.2  2004/02/18 22:16:18  soliday
# Changed the EPICS_CA_ADDR_LIST because the workstation that runs the
# related epics server has changed.
#
# Revision 1.1  2004/02/13 21:00:52  soliday
# Program to be used by beamlines to see if they have been flagged as absent.
#
#

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.5 $ \$Author: soliday $"

#set env(EPICS_CA_ADDR_LIST) "164.54.3.61"
#set env(EPICS_CA_AUTO_ADDR_LIST) NO

proc SetState {args} {
    set unit ""
    set id ""
    APSStrictParseArguments {unit id}
    global state
    if {$state($unit) == "Absent"} {
        $id.state configure -bg red
        $id.dispute configure -state normal
    } elseif {$state($unit) == "Present"} {
        $id.state configure -bg green
        $id.dispute configure -state disabled
    } else {
        $id.state configure -bg grey85
        $id.dispute configure -state disabled
    }
}

proc SetDispute {args} {
    set unit ""
    APSStrictParseArguments {unit}
    global disputed2 dispute
    set disputed2($unit) $dispute($unit)
    if {[catch {pv putw disputed2($unit)} result]} {
        puts "$result"
    }
}

proc SetDisputed {args} {
    set unit ""
    APSStrictParseArguments {unit}
    global disputed dispute
    set dispute($unit) $disputed($unit)
}

proc SetNoted {args} {
    set unit ""
    set id ""
    APSStrictParseArguments {unit id}
    global noted
    if {$noted($unit) == "Noted"} {
        $id.noted configure -bg yellow -fg black
    } else {
        $id.noted configure -bg grey85 -fg grey85
    }
}

proc AlertFC {args} {
    global state disputed
    foreach a [array names state] {
        if {($state($a) == "Absent") && ($disputed($a) == "Reset")} {
            bell
            break
        }
    }
    after 10000 AlertFC
}

APSApplication . -name "Attendance Notification" -version $CVSRevisionAuthor -overview "Monitor beamline attendance."

pv toggleErrorMode

for {set i 1} {$i <= 40} {incr i} {
    append sectors "$i "
}
append sectors "N "

set sector "0"
set args $argv

APSStrictParseArguments {sector}

if {[lsearch -exact $sectors $sector] == -1} {
    APSAlertBox .alert -parent .userFrame -errorMessage "Usage: AttendanceNotification -sector <num>" -type error
    exit
}

global tcl_platform
if {[string range $tcl_platform(os) 0 6] == "Windows"} {
    set beamlineFile "c:/beamlines/stations.sdds"
} else {
    set beamlineFile "/home/helios/oagData/flcoord/stations.sdds"
}

if {[catch {exec sddsprocess $beamlineFile -pipe=out -match=column,Sector=$sector -nowarn | sdds2stream -pipe=in -columns=Sector,Beamline,StationA,StationB,StationC,StationD,StationE,StationI,StationX} sectorInfo]} {
    APSAlertBox .alert -parent .userFrame -errorMessage "Unable to read stations.sdds file\n$sectorInfo" -type error
    exit
}

if {![llength $sectorInfo]} {
    APSAlertBox .alert -parent .userFrame -errorMessage "No beamlines defined for sector $sector"
    exit
}

foreach "sector beamline stationA stationB stationC stationD stationE stationI stationX" $sectorInfo {
    foreach station "A B C D E I X" {
        if {[set station$station] == "y"} {
            append pvNames "S${sector}${beamline}${station}:MarkedAbsentBO S${sector}${beamline}${station}:MarkedAbsentBO S${sector}${beamline}${station}:AbsenceDisputedBO S${sector}${beamline}${station}:AbsenceDisputedBO S${sector}${beamline}${station}:DisputeNotedBO S${sector}${beamline}${station}:DisputeNotedBO "
            append varNames "state(${sector}.${beamline}.${station}) state2(${sector}.${beamline}.${station}) disputed(${sector}.${beamline}.${station}) disputed2(${sector}.${beamline}.${station}) noted(${sector}.${beamline}.${station}) noted2(${sector}.${beamline}.${station}) "
        }
    }
}
if {[catch {pv linkw $varNames $pvNames 30} results]} {
    APSAlertBox .alert -parent .userFrame -errorMessage "Unable to link to PVs\n$results" -type error
    exit
}


pack [label .userFrame.label -text "Sector \#$sector" -font "Courier 20 bold"]
pack [frame .userFrame.display]
foreach "sector beamline stationA stationB stationC stationD stationE stationI stationX" $sectorInfo {
    pack [frame .userFrame.display.beamline${beamline}] -side left
    foreach station "A B C D E I X" {
        if {[set station$station] == "y"} {
            APSFrame .station${station} \
                -parent .userFrame.display.beamline${beamline} \
                -label "$beamline $station" \
                -packOption "-pady 3"
            set w .userFrame.display.beamline${beamline}.station${station}.frame
            pack [label ${w}.state -textvariable state(${sector}.${beamline}.${station})]
            pack [checkbutton ${w}.dispute -text "Dispute" -variable dispute(${sector}.${beamline}.${station}) -onvalue "Disputed" -offvalue "Reset" -command "SetDispute -unit ${sector}.${beamline}.${station}"]
            pack [label ${w}.noted -textvariable noted(${sector}.${beamline}.${station})]



            pv umon state(${sector}.${beamline}.${station}) \
                "SetState -unit ${sector}.${beamline}.${station} -id $w"
            pv umon disputed(${sector}.${beamline}.${station}) \
                "SetDisputed -unit ${sector}.${beamline}.${station}"
            pv umon noted(${sector}.${beamline}.${station}) \
                "SetNoted -unit ${sector}.${beamline}.${station} -id $w"

            SetState -unit ${sector}.${beamline}.${station} -id $w
            SetDisputed -unit ${sector}.${beamline}.${station}
            SetNoted -unit ${sector}.${beamline}.${station} -id $w
        }
    }
}

after 10000 AlertFC
