namespace eval ccu4n2 {
}

proc stdConfig::ccu4n2 {} {
  variable node
  
  controller syncedprot

  pollperiod 5 5
  obj ccu4flow wr
  prop check ccu4n2::writeCmd
  prop write stdSct::complete
  prop read ccu4n2::readCmd
  prop enum watching,fill,inactive
  prop init 1
  
  kids "LN2 fill" {
    node state upd -text
  }
}

proc ccu4n2::writeCmd {} {
  switch -- [sct target] {
    0 { # watching
      cc nc 3
      cc na 1
    }
    1 { # fill
      cc nc 1
    }
    2 { # inactive
      cc nc 2
    }
  }
  sct update [sct target]
}

proc ccu4n2::readCmd {} {
  set nv [hvali /cc/nv]
  set na [sctval /cc/na]
  set enumtxt "watching,fill,inactive"
  set errtxt ""
  switch -- $nv {
    0 {
      set txt "fill valve off"
      if {$na} {
        sct update 0
      } else {
        sct update 2
      }
    }
    1 {
      set txt "filling"
      sct update 1
    }
    2 {
      set txt "no fill valve"
      sct update 2
      set enumtxt "$txt=2"
    }
    3 {
      sct update 2
      set errtxt timeout
    }
    4 {
      sct update 2
      set errtxt timeout1
    }
    default {
      set errtxt "unknown error"
    }
  }
  sct enum ${enumtxt}
  if {$errtxt eq ""} {
    updateval [sct]/state $txt
    hupdate [sct]/status ""
  } else {
    hupdate [sct]/status $errtxt
    updateval [sct]/state $errtxt
  }
  return idle
}
