From 979664dc344aa1454acedc9537c66492fe1c21cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marti=CC=81n=20M?= Date: Thu, 22 Oct 2020 02:59:00 -0300 Subject: [PATCH] getDeviceIP: refactor --- src/data/errors.js | 1 + src/mixins/getDeviceIP.js | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/data/errors.js b/src/data/errors.js index 32004be..52a045d 100644 --- a/src/data/errors.js +++ b/src/data/errors.js @@ -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', diff --git a/src/mixins/getDeviceIP.js b/src/mixins/getDeviceIP.js index db2e109..fdcea75 100644 --- a/src/mixins/getDeviceIP.js +++ b/src/mixins/getDeviceIP.js @@ -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} */ 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; }, };