UNPKG

domotz-remote-pawn

Version:

Domotz Agent

86 lines (72 loc) 3.49 kB
/** 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. * */ const LOG_EVENT = 'HANDLER - (connectionCheck)'; function getSeconds(milliseconds) { return parseInt(milliseconds / 1000, 10); } var connectionCheck = function (resourceLocator) { var status = resourceLocator.status; if (status.get() !== status.CONFIGURED) { return; } console.info(LOG_EVENT); 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; var engineClient = resourceLocator.engineRequest; var offlineDuration = null; 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. */ spawn('ping', ['-c', '4', configuration.ping_address || 'echo.domotz.com'], {detached: true}) .then(function() { /* Ping successful */ connectivity.setStatus(connectivity.states.ONLINE, null); stats = fileUtils.getFileStats(fileName); if (stats !== undefined && stats.isFile()) { offlineDuration = getSeconds(Date.now() - stats.ctime.getTime()); console.info(LOG_EVENT + ': Status of Internet Connection ==> Recovered. ' + 'Has been offline for ' + offlineDuration); fileUtils.deleteFile(fileName); engineClient.sendWithPromise('/network/connection-recovery', 'POST', {lost_since: offlineDuration}); } else { console.info(LOG_EVENT + ': Status of Internet Connection ==> OK'); } }) .catch(function() { console.warn(LOG_EVENT + ": Ping failed"); stats = fileUtils.getFileStats(fileName); if (stats !== undefined && stats.isFile()) { var offlineDate = stats.ctime.toString(); console.log(LOG_EVENT + ": 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 { console.log(LOG_EVENT + ": First offline event, create file"); var date = new Date(); fileUtils.writeFile(fileName, date, true); connectivity.setStatus(connectivity.states.OFFLINE, 0); } }); }; module.exports.handler = connectionCheck;