mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-27 03:39:55 +00:00
commit
c89e5785d8
5
.gitignore
vendored
5
.gitignore
vendored
@ -1 +1,4 @@
|
|||||||
node_modules
|
/node_modules
|
||||||
|
|
||||||
|
!/modules/node_helper
|
||||||
|
!/modules/node_helper/**
|
@ -16,7 +16,8 @@ Things that still have to be implemented or changed.
|
|||||||
####Loader
|
####Loader
|
||||||
- Loading of module uses `eval()`. We might want to look into a better solution. [loader.js#L112](https://github.com/MichMich/MagicMirror/blob/v2-beta/js/loader.js#L112).
|
- Loading of module uses `eval()`. We might want to look into a better solution. [loader.js#L112](https://github.com/MichMich/MagicMirror/blob/v2-beta/js/loader.js#L112).
|
||||||
|
|
||||||
|
####NodeHelper
|
||||||
|
- The node_helper superclass creates a seperate socket connection for each module. It's preferred to use the overall socket connection of the server.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -62,4 +62,8 @@
|
|||||||
|
|
||||||
return Class;
|
return Class;
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
|
||||||
|
/*************** DO NOT EDIT THE LINE BELOW ***************/
|
||||||
|
if (typeof module !== 'undefined') {module.exports = Class;}
|
@ -56,31 +56,20 @@ function loadConfig (callback) {
|
|||||||
|
|
||||||
function loadModule(moduleName) {
|
function loadModule(moduleName) {
|
||||||
var helperPath = __dirname + '/../modules/' + moduleName + '/node_helper.js';
|
var helperPath = __dirname + '/../modules/' + moduleName + '/node_helper.js';
|
||||||
|
var loadModule = true;
|
||||||
try {
|
try {
|
||||||
fs.accessSync(helperPath, fs.R_OK);
|
fs.accessSync(helperPath, fs.R_OK);
|
||||||
|
|
||||||
var child = spawn('node', [helperPath]);
|
|
||||||
|
|
||||||
// Make sure the output is logged.
|
|
||||||
child.stdout.on('data', function(data) {
|
|
||||||
process.stdout.write('[' + moduleName + '] ' + data);
|
|
||||||
});
|
|
||||||
|
|
||||||
child.stderr.on('data', function(data) {
|
|
||||||
process.stdout.write('[' + moduleName + '] ' + data);
|
|
||||||
});
|
|
||||||
|
|
||||||
child.on('close', function(code) {
|
|
||||||
console.log(moduleName + ' closing code: ' + code);
|
|
||||||
});
|
|
||||||
|
|
||||||
//Log module name
|
|
||||||
console.log("Started helper script for module: " + moduleName + ".");
|
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
loadModule = false;
|
||||||
console.log("No helper found for module: " + moduleName + ".");
|
console.log("No helper found for module: " + moduleName + ".");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (loadModule) {
|
||||||
|
var Module = require(helperPath);
|
||||||
|
var m = new Module();
|
||||||
|
m.setName(moduleName);
|
||||||
|
m.start();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadModules(modules) {
|
function loadModules(modules) {
|
||||||
@ -112,7 +101,9 @@ loadConfig(function(c) {
|
|||||||
// initialization and is ready to create browser windows.
|
// initialization and is ready to create browser windows.
|
||||||
app.on('ready', function() {
|
app.on('ready', function() {
|
||||||
var server = new Server(config, function() {
|
var server = new Server(config, function() {
|
||||||
createWindow();
|
setTimeout(function() {
|
||||||
|
createWindow();
|
||||||
|
}, 1000);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
111
modules/newsfeed/fetcher.js
Normal file
111
modules/newsfeed/fetcher.js
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
/* Magic Mirror
|
||||||
|
* Fetcher
|
||||||
|
*
|
||||||
|
* By Michael Teeuw http://michaelteeuw.nl
|
||||||
|
* MIT Licensed.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var NewsFetcher = require('./newsfetcher.js');
|
||||||
|
|
||||||
|
/* 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 = [];
|
||||||
|
|
||||||
|
var fetchFailedCallback = function() {};
|
||||||
|
var itemsReceivedCallback = function() {};
|
||||||
|
|
||||||
|
/* 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) {
|
||||||
|
items = fetchedItems;
|
||||||
|
self.broadcastItems();
|
||||||
|
scheduleTimer();
|
||||||
|
}, function(error) {
|
||||||
|
fetchFailedCallback(self, 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.');
|
||||||
|
itemsReceivedCallback(self);
|
||||||
|
};
|
||||||
|
|
||||||
|
this.onReceive = function(callback) {
|
||||||
|
itemsReceivedCallback = callback;
|
||||||
|
};
|
||||||
|
|
||||||
|
this.onError = function(callback) {
|
||||||
|
fetchFailedCallback = callback;
|
||||||
|
};
|
||||||
|
|
||||||
|
this.url = function() {
|
||||||
|
return url;
|
||||||
|
};
|
||||||
|
|
||||||
|
this.items = function() {
|
||||||
|
return items;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = Fetcher;
|
53
modules/newsfeed/newsfetcher.js
Normal file
53
modules/newsfeed/newsfetcher.js
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
/* Magic Mirror
|
||||||
|
* NewsFetcher
|
||||||
|
*
|
||||||
|
* By Michael Teeuw http://michaelteeuw.nl
|
||||||
|
* MIT Licensed.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var FeedMe = require('feedme');
|
||||||
|
var request = require('request');
|
||||||
|
|
||||||
|
var NewsFetcher = function() {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
self.successCallback = function(){};
|
||||||
|
self.errorCallback = function(){};
|
||||||
|
|
||||||
|
self.items = [];
|
||||||
|
|
||||||
|
var parser = new FeedMe();
|
||||||
|
|
||||||
|
parser.on('item', function(item) {
|
||||||
|
//console.log(item);
|
||||||
|
self.items.push({
|
||||||
|
title: item.title,
|
||||||
|
pubdate: item.pubdate,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
parser.on('end', function(item) {
|
||||||
|
self.successCallback(self.items);
|
||||||
|
});
|
||||||
|
|
||||||
|
parser.on('error', function(error) {
|
||||||
|
self.errorCallback(error);
|
||||||
|
});
|
||||||
|
|
||||||
|
/* 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);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = NewsFetcher;
|
@ -1,184 +1,68 @@
|
|||||||
|
var NodeHelper = require('node_helper');
|
||||||
// Load modules.
|
|
||||||
var request = require('request');
|
|
||||||
var FeedMe = require('feedme');
|
|
||||||
var validUrl = require('valid-url');
|
var validUrl = require('valid-url');
|
||||||
var MMSocket = require('../../js/socketclient.js');
|
var Fetcher = require('./fetcher.js');
|
||||||
var socket = new MMSocket('newsfeed');
|
|
||||||
|
|
||||||
var fetchers = {};
|
module.exports = NodeHelper.create({
|
||||||
|
// Subclass start method.
|
||||||
|
start: function() {
|
||||||
|
console.log('Starting module: ' + this.name);
|
||||||
|
|
||||||
// Register the notification callback.
|
this.fetchers = [];
|
||||||
socket.setNotificationCallback(function(notification, payload) {
|
},
|
||||||
if(notification === 'ADD_FEED') {
|
|
||||||
createFetcher(payload.url, payload.reloadInterval);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
/* createFetcher(url, reloadInterval)
|
// Subclass socketNotificationReceived received.
|
||||||
* Creates a fetcher for a new url if it doesn't exsist yet.
|
socketNotificationReceived: function(notification, payload) {
|
||||||
* Otherwise it reoses the exsisting one.
|
if(notification === 'ADD_FEED') {
|
||||||
*
|
this.createFetcher(payload.url, payload.reloadInterval);
|
||||||
* 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()
|
/* createFetcher(url, reloadInterval)
|
||||||
* Initiate fetchNews();
|
* 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.
|
||||||
*/
|
*/
|
||||||
this.startFetch = function() {
|
|
||||||
fetchNews();
|
|
||||||
};
|
|
||||||
|
|
||||||
/* broadcastItems()
|
createFetcher: function(url, reloadInterval) {
|
||||||
* Broadcast the exsisting items.
|
var self = this;
|
||||||
*/
|
|
||||||
this.broadcastItems = function() {
|
if (!validUrl.isUri(url)){
|
||||||
if (items.length <= 0) {
|
self.sendSocketNotification('INCORRECT_URL', url);
|
||||||
//console.log('No items to broadcast yet.');
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//console.log('Broadcasting ' + items.length + ' items.');
|
|
||||||
socket.sendNotification('NEWS_ITEMS', {
|
|
||||||
url: url,
|
|
||||||
items: items
|
|
||||||
});
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
/* NewsFetcher
|
var fetcher;
|
||||||
* Responsible for requesting retrieving the data.
|
if (typeof self.fetchers[url] === 'undefined') {
|
||||||
*/
|
console.log('Create new news fetcher for url: ' + url + ' - Interval: ' + reloadInterval);
|
||||||
|
fetcher = new Fetcher(url, reloadInterval);
|
||||||
|
|
||||||
|
fetcher.onReceive(function(fetcher) {
|
||||||
|
self.sendSocketNotification('NEWS_ITEMS', {
|
||||||
|
url: fetcher.url(),
|
||||||
|
items: fetcher.items()
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
var NewsFetcher = function() {
|
fetcher.onError(function(fetcher, error) {
|
||||||
var self = this;
|
self.sendSocketNotification('FETCH_ERROR', {
|
||||||
|
url: fetcher.url(),
|
||||||
self.successCallback = function(){};
|
error: error
|
||||||
self.errorCallback = function(){};
|
});
|
||||||
|
});
|
||||||
self.items = [];
|
|
||||||
|
|
||||||
var parser = new FeedMe();
|
|
||||||
|
|
||||||
parser.on('item', function(item) {
|
|
||||||
//console.log(item);
|
|
||||||
self.items.push({
|
|
||||||
title: item.title,
|
|
||||||
pubdate: item.pubdate,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
parser.on('end', function(item) {
|
|
||||||
self.successCallback(self.items);
|
|
||||||
});
|
|
||||||
|
|
||||||
parser.on('error', function(error) {
|
|
||||||
self.errorCallback(error);
|
|
||||||
});
|
|
||||||
|
|
||||||
/* 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);
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
|
self.fetchers[url] = fetcher;
|
||||||
|
} else {
|
||||||
|
console.log('Use exsisting news fetcher for url: ' + url);
|
||||||
|
fetcher = self.fetchers[url];
|
||||||
|
fetcher.setReloadInterval(reloadInterval);
|
||||||
|
fetcher.broadcastItems();
|
||||||
|
}
|
||||||
|
|
||||||
|
fetcher.startFetch();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
72
modules/node_modules/node_helper/index.js
generated
vendored
Normal file
72
modules/node_modules/node_helper/index.js
generated
vendored
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
/* Magic Mirror
|
||||||
|
* Node Helper Superclass
|
||||||
|
*
|
||||||
|
* By Michael Teeuw http://michaelteeuw.nl
|
||||||
|
* MIT Licensed.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var Class = require('../../../js/class.js');
|
||||||
|
var MMSocket = require('../../../js/socketclient.js');
|
||||||
|
|
||||||
|
NodeHelper = Class.extend({
|
||||||
|
init: function() {
|
||||||
|
console.log('Initializing new module helper ...');
|
||||||
|
},
|
||||||
|
|
||||||
|
start: function() {
|
||||||
|
console.log('Staring module helper: ' + this.name);
|
||||||
|
},
|
||||||
|
|
||||||
|
/* socketNotificationReceived(notification, payload)
|
||||||
|
* This method is called when a socket notification arrives.
|
||||||
|
*
|
||||||
|
* argument notification string - The identifier of the noitication.
|
||||||
|
* argument payload mixed - The payload of the notification.
|
||||||
|
*/
|
||||||
|
socketNotificationReceived: function(notification, payload) {
|
||||||
|
Log.log(this.name + ' received a socket notification: ' + notification + ' - Payload: ' + payload);
|
||||||
|
},
|
||||||
|
|
||||||
|
/* setName(data)
|
||||||
|
* Set the module name.
|
||||||
|
*
|
||||||
|
* argument name string - Module name.
|
||||||
|
*/
|
||||||
|
setName: function(name) {
|
||||||
|
this.name = name;
|
||||||
|
this.socket();
|
||||||
|
},
|
||||||
|
|
||||||
|
/* socket()
|
||||||
|
* Returns a socket object. If it doesn't exsist, it's created.
|
||||||
|
* It also registers the notification callback.
|
||||||
|
*/
|
||||||
|
socket: function() {
|
||||||
|
if (typeof this._socket === 'undefined') {
|
||||||
|
this._socket = this._socket = new MMSocket(this.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
var self = this;
|
||||||
|
this._socket.setNotificationCallback(function(notification, payload) {
|
||||||
|
self.socketNotificationReceived(notification, payload);
|
||||||
|
});
|
||||||
|
|
||||||
|
return this._socket;
|
||||||
|
},
|
||||||
|
|
||||||
|
/* sendSocketNotification(notification, payload)
|
||||||
|
* Send a socket notification to the node helper.
|
||||||
|
*
|
||||||
|
* argument notification string - The identifier of the noitication.
|
||||||
|
* argument payload mixed - The payload of the notification.
|
||||||
|
*/
|
||||||
|
sendSocketNotification: function(notification, payload) {
|
||||||
|
this.socket().sendNotification(notification, payload);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
NodeHelper.create = function(moduleDefinition) {
|
||||||
|
return NodeHelper.extend(moduleDefinition);
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = NodeHelper;
|
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
.weatherforecast .weather-icon {
|
.weatherforecast .weather-icon {
|
||||||
padding-right: 30px;
|
padding-right: 30px;
|
||||||
|
text-align: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
.weatherforecast .min-temp {
|
.weatherforecast .min-temp {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user