4 Commits
Author SHA1 Message Date
gisle bf903c1d71 fix crash in hue2 driver.
continuous-integration/drone/push Build is passing
2023-08-31 21:19:53 +02:00
gisle 19eb205ee3 try again.
continuous-integration/drone/push Build is passing
2023-08-31 20:37:54 +02:00
gisle edb45c74b6 Merge branch 'asmodeus' of git.aiterp.net:lucifer/new-server into asmodeus
continuous-integration/drone/push Build is passing
2023-08-31 20:25:59 +02:00
gisle c4b391db66 add forgetting devices to hue2 (untested yolo-deploy). 2023-08-31 20:25:26 +02:00
3 changed files with 88 additions and 0 deletions
+18
View File
@@ -252,6 +252,9 @@ func (b *Bridge) MakeCongruent(ctx context.Context) (int, error) {
updates := make(map[string]ResourceUpdate)
for _, device := range b.devices {
resource := b.resources[device.InternalID]
if resource == nil {
continue
}
// Update device
if resource.Metadata.Name != device.Name {
@@ -619,3 +622,18 @@ func (b *Bridge) SearchDevices(ctx context.Context, timeout time.Duration) ([]mo
// Return said devices.
return newDevices, nil
}
func (b *Bridge) Forget(ctx context.Context, device models.Device) error {
b.mu.Lock()
resource := b.resources[device.InternalID]
b.mu.Unlock()
if resource == nil {
return errors.New("resource not found")
}
return b.client.DeleteResource(ctx, ResourceLink{
ID: device.InternalID,
Kind: "device",
})
}
+66
View File
@@ -92,6 +92,10 @@ func (c *Client) UpdateResource(ctx context.Context, link ResourceLink, update R
return c.put(ctx, link.Path(), update, nil)
}
func (c *Client) DeleteResource(ctx context.Context, link ResourceLink) error {
return c.delete(ctx, link.Path(), nil)
}
func (c *Client) LegacyDiscover(ctx context.Context, kind string) error {
return c.legacyPost(ctx, kind, nil, nil)
}
@@ -268,6 +272,36 @@ func (c *Client) post(ctx context.Context, path string, body interface{}, target
return json.NewDecoder(res.Body).Decode(&target)
}
func (c *Client) delete(ctx context.Context, path string, target interface{}) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-c.ch:
defer func() {
c.ch <- struct{}{}
}()
}
req, err := http.NewRequest("PUT", fmt.Sprintf("https://%s/%s", c.host, path), nil)
if err != nil {
return err
}
req.Header.Set("hue-application-key", c.token)
res, err := httpClient.Do(req.WithContext(ctx))
if err != nil {
return err
}
defer res.Body.Close()
if target == nil {
return nil
}
return json.NewDecoder(res.Body).Decode(&target)
}
func (c *Client) legacyPost(ctx context.Context, resource string, body interface{}, target interface{}) error {
select {
case <-ctx.Done():
@@ -305,6 +339,38 @@ func (c *Client) legacyPost(ctx context.Context, resource string, body interface
return json.NewDecoder(res.Body).Decode(target)
}
func (c *Client) legacyDelete(ctx context.Context, resource string, target interface{}) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-c.ch:
defer func() {
c.ch <- struct{}{}
}()
}
if c.token != "" {
resource = c.token + "/" + resource
}
req, err := http.NewRequest("DELETE", fmt.Sprintf("http://%s/api/%s", c.host, resource), nil)
if err != nil {
return err
}
res, err := httpClient.Do(req.WithContext(ctx))
if err != nil {
return err
}
defer res.Body.Close()
if target == nil {
return nil
}
return json.NewDecoder(res.Body).Decode(target)
}
func reqBody(body interface{}) (io.Reader, error) {
if body == nil {
return nil, nil
+4
View File
@@ -100,6 +100,10 @@ func (d *Driver) Run(ctx context.Context, bridge models.Bridge, ch chan<- models
return d.ensureBridge(bridge).Run(ctx, ch)
}
func (d *Driver) ForgetDevice(ctx context.Context, bridge models.Bridge, device models.Device) error {
return d.ensureBridge(bridge).Forget(ctx, device)
}
func (d *Driver) ensureBridge(info models.Bridge) *Bridge {
d.mu.Lock()
for _, bridge := range d.bridges {