-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhuevent.go
421 lines (333 loc) · 9.44 KB
/
huevent.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path"
"time"
)
type config struct {
uri string
deviceIds []string
hasFilter bool
stateMap *map[string]map[string]string
responseMap *map[string]interface{}
pollTimeMs time.Duration
logHTTPError bool
shouldExit bool
hooks *[]Hook
DEBUG bool
printSensors bool
}
type HueventConfig struct {
Config struct {
Ip string `yaml:"ip"`
User string `yaml:"user"`
Rate int `yaml:"pollingRateMs,omitempty"`
} `yaml:"config"`
Hooks []Hook `yaml:"hooks"`
DeviceFilter []string `yaml:"deviceFilter"`
}
type Hook struct {
DeviceID string `yaml:"deviceId"`
EventType string `yaml:"eventType"`
TriggerOn string `yaml:"triggerOn,omitempty"`
Cmd string `yaml:"cmd"`
}
type hueBridgeResponse struct {
ID string `json:"id"`
InternalIpaddress string `json:"internalipaddress"`
}
type hueBridgePairResponse struct {
Error struct {
Type string
Address string
Description string
}
Success struct {
Username string
}
}
// DEBUG huevent debug flag
var DEBUG = false
func main() {
var conf = makeConfig()
if conf.DEBUG {
fmt.Printf("current configuration: %#v\n", conf)
}
for {
poll(&conf)
time.Sleep(conf.pollTimeMs * time.Millisecond)
}
}
func myUsage() {
fmt.Printf("huevent - get events from buttons and sensors\n")
fmt.Printf("Usage: %s [OPTIONS] \n", os.Args[0])
flag.PrintDefaults()
}
func pairBridge(configpath string) {
if DEBUG {
fmt.Printf("pair bridge, ask https://discovery.meethue.com/\n")
}
resp, err := http.Get("https://discovery.meethue.com/")
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
if DEBUG {
fmt.Printf("response from https://discovery.meethue.com/ %s\n", body)
}
var hueBridges = []hueBridgeResponse{}
unmarshalErr := json.Unmarshal(body, &hueBridges)
if unmarshalErr != nil {
panic(unmarshalErr)
}
if len(hueBridges) == 0 {
fmt.Printf("no bridges found\n")
os.Exit(1)
}
values := map[string]string{"devicetype": "huevent"}
jsonValue, _ := json.Marshal(values)
var uri = fmt.Sprintf("http://%s/api", hueBridges[0].InternalIpaddress)
resp, err = http.Post(uri, "application/json", bytes.NewBuffer(jsonValue))
//noinspection ALL
body, err = ioutil.ReadAll(resp.Body)
if DEBUG {
fmt.Printf("response pairing %s: %s\n", hueBridges[0].InternalIpaddress, body)
}
var hueResponse []hueBridgePairResponse
_ = json.Unmarshal(body, &hueResponse)
if hueResponse[0].Success.Username == "" {
fmt.Printf("Error while pairing with %s: %s \n", hueBridges[0].InternalIpaddress, hueResponse[0].Error.Description)
os.Exit(1)
}
var pairingSuccess = map[string]map[string]string{}
pairingSuccess["config"] = map[string]string{}
pairingSuccess["config"]["ip"] = hueBridges[0].InternalIpaddress
pairingSuccess["config"]["user"] = hueResponse[0].Success.Username
var currentConfig = readConfig(configpath)
currentConfig.Config.Ip = hueBridges[0].InternalIpaddress
currentConfig.Config.User = hueResponse[0].Success.Username
writeConfig(currentConfig, configpath)
os.Exit(0)
}
func makeConfig() config {
exitOnEvent := flag.Bool("exit", false, "exit on event")
hueventConfigPath := flag.String("config", configPath(), "path to config file")
printSensors := flag.Bool("sensors", false, "print sensors response and exit")
debug := flag.Bool("debug", false, "enable some debug output")
pair := flag.Bool("pair", false, "pair hue bridge")
flag.Usage = myUsage
flag.Parse()
DEBUG = *debug
if *pair {
pairBridge(*hueventConfigPath)
}
var hueventConfig = readConfig(*hueventConfigPath)
if *debug {
fmt.Printf("Read Config %+s\n", hueventConfig)
}
if hueventConfig.Config.Ip == "" || hueventConfig.Config.User == "" {
fmt.Printf("Error: no Hue-Bridge configured at %s, run huevent with -pair to configure\n", *hueventConfigPath)
os.Exit(1)
}
var pollTimeMs = time.Duration(333)
if hueventConfig.Config.Rate > 0 {
pollTimeMs = time.Duration(hueventConfig.Config.Rate)
}
if *debug {
fmt.Printf("Polling-Rate: %s\n", pollTimeMs * time.Millisecond)
}
stateMap := make(map[string]map[string]string)
var hasFilter = false
var deviceIds = hueventConfig.DeviceFilter
for _, deviceID := range deviceIds {
stateMap[deviceID] = make(map[string]string)
hasFilter = true
}
responseMap := make(map[string]interface{})
return config{
uri: fmt.Sprintf("http://%s/api/%s/sensors", hueventConfig.Config.Ip, hueventConfig.Config.User),
deviceIds: deviceIds,
stateMap: &stateMap,
responseMap: &responseMap,
hasFilter: hasFilter,
pollTimeMs: pollTimeMs,
logHTTPError: true,
hooks: &hueventConfig.Hooks,
shouldExit: *exitOnEvent,
DEBUG: *debug,
printSensors: *printSensors}
}
func poll(conf *config) {
resp, err := http.Get(conf.uri)
if err != nil {
if conf.logHTTPError {
_, _ = fmt.Fprintln(os.Stderr, err)
conf.logHTTPError = false
}
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err == nil {
_ = json.Unmarshal(body, conf.responseMap)
parseJSONMap(conf.responseMap, conf)
} else {
_, _ = fmt.Fprintln(os.Stderr, err)
}
if !conf.logHTTPError {
conf.logHTTPError = true
}
if conf.printSensors {
var prettyJSON bytes.Buffer
_ = json.Indent(&prettyJSON, body, "", "\t")
fmt.Printf("%s\n", prettyJSON.String())
os.Exit(0)
}
}
func exit(device string, eventType string, triggerOn string, conf *config) {
fmt.Printf("%s\t%s\t%s\n", device, eventType, triggerOn)
for _, hook := range *conf.hooks {
if hook.DeviceID != device || hook.EventType != eventType {
continue
}
if hook.TriggerOn == "" || hook.TriggerOn == triggerOn {
//noinspection ALL
go executeCommand(hook.Cmd, device, eventType, triggerOn)
}
}
if conf.shouldExit {
os.Exit(0)
}
}
func updateButtonMap(update map[string]interface{}, conf *config, device1 interface{}) {
var device = device1.(string)
var btnStateMap = (*conf.stateMap)[device]
var key = "unknown"
var eventType = "unknown"
if update["buttonevent"] != nil {
key = fmt.Sprintf("%v", update["buttonevent"].(float64))
eventType = "buttonevent"
}
if update["presence"] != nil {
key = fmt.Sprintf("%v", update["presence"].(bool))
eventType = "presence"
}
if update["lightlevel"] != nil {
key = fmt.Sprintf("%v", update["lightlevel"].(float64))
eventType = "lightlevel"
}
if update["temperature"] != nil {
key = fmt.Sprintf("%v", update["temperature"].(float64))
eventType = "temperature"
}
if eventType == "unknown" {
return
}
var value = update["lastupdated"].(string)
// check for known button
if val, ok := btnStateMap[key]; ok {
// check if button pressed
if val != value {
exit(device, eventType, key, conf)
}
} else {
// unknown button, check for initial run
if len(btnStateMap) != 0 {
exit(device, eventType, key, conf)
}
}
btnStateMap[key] = value
}
func parseJSONMap(jsonAsMap *map[string]interface{}, conf *config) {
for _, v := range *jsonAsMap {
if subObject, ok := v.(map[string]interface{}); ok {
if deviceId, ok := subObject["uniqueid"]; ok {
addNewSensorToStateMap(deviceId, conf)
if stateObject, ok := subObject["state"]; ok && hasKey(deviceId, conf.stateMap) {
updateButtonMap(stateObject.(map[string]interface{}), conf, deviceId)
}
}
parseJSONMap(&subObject, conf)
}
}
}
func addNewSensorToStateMap(deviceId interface{}, conf *config) {
if conf.hasFilter {
// do not add a sensor if the argument filter is enabled
return
}
if _, ok := (*conf.stateMap)[deviceId.(string)]; ok {
// do not add the sensor if the sensor is still added
return
}
// allocate a new state map, add device to stateMap
(*conf.stateMap)[deviceId.(string)] = make(map[string]string)
}
func hasKey(a interface{}, map1 *map[string]map[string]string) bool {
_, ok := (*map1)[a.(string)]
return ok
}
func executeCommand(cmdString string, deviceId string, eventType string, payload string) error {
cmd := exec.Command("/bin/sh", "-c", cmdString)
extraEnv := []string{
fmt.Sprintf("HUEVENT_DEVICE_ID=%s", deviceId),
fmt.Sprintf("HUEVENT_EVENT_TYPE=%s", eventType),
fmt.Sprintf("HUEVENT_PAYLOAD=%s", payload)}
cmd.Env = append(os.Environ(), extraEnv...)
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
return cmd.Run()
}
func PathExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
func configPath() string {
var configDirectory = path.Join(os.Getenv("HOME"), ".huevent")
return path.Join(configDirectory, "huevent.yml")
}
func readConfig(filepath string) HueventConfig {
var hueventConfig = HueventConfig{}
content, readErr := ioutil.ReadFile(filepath)
if readErr != nil {
writeConfig(HueventConfig{}, filepath)
}
unmarshalErr := yaml.Unmarshal(content, &hueventConfig)
if unmarshalErr != nil {
fmt.Printf("Can't parse config file (%s), delete it and create a new with -pair argument", filepath)
}
return hueventConfig
}
func writeConfig(config HueventConfig, filepath string) {
var a, err = yaml.Marshal(&config)
if err != nil {
log.Fatalf("error: %v", err)
}
if DEBUG {
fmt.Printf("Write Config %s \n %s \n", filepath, a)
}
if !PathExists(filepath) {
var configDir = path.Dir(filepath)
dirErr := os.MkdirAll(configDir, 0755)
if dirErr != nil {
panic(dirErr)
}
}
writeErr := ioutil.WriteFile(filepath, a, 0644)
if writeErr != nil {
panic(writeErr)
}
}