# BF dilution pressure readings pc9193 port 1234
namespace eval bfgh {} {
}

proc stdConfig::bfgh {} {
  controller std "\n" 5
  prop startcmd "names"
   
  obj BlueForsPressureLog -none
  kids "BF Gashandling Sensors" {
    node pivc rd
    prop readcmd "mgstatus 1"
    prop readfmt "S05: %g"
    
    node pstill rd
    prop readcmd "mgstatus 2"
    prop readfmt "S05: %g"
    
    node pcond rd
    prop readcmd "mgstatus 3"
    prop readfmt "S05: %g"
    
    node phep rd
    prop readcmd "mgstatus 4"
    prop readfmt "S05: %g"
    
    node pdump rd
    prop readcmd "mgstatus 5"
    prop readfmt "S05: %g"

    node paux rd
    prop readcmd "mgstatus 6"
    prop readfmt "S05: %g"
    
    node flow rd
    prop readcmd "fmstatus"
    prop readfmt "S09: %g"
   
    node v11 wr
    prop help "Dump PumpOUT"
    prop write bfgh::setrmt
    prop enum 1
    prop read bfgh::chanread

    foreach {node help} {
      v2 "Gate Bypass"
      v1 "Gate Valve"
      turbo1 "Still Turbo" 
      v12 "Dump slow PumpOUT" 
      v3  "Still-Condenser Short"
      v10 "before He-3 Scroll"
      turbo2 "Aux. Turbo"
      v14 "common Vacuum 1"
      v4 "Compressor Bypass"
      v13 "Dump return" 
      compressor "condensation Compressor" 
      v15 " !!!Still-Aux Short!!!" 
      v5 "Compressor out" 
      hs-still "Heat switch 4K-Still"
      v21 "Aux Turbo bypass" 
      v16  "common vacuum 2"
      v6 "Compressor in" 
      scroll1 "He-3 Scroll" 
      v22 "Aux Turbo in" 
      v17 " !!!Condeser-Aux short!!!"
      v7 "Ln2 Trap out"
      scroll2 "Aux Scroll"
      v23 "Aux Turbo out" 
      v18 " !!!Still-Aux short2!!!" 
      v8 "LN2 Trap bypass" 
      pulsetube "Pulse Tube" 
      v19 "Venting" 
      v20 "aux (Venting 2)" 
      v9 "LN2 Trap in" 
      hs-mc "Heat Switch Still-MC"
      ext "Still Heater"
    } {
      node $node out
      prop help $help 
      prop write bfgh::setrmt
      prop enum 1
    }     
  }
  node stillpower par 0.008
  prop help "when ext is on, stillpower is applied to /tbf/set/manualpower. set stillpower to 0 for no link between /bfgh/ext and /tbf/set"
  
  variable ctrl
  variable path
  $ctrl updatescript $path/ext bfgh::ext_update
}

proc bfgh::ext_update {value} {
  set mp [hval [sct parent]/stillpower]
  if {$mp} {
    set old [hval /tbf/set/mode]
    set oldp [hval /tbf/set/manualpower]
    if {$value} {
      set new 2
      set newp $mp
    } else {
      set new 0
      set newp 0
    }
    if {$new != $old} {
      hset /tbf/set/mode $new
    }
    if {$newp != $oldp} {
      hset /tbf/set/manualpower $newp
    }
  }
}

proc bfgh::chanread {} {
  sct send "status"
  return bfgh::chanupd 
}

proc bfgh::chanupd {} {
  set response [string range [sct result] 5 end]
  foreach chan [split $response ","] {
    set c [split $chan "="]
    hupdate [sct parent]/[lindex $c 0] [lindex $c 1]
  }
  hdelprop [sct] geterror
  return idle
}

proc  bfgh::setrmt {} {
  sct send "control 1"
  return bfgh::takectrl
}

proc  bfgh::takectrl {} {
  sct send "remote 1"
  return bfgh::writechan
}

proc bfgh::writechan {} {
  set node [lindex [split [sct] "/"] end]
  if {$node == "v15"|| $node == "v17"|| $node == "v18"} {
    hdelprop [sct parent]/$node requested
    clientput "ERROR: you are not allowed to change these values"
    # this are special valves they need confirmation 
  } else {
    if {[sct target]} {
      sct send "on $node"
    } else {
      sct send "off $node"
    }
  } 
  return bfgh::rtnctrl
}


proc bfgh::rtnctrl {} {
  sct send "remote 0"
  return bfgh::rtnrmt
}


proc bfgh::rtnrmt {} {
  sct send "control 0"
  return bfgh::chanread
}
