2016-03-31 11:05:32 +02:00
|
|
|
/* Magic Mirror
|
|
|
|
* Node Helper: Calendar
|
|
|
|
*
|
2020-04-28 23:05:28 +02:00
|
|
|
* By Michael Teeuw https://michaelteeuw.nl
|
2016-03-31 11:05:32 +02:00
|
|
|
* MIT Licensed.
|
|
|
|
*/
|
|
|
|
|
2020-05-25 23:03:19 +02:00
|
|
|
const NodeHelper = require("node_helper");
|
|
|
|
const validUrl = require("valid-url");
|
|
|
|
const CalendarFetcher = require("./calendarfetcher.js");
|
|
|
|
const Logger = require("../../../js/logger");
|
2016-03-31 11:05:32 +02:00
|
|
|
|
|
|
|
module.exports = NodeHelper.create({
|
|
|
|
// Override start method.
|
2020-05-11 22:22:32 +02:00
|
|
|
start: function () {
|
2020-05-25 23:03:19 +02:00
|
|
|
Logger.log("Starting node helper for: " + this.name);
|
2016-03-31 11:05:32 +02:00
|
|
|
this.fetchers = [];
|
|
|
|
},
|
|
|
|
|
|
|
|
// Override socketNotificationReceived method.
|
2020-05-11 22:22:32 +02:00
|
|
|
socketNotificationReceived: function (notification, payload) {
|
2016-04-05 14:35:11 -04:00
|
|
|
if (notification === "ADD_CALENDAR") {
|
2020-06-01 13:12:54 +02:00
|
|
|
this.createFetcher(payload.url, payload.fetchInterval, payload.excludedEvents, payload.maximumEntries, payload.maximumNumberOfDays, payload.auth, payload.broadcastPastEvents, payload.id);
|
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-01 13:12:54 +02:00
|
|
|
createFetcher: function (url, fetchInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth, broadcastPastEvents, identifier) {
|
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-01 13:12:54 +02:00
|
|
|
self.sendSocketNotification("INCORRECT_URL", { id: identifier, url: url });
|
2016-03-31 11:05:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var fetcher;
|
2020-06-01 13:12:54 +02:00
|
|
|
if (typeof self.fetchers[identifier + url] === "undefined") {
|
2020-05-25 23:03:19 +02:00
|
|
|
Logger.log("Create new calendar fetcher for url: " + url + " - Interval: " + fetchInterval);
|
2019-04-10 16:50:18 -04:00
|
|
|
fetcher = new CalendarFetcher(url, fetchInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth, broadcastPastEvents);
|
2016-04-03 19:52:13 +02:00
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
fetcher.onReceive(function (fetcher) {
|
2016-04-05 14:35:11 -04:00
|
|
|
self.sendSocketNotification("CALENDAR_EVENTS", {
|
2020-06-01 13:12:54 +02:00
|
|
|
id: identifier,
|
2016-03-31 11:05:32 +02:00
|
|
|
url: fetcher.url(),
|
|
|
|
events: fetcher.events()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
fetcher.onError(function (fetcher, error) {
|
2020-05-25 23:03:19 +02:00
|
|
|
Logger.error("Calendar Error. Could not fetch calendar: ", fetcher.url(), error);
|
2016-04-05 14:35:11 -04:00
|
|
|
self.sendSocketNotification("FETCH_ERROR", {
|
2020-06-01 13:12:54 +02:00
|
|
|
id: identifier,
|
2016-03-31 11:05:32 +02:00
|
|
|
url: fetcher.url(),
|
|
|
|
error: error
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-01 13:12:54 +02:00
|
|
|
self.fetchers[identifier + url] = fetcher;
|
2016-03-31 11:05:32 +02:00
|
|
|
} else {
|
2020-05-25 23:03:19 +02:00
|
|
|
Logger.log("Use existing calendar fetcher for url: " + url);
|
2020-06-01 13:12:54 +02:00
|
|
|
fetcher = self.fetchers[identifier + url];
|
2016-03-31 11:05:32 +02:00
|
|
|
fetcher.broadcastEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
fetcher.startFetch();
|
|
|
|
}
|
|
|
|
});
|