UNPKG

domotz-remote-pawn

Version:

Domotz Agent

118 lines (108 loc) 4.76 kB
/** This file is part of Domotz Agent. * Copyright (C) 2017 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 Massimiliano Cuzzoli <massi@domotz.com> on 30/10/17. * */ const LOG_PREFIX = 'LUXUL_JAM_DEVICES - '; const API_VERSION = 2; var luxulUbusManager = require('./luxulUbusManager'); var jamStateManager = require('./jamStateManager'); var jamDriver = function (engineRequest, path, q, log) { 'use strict'; var myConsole = log.decorateLogs(); var blockingFeatureAvailable = null; var stateManager = jamStateManager(); var ubusManager = luxulUbusManager(q); function handleServerResponse(data) { ubusManager .getSessionToken() .then(function (token) { ubusManager .getBlockedDevices(token) .then(function (ubusResponse) { if (ubusResponse && ubusResponse[1] && ubusResponse[1].blocked) { var blockedDevices = ubusResponse[1].blocked; myConsole.debug('%s Already Blocked MAC #%d', LOG_PREFIX, blockedDevices.length); myConsole.debug('%s MAC list to block : #%d', LOG_PREFIX, data.length); return stateManager.update(blockedDevices, data); } else { throw new Error('Unable to retrieve blocked mac list ' + JSON.stringify(ubusResponse)); } }) .then(function (isActionRequired) { myConsole.debug('%s is action required : %s', LOG_PREFIX, isActionRequired); if (isActionRequired) { return ubusManager .unblockDeviceList(stateManager.getMacAddressesToUnblock()) .then(ubusManager.blockDeviceList(stateManager.getMacAddressesToBlock())); } }) .catch(function (error) { myConsole.error('%s Error on device jamming : %s', LOG_PREFIX, JSON.stringify(error)); }); }) .catch(function () { myConsole.error('Unable to get session token'); }); } function updateMacAddresses() { myConsole.info(LOG_PREFIX + 'Updating mac addresses list'); engineRequest .sendWithPromise('/device/jammed', 'GET') .then(function (data) { handleServerResponse(data); }) .catch(function (error) { myConsole.error('%s Cannot update list, assuming no device to jam, %s', LOG_PREFIX, JSON.stringify(error)); if (error.status.toString()[0] === '4') { myConsole.error('%s Client error %s, set jammed list as empty', LOG_PREFIX, error.status); handleServerResponse([]); } else { myConsole.error('%s Server error %s, leave jammed list untouched', LOG_PREFIX, error.status); } }); } return { updateMacAddresses: function () { if (blockingFeatureAvailable === true) { updateMacAddresses(); return; } if (blockingFeatureAvailable === null) { try { blockingFeatureAvailable = true; updateMacAddresses(); } catch (error) { blockingFeatureAvailable = false; } } }, killDeviceJammer: function () { return ubusManager.unblockAllDevices().catch(function (error) { myConsole.error('%s Error on unblock all devices : %s', LOG_PREFIX, error); }); }, isBlockingFeatureAvailable: function () { return blockingFeatureAvailable; }, luxulApiVersion: function () { return API_VERSION; }, }; }; module.exports.jamDriver = jamDriver; // For testing purposes only