@@ -2,7 +2,6 @@ package exporter
2
2
3
3
import (
4
4
"context"
5
- "encoding/json"
6
5
"github.com/shimmeringbee/controller/state"
7
6
"github.com/shimmeringbee/da"
8
7
"github.com/shimmeringbee/da/capabilities"
@@ -31,12 +30,19 @@ type ExportedGateway struct {
31
30
32
31
const DefaultCapabilityTimeout = 1 * time .Second
33
32
34
- type DeviceExporter struct {
33
+ type deviceExporter struct {
35
34
DeviceOrganiser * state.DeviceOrganiser
36
35
GatewayMapper state.GatewayMapper
37
36
}
38
37
39
- func (de * DeviceExporter ) ExportDevice (ctx context.Context , daDevice da.Device ) ExportedDevice {
38
+ func NewDeviceExporter (do * state.DeviceOrganiser , gm state.GatewayMapper ) DeviceExporter {
39
+ return & deviceExporter {
40
+ DeviceOrganiser : do ,
41
+ GatewayMapper : gm ,
42
+ }
43
+ }
44
+
45
+ func (de * deviceExporter ) ExportDevice (ctx context.Context , daDevice da.Device ) ExportedDevice {
40
46
capabilityList := map [string ]any {}
41
47
42
48
for _ , capFlag := range daDevice .Capabilities () {
@@ -58,7 +64,7 @@ func (de *DeviceExporter) ExportDevice(ctx context.Context, daDevice da.Device)
58
64
}
59
65
}
60
66
61
- func (de * DeviceExporter ) ExportSimpleDevice (ctx context.Context , daDevice da.Device ) ExportedSimpleDevice {
67
+ func (de * deviceExporter ) ExportSimpleDevice (ctx context.Context , daDevice da.Device ) ExportedSimpleDevice {
62
68
capabilityList := []string {}
63
69
64
70
for _ , capFlag := range daDevice .Capabilities () {
@@ -80,7 +86,7 @@ func (de *DeviceExporter) ExportSimpleDevice(ctx context.Context, daDevice da.De
80
86
}
81
87
}
82
88
83
- func (de * DeviceExporter ) ExportCapability (pctx context.Context , uncastCapability any ) any {
89
+ func (de * deviceExporter ) ExportCapability (pctx context.Context , uncastCapability any ) any {
84
90
ctx , cancel := context .WithTimeout (pctx , DefaultCapabilityTimeout )
85
91
defer cancel ()
86
92
@@ -134,52 +140,7 @@ func (de *DeviceExporter) ExportCapability(pctx context.Context, uncastCapabilit
134
140
return retVal
135
141
}
136
142
137
- type SettableUpdateTime interface {
138
- SetUpdateTime (time.Time )
139
- }
140
-
141
- type SettableChangeTime interface {
142
- SetChangeTime (time.Time )
143
- }
144
-
145
- type NullableTime time.Time
146
-
147
- func (n NullableTime ) MarshalJSON () ([]byte , error ) {
148
- under := time .Time (n )
149
-
150
- if under .IsZero () {
151
- return []byte ("null" ), nil
152
- } else {
153
- return json .Marshal (under )
154
- }
155
- }
156
-
157
- type LastUpdate struct {
158
- LastUpdate * NullableTime `json:",omitempty"`
159
- }
160
-
161
- func (lut * LastUpdate ) SetUpdateTime (t time.Time ) {
162
- nullableTime := NullableTime (t )
163
- lut .LastUpdate = & nullableTime
164
- }
165
-
166
- type LastChange struct {
167
- LastChange * NullableTime `json:",omitempty"`
168
- }
169
-
170
- func (lct * LastChange ) SetChangeTime (t time.Time ) {
171
- nullableTime := NullableTime (t )
172
- lct .LastChange = & nullableTime
173
- }
174
-
175
- type ProductInformation struct {
176
- Name string `json:",omitempty"`
177
- Manufacturer string `json:",omitempty"`
178
- Serial string `json:",omitempty"`
179
- Version string `json:",omitempty"`
180
- }
181
-
182
- func (de * DeviceExporter ) convertProductInformation (ctx context.Context , hpi capabilities.ProductInformation ) any {
143
+ func (de * deviceExporter ) convertProductInformation (ctx context.Context , hpi capabilities.ProductInformation ) any {
183
144
pi , err := hpi .Get (ctx )
184
145
if err != nil {
185
146
return nil
@@ -193,13 +154,7 @@ func (de *DeviceExporter) convertProductInformation(ctx context.Context, hpi cap
193
154
}
194
155
}
195
156
196
- type TemperatureSensor struct {
197
- Readings []capabilities.TemperatureReading
198
- LastUpdate
199
- LastChange
200
- }
201
-
202
- func (de * DeviceExporter ) convertTemperatureSensor (ctx context.Context , ts capabilities.TemperatureSensor ) any {
157
+ func (de * deviceExporter ) convertTemperatureSensor (ctx context.Context , ts capabilities.TemperatureSensor ) any {
203
158
tsReadings , err := ts .Reading (ctx )
204
159
if err != nil {
205
160
return nil
@@ -210,13 +165,7 @@ func (de *DeviceExporter) convertTemperatureSensor(ctx context.Context, ts capab
210
165
}
211
166
}
212
167
213
- type RelativeHumiditySensor struct {
214
- Readings []capabilities.RelativeHumidityReading
215
- LastUpdate
216
- LastChange
217
- }
218
-
219
- func (de * DeviceExporter ) convertRelativeHumiditySensor (ctx context.Context , rhs capabilities.RelativeHumiditySensor ) any {
168
+ func (de * deviceExporter ) convertRelativeHumiditySensor (ctx context.Context , rhs capabilities.RelativeHumiditySensor ) any {
220
169
rhReadings , err := rhs .Reading (ctx )
221
170
if err != nil {
222
171
return nil
@@ -227,13 +176,7 @@ func (de *DeviceExporter) convertRelativeHumiditySensor(ctx context.Context, rhs
227
176
}
228
177
}
229
178
230
- type PressureSensor struct {
231
- Readings []capabilities.PressureReading
232
- LastUpdate
233
- LastChange
234
- }
235
-
236
- func (de * DeviceExporter ) convertPressureSensor (ctx context.Context , ps capabilities.PressureSensor ) any {
179
+ func (de * deviceExporter ) convertPressureSensor (ctx context.Context , ps capabilities.PressureSensor ) any {
237
180
psReadings , err := ps .Reading (ctx )
238
181
if err != nil {
239
182
return nil
@@ -244,14 +187,7 @@ func (de *DeviceExporter) convertPressureSensor(ctx context.Context, ps capabili
244
187
}
245
188
}
246
189
247
- type DeviceDiscovery struct {
248
- Discovering bool
249
- Duration int `json:",omitempty"`
250
- LastUpdate
251
- LastChange
252
- }
253
-
254
- func (de * DeviceExporter ) convertDeviceDiscovery (ctx context.Context , dd capabilities.DeviceDiscovery ) any {
190
+ func (de * deviceExporter ) convertDeviceDiscovery (ctx context.Context , dd capabilities.DeviceDiscovery ) any {
255
191
discoveryState , err := dd .Status (ctx )
256
192
if err != nil {
257
193
return nil
@@ -265,19 +201,7 @@ func (de *DeviceExporter) convertDeviceDiscovery(ctx context.Context, dd capabil
265
201
}
266
202
}
267
203
268
- type EnumerateDeviceCapability struct {
269
- Attached bool
270
- Errors []string
271
- }
272
-
273
- type EnumerateDevice struct {
274
- Enumerating bool
275
- Status map [string ]EnumerateDeviceCapability
276
- LastUpdate
277
- LastChange
278
- }
279
-
280
- func (de * DeviceExporter ) convertEnumerateDevice (ctx context.Context , ed capabilities.EnumerateDevice ) any {
204
+ func (de * deviceExporter ) convertEnumerateDevice (ctx context.Context , ed capabilities.EnumerateDevice ) any {
281
205
enumerateDeviceState , err := ed .Status (ctx )
282
206
if err != nil {
283
207
return nil
@@ -304,13 +228,7 @@ func (de *DeviceExporter) convertEnumerateDevice(ctx context.Context, ed capabil
304
228
}
305
229
}
306
230
307
- type AlarmSensor struct {
308
- Alarms map [string ]bool
309
- LastUpdate
310
- LastChange
311
- }
312
-
313
- func (de * DeviceExporter ) convertAlarmSensor (ctx context.Context , as capabilities.AlarmSensor ) any {
231
+ func (de * deviceExporter ) convertAlarmSensor (ctx context.Context , as capabilities.AlarmSensor ) any {
314
232
alarmSensorState , err := as .Status (ctx )
315
233
if err != nil {
316
234
return nil
@@ -327,13 +245,7 @@ func (de *DeviceExporter) convertAlarmSensor(ctx context.Context, as capabilitie
327
245
}
328
246
}
329
247
330
- type OnOff struct {
331
- State bool
332
- LastUpdate
333
- LastChange
334
- }
335
-
336
- func (de * DeviceExporter ) convertOnOff (ctx context.Context , oo capabilities.OnOff ) any {
248
+ func (de * deviceExporter ) convertOnOff (ctx context.Context , oo capabilities.OnOff ) any {
337
249
state , err := oo .Status (ctx )
338
250
if err != nil {
339
251
return nil
@@ -344,28 +256,7 @@ func (de *DeviceExporter) convertOnOff(ctx context.Context, oo capabilities.OnOf
344
256
}
345
257
}
346
258
347
- type PowerStatus struct {
348
- Mains []PowerMainsStatus `json:",omitempty"`
349
- Battery []PowerBatteryStatus `json:",omitempty"`
350
- LastUpdate
351
- LastChange
352
- }
353
-
354
- type PowerMainsStatus struct {
355
- Voltage * float64 `json:",omitempty"`
356
- Frequency * float64 `json:",omitempty"`
357
- Available * bool `json:",omitempty"`
358
- }
359
-
360
- type PowerBatteryStatus struct {
361
- Voltage * float64 `json:",omitempty"`
362
- MaximumVoltage * float64 `json:",omitempty"`
363
- MinimumVoltage * float64 `json:",omitempty"`
364
- Remaining * float64 `json:",omitempty"`
365
- Available * bool `json:",omitempty"`
366
- }
367
-
368
- func (de * DeviceExporter ) convertPowerSupply (ctx context.Context , capability capabilities.PowerSupply ) any {
259
+ func (de * deviceExporter ) convertPowerSupply (ctx context.Context , capability capabilities.PowerSupply ) any {
369
260
state , err := capability .Status (ctx )
370
261
if err != nil {
371
262
return nil
@@ -424,17 +315,7 @@ func (de *DeviceExporter) convertPowerSupply(ctx context.Context, capability cap
424
315
}
425
316
}
426
317
427
- type AlarmWarningDeviceStatus struct {
428
- Warning bool
429
- AlarmType * string `json:",omitempty"`
430
- Volume * float64 `json:",omitempty"`
431
- Visual * bool `json:",omitempty"`
432
- Duration * int `json:",omitempty"`
433
- LastUpdate
434
- LastChange
435
- }
436
-
437
- func (de * DeviceExporter ) convertAlarmWarningDevice (ctx context.Context , capability capabilities.AlarmWarningDevice ) any {
318
+ func (de * deviceExporter ) convertAlarmWarningDevice (ctx context.Context , capability capabilities.AlarmWarningDevice ) any {
438
319
state , err := capability .Status (ctx )
439
320
if err != nil {
440
321
return nil
@@ -457,15 +338,7 @@ func (de *DeviceExporter) convertAlarmWarningDevice(ctx context.Context, capabil
457
338
return status
458
339
}
459
340
460
- type IdentifyStatus struct {
461
- Identifying bool
462
- Duration * int `json:",omitempty"`
463
-
464
- LastUpdate
465
- LastChange
466
- }
467
-
468
- func (de * DeviceExporter ) convertIdentify (ctx context.Context , capability capabilities.Identify ) any {
341
+ func (de * deviceExporter ) convertIdentify (ctx context.Context , capability capabilities.Identify ) any {
469
342
state , err := capability .Status (ctx )
470
343
if err != nil {
471
344
return nil
@@ -483,11 +356,7 @@ func (de *DeviceExporter) convertIdentify(ctx context.Context, capability capabi
483
356
return status
484
357
}
485
358
486
- type DeviceWorkaroundsStatus struct {
487
- Enabled []string
488
- }
489
-
490
- func (de * DeviceExporter ) convertDeviceWorkarounds (ctx context.Context , capability capabilities.DeviceWorkarounds ) any {
359
+ func (de * deviceExporter ) convertDeviceWorkarounds (ctx context.Context , capability capabilities.DeviceWorkarounds ) any {
491
360
state , err := capability .Enabled (ctx )
492
361
if err != nil {
493
362
return nil
@@ -499,3 +368,9 @@ func (de *DeviceExporter) convertDeviceWorkarounds(ctx context.Context, capabili
499
368
500
369
return status
501
370
}
371
+
372
+ type DeviceExporter interface {
373
+ ExportDevice (context.Context , da.Device ) ExportedDevice
374
+ ExportSimpleDevice (context.Context , da.Device ) ExportedSimpleDevice
375
+ ExportCapability (context.Context , any ) any
376
+ }
0 commit comments