|
|
@ -222,17 +222,31 @@ func (b *Bridge) Update(devices ...models.Device) { |
|
|
|
} |
|
|
|
|
|
|
|
func (b *Bridge) MakeCongruent(ctx context.Context) (int, error) { |
|
|
|
// Eat the event if there's a pending update.
|
|
|
|
select { |
|
|
|
case <-b.needsUpdate: |
|
|
|
default: |
|
|
|
// Eat all event if there's a pending update.
|
|
|
|
exhausted := false |
|
|
|
for !exhausted { |
|
|
|
select { |
|
|
|
case <-b.needsUpdate: |
|
|
|
default: |
|
|
|
exhausted = true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
b.mu.Lock() |
|
|
|
dur := time.Millisecond * 200 |
|
|
|
dur := time.Millisecond * 100 |
|
|
|
updates := make(map[string]ResourceUpdate) |
|
|
|
for _, device := range b.devices { |
|
|
|
resource := b.resources[device.InternalID] |
|
|
|
|
|
|
|
// Update device
|
|
|
|
if resource.Metadata.Name != device.Name { |
|
|
|
name := device.Name |
|
|
|
updates["device/"+resource.ID] = ResourceUpdate{ |
|
|
|
Name: &name, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Update light
|
|
|
|
if lightID := resource.ServiceID("light"); lightID != nil { |
|
|
|
light := b.resources[*lightID] |
|
|
|
update := ResourceUpdate{TransitionDuration: &dur} |
|
|
@ -286,13 +300,14 @@ func (b *Bridge) MakeCongruent(ctx context.Context) (int, error) { |
|
|
|
return 0, nil |
|
|
|
} |
|
|
|
|
|
|
|
log.Println(fmt.Sprintf("[Bridge %d]", b.externalID), "Updating", len(updates), "services...") |
|
|
|
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] |
|
|
|
split := strings.SplitN(key, "/", 2) |
|
|
|
link := ResourceLink{Kind: split[0], ID: split[1]} |
|
|
|
|
|
|
|
eg.Go(func() error { |
|
|
|
return b.client.UpdateResource(ctx, link, update) |
|
|
|
}) |
|
|
@ -334,6 +349,7 @@ func (b *Bridge) GenerateDevices() []models.Device { |
|
|
|
} |
|
|
|
|
|
|
|
device := models.Device{ |
|
|
|
BridgeID: b.externalID, |
|
|
|
InternalID: resource.ID, |
|
|
|
Name: resource.Metadata.Name, |
|
|
|
DriverProperties: map[string]interface{}{ |
|
|
@ -530,6 +546,7 @@ func (b *Bridge) applyPatches(patches []ResourceData) { |
|
|
|
if patch.Status != nil { |
|
|
|
resCopy.Status = patch.Status |
|
|
|
} |
|
|
|
resCopy.Metadata.Name = patch.Metadata.Name |
|
|
|
|
|
|
|
newResources[patch.ID] = &resCopy |
|
|
|
} |
|
|
|