Skip to content

Commit

Permalink
LAN Manager remove duplicate (#1792)
Browse files Browse the repository at this point in the history
  • Loading branch information
atrovato authored Jan 11, 2024
1 parent 5a10629 commit 87d2858
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 3 deletions.
15 changes: 12 additions & 3 deletions 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 All @@ -45,10 +53,11 @@ async function scan() {

const result = await new Promise((resolve) => {
const onError = (err) => {
if (err !== 'Scan cancelled') {
const cancelled = err === 'Scan cancelled';
if (!cancelled) {
logger.error('LANManager fails to discover devices over network -', err);
}
scanDone([], false);
scanDone([], cancelled);
return resolve([]);
};

Expand Down
76 changes: 76 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 Expand Up @@ -101,6 +143,40 @@ describe('LANManager scan', () => {
});
});

it('scan devices cancel', 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(0).yieldsRight('Scan cancelled');

const result = await manager.scan();

expect(manager.scanning).to.equal(false);
expect(result).deep.equal([]);

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: false,
success: true,
},
});
});

it('already scanning', async () => {
manager.scanning = true;

Expand Down

0 comments on commit 87d2858

Please sign in to comment.