From a358bb0e8e2b9cc5776af4aebafb4bda087107bc Mon Sep 17 00:00:00 2001 From: Jozef Kralik Date: Tue, 23 Jan 2024 13:41:20 +0000 Subject: [PATCH] allow to discover mutliple devices on the same endpoint --- client/core/getDevices.go | 42 ++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/client/core/getDevices.go b/client/core/getDevices.go index 257302ea..c6a278ee 100644 --- a/client/core/getDevices.go +++ b/client/core/getDevices.go @@ -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) {