Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .github/ping.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/MMM-Ping/blob/master/.github/ping.png)

## Installation

Expand All @@ -29,8 +30,27 @@ Add module configuration to config.js.
},
```

```js
{
module: 'ping',
position: 'top_left',
header: "Surveillance WiFi",
config: {
updateInterval: 1,
showAlways: true,
showAbsoluteTime: true,
rebootIfNoPing: false,
rebootDelay: 20
}
},
```

|Option|Description|
|---|---|
|`showAlways`|Should the status always be shown or just if the connection is lost?<br><br>**Default value:** `false`|
|`updateInterval`|How often does the content needs to be fetched? (Minutes)<br><br>**Default value:** `10`|
|`animationSpeed`|Speed of the update animation. (Seconds)<br><br>**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<br><br>**Default value:** `false`|
|`AbsoluteTimeFormat`|Format to display the AbsoluteTime<br><br>**Default value:** `dd - HH:mm:ss`|
|`rebootIfNoPing`|should we ask the RPI to reboot if no ping during some delay ?<br><br>**Default value:** `false`|
|`rebootDelay`|Delay for reboot request if rebootIfNoPing=true <br><br>**Default value:** `20`|
73 changes: 39 additions & 34 deletions node_helper.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
/* 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 = new Date(); //We consider a success at start-up
},
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");
}

}
});
158 changes: 95 additions & 63 deletions ping.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,95 @@
/* global Module */
/* Magic Mirror
* Module: ping
*
* By Christopher Fenner https://github.com/CFenner
* MIT Licensed.
*/
Module.register('ping', {
defaults: {
animationSpeed: 1,
updateInterval: 10,
showAlways: false
},
payload: {},
start: function() {
Log.info('Starting module: ' + this.name);
this.update();
// refresh every x minutes
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);
}
}
},
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);
}
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;
}
});