1 // (c) Bernhard Tittelbach, 2013
10 type DoorCmdHandler struct {
11 Checker func([][]byte) error
15 var cmdToDoorCmdHandler = map[string]DoorCmdHandler{
16 "open": DoorCmdHandler{checkCmdDoorControl, "o"},
17 "close": DoorCmdHandler{checkCmdDoorControl, "c"},
18 "toggle": DoorCmdHandler{checkCmdDoorControl, "t"},
19 "status": DoorCmdHandler{checkCmdNoArgs, "s"},
22 // ---------- Talk with Firmware directly in response to stuff it sends ------------
24 func WorkaroundFirmware(serial_wr chan string) (in chan [][]byte) {
25 in = make(chan [][]byte, 5)
27 var last_state_time time.Time
28 var last_door_state string
29 for firmware_output := range in {
30 Debug_.Printf("WorkaroundFirmware Input: %s", firmware_output)
31 if len(firmware_output) > 1 && string(firmware_output[0]) == "State:" {
32 last_state_time = time.Now()
33 last_door_state = string(firmware_output[1])
35 if len(firmware_output) == 5 &&
36 string(firmware_output[0]) == "Info(ajar):" &&
37 string(firmware_output[4]) == "ajar" &&
38 time.Now().Sub(last_state_time) < 30*time.Second &&
39 last_door_state == "timeout_after_open" {
40 //If we were in state "timeout_after_open" and within 30s the door was openend anyway,
41 //we send another "open" command
42 serial_wr <- cmdToDoorCmdHandler["open"].FirmwareChar
43 Syslog_.Print("Telling Firmware to open, since door was ajar'ed after timeout_after_open")
50 // ---------- ZMQ Command Handling Code -------------
52 func checkCmdDoorControl(tokens [][]byte) error {
53 doorctrl_usage := "syntax: <open|close|toggle> <method> <nickname>"
54 if len(tokens) < 2 || len(tokens) > 3 {
55 return errors.New(doorctrl_usage)
57 cmd := string(tokens[0])
58 if !(cmd == "open" || cmd == "close" || cmd == "toggle") {
59 return errors.New(doorctrl_usage)
61 method := string(tokens[1])
62 if !(method == "Button" || method == "ssh" || method == "SSH" || method == "Phone") {
63 return errors.New("method must be one either Button, SSH or Phone")
65 if (len(tokens) == 2 || len(tokens[2]) == 0) && method != "Button" {
66 return errors.New("Operator nickname must be given")
71 func checkCmdNoArgs(tokens [][]byte) error {
73 return errors.New("command does not accept arguments")
78 func HandleCommand(tokens [][]byte, serial_wr chan string, serial_rd chan [][]byte) error {
80 return errors.New("No Command to handle")
83 dch, present := cmdToDoorCmdHandler[string(tokens[0])]
85 return errors.New("Unknown Command")
88 if err := dch.Checker(tokens); err != nil {
92 serial_wr <- dch.FirmwareChar