Quick and dirty, but should get you going :)
You have to use the node_helper, because using XMLHttpRequest would result in an “origin error”.
MMM-backlog.js
/* global Module */
/* Magic Mirror
* Module: MMM-backlog
*
* By Stefan Krause http://yawns.de
* MIT Licensed.
*/
Module.register('MMM-backlog',{
defaults: {
units: config.units,
animationSpeed: 1000,
updateInterval: 1000 * 3600, //update every hour
refreshInterval: 1000 * 60 * 10, //refresh every minute
timeFormat: config.timeFormat,
lang: config.language,
initialLoadDelay: 0, // 0 seconds delay
retryDelay: 2500,
fileUrl: "https://www.dropbox.com/s/k1t6mjjc3knxp3a/dataset.json?raw=1"
},
// Define required scripts.
getScripts: function() {
return ["moment.js"];
},
// Define requird styles
getStyles: function() {
return ["font-awesome.css"];
},
start: function() {
Log.info('Starting module: ' + this.name);
this.loaded = false;
this.sendSocketNotification('CONFIG', this.config);
},
getDom: function() {
var wrapper = document.createElement("div");
if (!this.loaded) {
wrapper.innerHTML = this.translate('LOADING');
wrapper.className = "dimmed light small";
return wrapper;
}
if (!this.data) {
wrapper.innerHTML = "No data";
wrapper.className = "dimmed light small";
return wrapper;
}
var t = this.data.graph;
var content = document.createElement("div");
content.innerHTML = "";
for (var i in t.datasequences) {
content.innerHTML += t.title + " - " + t.datasequences[i].title + "<br />";
for (var j in t.datasequences[i].datapoints) {
content.innerHTML += t.datasequences[i].datapoints[j].title + ": " + t.datasequences[i].datapoints[j].value + "<br />";
}
}
wrapper.appendChild(content);
return wrapper;
},
socketNotificationReceived: function(notification, payload) {
if (notification === "STARTED") {
this.updateDom();
}
else if (notification === "DATA") {
this.loaded = true;
this.processData(JSON.parse(payload));
this.updateDom();
}
},
/* processData(data)
* Uses the received data to set the various values.
*
* argument data object - tide information received form worldtides.info
*/
processData: function(data) {
if (!data) {
// Did not receive usable new data.
// Maybe this needs a better check?
return;
}
this.data = data;
this.loaded = true;
this.updateDom(this.config.animationSpeed);
}
});
node_helper.js
'use strict';
/* Magic Mirror
* Module: MMM-backlog
*
* By Stefan Krause http://yawns.de
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
var request = require('request');
module.exports = NodeHelper.create({
start: function() {
this.started = false;
this.config = null;
},
getData: function() {
var self = this;
var myUrl = this.config.fileUrl;
request({
url: myUrl,
method: 'GET',
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
self.sendSocketNotification("DATA", body);
}
});
setTimeout(function() { self.getData(); }, this.config.refreshInterval);
},
socketNotificationReceived: function(notification, payload) {
var self = this;
if (notification === 'CONFIG' && self.started == false) {
self.config = payload;
self.sendSocketNotification("STARTED", true);
self.getData();
self.started = true;
}
}
});