#!/bin/sh  
# \
exec oagtclsh "$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)]

# Input files: /home/helios/oagData/deviceConfig/IOCs.sdds
# Output files: IOCscreen.adl
#               SRIOCscreen.adl

APSStandardSetup

source adl.tcl

proc addLegend {args} {
    global adlOutput
    set x 50
    set y 50
    APSStrictParseArguments {x y}
    
    addOvalObject -x $x -y $y -clr 61
    addTextObject -x [expr $x + 10] -y $y -width 32 -height 10 -clr 14 -textix IOCNAME
    addRectangleObject -x [expr $x + 115] -y $y -width 20 -height 6 -clr 50
    addRectangleObject -x [expr $x + 140] -y $y -width 20 -height 6 -clr 50
    addRectangleObject -x [expr $x + 165] -y $y -width 20 -height 6 -clr 50
    addRectangleObject -x [expr $x + 115] -y $y -width 15 -height 6 -clr 15
    addRectangleObject -x [expr $x + 140] -y $y -width 10 -height 6 -clr 15
    addRectangleObject -x [expr $x + 165] -y $y -width 5 -height 6 -clr 15
    addTextObject -x [expr $x + 165] -y [expr $y + 10] -width 80 -height 11 -textix "Free CPU %"
    addTextObject -x [expr $x + 140] -y [expr $y + 23] -width 150 -height 11 -textix "Free FD %"
    addTextObject -x [expr $x + 115] -y [expr $y + 36] -width 130 -height 11 -textix "Free Memory %"
    addTextObject -x $x -y [expr $y + 36] -width 80 -height 11 -textix "Status"
    addLineObject -x [expr $x + 3] -y [expr $y + 10] -width 0 -height 22
    addLineObject -x [expr $x + 125] -y [expr $y + 10] -width 0 -height 22
    addLineObject -x [expr $x + 150] -y [expr $y + 10] -width 0 -height 9

}

proc addIOC {args} {
    global fdmax memmax memPV fdPV
    set x 50
    set y 50
    set textwidth 100
    set barwidth 20
    set name ioclic1
    set relatedDisplay ""
    set relatedDisplayArgs ""
    set height 7
    APSStrictParseArguments {x y name platform textwidth barwidth relatedDisplay relatedDisplayArgs height}

    if {$platform == "VxWorks"} {
        if {$memmax($name) < 1.67772e+07} {
            set memmax($name) 1.67772e+07
        }
        if {$fdmax($name) < 250} {
            set fdmax($name) 250
        }
    } elseif {$platform == "RTEMS"} {
        if {$memmax($name) < 1.25716e+07} {
            set memmax($name) 1.25716e+07
        }
        if {$fdmax($name) < 150} {
            set fdmax($name) 150
        }
    } else {
        if {$memmax($name) < 1.30236e+08} {
            set memmax($name) 1.30236e+08
        }
        if {$fdmax($name) < 1024} {
            set fdmax($name) 1024
        }
    }


    if {$relatedDisplay != ""} {
        addRelatedDisplay -x $x -y $y -width 165 -height $height -clr 6 -bclr 6 -invisible 1 -display1name /C2/screens/adl/$relatedDisplay -display1arguments $relatedDisplayArgs
    }

    addOvalObject -x $x -y $y -clr 20 -chan ${name}:status -vis "if zero"
    addOvalObject -x $x -y $y -clr 61

    incr x 10
    addTextObject -x $x -y $y -width $textwidth -height $height -textix [string toupper $name]

    incr x [expr $textwidth + 5]
    addRectangleObject -x [expr $x - 2] -y [expr $y - 0] -width [expr $barwidth + 4] -height [expr $height + 0] -alarmPV $memPV($name) -alarmLimit [expr int($memmax($name) * .10)] -clr 20
    addBarObject -x $x -y [expr $y + ($height - 6) / 2] -width $barwidth -height 6 -chan $memPV($name) -hoprSrc default -hoprDefault $memmax($name) -clr 15 -bclr 50

    incr x [expr $barwidth + 5]
    if {$fdPV($name) != ""} {
        addRectangleObject -x [expr $x - 2] -y [expr $y - 0] -width [expr $barwidth + 4] -height [expr $height + 0] -alarmPV ${name}:FD_FREE -alarmLimit [expr int($fdmax($name) * .10)] -clr 20
        addBarObject -x $x -y [expr $y + ($height - 6) / 2] -width $barwidth -height 6 -chan ${name}:FD_FREE -hoprSrc default -hoprDefault $fdmax($name) -clr 15 -bclr 50
    }

    incr x [expr $barwidth + 5]
    addRectangleObject -x [expr $x - 2] -y [expr $y - 0] -width [expr $barwidth + 4] -height [expr $height + 0] -alarmPV ${name}:load
    addBarObject -x $x -y [expr $y + ($height - 6) / 2] -width $barwidth -height 6 -chan ${name}:load -direction left -clr 50 -bclr 15

}

proc addEngineeringIOC {args} {
    global fdmax memmax
    set x 50
    set y 50
    set textwidth 100
    set barwidth 20
    set name ioclic1
    set relatedDisplay ""
    set relatedDisplayArgs ""
    set height 7
    APSStrictParseArguments {x y name platform textwidth barwidth relatedDisplay relatedDisplayArgs height}

    if {$relatedDisplay != ""} {
        addRelatedDisplay -x $x -y $y -width 165 -height $height -clr 6 -bclr 6 -invisible 1 -display1name /C2/screens/adl/$relatedDisplay -display1arguments $relatedDisplayArgs
    }

    addOvalObject -x $x -y $y -clr 20 -chan ${name}:status -vis "if zero"
    addOvalObject -x $x -y $y -clr 61

    incr x 10
    addTextObject -x $x -y $y -width $textwidth -height $height -textix [string toupper $name]

    incr x [expr $textwidth + 5]
    addTextUpdateObject -x $x -y $y -width 30 -height $height -clr 60 -bclr 14 -clrmod "alarm" -chan ${name}:ascaCount

    incr x 35
    addTextUpdateObject -x $x -y $y -width 30 -height $height -clr 60 -bclr 14 -clrmod "alarm" -chan ${name}:dbcaCount

    incr x 35
    addTextUpdateObject -x $x -y $y -width 30 -height $height -clr 60 -bclr 14 -clrmod "alarm" -chan ${name}:seqcaCount

    incr x 40
    addTextUpdateObject -x $x -y $y -width 30 -height $height -clr 60 -bclr 14 -clrmod "alarm" -chan ${name}:ascaDisco

    incr x 35
    addTextUpdateObject -x $x -y $y -width 30 -height $height -clr 60 -bclr 14 -clrmod "alarm" -chan ${name}:dbcaDisco

    incr x 35
    addTextUpdateObject -x $x -y $y -width 30 -height $height -clr 60 -bclr 14 -clrmod "alarm" -chan ${name}:seqcaDisco
}

proc writeAdlFile {args} {
    global adlOutput
    set name newDisplay.adl
    APSStrictParseArguments {name}

    set fid [open $name w]
    puts $fid $adlOutput
    close $fid
}
    

proc IdentifyAllCategories {} {
    global data
    set tagList [lsort -unique [lindex $data(Column.Tag) 0]]
    foreach tag $tagList {
        puts [join [split $tag ,] " "]
    }
}

proc AddIOCs {args} {
    global data
    set x 50
    set y 50
    set machine "LINAC"
    APSStrictParseArguments {x y machine}
    puts "Added IOCs for $machine"
    set tagList [lsort -unique [lindex $data(Column.Tag) 0]]
    set newColumn 0
    foreach tag $tagList {
        set beamline [lindex [split $tag ,] 0]
        if {$beamline == $machine} {
            set category [lrange [join [split $tag ,] " "] 1 end]
            addTextObject -x [expr $x + 10] -y $y -textix "$machine $category" -height 13
            incr y 14
            foreach name [lindex $data(Column.Name) 0] \
              tagValue [lindex $data(Column.Tag) 0] \
              operational [lindex $data(Column.Operational) 0] \
              platform [lindex $data(Column.Platform) 0] \
              displayFile [lindex $data(Column.DisplayPath) 0] \
              displayArgument [lindex $data(Column.DisplayArgument) 0] {
                  if {($tagValue == $tag) && ($operational == 1)} {
                      addIOC -x $x -y $y -height 10 -name $name -platform $platform -barwidth 20 -relatedDisplay $displayFile -relatedDisplayArgs $displayArgument
                      incr y 11
                      if {$y > 900} {
                          incr x 200
                          if {$x == 610} {
                              set y 90
                              set newColumn 1
                          } else {
                              set y 10
                              set newColumn 1
                          }
                      }
                  }
              }
            if {$newColumn == 0} {
                incr y 5
            }
            set newColumn 0
        }
    }
    return $y
}

proc PrintCategories {args} {
    global data scannedCategories
    set tagList [lsort -unique [lindex $data(Column.Tag) 0]]
    foreach tag $tagList {
        set beamline [lindex [split $tag ,] 0]
        puts "$beamline [lrange [join [split $tag ,] " "] 1 end]"
    }
}

proc AddSRIOCs {args} {
    global data
    set x 50
    set y 50
    set machine "SR"
    set category "BPM"
    APSStrictParseArguments {x y category}
    lappend scannedCategories $category
    puts "Added IOCs for $machine"
    set tagList [lsort -unique [lindex $data(Column.Tag) 0]]
    set newColumn 0
    foreach tag $tagList {
        set beamline [lindex [split $tag ,] 0]
        if {$beamline == $machine} {
            if {$category != [lindex [split $tag ,] 1]} { continue }
            set cat [lrange [join [split $tag ,] " "] 1 end]
            addTextObject -x [expr $x + 10] -y $y -textix "$machine $cat" -height 13
            incr y 14
            foreach name [lindex $data(Column.Name) 0] \
              tagValue [lindex $data(Column.Tag) 0] \
              operational [lindex $data(Column.Operational) 0] \
              platform [lindex $data(Column.Platform) 0] \
              displayFile [lindex $data(Column.DisplayPath) 0] \
              displayArgument [lindex $data(Column.DisplayArgument) 0] {
                  if {($tagValue == $tag) && ($operational == 1)} {
                      addIOC -x $x -y $y -height 10 -name $name -platform $platform -barwidth 20 -relatedDisplay $displayFile -relatedDisplayArgs $displayArgument
                      incr y 11
                      if {$y > 1012} {
                          incr x 200
                          if {$x == 1410} {
                              set y 90
                              set newColumn 1
                          } else {
                              set y 10
                              set newColumn 1
                          }
                      }
                  }
              }
            if {$newColumn == 0} {
                incr y 5
            }
            set newColumn 0
        }
    }
    return $y
}

proc AddEngineeringHeader {args} {
    global data
    set x 50
    set y 50
    APSStrictParseArguments {x y}

    addTextObject -x [expr $x + 115] -y $y -width 105 -alignCentered 1 -textix "CA CONNECTIONS" -height 12
    addTextObject -x [expr $x + 225] -y $y -width 105 -alignCentered 1 -textix "CA DISCONNECTS" -height 12
    incr y 12
    addTextObject -x [expr $x + 115] -y $y -width 35 -alignCentered 1 -textix "AS" -height 12
    addTextObject -x [expr $x + 150] -y $y -width 35 -alignCentered 1 -textix "DB" -height 12
    addTextObject -x [expr $x + 185] -y $y -width 35 -alignCentered 1 -textix "SEQ" -height 12
    addTextObject -x [expr $x + 225] -y $y -width 35 -alignCentered 1 -textix "AS" -height 12
    addTextObject -x [expr $x + 260] -y $y -width 35 -alignCentered 1 -textix "DB" -height 12
    addTextObject -x [expr $x + 295] -y $y -width 35 -alignCentered 1 -textix "SEQ" -height 12


    incr y 15
    return $y
}

proc AddEngineeringIOCs {args} {
    global data
    set x 50
    set y 50
    set machine "LINAC"
    set category ""
    APSStrictParseArguments {x y machine category}
    puts "Added IOCs for $machine $category"
    set tagList [lsort -unique [lindex $data(Column.Tag) 0]]
    set newColumn 0
    foreach tag $tagList {
        set beamline [lindex [split $tag ,] 0]
        if {$beamline == $machine} {
            if {[llength $category]} {
                if {$category != [lrange [split $tag ,] 1 end]} { continue }
                set cat $category
            } else {
                set cat [lrange [join [split $tag ,] " "] 1 end]
            }
            addTextObject -x [expr $x + 10] -y $y -textix "$machine $cat" -height 13
            incr y 14
            foreach name [lindex $data(Column.Name) 0] \
              tagValue [lindex $data(Column.Tag) 0] \
              operational [lindex $data(Column.Operational) 0] \
              platform [lindex $data(Column.Platform) 0] \
              displayFile [lindex $data(Column.DisplayPath) 0] \
              displayArgument [lindex $data(Column.DisplayArgument) 0] {
                  if {($tagValue == $tag) && ($operational == 1)} {
                      addEngineeringIOC -x $x -y $y -height 11 -name $name -platform $platform -barwidth 20 -relatedDisplay $displayFile -relatedDisplayArgs $displayArgument
                      incr y 12
                      if {$y > 1010} {
                          incr x 350
                          set newColumn 1
                          set y [AddEngineeringHeader -x $x -y 10]
                      }
                  }
              }
            if {$newColumn == 0} {
                incr y 5
            }
            set newColumn 0
        }
    }
    return $y
}


sdds load /home/helios/oagData/deviceConfig/IOCs.sdds data

#Find limits
if {[catch {APScavget -pend=5 -list=[join [lindex $data(Column.Name) 0] ,] -list=:FD_MAX,:MEM_MAX,:memoryTotal -floatformat=%lf} results]} {
    puts "Error: $results"
    exit
}

foreach name [lindex $data(Column.Name) 0] "fdValue memValue mem2Value" [split $results \n] {
    set fdPV($name) ${name}:FD_FREE
    if {$fdValue == "?"} {
        set fdValue 100
    } elseif {$fdValue == ""} {
        set fdValue 100
        set fdPV($name) ""
    } elseif {$fdValue < 100} {
        set fdValue 100
    }
    set fdmax($name) [expr int($fdValue)]

    set memPV($name) ${name}:MEM_FREE
    if {$memValue == "?"} {
        if {$mem2Value == "?"} {
            set memValue 100
        } elseif {$mem2Value < 100} {
            set memValue 100
            set memPV($name) ${name}:memoryFree
            set fdPV($name) ""
        } else {
            set memValue [expr int($mem2Value)]
            set memPV($name) ${name}:memoryFree
            set fdPV($name) ""
        }
    } elseif {$memValue < 100} {
        set memValue 100
    }
    set memmax($name) [expr int($memValue)]
}

#IOCscreen.adl
set adlOutput ""
addFileObject -name IOCscreen.adl
addDisplayObject -width 840 -height 940
addColorMapObject

set y [AddIOCs -x 10 -y 10 -machine Injector-TS]
set y [AddIOCs -x 10 -y $y -machine LINAC]
set y [AddIOCs -x 10 -y $y -machine LEA]
set y [AddIOCs -x 210 -y $y -machine LTP]
set y [AddIOCs -x 210 -y $y -machine PAR]
set y [AddIOCs -x 210 -y $y -machine PTB]
set y [AddIOCs -x 210 -y $y -machine Booster]
set y [AddIOCs -x 410 -y $y -machine BTS]
set y [AddIOCs -x 410 -y $y -machine Facility]
set y [AddIOCs -x 610 -y $y -machine Beamlines]

addLegend -x 610 -y 10
addShellCommand -x 610 -y 820 -width 185 -height 20 -label "Check IOC Status" -command "checkIOCstatus &"
addRelatedDisplay -x 610 -y 850 -width 185 -height 20 -label "SR IOCs" -display1name SRIOCscreen.adl -display1label "SR IOCs"
addRelatedDisplay -x 610 -y 880 -width 185 -height 20 -label "IOC Engineering" \
  -display1name IOCengineering.adl -display1label "Injector IOCs" \
  -display2name IOCFacilityEngineering.adl -display2label "Facility IOCs" \
  -display3name IOCSR1Engineering.adl -display3label "SR IOCs Screen 1" \
  -display4name IOCSR2Engineering.adl -display4label "SR IOCs Screen 2" \
  -display5name IOCSR3Engineering.adl -display5label "SR IOCs Screen 3"

writeAdlFile -name IOCscreen.adl

#SRIOCscreen.adl
set adlOutput ""
addFileObject -name SRIOCscreen.adl
addDisplayObject -width 1640 -height 1030
addColorMapObject

set y [AddSRIOCs -x 10 -y 10 -category BPM]
set y [AddSRIOCs -x 210 -y $y -category Control]
set y [AddSRIOCs -x 210 -y $y -category DAQ]
set y [AddSRIOCs -x 410 -y $y -category Diagnostic]
set y [AddSRIOCs -x 810 -y $y -category Feedback]
set y [AddSRIOCs -x 810 -y $y -category "Front-End"]
set y [AddSRIOCs -x 810 -y $y -category "ID/SCU"]
set y [AddSRIOCs -x 810 -y $y -category "Injection"]
set y [AddSRIOCs -x 1010 -y $y -category "Interlock"]
set y [AddSRIOCs -x 1010 -y $y -category "Motor"]
set y [AddSRIOCs -x 1010 -y $y -category "PS"]
set y [AddSRIOCs -x 1210 -y $y -category "RF"]
set y [AddSRIOCs -x 1210 -y $y -category "Safety"]
set y [AddSRIOCs -x 1410 -y $y -category "Temperature"]
set y [AddSRIOCs -x 1410 -y $y -category "Timing"]
set y [AddSRIOCs -x 1410 -y $y -category "Vacuum"]

addLegend -x 1410 -y 10
addRelatedDisplay -x 1410 -y 980 -width 165 -height 20 -display1name IOCscreen.adl -display1label "Other IOCs" -label "Other IOCs"
addShellCommand -x 1410 -y 950 -width 165 -height 20 -label "Check IOC Status" -command "checkIOCstatus &"

addShellCommand -x 1410 -y 920 -width 185 -height 20 -label "Check IOC Status" -command "checkIOCstatus &"
addRelatedDisplay -x 1410 -y 950 -width 185 -height 20 -label "Injector/Facility IOCs" -display1name IOCscreen.adl -display1label "Injector/Facility IOCs"
addRelatedDisplay -x 1410 -y 980 -width 185 -height 20 -label "IOC Engineering" \
  -display1name IOCengineering.adl -display1label "Injector IOCs" \
  -display2name IOCFacilityEngineering.adl -display2label "Facility IOCs" \
  -display3name IOCSR1Engineering.adl -display3label "SR IOCs Screen 1" \
  -display4name IOCSR2Engineering.adl -display4label "SR IOCs Screen 2" \
  -display5name IOCSR3Engineering.adl -display5label "SR IOCs Screen 3"

writeAdlFile -name SRIOCscreen.adl

#IOCengineering.adl
set adlOutput ""
addFileObject -name IOCengineering.adl
addDisplayObject -width 1400 -height 950
addColorMapObject

set y [AddEngineeringHeader -x 10 -y 10]
set y [AddEngineeringIOCs -x 10 -y $y -machine Injector-TS]
set y [AddEngineeringIOCs -x 10 -y $y -machine LINAC]
set y [AddEngineeringHeader -x 360 -y 10]
set y [AddEngineeringIOCs -x 360 -y $y -machine LEA]
set y [AddEngineeringIOCs -x 360 -y $y -machine LTP]
set y [AddEngineeringIOCs -x 360 -y $y -machine PAR]
set y [AddEngineeringIOCs -x 360 -y $y -machine PTB]
set y [AddEngineeringHeader -x 710 -y 10]
set y [AddEngineeringIOCs -x 710 -y $y -machine Booster]
set y [AddEngineeringHeader -x 1060 -y 10]
set y [AddEngineeringIOCs -x 1060 -y $y -machine BTS]

writeAdlFile -name IOCengineering.adl

#IOCFacilityEngineering.adl
set adlOutput ""
addFileObject -name IOCFacilityEngineering.adl
addDisplayObject -width 350 -height 850
addColorMapObject

set y [AddEngineeringHeader -x 10 -y 10]
set y [AddEngineeringIOCs -x 10 -y $y -machine Facility]
set y [AddEngineeringIOCs -x 10 -y $y -machine Beamlines]

writeAdlFile -name IOCFacilityEngineering.adl

#IOCSR1Engineering.adl
set adlOutput ""
addFileObject -name IOCSR1Engineering.adl
addDisplayObject -width 1400 -height 1030
addColorMapObject

set y [AddEngineeringHeader -x 10 -y 10]
set y [AddEngineeringIOCs -x 10 -y $y -category BPM -machine SR]
set y [AddEngineeringHeader -x 710 -y 10]
set y [AddEngineeringIOCs -x 710 -y $y -category Control -machine SR]
set y [AddEngineeringIOCs -x 710 -y $y -category DAQ -machine SR]
set y [AddEngineeringIOCs -x 1060 -y $y -category Feedback -machine SR]
set y [AddEngineeringIOCs -x 1060 -y $y -category Front-End -machine SR]
set y [AddEngineeringIOCs -x 1060 -y $y -category Injection -machine SR]
set y [AddEngineeringIOCs -x 1060 -y $y -category Motor -machine SR]
set y [AddEngineeringIOCs -x 1060 -y $y -category Temperature -machine SR]
set y [AddEngineeringIOCs -x 1060 -y $y -category Timing -machine SR]

writeAdlFile -name IOCSR1Engineering.adl

#IOCSR2Engineering.adl
set adlOutput ""
addFileObject -name IOCSR2Engineering.adl
addDisplayObject -width 1400 -height 1030
addColorMapObject

set y [AddEngineeringHeader -x 10 -y 10]
set y [AddEngineeringIOCs -x 10 -y $y -category Diagnostic -machine SR]
set y [AddEngineeringIOCs -x 10 -y $y -category "Diagnostic Motor" -machine SR]
set y [AddEngineeringIOCs -x 360 -y $y -category "Diagnostic Timing" -machine SR]
set y [AddEngineeringIOCs -x 360 -y $y -category "ID/SCU" -machine SR]
set y [AddEngineeringIOCs -x 360 -y $y -category "Interlock" -machine SR]
set y [AddEngineeringIOCs -x 710 -y $y -category "PS" -machine SR]
set y [AddEngineeringHeader -x 1060 -y 10]
set y [AddEngineeringIOCs -x 1060 -y $y -category "PS Injection" -machine SR]
set y [AddEngineeringIOCs -x 1060 -y $y -category "PS Interlock" -machine SR]
set y [AddEngineeringIOCs -x 1060 -y $y -category RF -machine SR]

writeAdlFile -name IOCSR2Engineering.adl

#IOCSR3Engineering.adl
set adlOutput ""
addFileObject -name IOCSR3Engineering.adl
addDisplayObject -width 350 -height 1030
addColorMapObject

set y [AddEngineeringHeader -x 10 -y 10]
set y [AddEngineeringIOCs -x 10 -y $y -category Safety -machine SR]
set y [AddEngineeringIOCs -x 10 -y $y -category Vacuum -machine SR]

writeAdlFile -name IOCSR3Engineering.adl

#PrintCategories
