|
|
@ -45,7 +45,7 @@ func (b *Bridge) Run(ctx context.Context, eventCh chan<- models.Event) error { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
log.Println(fmt.Sprintf("[Bridge %d]", b.externalID), "Updated", updated, "hue services at startup.") |
|
|
|
log.Println(fmt.Sprintf("[Bridge %d]", b.externalID), "Updated", updated, "hue services (startup)") |
|
|
|
|
|
|
|
lightRefreshTimer := time.NewTicker(time.Second * 5) |
|
|
|
defer lightRefreshTimer.Stop() |
|
|
@ -222,7 +222,7 @@ func (b *Bridge) Update(devices ...models.Device) { |
|
|
|
} |
|
|
|
|
|
|
|
func (b *Bridge) MakeCongruent(ctx context.Context) (int, error) { |
|
|
|
// Eat all event if there's a pending update.
|
|
|
|
// Exhaust the channel to avoid more updates.
|
|
|
|
exhausted := false |
|
|
|
for !exhausted { |
|
|
|
select { |
|
|
@ -294,14 +294,26 @@ func (b *Bridge) MakeCongruent(ctx context.Context) (int, error) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if len(updates) > 0 { |
|
|
|
// Optimistically apply the updates to the states, so that the driver assumes they are set until
|
|
|
|
// proven otherwise by the SSE client.
|
|
|
|
newResources := make(map[string]*ResourceData, len(b.resources)) |
|
|
|
for key, value := range b.resources { |
|
|
|
newResources[key] = value |
|
|
|
} |
|
|
|
for key, update := range updates { |
|
|
|
id := strings.SplitN(key, "/", 2)[1] |
|
|
|
newResources[id] = newResources[id].WithUpdate(update) |
|
|
|
} |
|
|
|
b.resources = newResources |
|
|
|
} |
|
|
|
b.mu.Unlock() |
|
|
|
|
|
|
|
if len(updates) == 0 { |
|
|
|
return 0, nil |
|
|
|
} |
|
|
|
|
|
|
|
log.Println(fmt.Sprintf("[Bridge %d]", b.externalID), "Updating", len(updates), "hue services...") |
|
|
|
|
|
|
|
eg, ctx := errgroup.WithContext(ctx) |
|
|
|
for key := range updates { |
|
|
|
update := updates[key] |
|
|
@ -315,23 +327,10 @@ func (b *Bridge) MakeCongruent(ctx context.Context) (int, error) { |
|
|
|
|
|
|
|
err := eg.Wait() |
|
|
|
if err != nil { |
|
|
|
return len(updates), err |
|
|
|
} |
|
|
|
// Try to restore light states.
|
|
|
|
_ = b.Refresh(ctx, "light") |
|
|
|
|
|
|
|
if len(updates) > 0 { |
|
|
|
// Optimistically apply the updates to the states, so that the driver assumes they are set until
|
|
|
|
// proven otherwise by the SSE client.
|
|
|
|
b.mu.Lock() |
|
|
|
newResources := make(map[string]*ResourceData, len(b.resources)) |
|
|
|
for key, value := range b.resources { |
|
|
|
newResources[key] = value |
|
|
|
} |
|
|
|
for key, update := range updates { |
|
|
|
id := strings.SplitN(key, "/", 2)[1] |
|
|
|
newResources[id] = newResources[id].WithUpdate(update) |
|
|
|
} |
|
|
|
b.resources = newResources |
|
|
|
b.mu.Unlock() |
|
|
|
return len(updates), err |
|
|
|
} |
|
|
|
|
|
|
|
return len(updates), nil |
|
|
|