diff --git a/internal/drivers/hue2/bridge.go b/internal/drivers/hue2/bridge.go index aada532..f9e6606 100644 --- a/internal/drivers/hue2/bridge.go +++ b/internal/drivers/hue2/bridge.go @@ -55,6 +55,7 @@ func (b *Bridge) Run(ctx context.Context, eventCh chan<- models.Event) error { absences := make(map[int]time.Time) lastPress := make(map[string]time.Time) + lastUpdate := time.Now() needFull := false for { @@ -62,6 +63,10 @@ func (b *Bridge) Run(ctx context.Context, eventCh chan<- models.Event) error { case <-ctx.Done(): return ctx.Err() case <-lightRefreshTimer.C: + if time.Since(lastUpdate) < time.Second*10 { + continue + } + if needFull { err := b.RefreshAll(ctx) if err != nil { @@ -79,9 +84,16 @@ func (b *Bridge) Run(ctx context.Context, eventCh chan<- models.Event) error { return err } if updated > 0 { + // Force this to cool for 30 seconds unless a manual update occurs. + if updated > 10 { + lastUpdate = time.Now().Add(time.Second * 15) + } + log.Println(fmt.Sprintf("[Bridge %d]", b.externalID), "Updated", updated, "hue services (regular check)") } case <-b.needsUpdate: + lastUpdate = time.Now() + updated, err := b.MakeCongruent(ctx) if err != nil { return err @@ -125,9 +137,11 @@ func (b *Bridge) Run(ctx context.Context, eventCh chan<- models.Event) error { continue } + b.mu.Lock() if b.resources[patch.Owner.ID] == nil { needFull = true } + b.mu.Unlock() device, deviceOK := b.devices[patch.Owner.ID] if !deviceOK || device.ID == 0 { @@ -141,7 +155,7 @@ func (b *Bridge) Run(ctx context.Context, eventCh chan<- models.Event) error { } else if patch.Button.LastEvent == "long_release" { valid = false } else { - valid = lastPress[patch.ID].Unix() != data.CreationTime.Unix() + valid = data.CreationTime.Sub(lastPress[patch.ID]) >= time.Second*2 } if valid {