proc helium_register {ctrl node {vessel none}} {
  global device_name
  if {[catch {set device_name}]} {
    set device_name [result device name]
  }
  catch {
    if {$vessel eq "none"} {
      set p "prep0"
      set instrument [result instrument]
      set label ${instrument}_$device_name
    } else {
      set p "prep0v"
      set instrument [result instrument]_v
      set label ${instrument}essel
    }
    $ctrl updatescript $node "helium_update $p $instrument $label"
    if {[sicsdescriptor $p] eq "notfound"} {
      stdConfig::make -name $p -driver seaclient -port samenv:8640 -permanent 2
      catch {seaclient_list makeitem $p}
#      stdConfig::make -name $p -driver seaclient -port samenv:8640 -permanent 0
    }
  }
}

proc helium_update {p instrument label value} {
# Usage example: _lev updatescript /lev "helium_update prep0v dmc dmc_vessel"
  catch {
    if {[silent "" sct geterror] eq ""} {
      $p / "helium_instr_update $instrument $label $value"
    } else {
      $p / "helium_instr_update $instrument $label none"
    }
  }
}

proc helium_instr_update {instrument label value} {
  # this script is used on seaman
  global helium_last

  set now [DoubleTime]
  if {[helium_list exists $instrument]} {
    if {$value eq "none"} {
      helium_list deleteitem $instrument
      catch {helium_ts deleteitem $instrument}
      GraphItem shown helium_list.$instrument 0
    } else {
      if {[silent "" hdbgetpropval /helium_list/$instrument logger_name] eq ""} {
        logsetup /helium_list/$instrument
      }
      helium_list $instrument $value
      helium_ts $instrument $now
    }
  } elseif {$value ne "none"} {
    helium_list makeitem $instrument $value
    GraphAdd helium_list.$instrument %_1 $label auto
    logsetup /helium_list/$instrument
    helium_ts makeitem $instrument $now
  }
  if {$now > [silent 0 set helium_last] + 60} {
    set helium_last $now
    # check for items to be removed
    foreach ins [helium_list items] {
      set last [silent $now result helium_ts $ins]
      if {$now > $last + 600} {
        catch {helium_list deleteitem $ins}
        catch {helium_ts deleteitem $ins}
        GraphItem shown helium_list.$ins 0
      }
    }
  }
}

publishLazy helium_instr_update
