Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(getDeviceIP): add check for mac address in device.params #204

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/classes/Zeroconf.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Zeroconf {
*/
static fixMacAddresses(hosts) {
return hosts.map(host => {
const octets = host.mac.split(':');
const octets = host.mac.split(/[:-]/);

const fixedMac = octets.map(octet => {
if (octet.length === 1) {
Expand Down
2 changes: 1 addition & 1 deletion src/mixins/getDeviceIP.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
* @returns {Promise<string>}
*/
getDeviceIP(device) {
const mac = device.extra.extra.staMac;
const mac = device?.extra?.extra?.staMac || device?.params?.staMac;
const arpItem = this.arpTable.find(
item => item.mac.toLowerCase() === mac.toLowerCase()
);
Expand Down
65 changes: 65 additions & 0 deletions test/device-control.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,68 @@ describe('device control using WebSockets: errors and exceptions', () => {
}
});
});

describe('device control using Zeroconf', () => {

test('getDeviceIP with mac address on device.params', () => {
const staMac = '3A:5C:E6:C9:8A:9E';
const ip = '192.101.1.102';
const device = aDevice().withParams({staMac}).device();
const arpEntry = anArpTableEntry().withIp(ip).withMac(staMac).entry();
const devicesCache = [device];
const arpTable = [arpEntry];

const conn = new ewelink({ email: 'someEmail', password: 'some password', devicesCache, arpTable });

expect(conn.getZeroconfUrl(device)).toBe(`http://${ip}:8081/zeroconf`);
});

test('getDeviceIP with mac address on device.extra.extra', () => {
const staMac = '3A:5C:E6:C9:8A:9E';
const ip = '192.101.1.102';
const device = aDevice().withExtra({staMac}).device();
const arpEntry = anArpTableEntry().withIp(ip).withMac(staMac).entry();
const devicesCache = [device];
const arpTable = [arpEntry];

const conn = new ewelink({ email: 'someEmail', password: 'some password', devicesCache, arpTable });

expect(conn.getZeroconfUrl(device)).toBe(`http://${ip}:8081/zeroconf`);
});
});

function anArpTableEntry() {
const entry = {};
return {
withIp: function(ip) {
entry.ip = ip;
return this;
},
withMac: function(mac) {
entry.mac = mac;
return this;
},
entry: () => entry
}
}

function aDevice() {
let device = {};
return {
withExtra: function(extra) {
device = {
...device,
extra: {
...device.extra,
extra
}
}
return this;
},
withParams: function(params) {
device.params = params;
return this;
},
device: () => device
}
}
9 changes: 9 additions & 0 deletions test/zeroconf.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ describe('zeroconf: save arp table to file', () => {
expect(arpTable.file).toBe(file);
});

test('fix mac address for address with -', async () => {
const someIp = `127.0.0.1`;
const macWithKebabCase = `01:02-03-0a-0b-0c`;

const fixedAddressed = Zeroconf.fixMacAddresses([{ip: someIp, mac: macWithKebabCase}]);

expect(fixedAddressed[0].mac).toEqual(`01:02:03:0a:0b:0c`);
});

test('error saving arp table file', async () => {
jest.setTimeout(30000);
const file = '/tmp/non-existent-folder/arp-table.json';
Expand Down