diff --git a/device.go b/device.go index b4fb60d..4efe287 100644 --- a/device.go +++ b/device.go @@ -29,7 +29,7 @@ type device struct { n *node // Mutable data, obtain lock first. - deviceId uint16 + deviceId int deviceIdSet bool capabilities map[da.Capability]implcaps.ZDACapability diff --git a/enumerate_device.go b/enumerate_device.go index 3c1e65e..5b05c69 100644 --- a/enumerate_device.go +++ b/enumerate_device.go @@ -222,44 +222,35 @@ func (e enumerateDevice) runRules(inv inventory) (inventory, error) { } type inventoryDevice struct { - deviceId uint16 + deviceId int endpoints []endpointDetails } func (e enumerateDevice) groupInventoryDevices(inv inventory) []inventoryDevice { - devices := map[uint16]*inventoryDevice{} + devices := map[int]*inventoryDevice{} + var endpoints []int - for _, ep := range inv.endpoints { - invDev := devices[ep.description.DeviceID] - if invDev == nil { - invDev = &inventoryDevice{deviceId: ep.description.DeviceID} - devices[ep.description.DeviceID] = invDev - } - - invDev.endpoints = append(invDev.endpoints, ep) - - sort.Slice(invDev.endpoints, func(i, j int) bool { - return invDev.endpoints[i].description.Endpoint < invDev.endpoints[j].description.Endpoint - }) + for eid, ep := range inv.endpoints { + invDev := &inventoryDevice{deviceId: int(eid), endpoints: []endpointDetails{ep}} + devices[int(eid)] = invDev + endpoints = append(endpoints, int(eid)) } + sort.Ints(endpoints) + var outDevices []inventoryDevice - for _, invDev := range devices { - outDevices = append(outDevices, *invDev) + for _, ep := range endpoints { + outDevices = append(outDevices, *devices[ep]) } - sort.Slice(outDevices, func(i, j int) bool { - return outDevices[i].deviceId < outDevices[j].deviceId - }) - return outDevices } -func (e enumerateDevice) updateNodeTable(ctx context.Context, n *node, inventoryDevices []inventoryDevice) map[uint16]*device { +func (e enumerateDevice) updateNodeTable(ctx context.Context, n *node, inventoryDevices []inventoryDevice) map[int]*device { ctx, end := e.logger.Segment(ctx, "Updating node table.") defer end() - deviceIdMapping := map[uint16]*device{} + deviceIdMapping := map[int]*device{} var unsetDevice []*device = nil /* Look for devices that exist but don't have a deviceId. */ diff --git a/enumerate_device_test.go b/enumerate_device_test.go index 78c01a6..285764b 100644 --- a/enumerate_device_test.go +++ b/enumerate_device_test.go @@ -285,21 +285,26 @@ func Test_enumerateDevice_groupInventoryDevices(t *testing.T) { DeviceID: 1, }, }, + }, + }, + { + deviceId: 2, + endpoints: []endpointDetails{ { description: zigbee.EndpointDescription{ - Endpoint: 3, - DeviceID: 1, + Endpoint: 2, + DeviceID: 2, }, }, }, }, { - deviceId: 2, + deviceId: 3, endpoints: []endpointDetails{ { description: zigbee.EndpointDescription{ - Endpoint: 2, - DeviceID: 2, + Endpoint: 3, + DeviceID: 1, }, }, }, @@ -346,7 +351,7 @@ func Test_enumerateDevice_updateNodeTable(t *testing.T) { mdm.On("createNextDevice", n).Return(d) - expectedDeviceId := uint16(0x2000) + expectedDeviceId := 0x2000 id := []inventoryDevice{ { @@ -364,7 +369,7 @@ func Test_enumerateDevice_updateNodeTable(t *testing.T) { mdm := &mockDeviceManager{} defer mdm.AssertExpectations(t) - existingDeviceId := uint16(0x2000) + existingDeviceId := 0x2000 ed := enumerateDevice{logger: logwrap.New(discard.Discard()), dm: mdm} d := &device{m: &sync.RWMutex{}, deviceId: existingDeviceId, deviceIdSet: true} @@ -386,7 +391,7 @@ func Test_enumerateDevice_updateNodeTable(t *testing.T) { mdm := &mockDeviceManager{} defer mdm.AssertExpectations(t) - existingDeviceId := uint16(0x2000) + existingDeviceId := 0x2000 ed := enumerateDevice{logger: logwrap.New(discard.Discard()), dm: mdm} d := &device{m: &sync.RWMutex{}, deviceId: 0} @@ -408,7 +413,7 @@ func Test_enumerateDevice_updateNodeTable(t *testing.T) { mdm := &mockDeviceManager{} defer mdm.AssertExpectations(t) - unwantedDeviceId := uint16(0x2000) + unwantedDeviceId := 0x2000 address := IEEEAddressWithSubIdentifier{IEEEAddress: zigbee.GenerateLocalAdministeredIEEEAddress(), SubIdentifier: 0} mdm.On("removeDevice", address).Return(true)