firmware/user workaround: send open if user opens door after timeout_after_open
authorBernhard Tittelbach <xro@realraum.at>
Mon, 26 Jan 2015 23:25:14 +0000 (23:25 +0000)
committerBernhard Tittelbach <xro@realraum.at>
Mon, 26 Jan 2015 23:25:14 +0000 (23:25 +0000)
go/door_daemon_zmq/handle_commands.go
go/door_daemon_zmq/main.go

index e8c3334..a099196 100644 (file)
@@ -3,63 +3,92 @@
 package main
 
 import (
-    "errors"
+       "errors"
+       "time"
 )
 
 type DoorCmdHandler struct {
-    Checker func([][]byte)(error)
-    FirmwareChar string
+       Checker      func([][]byte) error
+       FirmwareChar string
 }
 
-var cmdToDoorCmdHandler = map[string]DoorCmdHandler {
-  "open": DoorCmdHandler{ checkCmdDoorControl, "o"},
-  "close": DoorCmdHandler{ checkCmdDoorControl, "c"},
-  "toggle": DoorCmdHandler{ checkCmdDoorControl, "t"},
-  "status": DoorCmdHandler{ checkCmdNoArgs, "s"},
+var cmdToDoorCmdHandler = map[string]DoorCmdHandler{
+       "open":   DoorCmdHandler{checkCmdDoorControl, "o"},
+       "close":  DoorCmdHandler{checkCmdDoorControl, "c"},
+       "toggle": DoorCmdHandler{checkCmdDoorControl, "t"},
+       "status": DoorCmdHandler{checkCmdNoArgs, "s"},
 }
 
-// ---------- Command Handling Code -------------
+// ---------- Talk with Firmware directly in response to stuff it sends ------------
 
-func checkCmdDoorControl(tokens [][]byte) (error) {
-    doorctrl_usage := "syntax: <open|close|toggle> <method> <nickname>"
-    if len(tokens) < 2  || len(tokens) > 3 {
-        return errors.New(doorctrl_usage)
-    }
-    cmd := string(tokens[0])
-    if ! (cmd == "open" || cmd == "close" || cmd == "toggle") {
-        return errors.New(doorctrl_usage)
-    }
-    method := string(tokens[1])
-    if ! (method == "Button" || method == "ssh" || method == "SSH" || method == "Phone") {
-        return errors.New("method must be one either Button, SSH or Phone")
-    }
-    if (len(tokens) == 2 || len(tokens[2]) == 0) && method != "Button" {
-        return errors.New("Operator nickname must be given")
-    }
-    return nil
+func WorkaroundFirmware(serial_wr chan string) (in chan [][]byte) {
+       in = make(chan [][]byte, 1)
+       go func() {
+               var last_state_time time.Time
+               var last_door_state string
+               for firmware_output := range in {
+                       Debug_.Printf("WorkaroundFirmware Input: %s", firmware_output)
+                       if len(firmware_output) > 1 && string(firmware_output[0]) == "State:" {
+                               last_state_time = time.Now()
+                               last_door_state = string(firmware_output[1])
+                       }
+                       if len(firmware_output) == 5 &&
+                               string(firmware_output[0]) == "Info(ajar):" &&
+                               string(firmware_output[4]) == "ajar" &&
+                               time.Now().Sub(last_state_time) < 30*time.Second &&
+                               last_door_state == "timeout_after_open" {
+                               //If we were in state "timeout_after_open" and within 30s the door was openend anyway,
+                               //we send another "open" command
+                               serial_wr <- cmdToDoorCmdHandler["open"].FirmwareChar
+                               Syslog_.Print("Telling Firmware to open, since door was ajar'ed after timeout_after_open")
+                       }
+               }
+       }()
+       return in
 }
 
-func checkCmdNoArgs(tokens [][]byte) (error) {
-    if len(tokens) != 1 {
-        return errors.New("command does not accept arguments")
-    }
-    return nil
+// ---------- ZMQ Command Handling Code -------------
+
+func checkCmdDoorControl(tokens [][]byte) error {
+       doorctrl_usage := "syntax: <open|close|toggle> <method> <nickname>"
+       if len(tokens) < 2 || len(tokens) > 3 {
+               return errors.New(doorctrl_usage)
+       }
+       cmd := string(tokens[0])
+       if !(cmd == "open" || cmd == "close" || cmd == "toggle") {
+               return errors.New(doorctrl_usage)
+       }
+       method := string(tokens[1])
+       if !(method == "Button" || method == "ssh" || method == "SSH" || method == "Phone") {
+               return errors.New("method must be one either Button, SSH or Phone")
+       }
+       if (len(tokens) == 2 || len(tokens[2]) == 0) && method != "Button" {
+               return errors.New("Operator nickname must be given")
+       }
+       return nil
+}
+
+func checkCmdNoArgs(tokens [][]byte) error {
+       if len(tokens) != 1 {
+               return errors.New("command does not accept arguments")
+       }
+       return nil
 }
 
 func HandleCommand(tokens [][]byte, serial_wr chan string, serial_rd chan [][]byte) error {
-    if len(tokens) < 1 {
-        return errors.New("No Command to handle")
-    }
+       if len(tokens) < 1 {
+               return errors.New("No Command to handle")
+       }
 
-    dch, present := cmdToDoorCmdHandler[string(tokens[0])]
-    if ! present {
-        return errors.New("Unknown Command")
-    }
+       dch, present := cmdToDoorCmdHandler[string(tokens[0])]
+       if !present {
+               return errors.New("Unknown Command")
+       }
 
-    if err := dch.Checker(tokens); err != nil {
-        return err
-    }
+       if err := dch.Checker(tokens); err != nil {
+               return err
+       }
 
-    serial_wr <- dch.FirmwareChar
-    return nil
+       serial_wr <- dch.FirmwareChar
+       return nil
 }
index 84db848..c942219 100644 (file)
 package main
 
 import (
-    "os"
-    "flag"
-    "time"
+       "flag"
+       "os"
+       "time"
 )
 
 //~ func StringArrayToByteArray(ss []string) [][]byte {
-    //~ bb := make([][]byte, len(ss))
-    //~ for index, s := range(ss) {
-        //~ bb[index] = []byte(s)
-    //~ }
-    //~ return bb
+//~ bb := make([][]byte, len(ss))
+//~ for index, s := range(ss) {
+//~ bb[index] = []byte(s)
+//~ }
+//~ return bb
 //~ }
 
 // ---------- Main Code -------------
 
 var (
-    cmd_port_ string
-    pub_port_ string
-    door_tty_path_ string
-    enable_syslog_ bool
-    enable_debug_ bool
+       cmd_port_      string
+       pub_port_      string
+       door_tty_path_ string
+       enable_syslog_ bool
+       enable_debug_  bool
 )
 
 func init() {
-    flag.StringVar(&cmd_port_, "cmdport", "ipc:///run/tuer/door_cmd.ipc", "zmq command socket path")
-    flag.StringVar(&pub_port_, "pubport", "tcp://*:4242", "zmq public/listen socket path")
-    flag.StringVar(&door_tty_path_, "device", "/dev/door", "door tty device path")
-    flag.BoolVar(&enable_syslog_, "syslog", false, "enable logging to syslog")
-    flag.BoolVar(&enable_debug_, "debug", false, "enable debug output")
-    flag.Parse()
+       flag.StringVar(&cmd_port_, "cmdport", "ipc:///run/tuer/door_cmd.ipc", "zmq command socket path")
+       flag.StringVar(&pub_port_, "pubport", "tcp://*:4242", "zmq public/listen socket path")
+       flag.StringVar(&door_tty_path_, "device", "/dev/door", "door tty device path")
+       flag.BoolVar(&enable_syslog_, "syslog", false, "enable logging to syslog")
+       flag.BoolVar(&enable_debug_, "debug", false, "enable debug output")
+       flag.Parse()
 }
 
 func main() {
-    if enable_syslog_ { LogEnableSyslog()}
-    if enable_debug_ { LogEnableDebuglog()}
-    Syslog_.Print("started")
-    defer Syslog_.Print("exiting")
+       if enable_syslog_ {
+               LogEnableSyslog()
+       }
+       if enable_debug_ {
+               LogEnableDebuglog()
+       }
+       Syslog_.Print("started")
+       defer Syslog_.Print("exiting")
+
+       zmqctx, cmd_chans, pub_chans := ZmqsInit(cmd_port_, pub_port_)
+       defer cmd_chans.Close()
+       defer pub_chans.Close()
+       defer zmqctx.Close()
 
-    zmqctx, cmd_chans, pub_chans := ZmqsInit(cmd_port_, pub_port_)
-    defer cmd_chans.Close()
-    defer pub_chans.Close()
-    defer zmqctx.Close()
+       serial_wr, serial_rd, err := OpenAndHandleSerial(door_tty_path_, 0)
+       defer close(serial_wr)
+       if err != nil {
+               panic(err)
+       }
 
-    serial_wr, serial_rd, err := OpenAndHandleSerial(door_tty_path_, 0)
-    defer close(serial_wr)
-    if err != nil {
-        panic(err)
-    }
+       workaround_in_chan := WorkaroundFirmware(serial_wr)
+       defer close(workaround_in_chan)
 
-    var next_incoming_serial_is_client_reply bool
-    timeout_chan := make(chan bool)
-    defer close(timeout_chan)
-    for {
-        select {
-            case incoming_ser_line, is_notclosed := <- serial_rd:
-                if is_notclosed {
-                    //~ if Syslog_ != nil { Syslog_.Print(ByteArrayToString(incoming_ser_line)) }
-                    if Syslog_ != nil { Syslog_.Printf("%s",incoming_ser_line) }
-                    if next_incoming_serial_is_client_reply {
-                        next_incoming_serial_is_client_reply = false
-                        cmd_chans.Out() <- incoming_ser_line
-                    }
-                    pub_chans.Out() <- incoming_ser_line
-                } else {
-                    Syslog_.Print("serial device disappeared, exiting")
-                    os.Exit(1)
-                }
-            case tv, timeout_notclosed := <- timeout_chan:
-                if timeout_notclosed && tv && next_incoming_serial_is_client_reply {
-                        next_incoming_serial_is_client_reply = false
-                        cmd_chans.Out() <- [][]byte{[]byte("ERROR"), []byte("No reply from firmware")}
-                }
-            case incoming_request, ic_notclosed := <- cmd_chans.In():
-                if ! ic_notclosed {
-                    Syslog_.Print("zmq socket died, exiting")
-                    os.Exit(2)
-                }
-                if string(incoming_request[0]) == "log" {
-                    if len(incoming_request) < 2 {
-                        cmd_chans.Out() <- [][]byte{[]byte("ERROR"), []byte("argument missing")}
-                        continue
-                    }
-                    Syslog_.Printf("Log: %s",incoming_request[1:])
-                    cmd_chans.Out() <- [][]byte{[]byte("Ok")}
-                    continue
-                }
-                Syslog_.Printf("%s",incoming_request)
-                 if err := HandleCommand(incoming_request, serial_wr, serial_rd); err != nil {
-                    out_msg := [][]byte{[]byte("ERROR"), []byte(err.Error())}
-                    cmd_chans.Out() <- out_msg
-                 } else {
-                    pub_chans.Out() <- incoming_request
-                    next_incoming_serial_is_client_reply = true
-                    go func(){time.Sleep(3*time.Second); timeout_chan <- true;}()
-                 }
-        }
-    }
+       var next_incoming_serial_is_client_reply bool
+       timeout_chan := make(chan bool)
+       defer close(timeout_chan)
+       for {
+               select {
+               case incoming_ser_line, is_notclosed := <-serial_rd:
+                       if is_notclosed {
+                               //~ if Syslog_ != nil { Syslog_.Print(ByteArrayToString(incoming_ser_line)) }
+                               if Syslog_ != nil {
+                                       Syslog_.Printf("%s", incoming_ser_line)
+                               }
+                               if next_incoming_serial_is_client_reply {
+                                       next_incoming_serial_is_client_reply = false
+                                       cmd_chans.Out() <- incoming_ser_line
+                               }
+                               pub_chans.Out() <- incoming_ser_line
+                               workaround_in_chan <- incoming_ser_line
+                       } else {
+                               Syslog_.Print("serial device disappeared, exiting")
+                               os.Exit(1)
+                       }
+               case tv, timeout_notclosed := <-timeout_chan:
+                       if timeout_notclosed && tv && next_incoming_serial_is_client_reply {
+                               next_incoming_serial_is_client_reply = false
+                               cmd_chans.Out() <- [][]byte{[]byte("ERROR"), []byte("No reply from firmware")}
+                       }
+               case incoming_request, ic_notclosed := <-cmd_chans.In():
+                       if !ic_notclosed {
+                               Syslog_.Print("zmq socket died, exiting")
+                               os.Exit(2)
+                       }
+                       if string(incoming_request[0]) == "log" {
+                               if len(incoming_request) < 2 {
+                                       cmd_chans.Out() <- [][]byte{[]byte("ERROR"), []byte("argument missing")}
+                                       continue
+                               }
+                               Syslog_.Printf("Log: %s", incoming_request[1:])
+                               cmd_chans.Out() <- [][]byte{[]byte("Ok")}
+                               continue
+                       }
+                       Syslog_.Printf("%s", incoming_request)
+                       if err := HandleCommand(incoming_request, serial_wr, serial_rd); err != nil {
+                               out_msg := [][]byte{[]byte("ERROR"), []byte(err.Error())}
+                               cmd_chans.Out() <- out_msg
+                       } else {
+                               pub_chans.Out() <- incoming_request
+                               next_incoming_serial_is_client_reply = true
+                               go func() { time.Sleep(3 * time.Second); timeout_chan <- true }()
+                       }
+               }
+       }
 }