From a3695a3ddd1e3a417d888a69a8c7a472bf7b5b8b Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Tue, 28 Sep 2021 21:57:28 +0200 Subject: [PATCH] some more work. --- app/services/publisher/scene.go | 19 +++++++++++++++++++ internal/drivers/lifx/bridge.go | 4 +++- internal/drivers/lifx/state.go | 1 + 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/app/services/publisher/scene.go b/app/services/publisher/scene.go index 0b7a8ea..98082ce 100644 --- a/app/services/publisher/scene.go +++ b/app/services/publisher/scene.go @@ -17,10 +17,25 @@ type Scene struct { due bool } +// UpdateScene updates the scene data and re-seats all devices. func (s *Scene) UpdateScene(data models.Scene) { + devices := make([]models.Device, 0, 16) + for ri, list := range s.roleList { + for _, device := range list { + devices = append(devices, device) + s.roleMap[device.ID] = -1 + } + } + + s.roleList = map[int][]models.Device {-1: devices} + + for _, device := range append(devices[:0:0], devices...) { + s.UpsertDevice(device) + } } +// UpsertDevice moves the device if neccesary and updates its state. func (s *Scene) UpsertDevice(device models.Device) { if s.data == nil { s.roleMap[device.ID] = -1 @@ -56,6 +71,7 @@ func (s *Scene) UpsertDevice(device models.Device) { s.data.Roles[newIndex].ApplyOrder(s.roleList[newIndex]) } +// RemoveDevice finds and remove a device. It's a noop if the device does not exist in this scene. func (s *Scene) RemoveDevice(device models.Device) { roleIndex, hasRoleIndex := s.roleMap[device.ID] if !hasRoleIndex { @@ -68,8 +84,11 @@ func (s *Scene) RemoveDevice(device models.Device) { break } } + + delete(s.roleMap, device.ID) } +// Run runs the scene func (s *Scene) Run() []models.Device { if s.data == nil { return []models.Device{} diff --git a/internal/drivers/lifx/bridge.go b/internal/drivers/lifx/bridge.go index cee191e..410ecd1 100644 --- a/internal/drivers/lifx/bridge.go +++ b/internal/drivers/lifx/bridge.go @@ -204,7 +204,9 @@ func (b *Bridge) Run(ctx context.Context, debug bool) error { b.checkAndUpdateState(state) } - if time.Since(state.discoveredTime) > time.Second*10 { + if time.Since(state.discoveredTime) > time.Second*10 && time.Since(state.fwSpamTime) > time.Second * 30 { + state.fwSpamTime = time.Now() + if state.firmware == nil { _, _ = client.Send(state.target, &GetHostFirmware{}) } diff --git a/internal/drivers/lifx/state.go b/internal/drivers/lifx/state.go index 864c8c9..bb0fac1 100644 --- a/internal/drivers/lifx/state.go +++ b/internal/drivers/lifx/state.go @@ -18,6 +18,7 @@ type State struct { lightStateTime time.Time requestTime time.Time updateTime time.Time + fwSpamTime time.Time acksPending []uint8 }