This commit is contained in:
2022-08-21 16:19:24 +02:00
parent b303a623f6
commit c3cc65618b
6 changed files with 29 additions and 25 deletions
+12 -10
View File
@@ -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 {
+5 -2
View File
@@ -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])
}