domotz-remote-pawn
Version:
Domotz Agent
79 lines (72 loc) • 2.79 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/>.
**/
/**
*
* Created by Tommaso Latini <tommaso@domotz.com> on 27/08/15.
*/
module.exports.factory = function (fileUtils, configuration, interfacesBindingStorage) {
function getDeviceToExcludeFilePath(discovery) {
var defaultPath = process.env.DOMOTZ_CONF_DIR + '/devices_to_exclude.conf';
return discovery && discovery.blacklist_file ? discovery.blacklist_file : defaultPath;
}
function loadBlacklistFromSettings() {
var settingsBlackList = [];
if (configuration.settings.blacklisted_devices) {
configuration.settings.blacklisted_devices.forEach(function (mac) {
settingsBlackList.push(mac.toUpperCase());
});
}
if (settingsBlackList.length === 0) {
return fileBlackList;
}
return settingsBlackList;
}
var filePath = getDeviceToExcludeFilePath(configuration.discovery),
content = fileUtils.readFile(filePath),
fileBlackList = [];
if (content) {
content
.toString('utf8')
.split('\n')
.forEach(function contentFilter(e) {
if (e) {
fileBlackList.push(e.toUpperCase());
}
});
}
if (fileBlackList.length) {
console.info('DISCOVERY - Devices to be excluded from monitoring: %s', fileBlackList.join(', '));
} else {
console.debug('DISCOVERY - No devices in blacklist');
}
return {
getDeviceToExclude: function () {
var blackList = loadBlacklistFromSettings();
var ipList = [];
for (var i = 0; i < blackList.length; i++) {
var mac = blackList[i];
var ips = interfacesBindingStorage.getIpsFromMac(mac);
ipList = ipList.concat(ips);
}
return ipList;
},
isBlackListedMac: function (mac) {
var blackList = loadBlacklistFromSettings();
return blackList.indexOf(mac.toUpperCase()) !== -1;
},
};
};