fixed hue updating too often, and change logic for fetchbyreference to support multiple tags, ids or bridges.

This commit is contained in:
2021-09-20 22:09:41 +02:00
parent 56a0525b9d
commit 2f9bd3c007
4 changed files with 44 additions and 19 deletions
+20 -9
View File
@@ -8,20 +8,21 @@ import (
"git.aiterp.net/lucifer/new-server/models"
"golang.org/x/sync/errgroup"
"io"
"net"
"net"
"net/http"
"strings"
"sync"
"time"
"time"
)
type Bridge struct {
mu sync.Mutex
host string
token string
externalID int
lightStates []*hueLightState
sensorStates []*hueSensorState
mu sync.Mutex
host string
token string
externalID int
lightStates []*hueLightState
sensorStates []*hueSensorState
syncingPublish uint32
}
func (b *Bridge) Refresh(ctx context.Context) error {
@@ -115,10 +116,20 @@ func (b *Bridge) SyncStale(ctx context.Context) error {
}
for i, input := range inputs {
iCopy := i
index := indices[i]
inputCopy := input
eg.Go(func() error { return b.putLightState(ctx, index, inputCopy) })
eg.Go(func() error {
err := b.putLightState(ctx, index, inputCopy)
if err != nil {
return err
}
b.lightStates[iCopy].stale = false
return nil
})
}
return eg.Wait()
+8
View File
@@ -10,6 +10,7 @@ import (
"net/http"
"strconv"
"sync"
"sync/atomic"
"time"
)
@@ -309,6 +310,9 @@ func (d *Driver) Publish(ctx context.Context, bridge models.Bridge, devices []mo
}
b.mu.Unlock()
atomic.StoreUint32(&b.syncingPublish, 1)
defer atomic.StoreUint32(&b.syncingPublish, 0)
return b.SyncStale(ctx)
}
@@ -327,6 +331,10 @@ func (d *Driver) Run(ctx context.Context, bridge models.Bridge, ch chan<- models
for {
select {
case <-selectedTicker.C:
if atomic.LoadUint32(&b.syncingPublish) == 1 {
continue
}
if ticksUntilRefresh <= 0 {
err := b.Refresh(ctx)
if err != nil {