From a98cba63096fb997336923b1311d74eb43c4a30e Mon Sep 17 00:00:00 2001 From: AgP42 Date: Wed, 18 Jul 2018 12:10:44 +0200 Subject: [PATCH 1/6] New preview --- .github/ping.png | Bin 0 -> 6160 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 .github/ping.png diff --git a/.github/ping.png b/.github/ping.png new file mode 100644 index 0000000000000000000000000000000000000000..861ea983c7fec4cd93b31319e5818b2b0798cc35 GIT binary patch literal 6160 zcmbVQbx@qomK`JzEChE5$shv*guy)_=nvQ61a}C|1O|5v1P|_m4-P?sySux)J6ZBx z?SH$kwzjLfzm{94Pxb9S-S-Pol$XT8B*z2*065Z8V#)vjlKpc%<0bNQ>3q^Z`20a~ zRF)J4lnznuJ~vR!KFWOr01#1F4+bxu+ZbP^G#vqeSM7f@Qny`!F#tf4D=qd>)lKg( z)y_P2I<5QM_9Y`YnFnVvG@)kJP#44I+qF8m<)08!ZGl<=wVzX5L=;oCzN~K&T#gJ7Gk|nPL4lcwm+bTtWjD+g+RfOVKxPpsQ(r{-m9!Br${> z^4@yt?F>MNZc~?!Q_k;&w)wUNfABxE`+p@*7`gWKbvmzr5}0_I0=m5ti35tCn^FO9 zUE3L?gS4-7(cyeN;qtp&p{iY{ z`Iu?^9yM-PCn{?c0jB9YUT`H3=e{!N!Dj9GKwn5DLu^t!3t>?+)gnQsN~-oPiK*Nl z8JvgtcV)OFVRkLwAtk{tjdmr_0O}~XM^uT4q~9J9Tpfqhp7qDFr^egrd$y&Ytft?m z8V?$k;mQ#zP(QHT9(;qg%RGwui|~>wUK7Ic+{5iuS9?e@~g#!MbG-|hF2xJ?JoFRx?rQd z`r09Ky`hxvGAHQhpCU#4MB+~8{TM6&2-fif{}7)h+&$DZABl`gJE8n7#f$rs{is-H zH*)#_alznI<;GO{2G{QjfzS95HGrZ~d9QzBvPRv%;bGldO8&z8?+XYpy)OP6Tr@0A z$m6?PqkXE$@osMsJd~A&86MtVIc0$7I^LYQ2KRUZ*G7>Iv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~F7+aErRrUaE@WX(^L$TZn6O^WQp_2)I?}SX?*j@I zeb1LwcN4T`+n8<9ZRvW{a`yOS=J@_S0$4W3SEZjKO|F7}t=cI;Z`trGEiM$Kt_a`Y z^U3ZG?tPeCwXs}NX^mx&_~&3gXNyH&RNpE!)}7V%sg5UqSNOo|lIc3ws>X7(tQpF= zwK+3iTdMzFhbORZI;h@d2n;%k(`{QDw96}zj}TB?nU4tdV!I8Kc9#^imkptbqIs-! z6LbQqSsQ2N{p#05BpN!YY|;j-J(}RH)3#K^efqYy#(esY_zay3a%$A44~NiNR-a?B zJ#@3>@z1hI;+fYJjzo#ldF`3Xel2%g9BS(<+rX{#e*6X43SN8(gy7Vn#g{Cn}A=Ogm`ccE`6_d)bc z9^mVArk9iN>qogweUc7Bz!87sQ`Fr$TKePeWGD~Ni|n<^e|jS=K*#E5drM2!!hmr@ zai)$Ir#2WlQz91|M+d)?cE@K;qs310C8TVQe^QV0Jx9=$$pydcB*hr4^D;P&8c@SG4fny^J(=9>>zkQt-spTwJ z@ZYljX!ZiIU?C+doj;J#Ci;&`WjN?LOr^gczy6UVX4zgT6hLrKdHqURT9#d^+9myH zUOp>6$U-n>$R1^62DI*rjU z^d*St+xI`6fnmQRLA#U+m*^phJF5ydQZ!UCVeltP*+pt-Wz`$nrWTfX2ml%5IObOsv@aMGVi*;wtKQ^b`j z&^rAo_dLdoMqi`gJ1To;TMq(ZBqrh^z6GL>pXqY46- Date: Wed, 18 Jul 2018 12:32:12 +0200 Subject: [PATCH 2/6] Add ping.js and associated README Modification of this file : - add the possibility to display the last absolute date and time of the successfull ping by using a new config parameter "showAbsoluteTime" - add management of "USER_PRESENCE" send by the module MMM-PIR. In case of no user present, the automatic update of ping module will be standby, and set again when the user return - add management of "module.hidden" by the use of both core system function "resume" and "suspend" : in case the module is hidden by the system (case of the use of a carousel or the module MMM-Pages or hidden by Remote-Control, or ....) then the ping supervision is stopped and will be resumed at the return of the possible display. This has no side effect with the param "showAlways". The module is only stopped in case of it cannot display its info to the user anyway. --- README.md | 18 ++++++++++++++- ping.js | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 82 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 8c2090e..8cb5e1d 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,8 @@ Module to check the network connection on the MagicMirror. -![Sonos Module](https://github.com/CFenner/MagicMirror-Ping-Module/blob/master/.github/preview.png) +![Ping Module](https://github.com/CFenner/MagicMirror-Ping-Module/blob/master/.github/preview.png) +![Ping Module](https://github.com/AgP42/MagicMirror-Ping-Module/blob/master/.github/ping.png) ## Installation @@ -29,8 +30,23 @@ Add module configuration to config.js. }, ``` +```js + { + module: 'ping', + position: 'top_left', + header: "Surveillance WiFi", + config: { + updateInterval: 1, + showAlways: true, + showAbsoluteTime: true + } + }, +``` + |Option|Description| |---|---| |`showAlways`|Should the status always be shown or just if the connection is lost?

**Default value:** `false`| |`updateInterval`|How often does the content needs to be fetched? (Minutes)

**Default value:** `10`| |`animationSpeed`|Speed of the update animation. (Seconds)

**Default value:** `1`| +|`showAbsoluteTime`|Add or not the last Absolute Time where the ping was successfull. In case of total freeze of the screen, still possible to know the last update

**Default value:** `false`| +|`AbsoluteTimeFormat`|Format to display the AbsoluteTime

**Default value:** `dd - HH:mm:ss`| diff --git a/ping.js b/ping.js index eecf6cf..7ef01fb 100644 --- a/ping.js +++ b/ping.js @@ -5,18 +5,29 @@ * By Christopher Fenner https://github.com/CFenner * MIT Licensed. */ + +//UserPresence Management (PIR sensor) +var UserPresence = true; //true by default, so no impact for user without a PIR sensor + Module.register('ping', { defaults: { animationSpeed: 1, updateInterval: 10, - showAlways: false + showAlways: false, + showAbsoluteTime: false, + AbsoluteTimeFormat: 'dd - HH:mm:ss' }, payload: {}, start: function() { Log.info('Starting module: ' + this.name); - this.update(); - // refresh every x minutes - setInterval( + + var ModulePingHidden = false; //by default it is displayed. Note : this.hidden has strange behaviour, so not used here + var IntervalID = 0; //Definition of the IntervalID to be able to stop and start is again + + //this.update(); removed because the new fonction "resume" bellow will also request a first update at start-up + + // refresh every x minutes and intervalD defined + this.IntervalID = setInterval( this.update.bind(this), this.config.updateInterval * 60 * 1000); }, @@ -32,6 +43,50 @@ Module.register('ping', { } } }, + + + suspend: function() { + this.ModulePingHidden = true; + Log.log("Fct suspend - ModuleHidden = " + this.ModulePingHidden); + this.GestionUpdateInterval(); //request of the function that manage all situation + }, + + resume: function() { + this.ModulePingHidden = false; + Log.log("Fct resume - ModuleHidden = " + this.ModulePingHidden); + this.GestionUpdateInterval(); + }, + + notificationReceived: function(notification, payload) { + if (notification === "USER_PRESENCE") { // notification send by module MMM-PIR-Sensor. See its README + Log.log("Fct notificationReceived USER_PRESENCE - payload = " + payload); + UserPresence = payload; + this.GestionUpdateInterval(); + } + }, + + GestionUpdateInterval: function() { + if (UserPresence === true && this.ModulePingHidden === false){ //both User present and module displayed + Log.log(this.name + " est revenu et user present ! On update"); + + // update now + this.update(); + + //if no IntervalID defined, we set one again. his is to avoid several setInterval simultaneously + if (this.IntervalID === 0){ + this.IntervalID = setInterval( + this.update.bind(this), + this.config.updateInterval * 60 * 1000); + } + }else{ //sinon (UserPresence = false OU ModuleHidden = true) + Log.log("Personne regarde : on stop l'update ! ID : " + this.IntervalID); + clearInterval(this.IntervalID); // on arrete l'intervalle d'update en cours + this.IntervalID=0; //on reset la variable + } + }, + + + getStyles: function() { return []; }, @@ -57,6 +112,12 @@ Module.register('ping', { span.className = "bright"; span.innerHTML = moment(this.payload.lastConnection).fromNow(); wrapper.appendChild(span); + if(this.config.showAbsoluteTime){ + span = document.createElement("div"); + span.className = "xsmall light"; + span.innerHTML = moment(this.payload.lastConnection).format(this.config.AbsoluteTimeFormat); + wrapper.appendChild(span); + } } return wrapper; } From 901e2c8cf0b72751312398cf28bc511beaeebd74 Mon Sep 17 00:00:00 2001 From: AgP42 Date: Wed, 18 Jul 2018 12:35:32 +0200 Subject: [PATCH 3/6] Update of screenshot --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8cb5e1d..ec607d9 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Module to check the network connection on the MagicMirror. ![Ping Module](https://github.com/CFenner/MagicMirror-Ping-Module/blob/master/.github/preview.png) -![Ping Module](https://github.com/AgP42/MagicMirror-Ping-Module/blob/master/.github/ping.png) +![Ping Module](https://github.com/AgP42/MMM-Ping/blob/master/.github/ping.png) ## Installation From 6419c45a855bfafc5c380089d4c8709b2006167b Mon Sep 17 00:00:00 2001 From: AgP42 Date: Tue, 24 Jul 2018 16:47:02 +0200 Subject: [PATCH 4/6] Possibility to reboot RPI if no PING during some delay I finally changed my mind and removed the limitation of PING in case when the module cannot be displayed (https://github.com/CFenner/MMM-Ping/pull/5). This new version keep the possibility to display last absolute date of successfull PING and add : - Possibility to call a RPI reboot (need MMM-Remote-Control) if a configurable delay without successfull PING is exceeded - this reboot will be log in pm2 log with GMT time of the reboot (in /home/pi/.pm2/logs) Known issue : the reboot is directly requested to the RPI, without proper close of the MagicMirror. Issue raised on MMM-Remote-Control module : https://github.com/Jopyth/MMM-Remote-Control/issues/85 --- node_helper.js | 74 +++++++++-------- ping.js | 219 +++++++++++++++++++++---------------------------- 2 files changed, 135 insertions(+), 158 deletions(-) diff --git a/node_helper.js b/node_helper.js index 1e5086c..d07eb2a 100644 --- a/node_helper.js +++ b/node_helper.js @@ -1,34 +1,40 @@ -/* Magic Mirror - * Module: ping - * - * By Christopher Fenner https://github.com/CFenner - * MIT Licensed. - */ -var NodeHelper = require('node_helper'); -var request = require('request'); - -module.exports = NodeHelper.create({ - start: function () { - console.log(this.name + ' helper started ...'); - this.lastConnection = 'never'; - }, - socketNotificationReceived: function(notification, payload) { - //console.log(notification); - if (notification === 'PING_REQUEST') { - var that = this; - request({ - url: 'http://www.google.com', - method: 'GET' - }, function(error, response, body) { - if (!error && response.statusCode == 200) { - that.lastConnection = new Date(); - } - that.sendSocketNotification('PING_RESPONSE', { - status: !error && response.statusCode == 200?"OK":"ERROR", - lastConnection: that.lastConnection - }); - } - ); - } - } -}); +/* Magic Mirror + * Module: ping + * + * By Christopher Fenner https://github.com/CFenner + * MIT Licensed. + */ +var NodeHelper = require('node_helper'); +var request = require('request'); + +module.exports = NodeHelper.create({ + start: function () { + console.log(this.name + ' helper started ...'); + this.lastConnection = 0; //in 1970, long time ago without a valid ping on google... did google exist ? ;-) + //so after a first RPI reboot without successfull ping, the reboot delay will be "config.updateInterval" and not "config.rebootDelay" + }, + socketNotificationReceived: function(notification, payload) { + //console.log(notification); + if (notification === 'PING_REQUEST') { + var that = this; + request({ + url: 'http://www.google.com', + method: 'GET' + }, function(error, response, body) { + if (!error && response.statusCode == 200) { + that.lastConnection = new Date(); + } + that.sendSocketNotification('PING_RESPONSE', { + status: !error && response.statusCode == 200?"OK":"ERROR", + lastConnection: that.lastConnection + }); + } + ); + } + //Log the reboot on pm2 out log, here : /home/pi/.pm2/logs + if (notification === 'LOG_REBOOT') { + console.log("RPI reboot requested at : " + payload + " GMT, after too long time without successfull PING"); + } + + } +}); diff --git a/ping.js b/ping.js index 7ef01fb..4d7149e 100644 --- a/ping.js +++ b/ping.js @@ -1,124 +1,95 @@ -/* global Module */ -/* Magic Mirror - * Module: ping - * - * By Christopher Fenner https://github.com/CFenner - * MIT Licensed. - */ - -//UserPresence Management (PIR sensor) -var UserPresence = true; //true by default, so no impact for user without a PIR sensor - -Module.register('ping', { - defaults: { - animationSpeed: 1, - updateInterval: 10, - showAlways: false, - showAbsoluteTime: false, - AbsoluteTimeFormat: 'dd - HH:mm:ss' - }, - payload: {}, - start: function() { - Log.info('Starting module: ' + this.name); - - var ModulePingHidden = false; //by default it is displayed. Note : this.hidden has strange behaviour, so not used here - var IntervalID = 0; //Definition of the IntervalID to be able to stop and start is again - - //this.update(); removed because the new fonction "resume" bellow will also request a first update at start-up - - // refresh every x minutes and intervalD defined - this.IntervalID = setInterval( - this.update.bind(this), - this.config.updateInterval * 60 * 1000); - }, - update: function() { - this.sendSocketNotification('PING_REQUEST'); - }, - socketNotificationReceived: function(notification, payload) { - if (notification === 'PING_RESPONSE') { - Log.info('received' + notification); - if(payload){ - this.payload = payload; - this.updateDom(this.config.animationSpeed * 1000); - } - } - }, - - - suspend: function() { - this.ModulePingHidden = true; - Log.log("Fct suspend - ModuleHidden = " + this.ModulePingHidden); - this.GestionUpdateInterval(); //request of the function that manage all situation - }, - - resume: function() { - this.ModulePingHidden = false; - Log.log("Fct resume - ModuleHidden = " + this.ModulePingHidden); - this.GestionUpdateInterval(); - }, - - notificationReceived: function(notification, payload) { - if (notification === "USER_PRESENCE") { // notification send by module MMM-PIR-Sensor. See its README - Log.log("Fct notificationReceived USER_PRESENCE - payload = " + payload); - UserPresence = payload; - this.GestionUpdateInterval(); - } - }, - - GestionUpdateInterval: function() { - if (UserPresence === true && this.ModulePingHidden === false){ //both User present and module displayed - Log.log(this.name + " est revenu et user present ! On update"); - - // update now - this.update(); - - //if no IntervalID defined, we set one again. his is to avoid several setInterval simultaneously - if (this.IntervalID === 0){ - this.IntervalID = setInterval( - this.update.bind(this), - this.config.updateInterval * 60 * 1000); - } - }else{ //sinon (UserPresence = false OU ModuleHidden = true) - Log.log("Personne regarde : on stop l'update ! ID : " + this.IntervalID); - clearInterval(this.IntervalID); // on arrete l'intervalle d'update en cours - this.IntervalID=0; //on reset la variable - } - }, - - - - getStyles: function() { - return []; - }, - getScripts: function() { - return ["moment.js"]; - }, - getTranslations: function() { - return { - en: "i18n/en.json", - es: "i18n/es.json", - de: "i18n/de.json", - fr: "i18n/fr.json" - }; - }, - getDom: function() { - var wrapper = document.createElement("div"); - if(this.config.showAlways || this.payload.status === "ERROR"){ - var span = document.createElement("div"); - span.innerHTML = this.translate("LAST_ACTIVE_CONNECTION"); - span.className = "small"; - wrapper.appendChild(span); - span = document.createElement("div"); - span.className = "bright"; - span.innerHTML = moment(this.payload.lastConnection).fromNow(); - wrapper.appendChild(span); - if(this.config.showAbsoluteTime){ - span = document.createElement("div"); - span.className = "xsmall light"; - span.innerHTML = moment(this.payload.lastConnection).format(this.config.AbsoluteTimeFormat); - wrapper.appendChild(span); - } - } - return wrapper; - } -}); +/* global Module */ +/* Magic Mirror + * Module: ping + * + * By Christopher Fenner https://github.com/CFenner + * MIT Licensed. + */ + +Module.register('ping', { + defaults: { + animationSpeed: 1, + updateInterval: 10, //min + showAlways: false, + showAbsoluteTime: false, + AbsoluteTimeFormat: 'dd - HH:mm:ss', + rebootIfNoPing: false, // should we ask the RPI to reboot if no ping during some delay ? + rebootDelay: 20 //if no PING for more than x min, request the RPI to reboot + }, + payload: {}, + start: function() { + Log.info('Starting module: ' + this.name); + + this.update(); + + setInterval( + this.update.bind(this), + this.config.updateInterval * 60 * 1000); + }, + update: function() { + this.sendSocketNotification('PING_REQUEST'); + }, + socketNotificationReceived: function(notification, payload) { + if (notification === 'PING_RESPONSE') { + Log.info('received ' + notification); + if(payload){ + this.payload = payload; + + if(this.payload.status === "ERROR" && this.config.rebootIfNoPing){ + + var actualErrorDelay = moment(new Date()) - moment(this.payload.lastConnection); + + Log.log("No PING for : " + actualErrorDelay / 1000 + "sec. Max acceptable delay set at : " + this.config.rebootDelay*60 +" s"); + + if(actualErrorDelay >= this.config.rebootDelay*60*1000){ + //Log.log("Reboot asked !"); + + //request (to node_helper) the reboot to be logged in pm2 out log + this.sendSocketNotification('LOG_REBOOT', new Date()); + + //request reboot and reboot to Remote-Control module + this.sendNotification('REMOTE_ACTION', {action: 'REBOOT'}); + } + } + + + this.updateDom(this.config.animationSpeed * 1000); + } + } + }, + + getStyles: function() { + return []; + }, + getScripts: function() { + return ["moment.js"]; + }, + getTranslations: function() { + return { + en: "i18n/en.json", + es: "i18n/es.json", + de: "i18n/de.json", + fr: "i18n/fr.json" + }; + }, + getDom: function() { + var wrapper = document.createElement("div"); + if(this.config.showAlways || this.payload.status === "ERROR"){ + var span = document.createElement("div"); + span.innerHTML = this.translate("LAST_ACTIVE_CONNECTION"); + span.className = "small"; + wrapper.appendChild(span); + span = document.createElement("div"); + span.className = "bright"; + span.innerHTML = moment(this.payload.lastConnection).fromNow(); + wrapper.appendChild(span); + if(this.config.showAbsoluteTime){ + span = document.createElement("div"); + span.className = "xsmall light"; + span.innerHTML = moment(this.payload.lastConnection).format(this.config.AbsoluteTimeFormat); + wrapper.appendChild(span); + } + } + return wrapper; + } +}); From e6a11cd0a96c6b7e458a51f496d3bb353d386aaf Mon Sep 17 00:00:00 2001 From: AgP42 Date: Tue, 24 Jul 2018 16:51:35 +0200 Subject: [PATCH 5/6] Add new config param Add param details for : - rebootIfNoPing: false, - rebootDelay: 20 --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ec607d9..e3bd453 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,9 @@ Add module configuration to config.js. config: { updateInterval: 1, showAlways: true, - showAbsoluteTime: true + showAbsoluteTime: true, + rebootIfNoPing: false, + rebootDelay: 20 } }, ``` @@ -50,3 +52,5 @@ Add module configuration to config.js. |`animationSpeed`|Speed of the update animation. (Seconds)

**Default value:** `1`| |`showAbsoluteTime`|Add or not the last Absolute Time where the ping was successfull. In case of total freeze of the screen, still possible to know the last update

**Default value:** `false`| |`AbsoluteTimeFormat`|Format to display the AbsoluteTime

**Default value:** `dd - HH:mm:ss`| +|`rebootIfNoPing`|should we ask the RPI to reboot if no ping during some delay ?

**Default value:** `false`| +|`rebootDelay`|Delay for reboot request if rebootIfNoPing=true

**Default value:** `20`| From 46ca97af50b93436674eacd5d86445ce9b093b67 Mon Sep 17 00:00:00 2001 From: AgP42 <12719820+AgP42@users.noreply.github.com> Date: Sat, 28 Jul 2018 19:06:32 +0200 Subject: [PATCH 6/6] Bug fix The initialization of this.lastConnection is now done at the start-up time and not 0 (1970). So in case of total network lost, the delay to be reach before reboot is well "rebootDelay" and not "updateInterval" --- node_helper.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/node_helper.js b/node_helper.js index d07eb2a..d919cae 100644 --- a/node_helper.js +++ b/node_helper.js @@ -10,8 +10,7 @@ var request = require('request'); module.exports = NodeHelper.create({ start: function () { console.log(this.name + ' helper started ...'); - this.lastConnection = 0; //in 1970, long time ago without a valid ping on google... did google exist ? ;-) - //so after a first RPI reboot without successfull ping, the reboot delay will be "config.updateInterval" and not "config.rebootDelay" + this.lastConnection = new Date(); //We consider a success at start-up }, socketNotificationReceived: function(notification, payload) { //console.log(notification);