Skip to content

Commit

Permalink
Filter unique MAC
Browse files Browse the repository at this point in the history
  • Loading branch information
atrovato committed Jan 10, 2024
1 parent 5026939 commit 25ca87f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
10 changes: 9 additions & 1 deletion server/services/lan-manager/lib/lan-manager.scan.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ async function scan() {
let deviceChanged = false;
if (nbDevices > 0) {
deviceChanged = this.discoveredDevices.length !== nbDevices;
this.discoveredDevices = discoveredDevices;
// Filter unique MAC
const filteredDevices = {};
discoveredDevices.forEach((device) => {
const { mac } = device;
if (mac && mac.length > 0) {
filteredDevices[mac] = device;
}
});
this.discoveredDevices = Object.values(filteredDevices);
}

this.scanning = false;
Expand Down
42 changes: 42 additions & 0 deletions server/test/services/lan-manager/lib/lan-manager.scan.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,48 @@ describe('LANManager scan', () => {

expect(manager.scanning).to.equal(false);
expect(result).deep.equal([{ device: '2', mac: 'mac' }]);
expect(manager.discoveredDevices).deep.equal([{ device: '2', mac: 'mac' }]);

assert.calledWithNew(NmapScan);
assert.calledTwice(gladys.event.emit);
assert.calledWithExactly(gladys.event.emit, EVENTS.WEBSOCKET.SEND_ALL, {
type: WEBSOCKET_MESSAGE_TYPES.LAN.SCANNING,
payload: {
scanning: true,
configured: true,
},
});
assert.calledWithExactly(gladys.event.emit, EVENTS.WEBSOCKET.SEND_ALL, {
type: WEBSOCKET_MESSAGE_TYPES.LAN.SCANNING,
payload: {
scanning: false,
configured: true,
deviceChanged: true,
success: true,
},
});
});

it('scan devices dupe found', async () => {
manager.configured = true;
manager.ipMasks = [
{ mask: '255.255.255.248/29', enabled: true },
{ mask: '192.168.0.1/10', enabled: false },
];

NmapScan.prototype.on.onCall(1).yieldsRight([
{ device: '1', mac: 'mac' },
{ device: '2', mac: 'mac' },
]);

const result = await manager.scan();

expect(manager.scanning).to.equal(false);
expect(result).deep.equal([
{ device: '1', mac: 'mac' },
{ device: '2', mac: 'mac' },
]);
expect(manager.discoveredDevices).deep.equal([{ device: '2', mac: 'mac' }]);

assert.calledWithNew(NmapScan);
assert.calledTwice(gladys.event.emit);
Expand Down

0 comments on commit 25ca87f

Please sign in to comment.