Skip to content

Commit

Permalink
Rework device grouping to use endpoints as the specification intended.
Browse files Browse the repository at this point in the history
  • Loading branch information
pwood committed May 7, 2024
1 parent 9aefa8f commit d9df708
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
2 changes: 1 addition & 1 deletion device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 13 additions & 22 deletions enumerate_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
23 changes: 14 additions & 9 deletions enumerate_device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
Expand Down Expand Up @@ -346,7 +351,7 @@ func Test_enumerateDevice_updateNodeTable(t *testing.T) {

mdm.On("createNextDevice", n).Return(d)

expectedDeviceId := uint16(0x2000)
expectedDeviceId := 0x2000

id := []inventoryDevice{
{
Expand All @@ -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}
Expand All @@ -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}
Expand All @@ -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)
Expand Down

0 comments on commit d9df708

Please sign in to comment.