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{ checkCmdDoorControl, "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 checkCmdStatus(tokens [][]byte) (error) {
44 return errors.New("status command does not accept arguments")
49 func HandleCommand(tokens [][]byte, topub chan <- [][]byte, serial_wr chan string) ([][]byte, error){
51 return nil, errors.New("No Command to handle")
54 dch, present := cmdToDoorCmdHandler[string(tokens[0])]
56 return nil, errors.New("Unknown Command")
59 if err := dch.Checker(tokens); err != nil {
60 //return error to sender
65 serial_wr <- dch.FirmwareChar
66 fw_reply := GetLastSerialLine()