domotz-remote-pawn
Version:
Domotz Agent
126 lines (108 loc) • 5.08 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 Andrea Azzara <a.azzara@domotz.com> on 22/01/16.
*
*/
function getSeconds(milliseconds) {
return parseInt(milliseconds / 1000, 10);
}
var connectionCheck = function (resourceLocator) {
var myConsole = resourceLocator.log.decorateLogs();
var fileName = process.env.DOMOTZ_CONF_DIR + '/connectivity_file';
var fileUtils = resourceLocator.utils.file;
var spawn = resourceLocator.utils.proc.spawn;
var connectivity = resourceLocator.networkTools.internetConnectionStatus;
var configuration = resourceLocator.configuration || {};
configuration.ping_address = configuration.ping_address || 'echo.domotz.com';
var isUbuntuCore = process.env.DPLATFORM === 'ubuntu_core';
var engineClient = resourceLocator.engineRequest;
var offlineDuration = null;
var networkInfo = require('./networkInfo');
function recordSuccessfulPing() {
'use strict';
if (resourceLocator.status.isConfigured()) {
connectivity.setStatus(connectivity.states.ONLINE, null);
stats = fileUtils.getFileStats(fileName);
if (stats !== undefined && stats.isFile()) {
offlineDuration = getSeconds(Date.now() - stats.ctime.getTime());
myConsole.info('Status of Internet Connection ==> Recovered. Has been offline for %s s', offlineDuration);
var cron = resourceLocator.periodicalTasks;
myConsole.debug('Restarting Heartbeat event ');
cron.resetPeriodicTask('heartBeat');
fileUtils.deleteFile(fileName);
engineClient.sendWithPromise('/network/connection-recovery', 'POST', { lost_since: offlineDuration });
myConsole.info('Retrieve Network Info after reconnecting to the internet');
networkInfo.handler(resourceLocator);
} else {
myConsole.info('Status of Internet Connection ==> OK');
}
}
}
function recordMissedPing() {
'use strict';
if (resourceLocator.status.isConfigured()) {
stats = fileUtils.getFileStats(fileName);
if (stats !== undefined && stats.isFile()) {
var offlineDate = stats.ctime.toString();
myConsole.info(': A file exists. The Offline event was at ' + offlineDate + ' offline for ' + offlineDuration);
offlineDuration = getSeconds(Date.now() - stats.ctime.getTime());
connectivity.setStatus(connectivity.states.OFFLINE, offlineDuration);
} else {
myConsole.info(': First offline event, create file');
var date = new Date().toISOString();
fileUtils.writeFile(fileName, date, true);
connectivity.setStatus(connectivity.states.OFFLINE, 0);
}
}
}
var stats;
/* If detached is false an external signal could be intercepted by the ping process and return an error, possible
* source of false positives. */
var option;
if (resourceLocator.utils.platform.isWindows()) {
option = [configuration.ping_address];
} else {
option = ['-c', '4', configuration.ping_address];
}
spawn('ping', option, { detached: true })
.then(function () {
/* Ping successful */
resourceLocator.emergencyNetworkRestore.disable();
recordSuccessfulPing();
})
.catch(function (error) {
myConsole.warn('Ping failed %s', error.toString());
try {
if (
isUbuntuCore &&
error.toString().search('EACCES') !== -1 &&
process.env.SNAP_NAME &&
process.env.SNAP_NAME.search('domotzpro-agent-publicstore') !== -1
) {
myConsole.warn('Setting agent status to ACTION_REQUIRED');
resourceLocator.status.set(resourceLocator.status.ACTION_REQUIRED);
myConsole.warn('New status ' + resourceLocator.status.get());
}
} catch (e) {
myConsole.error('Error %s', e.toString());
}
resourceLocator.emergencyNetworkRestore.checkEnable();
recordMissedPing();
});
};
module.exports.handler = connectionCheck;