forked from dariopb/ha-gosenseapp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gosenseapp.go
281 lines (238 loc) · 6.16 KB
/
gosenseapp.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
package gosenseapp
import (
"bufio"
"fmt"
"io"
"os"
"os/signal"
"path"
"path/filepath"
"strconv"
"strings"
"sync"
"syscall"
"time"
gosense "github.com/dariopb/gosense"
log "github.com/sirupsen/logrus"
)
type DeviceMetadata struct {
Name string `yaml:"name" json:"name"`
MAC string `yaml:"mac" json:"mac"`
SensorType gosense.SensorType `yaml:"sensorType" json:"sensorType"`
Present bool `yaml:"present" json:"present"`
}
type DeviceProperties struct {
}
type Sensor struct {
Metadata DeviceMetadata `yaml:"metadata" json:"metadata"`
Properties map[string]string `yaml:"properties" json:"properties"`
}
type MQTT struct {
ClientId string `yaml:"clientid" json:"clientid"`
Hostname string `yaml:"hostname" json:"hostname"`
User string `yaml:"user" json:"user"`
Password string `yaml:"password" json:"password"`
Port uint `yaml:"port" json:"port"`
SensorTopic string `yaml:"sensorTopic" json:"sensorTopic"`
DiscoveryTopic string `yaml:"discoveryTopic" json:"discoveryTopic"`
}
type ConfigFile struct {
Filename string `yaml:"ignore" json:"ignore"`
AppConfig *AppConfig
mtx sync.Mutex
}
type AppConfig struct {
DebugLevel log.Level `yaml:"debuglevel" json:"debuglevel"`
MQTT MQTT `yaml:"mqtt" json:"mqtt"`
Sensors map[string]*Sensor `yaml:"sensors" json:"sensors"`
}
var SenseData *ConfigFile
var SenseDev *gosense.WyzeSense
// FindSenseDevice tries to find the first sense dongle plugged in.
func FindSenseDevice() (string, error) {
devname := ""
root := "/sys/class/hidraw"
names, err := filepath.Glob(path.Join(root, "*"))
log.Info("Looking for sense dongle in /dev/hidraw*")
loop:
for _, dirname := range names {
fname := path.Join(dirname, "device/uevent")
file, err := os.Open(fname)
if err != nil {
continue
}
defer file.Close()
reader := bufio.NewReader(file)
for {
line, _, err := reader.ReadLine()
if err == io.EOF {
break
}
if strings.HasPrefix(string(line), "HID_ID=") &&
strings.Contains(string(line), "00001A86:0000E024") {
_, devname = path.Split(dirname)
devname = path.Join("/dev", devname)
break loop
}
}
}
if err != nil {
return "", err
}
if len(devname) > 0 {
log.Infof("Found sense dongle in [%s]", devname)
} else {
err = fmt.Errorf("can't find sense device")
}
return devname, err
}
func Run() {
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
log.SetOutput(os.Stdout)
var err error
confFilename, ok := os.LookupEnv("CONFIG_FILE")
if !ok {
confFilename = "app.yaml"
}
SenseData, err = NewAppData(confFilename)
if err != nil {
os.Exit(3)
}
if SenseData.AppConfig.DebugLevel != 0 {
log.SetLevel(SenseData.AppConfig.DebugLevel)
}
devicename, err := FindSenseDevice() // "/dev/hidraw2"
if err != nil {
log.Error(err.Error())
os.Exit(3)
}
alarmch := make(chan (gosense.Alarm), 10)
sensorch := make(chan (gosense.SenseSensor), 10)
s, err := gosense.NewWyzeSense(devicename, alarmch, sensorch)
if err != nil {
log.Error(err.Error())
os.Exit(3)
}
SenseDev = s
err = SenseDev.Start()
if err != nil {
log.Error(err.Error())
os.Exit(3)
}
//s.ScanSensor()
//s.DeleteSensor("7779768D")
//s.VerifySensor("7779768D")
// Start the REST api for easy qurying/managing
api, err := NewRestApi(8080)
if err != nil {
log.Error(err.Error())
os.Exit(3)
}
var mqtt *MQTTPublisher
if len(SenseData.AppConfig.MQTT.Hostname) > 0 {
// Start the MQTT publisher
mqtt = NewMQTTPublisher(SenseData.AppConfig.MQTT)
//if err != nil {
// log.Error(err.Error())
// os.Exit(3)
//}
} else {
log.Info(" => skipping HA MQTT publisher since no server was provided")
}
// Merge the data I have with the data collected
sensors, err := SenseDev.GetSensorList()
if err != nil {
log.Error(err.Error())
os.Exit(3)
}
SenseData.Lock()
for _, mac := range sensors {
sensor, ok := SenseData.AppConfig.Sensors[mac]
if !ok {
sensor = &Sensor{
Metadata: DeviceMetadata{
Name: mac,
MAC: mac,
Present: true,
},
Properties: make(map[string]string),
}
SenseData.AppConfig.Sensors[mac] = sensor
}
if mqtt != nil {
mqtt.sensorch <- *sensor
}
}
SenseData.Unlock()
SenseData.Save(SenseData.AppConfig)
loop:
for {
select {
case a := <-alarmch:
log.Infof("ALARM: %s, state: %d",
a.MAC, a.State)
// Only Alarms with flag 0xA2 (162) signal an open/close/motion alarm
if int(a.SensorFlags) != 162 {
log.Infof("ALARM: Ignoring Alarm with unknown flag: %x",
a.SensorFlags)
break
}
SenseData.Lock()
sensor, ok := SenseData.AppConfig.Sensors[a.MAC]
if ok {
sensor.Metadata.SensorType = a.SensorType
sensor.Properties["state"] = strconv.Itoa(int(a.State))
sensor.Properties["battery"] = strconv.Itoa(int(a.Battery))
sensor.Properties["signal"] = strconv.Itoa(int(a.SignalStrength))
sensor.Properties["timeLastAlarm"] = a.Timestamp.Format(time.RFC3339)
}
SenseData.Unlock()
SenseData.Save(SenseData.AppConfig)
if mqtt != nil {
mqtt.alarmch <- *sensor
}
break
case sensorAct := <-sensorch:
log.Infof("SENSOR: %s, state: %d [present: %t]",
sensorAct.MAC, sensorAct.SensorType, sensorAct.Present)
SenseData.Lock()
var sensor = &Sensor{
Metadata: DeviceMetadata{
Name: sensorAct.MAC,
MAC: sensorAct.MAC,
Present: sensorAct.Present,
},
Properties: make(map[string]string),
}
if sensorAct.Present {
var sensor *Sensor
sensor, ok := SenseData.AppConfig.Sensors[sensorAct.MAC]
if !ok {
sensor = &Sensor{
Metadata: DeviceMetadata{
Name: sensorAct.MAC,
MAC: sensorAct.MAC,
Present: sensorAct.Present,
},
Properties: make(map[string]string),
}
}
sensor.Metadata.Present = sensorAct.Present
SenseData.AppConfig.Sensors[sensorAct.MAC] = sensor
} else {
delete(SenseData.AppConfig.Sensors, sensorAct.MAC)
}
if mqtt != nil {
mqtt.sensorch <- *sensor
}
SenseData.Unlock()
SenseData.Save(SenseData.AppConfig)
break
case <-c:
break loop
}
}
mqtt.Close()
api.Close()
}