scripting system, hue fixes and such

This commit is contained in:
2023-08-29 00:06:57 +02:00
parent da8fd03e8a
commit 8776ed6df3
48 changed files with 1128 additions and 72 deletions
+3 -20
View File
@@ -7,7 +7,6 @@ import (
lucifer3 "git.aiterp.net/lucifer3/server"
"git.aiterp.net/lucifer3/server/device"
"git.aiterp.net/lucifer3/server/events"
"git.aiterp.net/lucifer3/server/internal/color"
"git.aiterp.net/lucifer3/server/internal/gentools"
"golang.org/x/sync/errgroup"
"log"
@@ -412,6 +411,9 @@ func (b *Bridge) Run(ctx context.Context, bus *lucifer3.EventBus) interface{} {
func (b *Bridge) makeCongruentLoop(ctx context.Context) {
for range b.triggerCongruenceCheckCh {
// I hate that I have to do this, but the SSE is not being reliable.
_, _ = b.RefreshAll()
if ctx.Err() != nil {
break
}
@@ -428,8 +430,6 @@ func (b *Bridge) makeCongruentLoop(ctx context.Context) {
colorFlags := b.colorFlags
b.mu.Unlock()
newActiveStates := make(map[string]device.State, 0)
updates := make(map[string]ResourceUpdate)
for id, desired := range desiredStates {
active, activeOK := activeStates[id]
@@ -452,7 +452,6 @@ func (b *Bridge) makeCongruentLoop(ctx context.Context) {
newActiveState := activeStates[id]
newActiveState.Power = gentools.Ptr(*desired.Power)
newActiveStates[id] = newActiveState
continue
}
@@ -463,7 +462,6 @@ func (b *Bridge) makeCongruentLoop(ctx context.Context) {
updated := false
update := ResourceUpdate{}
newActiveState := activeStates[id]
if active.Color != nil && desired.Color != nil {
ac := *active.Color
@@ -497,8 +495,6 @@ func (b *Bridge) makeCongruentLoop(ctx context.Context) {
acMirek = 1000000 / *ac.K
}
if acMirek != dcMirek {
newActiveState.Color = &color.Color{K: gentools.Ptr(*dc.K)}
update.Mirek = &dcMirek
updated = true
}
@@ -508,7 +504,6 @@ func (b *Bridge) makeCongruentLoop(ctx context.Context) {
if active.Intensity != nil && desired.Intensity != nil {
if math.Abs(*active.Intensity-*desired.Intensity) >= 0.01 {
update.Brightness = gentools.Ptr(*desired.Intensity * 100)
newActiveState.Intensity = gentools.Ptr(*desired.Intensity)
updated = true
}
}
@@ -516,7 +511,6 @@ func (b *Bridge) makeCongruentLoop(ctx context.Context) {
if updated {
update.TransitionDuration = gentools.Ptr(time.Millisecond * 101)
updates["light/"+*lightID] = update
newActiveStates[id] = newActiveState
}
}
@@ -539,17 +533,6 @@ func (b *Bridge) makeCongruentLoop(ctx context.Context) {
log.Println("Failed to run update", err)
}
b.mu.Lock()
activeStates = b.activeStates
b.mu.Unlock()
activeStates = gentools.CopyMap(activeStates)
for id, state := range newActiveStates {
activeStates[id] = state
}
b.mu.Lock()
b.activeStates = activeStates
b.mu.Unlock()
cancel()
// Wait the remaining time for the rate limit
+37
View File
@@ -205,6 +205,22 @@ func (res *ResourceData) GenerateEvent(hostname string, resources map[string]*Re
}
buttonCount := 0
switch res.ProductData.ProductArchetype {
case "candle_bulb":
hwMeta.Icon = "hue_lightbulb_e14"
case "sultan_bulb":
hwMeta.Icon = "hue_lightbulb_e27"
case "hue_signe":
hwMeta.Icon = "hue_signe"
case "unknown_archetype":
switch res.ProductData.ProductName {
case "Hue motion sensor":
hwMeta.Icon = "hue_motionsensor"
case "Hue dimmer switch":
hwMeta.Icon = "hue_dimmerswitch"
}
}
if res.ProductData != nil {
hwMeta.FirmwareVersion = res.ProductData.SoftwareVersion
}
@@ -365,6 +381,27 @@ type ResourceUpdate struct {
TransitionDuration *time.Duration
}
func (r ResourceUpdate) Split() []ResourceUpdate {
updates := make([]ResourceUpdate, 0, 2)
if r.Name != nil {
updates = append(updates, ResourceUpdate{Name: r.Name, TransitionDuration: r.TransitionDuration})
}
if r.Power != nil {
updates = append(updates, ResourceUpdate{Power: r.Power, TransitionDuration: r.TransitionDuration})
}
if r.ColorXY != nil {
updates = append(updates, ResourceUpdate{ColorXY: r.ColorXY, TransitionDuration: r.TransitionDuration})
}
if r.Brightness != nil {
updates = append(updates, ResourceUpdate{Brightness: r.Brightness, TransitionDuration: r.TransitionDuration})
}
if r.Mirek != nil {
updates = append(updates, ResourceUpdate{Mirek: r.Mirek, TransitionDuration: r.TransitionDuration})
}
return updates
}
func (r ResourceUpdate) MarshalJSON() ([]byte, error) {
chunks := make([]string, 0, 4)
if r.Name != nil {