Skip to content

Commit 794f82c

Browse files
committed
Migrate event generation out from webhooks into its own converter.
1 parent 98b22b8 commit 794f82c

File tree

12 files changed

+653
-414
lines changed

12 files changed

+653
-414
lines changed

interface/converters/exporter/device_exporter.go

Lines changed: 29 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package exporter
22

33
import (
44
"context"
5-
"encoding/json"
65
"github.com/shimmeringbee/controller/state"
76
"github.com/shimmeringbee/da"
87
"github.com/shimmeringbee/da/capabilities"
@@ -31,12 +30,19 @@ type ExportedGateway struct {
3130

3231
const DefaultCapabilityTimeout = 1 * time.Second
3332

34-
type DeviceExporter struct {
33+
type deviceExporter struct {
3534
DeviceOrganiser *state.DeviceOrganiser
3635
GatewayMapper state.GatewayMapper
3736
}
3837

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 {
4046
capabilityList := map[string]any{}
4147

4248
for _, capFlag := range daDevice.Capabilities() {
@@ -58,7 +64,7 @@ func (de *DeviceExporter) ExportDevice(ctx context.Context, daDevice da.Device)
5864
}
5965
}
6066

61-
func (de *DeviceExporter) ExportSimpleDevice(ctx context.Context, daDevice da.Device) ExportedSimpleDevice {
67+
func (de *deviceExporter) ExportSimpleDevice(ctx context.Context, daDevice da.Device) ExportedSimpleDevice {
6268
capabilityList := []string{}
6369

6470
for _, capFlag := range daDevice.Capabilities() {
@@ -80,7 +86,7 @@ func (de *DeviceExporter) ExportSimpleDevice(ctx context.Context, daDevice da.De
8086
}
8187
}
8288

83-
func (de *DeviceExporter) ExportCapability(pctx context.Context, uncastCapability any) any {
89+
func (de *deviceExporter) ExportCapability(pctx context.Context, uncastCapability any) any {
8490
ctx, cancel := context.WithTimeout(pctx, DefaultCapabilityTimeout)
8591
defer cancel()
8692

@@ -134,52 +140,7 @@ func (de *DeviceExporter) ExportCapability(pctx context.Context, uncastCapabilit
134140
return retVal
135141
}
136142

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 {
183144
pi, err := hpi.Get(ctx)
184145
if err != nil {
185146
return nil
@@ -193,13 +154,7 @@ func (de *DeviceExporter) convertProductInformation(ctx context.Context, hpi cap
193154
}
194155
}
195156

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 {
203158
tsReadings, err := ts.Reading(ctx)
204159
if err != nil {
205160
return nil
@@ -210,13 +165,7 @@ func (de *DeviceExporter) convertTemperatureSensor(ctx context.Context, ts capab
210165
}
211166
}
212167

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 {
220169
rhReadings, err := rhs.Reading(ctx)
221170
if err != nil {
222171
return nil
@@ -227,13 +176,7 @@ func (de *DeviceExporter) convertRelativeHumiditySensor(ctx context.Context, rhs
227176
}
228177
}
229178

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 {
237180
psReadings, err := ps.Reading(ctx)
238181
if err != nil {
239182
return nil
@@ -244,14 +187,7 @@ func (de *DeviceExporter) convertPressureSensor(ctx context.Context, ps capabili
244187
}
245188
}
246189

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 {
255191
discoveryState, err := dd.Status(ctx)
256192
if err != nil {
257193
return nil
@@ -265,19 +201,7 @@ func (de *DeviceExporter) convertDeviceDiscovery(ctx context.Context, dd capabil
265201
}
266202
}
267203

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 {
281205
enumerateDeviceState, err := ed.Status(ctx)
282206
if err != nil {
283207
return nil
@@ -304,13 +228,7 @@ func (de *DeviceExporter) convertEnumerateDevice(ctx context.Context, ed capabil
304228
}
305229
}
306230

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 {
314232
alarmSensorState, err := as.Status(ctx)
315233
if err != nil {
316234
return nil
@@ -327,13 +245,7 @@ func (de *DeviceExporter) convertAlarmSensor(ctx context.Context, as capabilitie
327245
}
328246
}
329247

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 {
337249
state, err := oo.Status(ctx)
338250
if err != nil {
339251
return nil
@@ -344,28 +256,7 @@ func (de *DeviceExporter) convertOnOff(ctx context.Context, oo capabilities.OnOf
344256
}
345257
}
346258

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 {
369260
state, err := capability.Status(ctx)
370261
if err != nil {
371262
return nil
@@ -424,17 +315,7 @@ func (de *DeviceExporter) convertPowerSupply(ctx context.Context, capability cap
424315
}
425316
}
426317

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 {
438319
state, err := capability.Status(ctx)
439320
if err != nil {
440321
return nil
@@ -457,15 +338,7 @@ func (de *DeviceExporter) convertAlarmWarningDevice(ctx context.Context, capabil
457338
return status
458339
}
459340

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 {
469342
state, err := capability.Status(ctx)
470343
if err != nil {
471344
return nil
@@ -483,11 +356,7 @@ func (de *DeviceExporter) convertIdentify(ctx context.Context, capability capabi
483356
return status
484357
}
485358

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 {
491360
state, err := capability.Enabled(ctx)
492361
if err != nil {
493362
return nil
@@ -499,3 +368,9 @@ func (de *DeviceExporter) convertDeviceWorkarounds(ctx context.Context, capabili
499368

500369
return status
501370
}
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

Comments
 (0)