From c3cc65618b77078eb8f42dabdbf9796137c9357b Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Sun, 21 Aug 2022 16:19:24 +0200 Subject: [PATCH] misc --- cmd/bustest/main.go | 13 ++++++------- effects/gradient.go | 2 +- events/device.go | 8 ++++---- services/effectenforcer.go | 2 +- services/nanoleaf/client.go | 22 ++++++++++++---------- services/nanoleaf/service.go | 7 +++++-- 6 files changed, 29 insertions(+), 25 deletions(-) diff --git a/cmd/bustest/main.go b/cmd/bustest/main.go index ebd7ca8..4be119d 100644 --- a/cmd/bustest/main.go +++ b/cmd/bustest/main.go @@ -25,7 +25,7 @@ func main() { bus.JoinCallback(func(event lucifer3.Event) bool { switch event.(type) { - case events.DevicesReady: + case events.DeviceReady: bus.RunCommand(commands.AddAlias{Match: "nanoleaf:10.80.1.14:2d0c", Alias: "lucifer:name:Hex 5"}) bus.RunCommand(commands.AddAlias{Match: "nanoleaf:10.80.1.14:542f", Alias: "lucifer:name:Hex 6"}) bus.RunCommand(commands.AddAlias{Match: "nanoleaf:10.80.1.14:e760", Alias: "lucifer:name:Hex 7"}) @@ -39,7 +39,7 @@ func main() { Match: "nanoleaf:10.80.1.14:*", Effect: effects.Pattern{ States: []device.State{ - {Power: p(true), Intensity: p(0.3), Color: p(color.MustParse("xy:0.22,0.18"))}, + {Power: p(true), Intensity: p(1.0), Color: p(color.MustParse("xy:0.22,0.18"))}, {Power: p(true), Intensity: p(0.4), Color: p(color.MustParse("xy:0.22,0.18"))}, {Power: p(true), Intensity: p(0.5), Color: p(color.MustParse("xy:0.22,0.18"))}, }, @@ -47,16 +47,15 @@ func main() { }, }) - time.Sleep(time.Second * 3) + time.Sleep(time.Second * 1) bus.RunCommand(commands.AddAlias{Match: "nanoleaf:10.80.1.14:*", Alias: "lucifer:tag:Magic Lamps"}) bus.RunCommand(commands.Assign{ Match: "lucifer:tag:Magic Lamps", - Effect: effects.Gradient{ + Effect: effects.Pattern{ States: []device.State{ - {Power: p(true), Intensity: p(0.5), Color: p(color.MustParse("xy:0.22,0.18"))}, - {Power: p(true), Intensity: p(0.8), Color: p(color.MustParse("xy:0.22,0.18"))}, + {Power: p(true), Intensity: p(1.0), Color: p(color.MustParse("hs:0,0.9"))}, + {Power: p(true), Intensity: p(0.7), Color: p(color.MustParse("hs:100,0.8"))}, }, - Interpolate: true, AnimationMS: 500, }, }) diff --git a/effects/gradient.go b/effects/gradient.go index 40c8a50..2f5106e 100644 --- a/effects/gradient.go +++ b/effects/gradient.go @@ -21,7 +21,7 @@ func (e Gradient) State(index, length, round int) device.State { if e.Reverse { round = -round } - walkedIndex := ((length + index) - round) % length + walkedIndex := ((length + index) - (round % length)) % length return gradientState(e.States, e.Interpolate, walkedIndex, length) } diff --git a/events/device.go b/events/device.go index 9d8f582..010ca50 100644 --- a/events/device.go +++ b/events/device.go @@ -52,13 +52,13 @@ func (e HardwareMetadata) EventDescription() string { return fmt.Sprintf("HardwareMetadata(id:%s, icon:%s, ...)", e.ID, e.Icon) } -// DevicesReady is triggered to indicate that all hardware states have been pushed to the bus ahead of this event. -type DevicesReady struct { +// DeviceReady is triggered to indicate that all hardware states have been pushed to the bus ahead of this event. +type DeviceReady struct { ID string `json:"id"` } -func (d DevicesReady) EventDescription() string { - return fmt.Sprintf("DevicesReady(id:%s)", d.ID) +func (d DeviceReady) EventDescription() string { + return fmt.Sprintf("DeviceReady(id:%s)", d.ID) } type DevicesUnreachable struct { diff --git a/services/effectenforcer.go b/services/effectenforcer.go index 69a9ac6..7e58048 100644 --- a/services/effectenforcer.go +++ b/services/effectenforcer.go @@ -76,7 +76,7 @@ func (s *effectEnforcer) runLoop(bus *lucifer3.EventBus) { deleteList := make([]int, 0, 8) batch := make(commands.SetStateBatch, 64) - for now := range time.NewTicker(time.Millisecond * 100).C { + for now := range time.NewTicker(time.Millisecond * 25).C { s.mu.Lock() for i, run := range s.list { diff --git a/services/nanoleaf/client.go b/services/nanoleaf/client.go index 02fa467..a895f6a 100644 --- a/services/nanoleaf/client.go +++ b/services/nanoleaf/client.go @@ -79,18 +79,16 @@ func (b *bridge) HardwareEvents() []lucifer3.Event { }) } - return append(results, events.DevicesReady{ - ID: fmt.Sprintf("nanoleaf:%s", b.host), - }) + return results } -func (b *bridge) Refresh(ctx context.Context) (bool, error) { +func (b *bridge) Refresh(ctx context.Context) ([]string, error) { overview, err := b.Overview(ctx) if err != nil { - return false, err + return []string{}, err } - changed := false + newIDs := make([]string, 0, 0) b.mu.Lock() PanelLoop: @@ -110,13 +108,13 @@ PanelLoop: } } - changed = true - idBytes := [2]byte{0, 0} binary.BigEndian.PutUint16(idBytes[:], panelInfo.PanelID) hexID := hex.EncodeToString(idBytes[:]) fullID := fmt.Sprintf("nanoleaf:%s:%s", b.host, hexID) + newIDs = append(newIDs, fullID) + b.panelIDMap[panelInfo.PanelID] = fullID b.panels = append(b.panels, &panel{ ID: panelInfo.PanelID, @@ -133,7 +131,7 @@ PanelLoop: } b.mu.Unlock() - return changed, nil + return newIDs, nil } func (b *bridge) Overview(ctx context.Context) (*Overview, error) { @@ -213,7 +211,7 @@ func (b *bridge) Run(ctx context.Context, bus *lucifer3.EventBus) error { Prefix: fmt.Sprintf("nanoleaf:%s", b.host), }) - _, err = b.Refresh(ctx) + newIDs, err := b.Refresh(ctx) if err != nil { return err } @@ -261,6 +259,10 @@ func (b *bridge) Run(ctx context.Context, bus *lucifer3.EventBus) error { for _, event := range b.HardwareEvents() { bus.RunEvent(event) } + for _, id := range newIDs { + bus.RunEvent(events.DeviceReady{ID: id}) + } + bus.RunEvent(events.DeviceReady{ID: fmt.Sprintf("nanoleaf:%s", b.host)}) for range ticker.C { if ctx.Err() != nil { diff --git a/services/nanoleaf/service.go b/services/nanoleaf/service.go index 128ab17..ce80492 100644 --- a/services/nanoleaf/service.go +++ b/services/nanoleaf/service.go @@ -44,7 +44,7 @@ func (s *service) HandleCommand(bus *lucifer3.EventBus, command lucifer3.Command if sub, ok := command.Matches("nanoleaf"); ok { if s.bridges[sub] != nil { go func(bridge *bridge) { - changed, err := bridge.Refresh(context.Background()) + newIDs, err := bridge.Refresh(context.Background()) if err != nil { bus.RunEvent(events.DeviceFailed{ ID: command.ID, @@ -52,10 +52,13 @@ func (s *service) HandleCommand(bus *lucifer3.EventBus, command lucifer3.Command }) return } - if changed { + if len(newIDs) > 0 { for _, event := range bridge.HardwareEvents() { bus.RunEvent(event) } + for _, id := range newIDs { + bus.RunEvent(events.DeviceReady{ID: id}) + } } }(s.bridges[sub]) }