websocket backend + frontend patching.

This commit is contained in:
2023-08-30 22:56:58 +02:00
parent e8b77ae69c
commit 64bb737569
17 changed files with 664 additions and 361 deletions
+11 -9
View File
@@ -29,6 +29,7 @@ func (d *Data) WithPatch(patches ...Patch) Data {
gentools.ApplyUpdatePtr(&pd.DesiredState, patch.Device.DesiredState)
gentools.ApplyUpdatePtr(&pd.Assignment, patch.Device.Assignment)
gentools.ApplyUpdatePtr(&pd.ActiveColorRGB, patch.Device.ActiveColorRGB)
gentools.ApplyUpdatePtr(&pd.DesiredColorRGB, patch.Device.DesiredColorRGB)
if patch.Device.AddAlias != nil {
pd.Aliases = append(pd.Aliases[:0:0], pd.Aliases...)
@@ -125,15 +126,16 @@ 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"`
ActiveColorRGB *color.RGB `json:"activeColorRgb"`
DesiredState *device.State `json:"desiredState"`
Aliases []string `json:"aliases"`
Assignment *uuid.UUID `json:"assignment"`
Sensors DeviceSensors `json:"sensors"`
ID string `json:"id"`
Name string `json:"name"`
HWMetadata *events.HardwareMetadata `json:"hwMetadata"`
HWState *events.HardwareState `json:"hwState"`
DesiredColorRGB *color.RGB `json:"desiredColorRgb"`
ActiveColorRGB *color.RGB `json:"activeColorRgb"`
DesiredState *device.State `json:"desiredState"`
Aliases []string `json:"aliases"`
Assignment *uuid.UUID `json:"assignment"`
Sensors DeviceSensors `json:"sensors"`
}
type DeviceSensors struct {
+4
View File
@@ -38,6 +38,9 @@ func (e Patch) EventDescription() string {
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())
case e.Device.DesiredColorRGB != nil:
col := color.Color{RGB: e.Device.DesiredColorRGB}
return fmt.Sprintf("uistate.Patch(device=%s, desiredColorRgb=%s)", e.Device.ID, col.String())
default:
return fmt.Sprintf("uistate.Patch(device=%s, ...other)", e.Device.ID)
}
@@ -61,6 +64,7 @@ type DevicePatch struct {
RemoveAlias *string `json:"removeAlias,omitempty"`
Assignment *uuid.UUID `json:"assignment,omitempty"`
ClearAssignment bool `json:"clearAssignment,omitempty"`
DesiredColorRGB *color.RGB `json:"desiredColorRgb,omitempty"`
ActiveColorRGB *color.RGB `json:"activeColorRgb,omitempty"`
ClearActiveColorRGB bool `json:"clearActiveColorRGB,omitempty"`
Sensors *DeviceSensors `json:"sensors,omitempty"`
+28 -6
View File
@@ -5,6 +5,7 @@ import (
"git.aiterp.net/lucifer3/server/commands"
"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"
"git.aiterp.net/lucifer3/server/services/script"
"github.com/google/uuid"
@@ -36,11 +37,29 @@ func (s *service) HandleCommand(bus *lucifer3.EventBus, command lucifer3.Command
switch command := command.(type) {
case commands.SetState:
patches = []Patch{{Device: &DevicePatch{ID: command.ID, DesiredState: &command.State}}}
rgb := color.Color{}
if command.State.Color != nil {
rgb, _ = command.State.Color.ToRGB()
}
patches = []Patch{{Device: &DevicePatch{
ID: command.ID,
DesiredState: &command.State,
DesiredColorRGB: rgb.RGB,
}}}
case commands.SetStateBatch:
for id, state := range command {
rgb := color.Color{}
if state.Color != nil {
rgb, _ = state.Color.ToRGB()
}
patches = append(patches, Patch{
Device: &DevicePatch{ID: id, DesiredState: gentools.ShallowCopy(&state)},
Device: &DevicePatch{
ID: id,
DesiredState: gentools.ShallowCopy(&state),
DesiredColorRGB: rgb.RGB,
},
})
}
case script.Update:
@@ -129,10 +148,13 @@ func (s *service) HandleEvent(bus *lucifer3.EventBus, event lucifer3.Event) {
}})
case events.AssignmentVariables:
// Always set the assignment
patches = append(patches, Patch{Assignment: &AssignmentPatch{
ID: event.ID,
Variables: event.Map,
}})
if len(event.Map) > 0 {
patches = append(patches, Patch{Assignment: &AssignmentPatch{
ID: event.ID,
Variables: event.Map,
}})
}
}
if len(patches) > 0 {