-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommand_mode.go
138 lines (126 loc) · 3.55 KB
/
command_mode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
// This file supports the "-command" functions
// There's not really any sanity checks here. I'm just gonna let
// the GO runtime panic() if the user doesn't provide enough parameters
import (
"fmt"
"os"
"sort"
"strconv"
)
func dump_buttons(buttons []Button) string {
sort.Slice(buttons, func(i, j int) bool {
return buttons[i].ButtonID < buttons[j].ButtonID
})
var res string
var state string
for _, b := range buttons {
state = "OFF"
if b.State == 1 {
state = "ON "
}
res += fmt.Sprintf("%6d %s %s\n", b.ButtonID, state, b.Name)
}
return res
}
func command_mode(cmd string, args []string) string {
switch cmd {
// List the buttons defined
case "getbuttons":
return dump_buttons(get_buttons(false))
// Define buttons
case "setname":
i, _ := strconv.Atoi(args[0])
set_button_name(i, args[1])
return get_button_name(i)
case "setstate":
i, _ := strconv.Atoi(args[0])
j, _ := strconv.Atoi(args[1])
set_button_state(i, j)
return get_button_name(i)
case "setstatebyname":
button := get_button_by_name(args[0])
i := button.ButtonID
j, _ := strconv.Atoi(args[1])
return set_button_state(i, j)
case "togglestate":
i, _ := strconv.Atoi(args[0])
toggle_button_state(i)
return get_button_name(i)
case "togglestatebyname":
button := get_button_by_name(args[0])
i := button.ButtonID
toggle_button_state(i)
return get_button_name(i)
case "pushcontact":
i, _ := strconv.Atoi(args[0])
push_update(i, 1, false)
return set_button_state(i, 0)
case "pushcontactbyname":
// This will push an update to the API to make Alexa
// think the contact is open, then set the DynamoDB
// value to 0 (closed), which itself pushes another update
// This can be used to trigger a routine on "open".
button := get_button_by_name(args[0])
i := button.ButtonID
push_update(i, 1, false)
return set_button_state(i, 0)
case "deletebutton":
i, _ := strconv.Atoi(args[0])
delete_button(i)
return get_button_name(i)
// Manage the web password
case "setpasswd":
set_button_name(DB_TOKEN_PSWD, args[0])
return get_button_name(DB_TOKEN_PSWD)
case "getpasswd":
return get_button_name(DB_TOKEN_PSWD)
// Initial setup for the Auth privileges
case "setclientid":
set_button_name(DB_TOKEN_ALEXA_ID, args[0])
return get_button_name(DB_TOKEN_ALEXA_ID)
case "setclientsecret":
set_button_name(DB_TOKEN_ALEXA_SECRET, args[0])
return get_button_name(DB_TOKEN_ALEXA_SECRET)
// Allow an alternate endpoint to be defined
case "setendpoint":
set_button_name(DB_API_ENDPOINT, args[0])
return get_button_name(DB_API_ENDPOINT)
// Verify they're set correctly
case "getclientid":
return get_button_name(DB_TOKEN_ALEXA_ID)
case "getclientsecret":
return get_button_name(DB_TOKEN_ALEXA_SECRET)
// Debugging stuff you probably don't want to call
case "getbuttonsall":
return dump_buttons(get_buttons(true))
case "gettoken":
return get_button_name(DB_TOKEN_AUTH)
case "settoken":
set_button_name(DB_TOKEN_AUTH, args[0])
return get_button_name(DB_TOKEN_AUTH)
case "refreshtoken":
return refresh_token(false)
case "forcerefreshtoken":
return refresh_token(true)
case "discovery":
return discovery_response()
case "statereport":
return state_response("xxx", args[0])
case "pushupdate":
i, _ := strconv.Atoi(args[0])
j, _ := strconv.Atoi(args[1])
return push_update(i, j, false)
case "lambda":
data := os.Getenv("TEST")
if data == "" {
fmt.Println("Set TEST variable")
os.Exit(255)
}
rest := process_lambda([]byte(data))
return string(rest)
default:
return "Unknown command: " + cmd
}
return ""
}