add color conversion in parse and rgb to ui state.

This commit is contained in:
2023-01-01 19:19:02 +01:00
parent fe5bfc1efc
commit cfb1727a89
6 changed files with 92 additions and 22 deletions
+13 -7
View File
@@ -4,6 +4,7 @@ import (
"git.aiterp.net/lucifer3/server/device"
"git.aiterp.net/lucifer3/server/effects"
"git.aiterp.net/lucifer3/server/events"
"git.aiterp.net/lucifer3/server/internal/color"
"git.aiterp.net/lucifer3/server/internal/gentools"
"github.com/google/uuid"
)
@@ -25,6 +26,7 @@ func (d *Data) WithPatch(patches ...Patch) Data {
gentools.ApplyUpdatePtr(&pd.HWMetadata, patch.Device.HWMetadata)
gentools.ApplyUpdatePtr(&pd.DesiredState, patch.Device.DesiredState)
gentools.ApplyUpdatePtr(&pd.Assignment, patch.Device.Assignment)
gentools.ApplyUpdatePtr(&pd.ActiveColorRGB, patch.Device.ActiveColorRGB)
if patch.Device.AddAlias != nil {
pd.Aliases = append(pd.Aliases[:0:0], pd.Aliases...)
@@ -42,6 +44,9 @@ func (d *Data) WithPatch(patches ...Patch) Data {
if patch.Device.ClearAssignment {
pd.Assignment = nil
}
if patch.Device.ClearActiveColorRGB {
pd.ActiveColorRGB = nil
}
if patch.Device.Delete {
delete(newData.Devices, pd.ID)
@@ -102,13 +107,14 @@ func (d *Data) ensureAssignment(id uuid.UUID) Assignment {
}
type Device struct {
ID string `json:"id"`
Name string `json:"name"`
HWMetadata *events.HardwareMetadata `json:"hwMetadata"`
HWState *events.HardwareState `json:"hwState"`
DesiredState *device.State `json:"desiredState"`
Aliases []string `json:"aliases"`
Assignment *uuid.UUID `json:"assignment"`
ID string `json:"id"`
Name string `json:"name"`
HWMetadata *events.HardwareMetadata `json:"hwMetadata"`
HWState *events.HardwareState `json:"hwState"`
ActiveColorRGB *color.RGB `json:"activeColorRgb"`
DesiredState *device.State `json:"desiredState"`
Aliases []string `json:"aliases"`
Assignment *uuid.UUID `json:"assignment"`
}
type Assignment struct {
+14 -11
View File
@@ -5,6 +5,7 @@ import (
"git.aiterp.net/lucifer3/server/device"
"git.aiterp.net/lucifer3/server/effects"
"git.aiterp.net/lucifer3/server/events"
"git.aiterp.net/lucifer3/server/internal/color"
"github.com/google/uuid"
)
@@ -29,17 +30,19 @@ func (e Patch) EventDescription() string {
}
type DevicePatch struct {
ID string `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
HWMetadata *events.HardwareMetadata `json:"hwMetadata,omitempty"`
HWState *events.HardwareState `json:"hwState,omitempty"`
DesiredState *device.State `json:"desiredState,omitempty"`
SetAliases []string `json:"setAliases,omitempty"`
AddAlias *string `json:"addAlias,omitempty"`
RemoveAlias *string `json:"removeAlias,omitempty"`
Assignment *uuid.UUID `json:"assignment,omitempty"`
ClearAssignment bool `json:"clearAssignment,omitempty"`
Delete bool `json:"delete,omitempty"`
ID string `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
HWMetadata *events.HardwareMetadata `json:"hwMetadata,omitempty"`
HWState *events.HardwareState `json:"hwState,omitempty"`
DesiredState *device.State `json:"desiredState,omitempty"`
SetAliases []string `json:"setAliases,omitempty"`
AddAlias *string `json:"addAlias,omitempty"`
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"`
Delete bool `json:"delete,omitempty"`
}
type AssignmentPatch struct {
+12
View File
@@ -56,6 +56,18 @@ func (s *service) HandleEvent(bus *lucifer3.EventBus, event lucifer3.Event) {
patches = []Patch{{Device: &DevicePatch{ID: event.ID, RemoveAlias: &event.Alias}}}
case events.HardwareState:
patches = []Patch{{Device: &DevicePatch{ID: event.ID, HWState: &event}}}
if event.State.Color != nil {
rgb, _ := event.State.Color.ToRGB()
patches = append(patches, Patch{Device: &DevicePatch{
ID: event.ID,
ActiveColorRGB: gentools.Ptr(rgb.RGB.Round()),
}})
} else {
patches = append(patches, Patch{Device: &DevicePatch{
ID: event.ID,
ClearActiveColorRGB: false,
}})
}
case events.HardwareMetadata:
patches = []Patch{{Device: &DevicePatch{ID: event.ID, HWMetadata: &event}}}
case events.AssignmentCreated: