Skip to content

Commit

Permalink
allow to discover mutliple devices on the same endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jkralik committed Jan 23, 2024
1 parent 95d8182 commit a358bb0
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions client/core/getDevices.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,32 @@ func (h *discoveryHandler) Handle(ctx context.Context, conn *client.Conn, links
if errC := conn.Close(); errC != nil {
h.handler.Error(fmt.Errorf("discovery handler cannot close connection: %w", errC))
}
link, err := GetResourceLink(links, device.ResourceURI)
if err != nil {
h.handler.Error(err)
return
}
deviceID := link.GetDeviceID()
if deviceID == "" {
h.handler.Error(fmt.Errorf("cannot determine deviceID"))
return
}
if len(link.ResourceTypes) == 0 {
h.handler.Error(fmt.Errorf("cannot get resource types for %v: is empty", deviceID))
return
deviceLinks := make(map[string]schema.ResourceLinks)
for _, link := range links {
deviceID := link.GetDeviceID()
if deviceID == "" {
h.handler.Error(fmt.Errorf("cannot determine deviceID"))
continue
}
deviceLinks[deviceID] = append(deviceLinks[deviceID], link)
}
_, loaded := h.filterDiscoveredDevices.LoadOrStore(deviceID, true)
if loaded {
return
for deviceID, links := range deviceLinks {
link, err := GetResourceLink(links, device.ResourceURI)
if err != nil {
h.handler.Error(err)
continue
}
if len(link.ResourceTypes) == 0 {
h.handler.Error(fmt.Errorf("cannot get resource types for %v: is empty", deviceID))
return
}
_, loaded := h.filterDiscoveredDevices.LoadOrStore(deviceID, true)
if loaded {
return
}
d := NewDevice(h.deviceCfg, deviceID, link.ResourceTypes, link.GetEndpoints)
h.handler.Handle(ctx, d)
}
d := NewDevice(h.deviceCfg, deviceID, link.ResourceTypes, link.GetEndpoints)
h.handler.Handle(ctx, d)
}

func (h *discoveryHandler) Error(err error) {
Expand Down

0 comments on commit a358bb0

Please sign in to comment.