1eb72e7beeb143cba50fb239bb24d27c199edbbc
[svn42.git] / go / door_daemon_zmq / handle_commands.go
1 // (c) Bernhard Tittelbach, 2013
2
3 package main
4
5 import (
6         "errors"
7         "time"
8 )
9
10 type DoorCmdHandler struct {
11         Checker      func([][]byte) error
12         FirmwareChar string
13 }
14
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"},
20 }
21
22 // ---------- Talk with Firmware directly in response to stuff it sends ------------
23
24 // The problem is this:
25 // If the key/motor turns just far enough so that the door is unlocked,
26 // but still get's blocked on the way (because the door clamped)
27 // the firmware will enter state "timeout_after_open" instead of "open"
28 // In this state, manual moving the key/cogwheel will not trigger the state "manual_movement"
29 // Users however will not notice that the door is in an error state and needs to manually be moved to the real open position,
30 // they have after all, already successfully entered the room.
31 // Without that information however, the r3-event-broker daemon, cannot tell if the door is being locked manuall from the inside
32 // or if someone else is locking the door for other reasons.
33 // Thus, if the door has been opened imperfectly and is then closed manually, it will trigger a presence=false event and switch off the lights.
34 //
35 // As Workaround, the door daemon watches for "timeout_after_open" events.
36 // If one is detected and followed by an "door is now ajar" info, we tell the firmware
37 // to open the door, causing it to move out of the error state and into the final open key/cogwheel position.
38 func WorkaroundFirmware(serial_wr chan string) (in chan [][]byte) {
39         in = make(chan [][]byte, 5)
40         go func() {
41                 var last_state_time time.Time
42                 var last_door_state string
43                 for firmware_output := range in {
44                         Debug_.Printf("WorkaroundFirmware Input: %s", firmware_output)
45                         if len(firmware_output) > 1 && string(firmware_output[0]) == "State:" {
46                                 last_state_time = time.Now()
47                                 last_door_state = string(firmware_output[1])
48                         }
49                         if len(firmware_output) == 5 &&
50                                 string(firmware_output[0]) == "Info(ajar):" &&
51                                 string(firmware_output[4]) == "ajar" &&
52                                 time.Now().Sub(last_state_time) < 30*time.Second &&
53                                 last_door_state == "timeout_after_open" {
54                                 //If we were in state "timeout_after_open" and within 30s the door was openend anyway,
55                                 //we send another "open" command
56                                 serial_wr <- cmdToDoorCmdHandler["open"].FirmwareChar
57                                 Syslog_.Print("Telling Firmware to open, since door was ajar'ed after timeout_after_open")
58                         }
59                 }
60         }()
61         return in
62 }
63
64 // ---------- ZMQ Command Handling Code -------------
65
66 func checkCmdDoorControl(tokens [][]byte) error {
67         doorctrl_usage := "syntax: <open|close|toggle> <method> <nickname>"
68         if len(tokens) < 2 || len(tokens) > 3 {
69                 return errors.New(doorctrl_usage)
70         }
71         cmd := string(tokens[0])
72         if !(cmd == "open" || cmd == "close" || cmd == "toggle") {
73                 return errors.New(doorctrl_usage)
74         }
75         method := string(tokens[1])
76         if !(method == "Button" || method == "ssh" || method == "SSH" || method == "Phone") {
77                 return errors.New("method must be one either Button, SSH or Phone")
78         }
79         if (len(tokens) == 2 || len(tokens[2]) == 0) && method != "Button" {
80                 return errors.New("Operator nickname must be given")
81         }
82         return nil
83 }
84
85 func checkCmdNoArgs(tokens [][]byte) error {
86         if len(tokens) != 1 {
87                 return errors.New("command does not accept arguments")
88         }
89         return nil
90 }
91
92 func HandleCommand(tokens [][]byte, serial_wr chan string, serial_rd chan [][]byte) error {
93         if len(tokens) < 1 {
94                 return errors.New("No Command to handle")
95         }
96
97         dch, present := cmdToDoorCmdHandler[string(tokens[0])]
98         if !present {
99                 return errors.New("Unknown Command")
100         }
101
102         if err := dch.Checker(tokens); err != nil {
103                 return err
104         }
105
106         serial_wr <- dch.FirmwareChar
107         return nil
108 }