namespace eval moxa {
}

proc stdConfig::moxa {} {
  variable ctrl
  variable node

  if {[controller tmo 10 2]} {
    controllerDesc "MOXA http connection"
  }
  
  obj moxa out
  default 0
  prop check moxa::check
  prop write moxa::getline
  prop connections ""
  
  $ctrl queue $node write moxa::getline
  
}

proc moxa::getline {} {
  sct send "GET /Mn_line.htm HTTP/1.1\r\n"
  return moxa::updateline
}

proc moxa::updateline {} {
  set r [sct result]
  set l 0
  set pattern {<TR><TD>(.*?)</TD><TD>TCP Server Mode</TD><TD>(.*?)</TD>.*?</TR>}
  array set iplist {}
  while {[regexp -start $l -indices ($pattern) $r idx]} {
    set l [lindex $idx 1]
    set line [string range $r [lindex $idx 0] $l]
    regexp $pattern $line all port ip
    if {$ip ne "Listen"} {
      lappend iplist($ip) [expr 3000+$port]
    }
  }
  sct connections [array get iplist]
  sct update 0
  clientput "connections: [sct connections]"
#    seaclient hostport ${ip}:${po}
#    seaclient cmd "disconnect_from [sct sicsdev]"
  return idle
}

proc moxa::check {} {
  sct connections ""
  sct update [sct target]
  [sct controllerName] reconnect
}

