-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccessory.go
84 lines (70 loc) · 1.84 KB
/
accessory.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
package wifioccupancy
import (
"github.com/brutella/hc/accessory"
"github.com/brutella/hc/characteristic"
"github.com/brutella/hc/log"
"github.com/brutella/hc/service"
. "github.com/deckarep/golang-set"
)
type Presence interface {
Watch(monitor chan<- bool) error
IsOccupied() bool
}
type Sensor struct {
*accessory.Accessory
OccupancySensor *service.OccupancySensor
presence Presence
addresses Set
}
func NewSensor(file string, addresses Set) *Sensor {
info := accessory.Info{
Name: "WifiOccupancy",
Manufacturer: "Compex",
SerialNumber: "6A02",
Model: "WPQ864",
}
acc := Sensor{
Accessory: accessory.New(info, accessory.TypeOther),
addresses: addresses,
}
acc.OccupancySensor = acc.createOccupancySensorSevice()
acc.AddService(acc.OccupancySensor.Service)
if file != "" {
acc.presence = NewFilePresence(file, addresses)
} else {
acc.presence = NewNetlinkPresence(addresses)
}
acc.watch()
log.Info.Println("Wifi occupancy sensor is ready")
return &acc
}
func (s *Sensor) AddAddress(address string) {
s.addresses.Add(address)
}
func (s *Sensor) RemoveAddress(address string) {
s.addresses.Remove(address)
}
func (s *Sensor) createOccupancySensorSevice() *service.OccupancySensor {
sensor := service.NewOccupancySensor()
detector := sensor.OccupancyDetected
detector.SetValue(characteristic.OccupancyDetectedOccupancyNotDetected)
return sensor
}
func (s *Sensor) watch() {
isOccupiedCh := make(chan bool, 16)
err := s.presence.Watch(isOccupiedCh)
if err != nil {
log.Info.Fatal(err)
}
sensor := s.OccupancySensor
detector := sensor.OccupancyDetected
go func() {
for isOccupied := range isOccupiedCh {
if isOccupied {
detector.SetValue(characteristic.OccupancyDetectedOccupancyDetected)
} else {
detector.SetValue(characteristic.OccupancyDetectedOccupancyNotDetected)
}
}
}()
}