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 // 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.
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)
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])
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")
64 // ---------- ZMQ Command Handling Code -------------
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)
71 cmd := string(tokens[0])
72 if !(cmd == "open" || cmd == "close" || cmd == "toggle") {
73 return errors.New(doorctrl_usage)
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")
79 if (len(tokens) == 2 || len(tokens[2]) == 0) && method != "Button" {
80 return errors.New("Operator nickname must be given")
85 func checkCmdNoArgs(tokens [][]byte) error {
87 return errors.New("command does not accept arguments")
92 func HandleCommand(tokens [][]byte, serial_wr chan string, serial_rd chan [][]byte) error {
94 return errors.New("No Command to handle")
97 dch, present := cmdToDoorCmdHandler[string(tokens[0])]
99 return errors.New("Unknown Command")
102 if err := dch.Checker(tokens); err != nil {
106 serial_wr <- dch.FirmwareChar