-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
96 lines (91 loc) · 2.16 KB
/
main.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
package main
import (
"context"
"encoding/json"
"fmt"
"net/url"
"os"
"path/filepath"
"time"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/gen2brain/beeep"
"github.com/kralicky/doorbelld/pkg/hue"
"github.com/kralicky/doorbelld/pkg/unifi"
)
func main() {
viper.SetConfigName("doorbelld")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
if home, err := os.UserHomeDir(); err == nil {
viper.AddConfigPath(filepath.Join(home, ".config"))
}
err := viper.ReadInConfig()
if err != nil {
log.Error(err)
}
url, err := url.Parse(viper.GetString("unifi.endpoint"))
if err != nil {
log.Fatal(err)
}
cfg := unifi.Config{
Endpoint: url,
}
hueCfg := hue.Config{
Endpoint: viper.GetString("hue.endpoint"),
Username: viper.GetString("hue.username"),
}
token, err := unifi.Login(cfg,
viper.GetString("unifi.username"), viper.GetString("unifi.password"))
if err != nil {
log.Fatal(err)
}
for {
listener, err := unifi.NewUpdateListener(context.Background(), cfg, token)
if err != nil {
fmt.Println(err)
time.Sleep(5 * time.Second)
continue
}
for {
update, open := <-listener.C
if !open {
break
}
log.WithField("kind", update.ActionFrame.Action).
WithField("key", update.ActionFrame.ModelKey).
Debug(string(update.DataFrame))
if update.ActionFrame.ModelKey == unifi.ModelKeyEvent &&
update.ActionFrame.Action == unifi.ActionKindAdd {
event := unifi.EventDataFrame{}
err := json.Unmarshal(update.DataFrame, &event)
if err != nil {
log.WithError(err).Warn("Could not unmarshal event data frame")
continue
}
if event.Type == "ring" {
log.Info("** DOORBELL IS RINGING **")
go func() {
if err := beeep.Notify("Doorbell is ringing", "", ""); err != nil {
log.Error(err)
}
}()
go func() {
if err := beeep.Beep(783.99, 450); err != nil {
log.Error(err)
}
if err := beeep.Beep(659.25, 550); err != nil {
log.Error(err)
}
}()
for i := 0; i < 3; i++ {
if err := hue.AlertAllLights(hueCfg); err != nil {
log.Error(err)
}
time.Sleep(1 * time.Second)
}
}
}
}
}
}