domotz-remote-pawn
Version:
Domotz Agent
94 lines (84 loc) • 4.04 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 17/10/2017.
*/
// Time after which we are pretty sure to be offline and actuate emergency network recovery procedures if network management is available
const DEFINITELY_OFFLINE_THRESHOLD_IN_SECONDS = 300;
const EMERGENCY_ADDRESS = '192.168.112.112/24';
const EMERGENCY_DEVICE = 'eth0';
const EMERGENCY_LABEL = 'eth0:112';
module.exports.factory = function (resourceLocator) {
'use strict';
var isUbuntuCore = process.env.DPLATFORM === 'ubuntu_core';
var versionFile = '/opt/domotz_tui/bin/VERSION';
var networkConfigBin = '/opt/domotz_tui/bin/domotz_network';
var file = resourceLocator.utils.file;
var ipWrapper = resourceLocator.utils.ipWrapper;
var lastFailure = null;
var emergencyInterfaceEnabled = false;
var myConsole = resourceLocator.log.decorateLogs();
var ADDRESS_IDENTIFIER = [EMERGENCY_ADDRESS, 'dev', EMERGENCY_DEVICE, 'label', EMERGENCY_LABEL];
function canManageNetwork() {
return (
(isUbuntuCore && !resourceLocator.utils.platform.isVirtualBox()) ||
(file.getFileStats(networkConfigBin) && file.getFileStats(versionFile)) ||
resourceLocator.lodash.get(resourceLocator, 'configuration.settings.emergency_interface.enabled', false)
);
}
function disable() {
myConsole.debug('Testing if emergency ip address must be disabled');
if (canManageNetwork()) {
lastFailure = null;
if (emergencyInterfaceEnabled) {
myConsole.info('Connection went up, disabling address %s', EMERGENCY_ADDRESS);
emergencyInterfaceEnabled = false;
return ipWrapper.call('addr', 'delete', ADDRESS_IDENTIFIER);
} else {
myConsole.debug('Emergency address not enabled, not disabling it');
}
} else {
myConsole.debug('Cannot manage network, disabling not possible');
}
}
function checkEnable() {
if (!canManageNetwork()) {
myConsole.debug('Cannot manage network, enabling not possible');
return;
}
var definitely_offline_threshold_in_seconds = resourceLocator.lodash.get(resourceLocator, 'configuration.settings.emergency_interface.threshold_in_s', DEFINITELY_OFFLINE_THRESHOLD_IN_SECONDS);
if (lastFailure === null) {
lastFailure = new Date();
myConsole.info('Connection went down, starting counter to %s seconds for emergency restore', definitely_offline_threshold_in_seconds);
return;
}
var elapsedS = (new Date() - lastFailure) / 1000;
if (elapsedS > definitely_offline_threshold_in_seconds && !emergencyInterfaceEnabled) {
emergencyInterfaceEnabled = true;
myConsole.info('Enabling emergency ip address ' + EMERGENCY_ADDRESS);
return ipWrapper.call('addr', 'add', ADDRESS_IDENTIFIER);
}
myConsole.info('Connection is down, %s seconds passed since first network failure', elapsedS);
}
function isEmergencyInterfaceEnabled() {
return emergencyInterfaceEnabled;
}
return {
disable: disable,
checkEnable: checkEnable,
isEmergencyInterfaceEnabled: isEmergencyInterfaceEnabled,
};
};