add sensor handling.

This commit is contained in:
2023-01-08 16:43:08 +01:00
parent 8c10b15dd8
commit 956bd970a0
12 changed files with 357 additions and 59 deletions
+14
View File
@@ -47,6 +47,14 @@ func (d *Data) WithPatch(patches ...Patch) Data {
if patch.Device.ClearActiveColorRGB {
pd.ActiveColorRGB = nil
}
if patch.Device.Sensors != nil {
if patch.Device.Sensors.LastMotion != nil {
pd.Sensors.LastMotion = gentools.ShallowCopy(patch.Device.Sensors.LastMotion)
}
if patch.Device.Sensors.Temperature != nil {
pd.Sensors.Temperature = gentools.ShallowCopy(patch.Device.Sensors.Temperature)
}
}
if patch.Device.Delete {
delete(newData.Devices, pd.ID)
@@ -115,6 +123,12 @@ type Device struct {
DesiredState *device.State `json:"desiredState"`
Aliases []string `json:"aliases"`
Assignment *uuid.UUID `json:"assignment"`
Sensors DeviceSensors `json:"sensors"`
}
type DeviceSensors struct {
LastMotion *float64 `json:"lastMotion,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
}
type Assignment struct {
+22 -4
View File
@@ -14,13 +14,30 @@ type Patch struct {
Device *DevicePatch `json:"device,omitempty"`
}
func (e Patch) VerboseKey() string {
return "uistate.Patch"
}
func (e Patch) EventDescription() string {
if e.Device != nil {
switch {
case e.Device.Name != nil:
return fmt.Sprintf("uistate.Patch(device=%s, name=%s)", e.Device.ID, e.Device.Name)
case e.Device.DesiredState != nil:
return fmt.Sprintf("uistate.Patch(device=%s, desired state)", e.Device.ID)
return fmt.Sprintf("uistate.Patch(device=%s, desiredState=%s)", e.Device.ID, e.Device.DesiredState.String())
case e.Device.HWState != nil:
return fmt.Sprintf("uistate.Patch(device=%s, hwState)", e.Device.ID)
case e.Device.HWMetadata != nil:
return fmt.Sprintf("uistate.Patch(device=%s, hwMetadata)", e.Device.ID)
case e.Device.AddAlias != nil:
return fmt.Sprintf("uistate.Patch(device=%s, addAlias=%s)", e.Device.ID, *e.Device.AddAlias)
case e.Device.RemoveAlias != nil:
return fmt.Sprintf("uistate.Patch(device=%s, removeAlias=%s)", e.Device.ID, *e.Device.RemoveAlias)
case e.Device.ActiveColorRGB != nil:
col := color.Color{RGB: e.Device.ActiveColorRGB}
return fmt.Sprintf("uistate.Patch(device=%s, activeColorRgb=%s)", e.Device.ID, col.String())
default:
return fmt.Sprintf("uistate.Patch(device=%s)", e.Device.ID)
return fmt.Sprintf("uistate.Patch(device=%s, ...other)", e.Device.ID)
}
} else if e.Assignment != nil {
return fmt.Sprintf("uistate.Patch(assignment=%s)", e.Assignment.ID)
@@ -40,8 +57,9 @@ type DevicePatch struct {
RemoveAlias *string `json:"removeAlias,omitempty"`
Assignment *uuid.UUID `json:"assignment,omitempty"`
ClearAssignment bool `json:"clearAssignment,omitempty"`
ActiveColorRGB *color.RGB `json:"activeColorRgb"`
ClearActiveColorRGB bool `json:"clearActiveColorRGB"`
ActiveColorRGB *color.RGB `json:"activeColorRgb,omitempty"`
ClearActiveColorRGB bool `json:"clearActiveColorRGB,omitempty"`
Sensors *DeviceSensors `json:"sensors,omitempty"`
Delete bool `json:"delete,omitempty"`
}
+10
View File
@@ -80,6 +80,16 @@ func (s *service) HandleEvent(bus *lucifer3.EventBus, event lucifer3.Event) {
ID: event.ID,
Delete: true,
}}}
case events.MotionSensed:
patches = []Patch{{Device: &DevicePatch{
ID: event.ID,
Sensors: &DeviceSensors{LastMotion: gentools.Ptr(event.SecondsSince)},
}}}
case events.TemperatureChanged:
patches = []Patch{{Device: &DevicePatch{
ID: event.ID,
Sensors: &DeviceSensors{Temperature: gentools.Ptr(event.Temperature)},
}}}
case events.DeviceAssigned:
// Un-assign from current assignment (if any)
if d, ok := s.data.Devices[event.DeviceID]; ok && d.Assignment != nil {