Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update node_helper.js #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions node_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,34 @@
* MIT Licensed.
*/
const NodeHelper = require("node_helper");
const CalendarFetcher = require("./calendarfetcher.js");
const Log = require("logger");
const CalendarFetcher = require("./calendarfetcher");

module.exports = NodeHelper.create({
// Override start method.
start: function () {
Log.log("Starting node helper for: " + this.name);
Log.log(`Starting node helper for: ${this.name}`);
this.fetchers = [];
},

// Override socketNotificationReceived method.
socketNotificationReceived: function (notification, payload) {
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();
}
},

/**
* Creates a fetcher for a new url if it doesn't exist yet.
* Otherwise it reuses the existing one.
*
* @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.
Expand All @@ -47,7 +54,7 @@ module.exports = NodeHelper.create({

let fetcher;
if (typeof this.fetchers[identifier + url] === "undefined") {
Log.log("Create new calendarfetcher for url: " + url + " - Interval: " + fetchInterval);
Log.log(`Create new calendarfetcher for url: ${url} - Interval: ${fetchInterval}`);
fetcher = new CalendarFetcher(url, fetchInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth, broadcastPastEvents, selfSignedCert);

fetcher.onReceive((fetcher) => {
Expand All @@ -65,7 +72,7 @@ module.exports = NodeHelper.create({

this.fetchers[identifier + url] = fetcher;
} else {
Log.log("Use existing calendarfetcher for url: " + url);
Log.log(`Use existing calendarfetcher for url: ${url}`);
fetcher = this.fetchers[identifier + url];
fetcher.broadcastEvents();
}
Expand All @@ -86,3 +93,4 @@ module.exports = NodeHelper.create({
});
}
});