From d1f42d932b820446f3c061abf456278b4c2b9e58 Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Sun, 21 Aug 2022 16:28:22 +0200 Subject: [PATCH] device ready handling. --- events/device.go | 25 +++++-------------------- services/effectenforcer.go | 14 +++++++++++++- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/events/device.go b/events/device.go index 010ca50..fcd51ab 100644 --- a/events/device.go +++ b/events/device.go @@ -29,11 +29,12 @@ type HardwareState struct { ColorFlags device.ColorFlag `json:"colorFlags"` Buttons []string `json:"buttons"` State device.State `json:"state"` + Unreachable bool `json:"unreachable"` } func (e HardwareState) EventDescription() string { - return fmt.Sprintf("HardwareState(id:%s, iname:%#+v, sflags:%s, cflags:%s, buttons:%v, state:%s)", - e.ID, e.InternalName, e.SupportFlags, e.ColorFlags, e.Buttons, e.State, + return fmt.Sprintf("HardwareState(id:%s, iname:%#+v, sflags:%s, cflags:%s, buttons:%v, state:%s, unreachable:%t)", + e.ID, e.InternalName, e.SupportFlags, e.ColorFlags, e.Buttons, e.State, e.Unreachable, ) } @@ -52,7 +53,8 @@ func (e HardwareMetadata) EventDescription() string { return fmt.Sprintf("HardwareMetadata(id:%s, icon:%s, ...)", e.ID, e.Icon) } -// DeviceReady is triggered to indicate that all hardware states have been pushed to the bus ahead of this event. +// DeviceReady is triggered to indicate that set-state commands will be heeded for the device +// by this ID. It may be unavailable, however. type DeviceReady struct { ID string `json:"id"` } @@ -61,14 +63,6 @@ func (d DeviceReady) EventDescription() string { return fmt.Sprintf("DeviceReady(id:%s)", d.ID) } -type DevicesUnreachable struct { - ID string `json:"id"` -} - -func (d DevicesUnreachable) EventDescription() string { - return fmt.Sprintf("DevicesUnreachable(id:%s)", d.ID) -} - type DeviceFailed struct { ID string `json:"id"` Error string `json:"error"` @@ -78,15 +72,6 @@ func (e DeviceFailed) EventDescription() string { return fmt.Sprintf("DeviceFailed(id:%s, err:%s)", e.ID, e.Error) } -type DeviceAvailable struct { - ID string - Name string -} - -func (e DeviceAvailable) EventDescription() string { - return fmt.Sprintf("DeviceAvailable(id:%s, name:%s)") -} - type DeviceAccepted struct { ID string `json:"id"` APIKey string `json:"apiKey"` diff --git a/services/effectenforcer.go b/services/effectenforcer.go index 7e58048..9cc39cc 100644 --- a/services/effectenforcer.go +++ b/services/effectenforcer.go @@ -4,6 +4,7 @@ import ( lucifer3 "git.aiterp.net/lucifer3/server" "git.aiterp.net/lucifer3/server/commands" "git.aiterp.net/lucifer3/server/device" + "git.aiterp.net/lucifer3/server/events" "sync" "sync/atomic" "time" @@ -35,7 +36,18 @@ func (s *effectEnforcer) Active() bool { return true } -func (s *effectEnforcer) HandleEvent(_ *lucifer3.EventBus, _ lucifer3.Event) {} +func (s *effectEnforcer) HandleEvent(_ *lucifer3.EventBus, event lucifer3.Event) { + switch event := event.(type) { + case events.DeviceReady: + // If the device is managed by the effect enforcer, cause the effect to be + // re-ran. + s.mu.Lock() + if run, ok := s.index[event.ID]; ok && run.due.IsZero() { + run.due = time.Now() + } + s.mu.Unlock() + } +} func (s *effectEnforcer) HandleCommand(bus *lucifer3.EventBus, command lucifer3.Command) { switch command := command.(type) {