UNPKG

domotz-remote-pawn

Version:

Domotz Agent

120 lines (105 loc) 4.2 kB
/** 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 getDevicesMaps() { var macToIp = {}; var ipToMac = {}; var ipToId = {}; var interfaceInfo = interfacesBindingStorage.getInterfaceInfo(); var knownDevices = devicesInfo.getDevicesCache(); if (knownDevices) { for (var mac in interfaceInfo) { if (interfaceInfo.hasOwnProperty(mac)) { macToIp[mac] = interfaceInfo[mac]; ipToMac[interfaceInfo[mac]] = mac; if (knownDevices[mac]) { ipToId[interfaceInfo[mac]] = knownDevices[mac].id; } else { console.warn("WARNING *** CANNOT FIND KNOWN DEVICE FOR MAC %s ", mac); } } } } return { macToIp: JSON.parse(JSON.stringify(macToIp)), ipToMac: JSON.parse(JSON.stringify(ipToMac)), ipToId: JSON.parse(JSON.stringify(ipToId)) }; } function getDeviceIdFromMac(mac) { "use strict"; var devicesMaps = getDevicesMaps(); var ip = devicesMaps.macToIp[mac] || devicesMaps.macToIp[mac.toLowerCase()]; if (!ip) { return null; } return devicesMaps.ipToId[ip]; } function getDeviceIpFromMac(mac) { "use strict"; var devicesMaps = getDevicesMaps(); return devicesMaps.macToIp[mac] || devicesMaps.macToIp[mac.toLowerCase()]; } 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, getDeviceIdFromMac: getDeviceIdFromMac, getDetailsFieldsForDevices: getDetailsFieldsForDevices, getDeviceIpFromMac: getDeviceIpFromMac }; }; module.exports.factory = factory;