domotz-remote-pawn
Version:
Domotz Agent
97 lines (89 loc) • 3.96 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 Iacopo Papalini <iacopo@domotz.com> on 08/09/16.
*
* You need fing installed on the device in order to enable the device
* network jamming
*/
const JAMMED_FILE_NAME = 'jammed.txt';
const FING_BIN_DEFAULT_PATH = '/usr/local/lib/fing/fing.bin';
var path = require('path');
var jamDevicesFileManager = require('./jamDevicesFileManager');
var fingProcessManager = require('./fingProcessManager');
var fingWrapper = function (childProcess, engineRequest, procManager, q, log) {
'use strict';
var myConsole = log.decorateLogs();
var env = process.env;
var macListFile = path.join(env.DOMOTZ_CONF_DIR, JAMMED_FILE_NAME);
var fingCommand = env.FING_BINARY_FULL_PATH || FING_BIN_DEFAULT_PATH;
var fileWriter = jamDevicesFileManager(macListFile, myConsole);
var processManager = fingProcessManager(fingCommand, macListFile, childProcess, procManager, q, myConsole);
function handleServerResponse(data) {
var shouldRun = fileWriter.write(data);
if (shouldRun && data.length > 0) {
processManager.assureFingIsRunning();
} else {
processManager.assureFingIsNotRunning();
}
}
function _updateMacAddresses() {
myConsole.info('Updating mac addresses list');
engineRequest
.sendWithPromise('/device/jammed', 'GET')
.then(function (data) {
handleServerResponse(data);
})
.catch(function (error) {
myConsole.error('Cannot update list, assuming no device to jam, %s', JSON.stringify(error));
if (error.status.toString()[0] === '4') {
myConsole.error('Client error %s, set jammed list as empty', error.status);
handleServerResponse([]);
} else {
myConsole.error('Server error %s, leave jammed list untouched', error.status);
}
});
}
return {
updateMacAddresses: function () {
if (fileWriter.isInternetPauseAvailable() === null) {
processManager
.getFingVersion()
.then(function (version) {
myConsole.info('Fing version is %s', version);
if (parseInt(version.split('.')[0]) >= 4) {
fileWriter.setInternetPauseAvailable(true);
} else {
myConsole.info('Fing version %s does not support Internet Pause', version);
fileWriter.setInternetPauseAvailable(false);
}
_updateMacAddresses();
})
.catch(function () {
myConsole.warn('Unable to get Fing version, Internet Pause feature will NOT be available');
_updateMacAddresses();
});
} else {
_updateMacAddresses();
}
},
killDeviceJammer: function () {
processManager.assureFingIsNotRunning();
},
getFingVersion: processManager.getFingVersion,
};
};
module.exports.fingWrapper = fingWrapper;