-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.go
331 lines (260 loc) · 8.41 KB
/
device.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package main
import (
"github.com/RobertMe/gocec"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"io/ioutil"
"sync"
)
type DeviceAddedHandler func(device *Device)
type DeviceRegistry struct {
configDevices map[string]*DeviceConfig
deviceAddedHandlers []DeviceAddedHandler
devicesMutex sync.Mutex
devices map[gocec.LogicalAddress]*Device
physicalAddressMap map[gocec.PhysicalAddress]*Device
}
type DeviceConfig struct {
Id string `yaml:"id"`
PhysicalAddress string `yaml:"physical_address"`
VendorId int `yaml:"vendor_id"`
OSD string `yaml:"osd"`
MqttTopic string `yaml:"mqtt_topic"`
Ignore bool `yaml:"ignore"`
}
type Device struct {
Id string
CecDevice *CecDeviceDescription
Config *DeviceConfig
LogicalAddress gocec.LogicalAddress
}
type CreateCecDeviceDescription func() *CecDeviceDescription
func NewDeviceRegistry(dataDirectory string) *DeviceRegistry {
return &DeviceRegistry{
configDevices: loadDevicesFromConfig(dataDirectory),
deviceAddedHandlers: make([]DeviceAddedHandler, 0),
devices: make(map[gocec.LogicalAddress]*Device),
physicalAddressMap: make(map[gocec.PhysicalAddress]*Device),
}
}
func loadDevicesFromConfig(dataDirectory string) (devices map[string]*DeviceConfig) {
logContext := log.WithFields(log.Fields{
"config_file": dataDirectory + "devices.yaml",
})
logContext.Info("Reading devices from config file")
devices = make(map[string]*DeviceConfig)
data, err := ioutil.ReadFile(dataDirectory + "devices.yaml")
if err != nil {
logContext.Info("Devices configuration file does not exist yet")
return
}
err = yaml.Unmarshal(data, &devices)
if err != nil {
logContext.WithFields(log.Fields{
"error": err,
}).Error("Devices configuration file could not be parsed as valid YAML")
return
}
return
}
func (registry *DeviceRegistry) RegisterDeviceAddedHandler(handler DeviceAddedHandler) {
log.Trace("Registering device added handler")
registry.deviceAddedHandlers = append(registry.deviceAddedHandlers, handler)
}
func (registry *DeviceRegistry) FindByLogicalAddress(address gocec.LogicalAddress) *Device {
logContext := log.WithFields(log.Fields{
"logical_address": address,
})
registry.devicesMutex.Lock()
defer registry.devicesMutex.Unlock()
device, ok := registry.devices[address]
if !ok {
logContext.Info("Could not find device by logical address")
return nil
}
if device.Config.Ignore {
logContext.Debug("Found device by logical address, but it's ignored")
return nil
}
logContext.WithFields(log.Fields{
"device.id": device.Id,
}).Debug("Found device by logical address")
return device
}
func (registry *DeviceRegistry) GetByCecDevice(address gocec.LogicalAddress, creator CreateCecDeviceDescription) *Device {
registry.devicesMutex.Lock()
logContext := log.WithFields(log.Fields{
"logical_address": address,
})
if device, ok := registry.devices[address]; ok {
registry.devicesMutex.Unlock()
logContext = logContext.WithFields(log.Fields{
"device.id": device.Config.Id,
})
if device.Config.Ignore {
logContext.Trace("Found device by logical address, but it's ignored")
return nil
}
logContext.Trace("Found device by logical address")
return device
}
description := creator()
if description.physicalAddress == [2]byte{0xFF, 0xFF} ||
description.vendor == gocec.VendorUnknown ||
description.OSD == "cec2mqtt" {
registry.devicesMutex.Unlock()
logContext.WithFields(log.Fields{
"description.physical_address": description.physicalAddress,
"description.vendor": uint(description.vendor),
"description.osd": description.OSD,
}).Debug("Ignoring device because it is incomplete or cec2mqtt")
return nil
}
device, ok := registry.physicalAddressMap[description.physicalAddress]
if ok {
if device.CecDevice.physicalAddress == description.physicalAddress &&
device.CecDevice.vendor == description.vendor {
registry.devices[address] = device
registry.devicesMutex.Unlock()
logContext = logContext.WithFields(log.Fields{
"physical_address": description.physicalAddress,
"vendor_id": description.vendor,
"description": description,
"device.id": device.Config.Id,
})
if device.Config.Ignore {
logContext.Debug("Found device by physical address and vendor, but it's ignored")
return nil
}
logContext.Debug("Found device by physical address and vendor")
return device
}
}
deviceConfig := registry.FindDevice(description.physicalAddress.String(), int(description.vendor), description.OSD)
device = &Device{
Id: deviceConfig.Id,
CecDevice: description,
Config: deviceConfig,
LogicalAddress: description.logicalAddress,
}
registry.devices[device.LogicalAddress] = device
registry.physicalAddressMap[description.physicalAddress] = device
registry.devicesMutex.Unlock()
logContext = logContext.WithFields(log.Fields{
"physical_address": description.physicalAddress,
"vendor": description.vendor,
"description": description,
"device.id": device.Config.Id,
})
if deviceConfig.Ignore {
logContext.Debug("Added new device, but not registering it as it's ignored")
return nil
}
logContext.Debug("Adding new device")
for _, handler := range registry.deviceAddedHandlers {
handler(device)
}
return device
}
func (registry *DeviceRegistry) FindDevice(physicalAddress string, vendorId int, name string) *DeviceConfig {
logContext := log.WithFields(log.Fields{
"physical_address": physicalAddress,
"vendor_id": vendorId,
"name": name,
})
logContext.Trace("Searching device in config")
var option *DeviceConfig
for _, device := range registry.configDevices {
deviceLogContext := logContext.WithFields(log.Fields{
"device.id": device.Id,
"device.physical_address": device.PhysicalAddress,
"device.vendor_id": device.VendorId,
"device.osd": device.OSD,
})
if device.PhysicalAddress == physicalAddress && device.VendorId == vendorId {
if device.OSD == name {
// Exact match so must be it
deviceLogContext.Info("Matched exact device from config")
return device
}
if option != nil {
deviceLogContext.Debug("Multiple possible matches are found. Skipping.")
option = nil
break
}
deviceLogContext.Debug("Found possible match")
option = device
} else if device.VendorId == vendorId && device.OSD == name {
if option != nil {
deviceLogContext.Debug("Multiple possible matches are found. Skipping.")
option = nil
break
}
deviceLogContext.Debug("Found possible match")
option = device
}
}
if option != nil {
logContext.WithFields(log.Fields{
"device.id": option.Id,
"device.physical_address": option.PhysicalAddress,
"device.vendor_id": option.VendorId,
"device.osd": option.OSD,
}).Info("Found device in config")
option.PhysicalAddress = physicalAddress
option.VendorId = vendorId
option.OSD = name
return option
}
var id string
for {
id = uuid.New().String()
if _, ok := registry.configDevices[id]; !ok {
break
}
}
device := &DeviceConfig{
Id: id,
PhysicalAddress: physicalAddress,
VendorId: vendorId,
OSD: name,
MqttTopic: name,
}
log.WithFields(log.Fields{
"device.id": device.Id,
"device.physical_address": device.PhysicalAddress,
"device.vendor_id": device.VendorId,
"device.osd": device.OSD,
"device.mqtt_topic": device.MqttTopic,
}).Info("Created new device")
registry.configDevices[id] = device
return device
}
func (registry *DeviceRegistry) Save(configPath string) error {
data, err := yaml.Marshal(registry.configDevices)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Error("Failed to convert devices configuration into YAML")
return err
}
err = ioutil.WriteFile(configPath+"devices.yaml", data, 0644)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"config_file": configPath + "devices.yaml",
}).Error("Failed to save devices configuration to file")
return err
}
return nil
}
func (registry *DeviceRegistry) List() []*Device {
registry.devicesMutex.Lock()
defer registry.devicesMutex.Unlock()
devices := make([]*Device, 0, len(registry.devices))
for _, device := range registry.devices {
devices = append(devices, device)
}
return devices
}