refactorings.
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
lucifer3 "git.aiterp.net/lucifer3/server"
|
||||
"git.aiterp.net/lucifer3/server/commands"
|
||||
"git.aiterp.net/lucifer3/server/device"
|
||||
"math/rand"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
@@ -58,7 +57,7 @@ func (s *effectEnforcer) HandleCommand(bus *lucifer3.EventBus, command lucifer3.
|
||||
}
|
||||
s.list = append(s.list, newRun)
|
||||
|
||||
// Remove the ids from any old run.
|
||||
// Switch over the indices.
|
||||
for _, id := range allowedIDs {
|
||||
if oldRun := s.index[id]; oldRun != nil {
|
||||
oldRun.remove(id)
|
||||
@@ -75,11 +74,12 @@ func (s *effectEnforcer) HandleCommand(bus *lucifer3.EventBus, command lucifer3.
|
||||
|
||||
func (s *effectEnforcer) runLoop(bus *lucifer3.EventBus) {
|
||||
deleteList := make([]int, 0, 8)
|
||||
commandQueue := make([]commands.SetState, 0, 32)
|
||||
batch := make(commands.SetStateBatch, 64)
|
||||
|
||||
for now := range time.NewTicker(time.Millisecond * 100).C {
|
||||
s.mu.Lock()
|
||||
for i, run := range s.list {
|
||||
|
||||
if run.dead {
|
||||
deleteList = append(deleteList, i-len(deleteList))
|
||||
continue
|
||||
@@ -88,17 +88,13 @@ func (s *effectEnforcer) runLoop(bus *lucifer3.EventBus) {
|
||||
continue
|
||||
}
|
||||
|
||||
r := rand.Int()
|
||||
for j, id := range run.ids {
|
||||
if id == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
state := run.effect.State(j, len(run.ids), run.round, r)
|
||||
commandQueue = append(commandQueue, commands.SetState{
|
||||
ID: id,
|
||||
State: state,
|
||||
})
|
||||
state := run.effect.State(j, len(run.ids), run.round)
|
||||
batch[id] = state
|
||||
}
|
||||
|
||||
if freq := run.effect.Frequency(); freq > 0 {
|
||||
@@ -123,12 +119,9 @@ func (s *effectEnforcer) runLoop(bus *lucifer3.EventBus) {
|
||||
}
|
||||
s.mu.Unlock()
|
||||
|
||||
if len(commandQueue) > 0 {
|
||||
for _, command := range commandQueue {
|
||||
bus.RunCommand(command)
|
||||
}
|
||||
|
||||
commandQueue = commandQueue[:0]
|
||||
if len(batch) > 0 {
|
||||
bus.RunCommand(batch)
|
||||
batch = make(commands.SetStateBatch, 64)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+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