refactorings.
This commit is contained in:
+16
-32
@@ -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 {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user