96 lines
3.6 KiB
JavaScript
Raw Normal View History

/* MagicMirror²
2016-03-31 11:05:32 +02:00
* Node Helper: Calendar
*
* By Michael Teeuw https://michaelteeuw.nl
2016-03-31 11:05:32 +02:00
* MIT Licensed.
*/
const NodeHelper = require("node_helper");
2021-02-18 19:14:53 +01:00
const Log = require("logger");
const CalendarFetcher = require("./calendarfetcher");
2016-03-31 11:05:32 +02:00
module.exports = NodeHelper.create({
// Override start method.
start () {
Log.log(`Starting node helper for: ${this.name}`);
2016-03-31 11:05:32 +02:00
this.fetchers = [];
},
// Override socketNotificationReceived method.
socketNotificationReceived (notification, payload) {
2016-04-05 14:35:11 -04:00
if (notification === "ADD_CALENDAR") {
this.createFetcher(payload.url, payload.fetchInterval, payload.excludedEvents, payload.maximumEntries, payload.maximumNumberOfDays, payload.auth, payload.broadcastPastEvents, payload.selfSignedCert, payload.id);
} else if (notification === "FETCH_CALENDAR") {
const key = payload.id + payload.url;
if (typeof this.fetchers[key] === "undefined") {
Log.error("Calendar Error. No fetcher exists with key: ", key);
this.sendSocketNotification("CALENDAR_ERROR", { error_type: "MODULE_ERROR_UNSPECIFIED" });
return;
}
this.fetchers[key].startFetch();
2016-03-31 11:05:32 +02:00
}
},
2020-08-03 11:19:54 +02:00
/**
* Creates a fetcher for a new url if it doesn't exist yet.
* Otherwise it reuses the existing one.
2020-08-03 11:19:54 +02:00
* @param {string} url The url of the calendar
* @param {number} fetchInterval How often does the calendar needs to be fetched in ms
* @param {string[]} excludedEvents An array of words / phrases from event titles that will be excluded from being shown.
* @param {number} maximumEntries The maximum number of events fetched.
* @param {number} maximumNumberOfDays The maximum number of days an event should be in the future.
* @param {object} auth The object containing options for authentication against the calendar.
* @param {boolean} broadcastPastEvents If true events from the past maximumNumberOfDays will be included in event broadcasts
* @param {boolean} selfSignedCert If true, the server certificate is not verified against the list of supplied CAs.
2020-08-03 11:19:54 +02:00
* @param {string} identifier ID of the module
2016-03-31 11:05:32 +02:00
*/
createFetcher (url, fetchInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth, broadcastPastEvents, selfSignedCert, identifier) {
try {
new URL(url);
} catch (error) {
Log.error("Calendar Error. Malformed calendar url: ", url, error);
this.sendSocketNotification("CALENDAR_ERROR", { error_type: "MODULE_ERROR_MALFORMED_URL" });
2016-03-31 11:05:32 +02:00
return;
}
let fetcher;
if (typeof this.fetchers[identifier + url] === "undefined") {
Log.log(`Create new calendarfetcher for url: ${url} - Interval: ${fetchInterval}`);
fetcher = new CalendarFetcher(url, fetchInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth, broadcastPastEvents, selfSignedCert);
fetcher.onReceive((fetcher) => {
this.broadcastEvents(fetcher, identifier);
2016-03-31 11:05:32 +02:00
});
fetcher.onError((fetcher, error) => {
Log.error("Calendar Error. Could not fetch calendar: ", fetcher.url(), error);
let error_type = NodeHelper.checkFetchError(error);
this.sendSocketNotification("CALENDAR_ERROR", {
2021-05-02 14:43:12 +02:00
id: identifier,
error_type
2016-03-31 11:05:32 +02:00
});
});
this.fetchers[identifier + url] = fetcher;
2016-03-31 11:05:32 +02:00
} else {
Log.log(`Use existing calendarfetcher for url: ${url}`);
fetcher = this.fetchers[identifier + url];
fetcher.broadcastEvents();
2016-03-31 11:05:32 +02:00
}
fetcher.startFetch();
},
/**
*
2021-01-02 21:08:53 +01:00
* @param {object} fetcher the fetcher associated with the calendar
* @param {string} identifier the identifier of the calendar
*/
broadcastEvents (fetcher, identifier) {
this.sendSocketNotification("CALENDAR_EVENTS", {
id: identifier,
url: fetcher.url(),
events: fetcher.events()
});
2016-03-31 11:05:32 +02:00
}
});