domotz-remote-pawn
Version:
Domotz Agent
82 lines (71 loc) • 2.9 kB
JavaScript
/** This file is part of Domotz Agent.
* Copyright (C) 2024 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 Andrea Azzara <a.azzara@domotz.com> on 18/01/2024.
*/
var lodash = require('lodash');
function netgearJamStateManager(log) {
var myConsole = log.decorateLogs();
var utils = require('./netgearUtils').utils();
var blockedMACAddresses = [];
return {
storeRules: function (macAddresses) {
blockedMACAddresses = macAddresses;
},
getBlockedMACAddresses: function () {
return blockedMACAddresses;
},
calculateDiff: function (currentlyBlockedDevices, devicesToBlock) {
/*
* devicesToBlock example
*
* {
* mac1: [ip1, ip2],
* mac2: [ip3, ip2],
* }
*
* */
myConsole.info('Calculating diff %s %s', JSON.stringify(currentlyBlockedDevices), JSON.stringify(devicesToBlock));
var lockedDeviceKeys = [];
var keysToBlock = [];
lodash.forOwn(devicesToBlock, function (ips, mac) {
ips.forEach(function (ip) {
var key = utils.generateFirewallKey(mac, ip);
lockedDeviceKeys.push(key);
if (currentlyBlockedDevices.indexOf(key) === -1) {
keysToBlock.push(key);
} else {
myConsole.debug('Device %s already blocked', key);
}
});
});
var newKeys = lodash.concat(lockedDeviceKeys, keysToBlock);
myConsole.debug(
'Locked devices %s, Keys to block %s, New keys %s',
JSON.stringify(lockedDeviceKeys),
JSON.stringify(keysToBlock),
JSON.stringify(newKeys)
);
var keysToUnblock = lodash.difference(currentlyBlockedDevices, newKeys);
myConsole.info('Devices to be unlocked %s', JSON.stringify(keysToUnblock));
return {
keysToUnblock: keysToUnblock,
keysToBlock: keysToBlock,
};
},
};
}
module.exports.netgearJamStateManager = netgearJamStateManager;