3b27fff10240fa4658d5ad1f2637fde8c16e00f3
[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 func WorkaroundFirmware(serial_wr chan string) (in chan [][]byte) {
25         in = make(chan [][]byte, 5)
26         go func() {
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])
34                         }
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")
44                         }
45                 }
46         }()
47         return in
48 }
49
50 // ---------- ZMQ Command Handling Code -------------
51
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)
56         }
57         cmd := string(tokens[0])
58         if !(cmd == "open" || cmd == "close" || cmd == "toggle") {
59                 return errors.New(doorctrl_usage)
60         }
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")
64         }
65         if (len(tokens) == 2 || len(tokens[2]) == 0) && method != "Button" {
66                 return errors.New("Operator nickname must be given")
67         }
68         return nil
69 }
70
71 func checkCmdNoArgs(tokens [][]byte) error {
72         if len(tokens) != 1 {
73                 return errors.New("command does not accept arguments")
74         }
75         return nil
76 }
77
78 func HandleCommand(tokens [][]byte, serial_wr chan string, serial_rd chan [][]byte) error {
79         if len(tokens) < 1 {
80                 return errors.New("No Command to handle")
81         }
82
83         dch, present := cmdToDoorCmdHandler[string(tokens[0])]
84         if !present {
85                 return errors.New("Unknown Command")
86         }
87
88         if err := dch.Checker(tokens); err != nil {
89                 return err
90         }
91
92         serial_wr <- dch.FirmwareChar
93         return nil
94 }