1 // (c) Bernhard Tittelbach, 2013
9 type DoorCmdHandler struct {
10 Checker func([][]byte)(error)
14 var cmdToDoorCmdHandler = map[string]DoorCmdHandler {
15 "open": DoorCmdHandler{ checkCmdDoorControl, "o"},
16 "close": DoorCmdHandler{ checkCmdDoorControl, "c"},
17 "toggle": DoorCmdHandler{ checkCmdDoorControl, "t"},
18 "status": DoorCmdHandler{ checkCmdNoArgs, "s"},
21 // ---------- Command Handling Code -------------
23 func checkCmdDoorControl(tokens [][]byte) (error) {
24 doorctrl_usage := "syntax: <open|close|toggle> <method> <nickname>"
26 return errors.New(doorctrl_usage)
28 cmd := string(tokens[0])
29 if ! (cmd == "open" || cmd == "close" || cmd == "toggle") {
30 return errors.New(doorctrl_usage)
32 method := string(tokens[1])
33 if ! (method == "Button" || method == "ssh" || method == "SSH" || method == "Phone") {
34 return errors.New("method must be one either Button, SSH or Phone")
36 if len(tokens[2]) == 0 && method != "Button" {
37 return errors.New("Operator nickname must be given")
42 func checkCmdNoArgs(tokens [][]byte) (error) {
44 return errors.New("command does not accept arguments")
49 func HandleCommand(tokens [][]byte, serial_wr chan string, serial_rd chan [][]byte) error {
51 return errors.New("No Command to handle")
54 dch, present := cmdToDoorCmdHandler[string(tokens[0])]
56 return errors.New("Unknown Command")
59 if err := dch.Checker(tokens); err != nil {
63 serial_wr <- dch.FirmwareChar