-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
267 lines (218 loc) · 6.66 KB
/
api.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package main
import (
"encoding/json"
"encoding/hex"
"net/http"
"github.com/gorilla/mux"
"github.com/vikstrous/zengge-lightcontrol/control"
"github.com/vikstrous/zengge-lightcontrol/remote"
_ "github.com/lib/pq"
"database/sql"
"fmt"
)
var rc *remote.Controller
var controller *control.Controller
var db *sql.DB
type ErrReponse struct {
Err string `json:"err"`
}
type SuccessResponse struct {
OK bool `json:"OK"`
}
type StatusResponse struct {
IsLive bool `json:"is_live"`
Light1 control.State `json:"light1"`
Light2 control.State `json:"light2"`
Light3 control.State `json:"light3"`
Light4 control.State `json:"light4"`
}
func _checkBulbState () string {
state, err := controller.GetState()
ctrl_err := ""
if err != nil {
ctrl_err = err.Error()
} else if !state.IsOn {
ctrl_err = "Bulb is not on"
}
return ctrl_err
}
func RemoteLoginCommand(w http.ResponseWriter, req *http.Request) {
_remoteLogin()
}
func _remoteLogin() {
rc = remote.NewController("http://wifi.magichue.net/WebMagicHome/ZenggeCloud/ZJ002.ashx", "8ff3e30e071c9ef5b304d83239d0c707", config_vars.DevID)
rc.Login()
}
func GetDevicesCommand(w http.ResponseWriter, req *http.Request) {
devices, err := rc.GetDevices()
if err != nil {
json.NewEncoder(w).Encode(err)
return
}
json.NewEncoder(w).Encode(devices)
}
func RegisterDeviceCommand(w http.ResponseWriter, req *http.Request) {
params := mux.Vars(req)
if params["mac"] == "" {
err := "Please provide a mac address"
json.NewEncoder(w).Encode(ErrReponse{Err: err})
return
}
rc.RegisterDevice(params["mac"])
json.NewEncoder(w).Encode(SuccessResponse{OK: true})
}
func DeregisterDeviceCommand(w http.ResponseWriter, req *http.Request) {
params := mux.Vars(req)
if params["mac"] == "" {
err := "Please provide a mac address"
json.NewEncoder(w).Encode(ErrReponse{Err: err})
return
}
rc.DeregisterDevice(params["mac"])
json.NewEncoder(w).Encode(SuccessResponse{OK: true})
}
func ChangeColorByMacCommand(w http.ResponseWriter, req *http.Request) {
params := mux.Vars(req)
err := ""
if params["mac"] == "" {
err = "Please provide a mac address"
}
if params["color"] == "" {
err = "Please provide a color address"
}
if err != "" {
json.NewEncoder(w).Encode(ErrReponse{Err: err})
return
}
mac := params["mac"]
color_str := params["color"]
err_color := _changeColorByMac(mac, color_str)
if err_color != nil {
json.NewEncoder(w).Encode(err_color)
return
}
resp := SuccessResponse{OK: true}
json.NewEncoder(w).Encode(resp)
}
func _changeColorByMac(mac string, color_str string) (err *ErrReponse) {
color := control.ParseColorString(color_str)
if color == nil {
err := &ErrReponse{Err: "Invalid color"}
return err
}
// set controller to this remote operator
remote_ := remote.NewRemoteTransport(rc, mac)
controller = &control.Controller{remote_}
ctrl_err := _checkBulbState()
if ctrl_err != "" {
err := &ErrReponse{Err: ctrl_err}
return err
}
controller.SetColor(*color)
return nil
}
func _setPowerByMac(mac string, state string) (err *ErrReponse) {
// set controller to this remote operator
remote_ := remote.NewRemoteTransport(rc, mac)
controller = &control.Controller{remote_}
_, ctrl_err := controller.GetState()
if ctrl_err != nil {
return &ErrReponse{Err: ctrl_err.Error()}
}
if state == "ON" {
controller.SetPower(true)
}
if state == "OFF" {
controller.SetPower(false)
}
return nil
}
func _setAllPower(state string) {
err := ""
if state != "ON" && state != "OFF" {
err = "Please provide a valid state (ON/OFF)"
}
if err != "" {
fmt.Printf(err)
return
}
for i, mac := range [4]string{config_vars.Mac1, config_vars.Mac2, config_vars.Mac3, config_vars.Mac4} {
err_power := _setPowerByMac(mac, state)
if err_power != nil {
fmt.Printf("bulb %d: %s failed turning %s. %s\n", i+1, mac, state, err_power)
}
}
}
func colorIsValid (c string) bool {
if len(c) != 6 {
return false
}
_, err := hex.DecodeString(c)
if err != nil {
return false
}
return true
}
func SendMoodCommand(w http.ResponseWriter, req *http.Request) {
params := mux.Vars(req)
if params["color"] == "" {
json.NewEncoder(w).Encode(ErrReponse{Err: "Invalid color"})
return
}
if !colorIsValid(params["color"]) {
json.NewEncoder(w).Encode(ErrReponse{Err: "Invalid color"})
return
}
// put color on queue
var retColor string
err := db.QueryRow(`INSERT INTO colors(timestamp, color) VALUES (CURRENT_TIMESTAMP, $1) RETURNING color;`, params["color"]).Scan(&retColor)
if err != nil {
// TODOOOO handle err
json.NewEncoder(w).Encode(err)
return
}
json.NewEncoder(w).Encode(SuccessResponse{OK: true})
}
func StartTickerCommand(w http.ResponseWriter, req *http.Request) {
params := mux.Vars(req)
if params["key"] != config_vars.Ticker_Key {
json.NewEncoder(w).Encode(ErrReponse{Err: "Incorrect key"})
return
}
_setAllPower("ON")
startTicker()
json.NewEncoder(w).Encode(SuccessResponse{OK: true})
}
func StopTickerCommand(w http.ResponseWriter, req *http.Request) {
params := mux.Vars(req)
if params["key"] != config_vars.Ticker_Key {
json.NewEncoder(w).Encode(ErrReponse{Err: "Incorrect key"})
return
}
_setAllPower("OFF")
stopTicker()
json.NewEncoder(w).Encode(SuccessResponse{OK: true})
}
func StatusCommand(w http.ResponseWriter, req *http.Request) {
resp := StatusResponse{IsLive: IS_LIVE}
for i, mac := range [4]string{config_vars.Mac1, config_vars.Mac2, config_vars.Mac3, config_vars.Mac4} {
remote_ := remote.NewRemoteTransport(rc, mac)
controller = &control.Controller{remote_}
control_res, _ := controller.GetState()
// needed so we don't deref a nil state
var state control.State
if control_res != nil {
state = *control_res
}
if i == 0 {
resp.Light1 = state
} else if i == 1{
resp.Light2 = state
} else if i == 2 {
resp.Light3 = state
} else if i == 3 {
resp.Light4 = state
}
}
json.NewEncoder(w).Encode(resp)
}