Add server (web/socket), create socket system, better helper loader.

- The Magic Mirror is now hosted via a express server, allowing you to
load it from an external client (for debugging.)
- It now includes a socket system to communicate between the
node_helper and the client module.
- node_helpers are now only loaded if the module is configured in the
config.
This commit is contained in:
Michael Teeuw
2016-03-30 12:20:46 +02:00
parent 15856574d7
commit 899d05bc32
12 changed files with 531 additions and 152 deletions

View File

@@ -16,11 +16,6 @@ Module.create({
reloadInterval: 10 * 60 * 1000, // every 10 minutes
updateInterval: 7.5 * 1000,
animationSpeed: 2.5 * 1000,
proxyUrl: 'http://localhost:8080/?url=',
initialLoadDelay: 0, // 5 seconds delay. This delay is used to keep the OpenWeather API happy.
retryDelay: 2500,
},
// Define required scripts.
@@ -37,14 +32,28 @@ Module.create({
this.newsItems = [];
this.loaded = false;
this.scheduleFetch(this.config.initialLoadDelay);
this.fetchTimer = null;
this.activeItem = 0;
this.fetchNews();
},
// Override socket notification handler.
socketNotificationReceived: function(notification, payload) {
if (notification === 'NEWS_ITEMS') {
if (payload.url === this.config.feedUrl) {
this.newsItems = payload.items;
if (!this.loaded) {
this.scheduleUpdateInterval();
}
this.loaded = true;
}
}
},
// Override dom generator.
getDom: function() {
var wrapper = document.createElement("div");
if (this.activeItem >= this.newsItems.length) {
@@ -57,6 +66,7 @@ Module.create({
var timestamp = document.createElement("div");
timestamp.className = "light small dimmed";
timestamp.innerHTML = this.capitalizeFirstLetter(moment(new Date(this.newsItems[this.activeItem].pubdate)).fromNow() + ':');
//timestamp.innerHTML = this.config.feedUrl;
wrapper.appendChild(timestamp);
}
@@ -77,30 +87,11 @@ Module.create({
* Requests new data from news proxy.
*/
fetchNews: function() {
var url = this.config.proxyUrl + encodeURIComponent(this.config.feedUrl);
var self = this;
var newsRequest = new XMLHttpRequest();
newsRequest.open("GET", url, true);
newsRequest.onreadystatechange = function() {
if(this.readyState === 4) {
if(this.status === 200) {
self.newsItems = JSON.parse(this.response);
if (!self.loaded) {
self.scheduleUpdateInterval();
}
self.loaded = true;
} else {
Log.error(self.name + ": Could not load news.");
}
self.scheduleFetch((self.loaded) ? -1 : self.config.retryDelay);
}
};
newsRequest.send();
Log.log('Add news feed to fetcher: ' + this.config.feedUrl);
this.sendSocketNotification('ADD_FEED', {
url: this.config.feedUrl,
reloadInterval: this.config.reloadInterval
});
},
/* scheduleUpdateInterval()
@@ -117,24 +108,6 @@ Module.create({
}, this.config.updateInterval);
},
/* scheduleFetch()
* Schedule next news fetch.
*
* argument delay number - Milliseconds before next update. If empty, this.config.reloadInterval is used.
*/
scheduleFetch: function(delay) {
var nextLoad = this.config.reloadInterval;
if (typeof delay !== 'undefined' && delay >= 0) {
nextLoad = delay;
}
var self = this;
clearTimeout(this.fetchTimer);
this.fetchTimer = setTimeout(function() {
self.fetchNews();
}, nextLoad);
},
/* capitalizeFirstLetter(string)
* Capitalizes the first character of a string.
*

View File

@@ -1,17 +1,142 @@
// Configuration.
var config = {
port: 8080
};
// Load modules.
var express = require('express');
var request = require('request');
var FeedMe = require('feedme');
var validUrl = require('valid-url');
var app = express();
var MMSocket = require('../../js/socketclient.js');
var socket = new MMSocket('newsfeed');
// Create NewsFetcher.
var NewsFetcher = (function() {
var fetchers = {};
// Register the notification callback.
socket.setNotificationCallback(function(notification, payload) {
if(notification === 'ADD_FEED') {
createFetcher(payload.url, payload.reloadInterval);
}
});
/* createFetcher(url, reloadInterval)
* Creates a fetcher for a new url if it doesn't exsist yet.
* Otherwise it reoses the exsisting one.
*
* attribute url string - URL of the news feed.
* attribute reloadInterval number - Reload interval in milliseconds.
*/
var createFetcher = function(url, reloadInterval) {
if (!validUrl.isUri(url)){
socket.sendNotification('INCORRECT_URL', url);
return;
}
var fetcher;
if (typeof fetchers[url] === 'undefined') {
console.log('Create new news fetcher for url: ' + url + ' - Interval: ' + reloadInterval);
fetcher = new Fetcher(url, reloadInterval);
fetchers[url] = fetcher;
} else {
console.log('Use exsisting news fetcher for url: ' + url);
fetcher = fetchers[url];
fetcher.setReloadInterval(reloadInterval);
fetcher.broadcastItems();
}
fetcher.startFetch();
};
/* Fetcher
* Responsible for requesting an update on the set interval and broadcasting the data.
*
* attribute url string - URL of the news feed.
* attribute reloadInterval number - Reload interval in milliseconds.
*/
var Fetcher = function(url, reloadInterval) {
var self = this;
var newsFetcher = new NewsFetcher();
if (reloadInterval < 1000) {
reloadInterval = 1000;
}
var reloadTimer = null;
var items = [];
/* private methods */
/* fetchNews()
* Request the new items from the newsFetcher.
*/
var fetchNews = function() {
//console.log('Fetch news.');
clearTimeout(reloadTimer);
reloadTimer = null;
newsFetcher.fetchNews(url, function(fetchedItems) {
//console.log(fetchedItems.length + ' items received.');
items = fetchedItems;
self.broadcastItems();
scheduleTimer();
}, function(error) {
//console.log('Unable to load news: ' + error);
socket.sendNotification('UNABLE_TO_LOAD_NEWS', {url:url, error:error});
scheduleTimer();
});
};
/* scheduleTimer()
* Schedule the timer for the next update.
*/
var scheduleTimer = function() {
//console.log('Schedule update timer.');
clearTimeout(reloadTimer);
reloadTimer = setTimeout(function() {
fetchNews();
}, reloadInterval);
};
/* public methods */
/* setReloadInterval()
* Update the reload interval, but only if we need to increase the speed.
*
* attribute interval number - Interval for the update in milliseconds.
*/
this.setReloadInterval = function(interval) {
if (interval > 1000 && interval < reloadInterval) {
reloadInterval = interval;
}
};
/* startFetch()
* Initiate fetchNews();
*/
this.startFetch = function() {
fetchNews();
};
/* broadcastItems()
* Broadcast the exsisting items.
*/
this.broadcastItems = function() {
if (items.length <= 0) {
//console.log('No items to broadcast yet.');
return;
}
//console.log('Broadcasting ' + items.length + ' items.');
socket.sendNotification('NEWS_ITEMS', {
url: url,
items: items
});
};
};
/* NewsFetcher
* Responsible for requesting retrieving the data.
*/
var NewsFetcher = function() {
var self = this;
self.successCallback = function(){};
@@ -33,41 +158,27 @@ var NewsFetcher = (function() {
self.successCallback(self.items);
});
parser.on('error', function(item) {
self.errorCallback();
parser.on('error', function(error) {
self.errorCallback(error);
});
return {
fetchNews: function(url, success, error) {
self.successCallback = success;
self.errorCallback = error;
request(url).pipe(parser);
}
/* public methods */
/* fetchNews()
* Fetch the new news items.
*
* attribute url string - The url to fetch.
* attribute success function(items) - Callback on succes.
* attribute error function(error) - Callback on error.
*/
self.fetchNews = function(url, success, error) {
self.successCallback = success;
self.errorCallback = error;
request(url).pipe(parser);
};
})();
};
// Create route for fetcher.
app.get('/', function (req, res) {
if (!validUrl.isUri(req.query.url)){
res.status(404).send('No valid feed URL.');
return;
}
NewsFetcher.fetchNews(req.query.url, function(items) {
res.send(items);
}, function() {
res.status(400).send('Could not parse feed.');
});
});
// Listen on port.
app.listen(config.port, function () {
console.log('Feed proxy is running on port: ' + config.port);
});
console.log('Starting feed proxy on port: ' + config.port);