2016-03-31 11:05:32 +02:00
|
|
|
/* Magic Mirror
|
|
|
|
* Node Helper: Calendar
|
|
|
|
*
|
2020-06-20 11:01:37 -07:00
|
|
|
* By Michael Teeuw http://michaelteeuw.nl
|
2016-03-31 11:05:32 +02:00
|
|
|
* MIT Licensed.
|
|
|
|
*/
|
|
|
|
|
2020-06-20 11:01:37 -07:00
|
|
|
var NodeHelper = require("node_helper");
|
|
|
|
var validUrl = require("valid-url");
|
|
|
|
var CalendarFetcher = require("./calendarfetcher.js");
|
2016-03-31 11:05:32 +02:00
|
|
|
|
|
|
|
module.exports = NodeHelper.create({
|
|
|
|
// Override start method.
|
2020-06-20 11:01:37 -07:00
|
|
|
start: function() {
|
|
|
|
var events = [];
|
|
|
|
|
2016-03-31 11:05:32 +02:00
|
|
|
this.fetchers = [];
|
2020-06-20 11:01:37 -07:00
|
|
|
|
|
|
|
console.log("Starting node helper for: " + this.name);
|
|
|
|
|
2016-03-31 11:05:32 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Override socketNotificationReceived method.
|
2020-06-20 11:01:37 -07:00
|
|
|
socketNotificationReceived: function(notification, payload) {
|
2016-04-05 14:35:11 -04:00
|
|
|
if (notification === "ADD_CALENDAR") {
|
2020-06-20 11:01:37 -07:00
|
|
|
//console.log('ADD_CALENDAR: ');
|
|
|
|
this.createFetcher(payload.url, payload.fetchInterval, payload.excludedEvents, payload.maximumEntries, payload.maximumNumberOfDays, payload.auth, payload.broadcastPastEvents);
|
2016-03-31 11:05:32 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/* createFetcher(url, reloadInterval)
|
2016-06-04 20:32:55 -06:00
|
|
|
* Creates a fetcher for a new url if it doesn't exist yet.
|
|
|
|
* Otherwise it reuses the existing one.
|
2016-03-31 11:05:32 +02:00
|
|
|
*
|
|
|
|
* attribute url string - URL of the news feed.
|
|
|
|
* attribute reloadInterval number - Reload interval in milliseconds.
|
|
|
|
*/
|
2020-06-20 11:01:37 -07:00
|
|
|
|
|
|
|
createFetcher: function(url, fetchInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth, broadcastPastEvents) {
|
2016-03-31 11:05:32 +02:00
|
|
|
var self = this;
|
|
|
|
|
2016-04-05 14:35:11 -04:00
|
|
|
if (!validUrl.isUri(url)) {
|
2020-06-20 11:01:37 -07:00
|
|
|
self.sendSocketNotification("INCORRECT_URL", {url: url});
|
2016-03-31 11:05:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var fetcher;
|
2020-06-20 11:01:37 -07:00
|
|
|
if (typeof self.fetchers[url] === "undefined") {
|
|
|
|
console.log("Create new calendar fetcher for url: " + url + " - Interval: " + fetchInterval);
|
|
|
|
fetcher = new CalendarFetcher(url, fetchInterval, excludedEvents, maximumNumberOfDays, auth, broadcastPastEvents);
|
|
|
|
|
|
|
|
fetcher.onReceive(function(fetcher) {
|
|
|
|
//console.log('Broadcast events.');
|
|
|
|
//console.log(fetcher.events());
|
2016-04-03 19:52:13 +02:00
|
|
|
|
2016-04-05 14:35:11 -04:00
|
|
|
self.sendSocketNotification("CALENDAR_EVENTS", {
|
2016-03-31 11:05:32 +02:00
|
|
|
url: fetcher.url(),
|
|
|
|
events: fetcher.events()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-20 11:01:37 -07:00
|
|
|
fetcher.onError(function(fetcher, error) {
|
|
|
|
console.error("Calendar Error. Could not fetch calendar: ", fetcher.url(), error);
|
2016-04-05 14:35:11 -04:00
|
|
|
self.sendSocketNotification("FETCH_ERROR", {
|
2016-03-31 11:05:32 +02:00
|
|
|
url: fetcher.url(),
|
|
|
|
error: error
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-20 11:01:37 -07:00
|
|
|
self.fetchers[url] = fetcher;
|
2016-03-31 11:05:32 +02:00
|
|
|
} else {
|
2020-06-20 11:01:37 -07:00
|
|
|
//console.log('Use existing news fetcher for url: ' + url);
|
|
|
|
fetcher = self.fetchers[url];
|
2016-03-31 11:05:32 +02:00
|
|
|
fetcher.broadcastEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
fetcher.startFetch();
|
|
|
|
}
|
|
|
|
});
|