getDeviceIP: refactor

This commit is contained in:
Martín M
2020-10-22 02:59:00 -03:00
parent f81fd771a6
commit 979664dc34
2 changed files with 11 additions and 2 deletions

View File

@@ -11,6 +11,7 @@ const errors = {
const customErrors = {
ch404: 'Device channel does not exist',
unknown: 'An unknown error occurred',
noARP: 'No ARP information found. You need to generate the ARP file.',
noDevice: 'No device found',
noDevices: 'No devices found',
noPower: 'No power usage data found',

View File

@@ -1,3 +1,5 @@
const errors = require('../data/errors');
module.exports = {
/**
* Get local IP address from a given MAC
@@ -6,10 +8,16 @@ module.exports = {
* @returns {Promise<string>}
*/
getDeviceIP(device) {
const mac = device.extra.extra.staMac;
if (!this.arpTable) {
throw new Error(errors.noARP);
}
const mac = device.extra.staMac;
const arpItem = this.arpTable.find(
item => item.mac.toLowerCase() === mac.toLowerCase()
(item) => item.mac.toLowerCase() === mac.toLowerCase()
);
return arpItem.ip;
},
};