refactorings.

This commit is contained in:
2022-08-21 11:17:42 +02:00
parent ae68679228
commit b303a623f6
14 changed files with 166 additions and 122 deletions
+16 -32
View File
@@ -8,6 +8,7 @@ import (
"encoding/json"
"fmt"
lucifer3 "git.aiterp.net/lucifer3/server"
"git.aiterp.net/lucifer3/server/commands"
"git.aiterp.net/lucifer3/server/device"
"git.aiterp.net/lucifer3/server/events"
"git.aiterp.net/lucifer3/server/internal/color"
@@ -168,45 +169,28 @@ func (b *bridge) URL(resource ...string) string {
}
func (b *bridge) Update(id string, change device.State) {
b.mu.Lock()
defer b.mu.Unlock()
transitionTime := time.Now().Add(time.Millisecond * 255)
b.mu.Lock()
for _, panel := range b.panels {
if panel.FullID == id {
if change.Intensity != nil {
panel.Intensity = *change.Intensity
}
if change.Color != nil {
rgbColor, ok := change.Color.ToRGB()
if !ok {
newColor := [4]byte{255, 255, 255, 255}
if newColor != panel.ColorRGBA {
panel.update(newColor, transitionTime)
}
continue
}
rgb := rgbColor.RGB.AtIntensity(panel.Intensity)
red := byte(rgb.Red * 255.0001)
green := byte(rgb.Green * 255.0001)
blue := byte(rgb.Blue * 255.0001)
newColor := [4]byte{red, green, blue, 255}
if newColor != panel.ColorRGBA {
panel.update(newColor, time.Now().Add(time.Millisecond*220))
}
}
if change.Power != nil {
panel.On = *change.Power
}
panel.apply(change, transitionTime)
break
}
}
b.mu.Unlock()
}
func (b *bridge) UpdateBatch(batch commands.SetStateBatch) {
transitionTime := time.Now().Add(time.Millisecond * 255)
b.mu.Lock()
for _, panel := range b.panels {
if change, ok := batch[panel.FullID]; ok {
panel.apply(change, transitionTime)
}
}
b.mu.Unlock()
}
func (b *bridge) Run(ctx context.Context, bus *lucifer3.EventBus) error {
+37
View File
@@ -2,6 +2,7 @@ package nanoleaf
import (
"encoding/binary"
"git.aiterp.net/lucifer3/server/device"
"time"
)
@@ -208,6 +209,42 @@ func (p *panel) update(colorRGBA [4]byte, transitionAt time.Time) {
p.TransitionAt = transitionAt
}
func (p *panel) apply(change device.State, transitionTime time.Time) {
if change.Power != nil {
p.On = *change.Power
}
if change.Intensity != nil {
p.Intensity = *change.Intensity
}
if change.Color != nil {
if !p.On {
newColor := [4]byte{0, 0, 0, 0}
if newColor != p.ColorRGBA {
p.update(newColor, transitionTime)
}
}
rgbColor, ok := change.Color.ToRGB()
if !ok {
newColor := [4]byte{255, 255, 255, 255}
if newColor != p.ColorRGBA {
p.update(newColor, transitionTime)
}
}
rgb := rgbColor.RGB.AtIntensity(p.Intensity)
red := byte(rgb.Red * 255.0001)
green := byte(rgb.Green * 255.0001)
blue := byte(rgb.Blue * 255.0001)
newColor := [4]byte{red, green, blue, 255}
if newColor != p.ColorRGBA {
p.update(newColor, transitionTime)
}
}
}
type panelUpdate []byte
func (u *panelUpdate) Add(message [8]byte) {
+5
View File
@@ -35,6 +35,11 @@ func (s *service) HandleCommand(bus *lucifer3.EventBus, command lucifer3.Command
s.bridges[sub].Update(command.ID, command.State)
}
case commands.SetStateBatch:
for _, b := range s.bridges {
b.UpdateBatch(command)
}
case commands.SearchDevices:
if sub, ok := command.Matches("nanoleaf"); ok {
if s.bridges[sub] != nil {