domotz-remote-pawn
Version:
Domotz Agent
100 lines (86 loc) • 3.45 kB
JavaScript
/** This file is part of Domotz Agent.
* Copyright (C) 2016 Domotz Ltd
*
* Domotz Agent is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Domotz Agent is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>.
**/
var factory = function (interfacesBindingStorage, devicesInfo) {
function _deepCopy(object) {
'use strict';
// Use only with base types arrays and objects - this method transforms Date(s) in strings, loses types for complex objects
return JSON.parse(JSON.stringify(object));
}
function getDevicesMaps() {
var macToIp = {};
var ipToMac = {};
return {
macToIp: _deepCopy(macToIp),
ipToMac: _deepCopy(ipToMac),
};
}
function getDeviceIpFromMac(mac) {
'use strict';
var devicesMaps = getDevicesMaps();
return devicesMaps.macToIp[mac] || devicesMaps.macToIp[mac.toLowerCase()];
}
function getDeviceMacFromIp(ip) {
'use strict';
var devicesMaps = getDevicesMaps();
return devicesMaps.ipToMac[ip];
}
function getKnownIpAddresses() {
var interfaceInfo = interfacesBindingStorage.getInterfaceInfo();
var knownDevices = devicesInfo.getDevicesCache();
var ipList = [];
if (knownDevices) {
for (var mac in interfaceInfo) {
if (interfaceInfo.hasOwnProperty(mac)) {
if (knownDevices[mac]) {
ipList.push(interfaceInfo[mac]);
} else {
console.warn('WARNING *** CANNOT FIND KNOWN DEVICE FOR MAC %s ', mac);
}
}
}
}
return ipList;
}
function getDetailsFieldsForDevices(fieldList) {
var interfaceInfo = interfacesBindingStorage.getInterfaceInfo();
var knownDevices = devicesInfo.getDevicesCache();
var deviceDetailsByIp = {};
if (knownDevices) {
for (var mac in interfaceInfo) {
if (interfaceInfo.hasOwnProperty(mac)) {
if (knownDevices[mac]) {
deviceDetailsByIp[interfaceInfo[mac]] = {};
for (var i = 0; i < fieldList.length; i++) {
deviceDetailsByIp[interfaceInfo[mac]][fieldList[i]] = knownDevices[mac].details[fieldList[i]];
}
} else {
console.warn('WARNING *** CANNOT FIND KNOWN DEVICE FOR MAC %s ', mac);
}
}
}
}
return deviceDetailsByIp;
}
return {
getDevicesMaps: getDevicesMaps,
getKnownIpAddresses: getKnownIpAddresses,
getDetailsFieldsForDevices: getDetailsFieldsForDevices,
getDeviceIpFromMac: getDeviceIpFromMac,
getDeviceMacFromIp: getDeviceMacFromIp,
};
};
module.exports.factory = factory;