This repository has been archived by the owner on Aug 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
179 lines (149 loc) · 3.73 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
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
package main
import (
"encoding/json"
"fmt"
"net"
"os"
"os/signal"
log "github.com/sirupsen/logrus"
"github.com/brutella/hc"
"github.com/brutella/hc/accessory"
hclog "github.com/brutella/hc/log"
_ "github.com/joho/godotenv/autoload"
"github.com/oleksandr/bonjour"
"github.com/zubairhamed/canopus"
)
func browseForTradfriHub() (net.IP, int) {
resolver, err := bonjour.NewResolver(nil)
if err != nil {
log.Panic(err)
}
results := make(chan *bonjour.ServiceEntry)
err = resolver.Browse("_coap._udp", "local.", results)
if err != nil {
log.Fatal(err)
}
res := <-results
log.Info(res)
resolver.Exit <- true
return res.AddrIPv4, res.Port
}
func (hub *tradfriHub) attachHcHandlers(bulbID string, acc *tradfriBulb) {
acc.Lightbulb.On.OnValueRemoteUpdate(func(on bool) {
hub.setBulbPower(bulbID, on)
log.Info("Light state: %s", on)
})
acc.Lightbulb.Brightness.OnValueRemoteUpdate(func(brightness int) {
hub.setBulbBrightness(bulbID, brightness)
log.Info("Light brightness: %d", brightness)
})
acc.Lightbulb.ColorTemperature.OnValueRemoteUpdate(func(temp int) {
hub.setBulbTemperature(bulbID, temp)
log.Info("Light temp: %d", temp)
})
}
var bulbs = map[string]string{ //TODO make this not hardcoded
"Floor Lamp": "65538",
"Bedside Lamp": "65537",
}
func main() {
ip, port := browseForTradfriHub()
hub := initTradfriHub(ip, port)
hclog.Debug.Enable()
primaryAccInfo := accessory.Info{
Name: fmt.Sprintf("%s Trådfri Bridge", os.Getenv("BRIDGE_NAME")),
}
bridge := accessory.New(primaryAccInfo, accessory.TypeBridge)
var accessories []*accessory.Accessory
for name, bulbID := range bulbs {
acc := newTradfriBulb(accessory.Info{
Name: name,
})
hub.attachHcHandlers(bulbID, acc)
accessories = append(accessories, acc.Accessory)
}
config := hc.Config{Pin: "12344321", StoragePath: "./db"}
t, err := hc.NewIPTransport(config, bridge, accessories...)
if err != nil {
log.Panic(err)
}
go t.Start()
sigCh := make(chan os.Signal)
signal.Notify(sigCh, os.Interrupt)
for {
select {
case <-sigCh:
t.Stop()
os.Exit(0)
}
}
}
type tradfriHub struct {
host string
conn canopus.Connection
}
func initTradfriHub(ip net.IP, port int) *tradfriHub {
hub := tradfriHub{}
hub.host = fmt.Sprintf("%s:%d", ip, port)
log.Infof("Connecting to gateway on %s...", hub.host)
var err error
hub.conn, err = canopus.DialDTLS(hub.host, "Client_identity", os.Getenv("TRADFRI_PSK"))
if err != nil {
log.Panic(err)
}
return &hub
}
func (hub *tradfriHub) getBulbStatus(id string) tradfriCOAPResponse {
req := canopus.NewRequest(canopus.MessageConfirmable, canopus.Get).(*canopus.CoapRequest)
req.SetRequestURI("/15001/" + id)
resp, err := hub.conn.Send(req)
if err != nil {
log.Panic(err)
}
var res tradfriCOAPResponse
json.Unmarshal(resp.GetMessage().GetPayload().GetBytes(), &res)
return res
}
func (hub *tradfriHub) setBulb(bulbID, data string) {
req := canopus.NewRequest(canopus.MessageConfirmable, canopus.Put).(*canopus.CoapRequest)
req.SetRequestURI("/15001/" + bulbID)
req.SetStringPayload(data)
_, err := hub.conn.Send(req)
if err != nil {
log.Error(err)
}
}
func (hub *tradfriHub) setBulbPower(bulbID string, on bool) {
var onInt int
if on {
onInt = 1
}
hub.setBulb(bulbID, fmt.Sprintf(`{
"3311": [{
"5850": %d
}]
}`, onInt))
}
func (hub *tradfriHub) setBulbBrightness(id string, b int) {
hub.setBulb(id, fmt.Sprintf(`{
"3311": [{
"5851": %d
}]
}`, b))
}
func (hub *tradfriHub) setBulbTemperature(id string, t int) {
var color string
if t < 200 { // whitest
color = "f5faf6"
} else if t < 300 {
color = "f1e0b5"
} else { // reddest
color = "efd275"
}
log.Info("setting to: " + color)
hub.setBulb(id, fmt.Sprintf(`{
"3311": [{
"5706": "%s"
}]
}`, color))
}