-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
155 lines (124 loc) · 2.89 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
package main
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"sync"
"time"
"github.com/brutella/hc"
"github.com/brutella/hc/accessory"
hclog "github.com/brutella/hc/log"
)
const (
discoveryInterval = 5 * time.Second
)
var devicePath = filepath.Join(os.Getenv("HOME"), ".homecontrol", "kasa")
type kasaAccessory struct {
device Device
acc *accessory.Switch
transport hc.Transport
lastSeen time.Time
}
type kasaAccessoryMap struct {
m map[string]*kasaAccessory
mu sync.Mutex
}
// ExpireOld cleans up devices that haven't been seen in a while
func (kam *kasaAccessoryMap) ExpireOld() {
kam.mu.Lock()
defer kam.mu.Unlock()
if kam.m == nil {
return
}
for _, ka := range kam.m {
if time.Since(ka.lastSeen) > 5*time.Minute {
log.Printf("Dropping lost accessory %q", ka.device.Name)
<-ka.transport.Stop()
delete(kam.m, ka.device.DeviceID)
}
}
}
func (kam *kasaAccessoryMap) AddOrUpdate(d Device) error {
kam.mu.Lock()
defer kam.mu.Unlock()
if kam.m == nil {
kam.m = map[string]*kasaAccessory{}
}
if a, ok := kam.m[d.DeviceID]; ok {
a.device = d
a.acc.Switch.On.SetValue(d.RelayState)
a.lastSeen = time.Now()
return nil
}
info := accessory.Info{
Name: d.Name,
Model: d.Model,
Manufacturer: "TP-Link",
FirmwareRevision: d.SoftwareVersion,
SerialNumber: d.DeviceID,
}
acc := accessory.NewSwitch(info)
acc.Switch.On.SetValue(d.RelayState)
hcConfig := hc.Config{
Pin: "00102003",
StoragePath: filepath.Join(devicePath, d.DeviceID),
}
t, err := hc.NewIPTransport(hcConfig, acc.Accessory)
if err != nil {
return fmt.Errorf("Unable to create homekit device: %w", err)
}
acc.Switch.On.OnValueRemoteUpdate(func(on bool) {
if err := d.Set(on); err != nil {
log.Printf("Unable to update switch %q to %t: %v", d.Name, on, err)
}
})
log.Printf("Creating accessory for %q at %v", d.Name, d.Addr)
ka := &kasaAccessory{
device: d,
acc: acc,
transport: t,
lastSeen: time.Now(),
}
go t.Start()
kam.m[d.DeviceID] = ka
return nil
}
func main() {
if x := os.Getenv("DEBUG"); x != "" {
hclog.Debug.Enable()
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func(ctx context.Context) {
log.Printf("Starting device discovery loop")
defer log.Printf("Exiting device discovery loop")
var kam kasaAccessoryMap
// The first loop iteration runs immediately, subsequent ones
// run on discoveryInterval.
var interval time.Duration
for {
select {
case <-ctx.Done():
return
case <-time.After(interval):
devices, err := discover()
if err != nil {
log.Printf("error discovering devices: %v", err)
break
}
for _, d := range devices {
if err := kam.AddOrUpdate(d); err != nil {
log.Println(err)
}
}
}
interval = discoveryInterval
}
}(ctx)
hc.OnTermination(func() {
cancel()
})
<-ctx.Done()
}