Browse Source

device ready handling.

beelzebub
Gisle Aune 2 years ago
parent
commit
d1f42d932b
  1. 25
      events/device.go
  2. 14
      services/effectenforcer.go

25
events/device.go

@ -29,11 +29,12 @@ type HardwareState struct {
ColorFlags device.ColorFlag `json:"colorFlags"` ColorFlags device.ColorFlag `json:"colorFlags"`
Buttons []string `json:"buttons"` Buttons []string `json:"buttons"`
State device.State `json:"state"` State device.State `json:"state"`
Unreachable bool `json:"unreachable"`
} }
func (e HardwareState) EventDescription() string { 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) 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 { type DeviceReady struct {
ID string `json:"id"` ID string `json:"id"`
} }
@ -61,14 +63,6 @@ func (d DeviceReady) EventDescription() string {
return fmt.Sprintf("DeviceReady(id:%s)", d.ID) 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 { type DeviceFailed struct {
ID string `json:"id"` ID string `json:"id"`
Error string `json:"error"` 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) 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 { type DeviceAccepted struct {
ID string `json:"id"` ID string `json:"id"`
APIKey string `json:"apiKey"` APIKey string `json:"apiKey"`

14
services/effectenforcer.go

@ -4,6 +4,7 @@ import (
lucifer3 "git.aiterp.net/lucifer3/server" lucifer3 "git.aiterp.net/lucifer3/server"
"git.aiterp.net/lucifer3/server/commands" "git.aiterp.net/lucifer3/server/commands"
"git.aiterp.net/lucifer3/server/device" "git.aiterp.net/lucifer3/server/device"
"git.aiterp.net/lucifer3/server/events"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
@ -35,7 +36,18 @@ func (s *effectEnforcer) Active() bool {
return true 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) { func (s *effectEnforcer) HandleCommand(bus *lucifer3.EventBus, command lucifer3.Command) {
switch command := command.(type) { switch command := command.(type) {

Loading…
Cancel
Save