Attach identifier to socket notifications to allow multiple instances

This commit is contained in:
DarthBrento 2020-06-01 13:12:54 +02:00
parent d31b696846
commit aeb287fa1d
2 changed files with 12 additions and 6 deletions

View File

@ -122,6 +122,9 @@ Module.register("calendar", {
// Override socket notification handler. // Override socket notification handler.
socketNotificationReceived: function (notification, payload) { socketNotificationReceived: function (notification, payload) {
if (this.identifier != payload.id)
{return;}
if (notification === "CALENDAR_EVENTS") { if (notification === "CALENDAR_EVENTS") {
if (this.hasCalendarURL(payload.url)) { if (this.hasCalendarURL(payload.url)) {
this.calendarData[payload.url] = payload.events; this.calendarData[payload.url] = payload.events;
@ -535,6 +538,7 @@ Module.register("calendar", {
*/ */
addCalendar: function (url, auth, calendarConfig) { addCalendar: function (url, auth, calendarConfig) {
this.sendSocketNotification("ADD_CALENDAR", { this.sendSocketNotification("ADD_CALENDAR", {
id: this.identifier,
url: url, url: url,
excludedEvents: calendarConfig.excludedEvents || this.config.excludedEvents, excludedEvents: calendarConfig.excludedEvents || this.config.excludedEvents,
maximumEntries: calendarConfig.maximumEntries || this.config.maximumEntries, maximumEntries: calendarConfig.maximumEntries || this.config.maximumEntries,

View File

@ -23,7 +23,7 @@ module.exports = NodeHelper.create({
socketNotificationReceived: function (notification, payload) { socketNotificationReceived: function (notification, payload) {
if (notification === "ADD_CALENDAR") { if (notification === "ADD_CALENDAR") {
//console.log('ADD_CALENDAR: '); //console.log('ADD_CALENDAR: ');
this.createFetcher(payload.url, payload.fetchInterval, payload.excludedEvents, payload.maximumEntries, payload.maximumNumberOfDays, payload.auth, payload.broadcastPastEvents); this.createFetcher(payload.url, payload.fetchInterval, payload.excludedEvents, payload.maximumEntries, payload.maximumNumberOfDays, payload.auth, payload.broadcastPastEvents, payload.id);
} }
}, },
@ -35,16 +35,16 @@ module.exports = NodeHelper.create({
* attribute reloadInterval number - Reload interval in milliseconds. * attribute reloadInterval number - Reload interval in milliseconds.
*/ */
createFetcher: function (url, fetchInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth, broadcastPastEvents) { createFetcher: function (url, fetchInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth, broadcastPastEvents, identifier) {
var self = this; var self = this;
if (!validUrl.isUri(url)) { if (!validUrl.isUri(url)) {
self.sendSocketNotification("INCORRECT_URL", { url: url }); self.sendSocketNotification("INCORRECT_URL", { id: identifier, url: url });
return; return;
} }
var fetcher; var fetcher;
if (typeof self.fetchers[url] === "undefined") { if (typeof self.fetchers[identifier + url] === "undefined") {
console.log("Create new calendar fetcher for url: " + url + " - Interval: " + fetchInterval); console.log("Create new calendar fetcher for url: " + url + " - Interval: " + fetchInterval);
fetcher = new CalendarFetcher(url, fetchInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth, broadcastPastEvents); fetcher = new CalendarFetcher(url, fetchInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth, broadcastPastEvents);
@ -53,6 +53,7 @@ module.exports = NodeHelper.create({
//console.log(fetcher.events()); //console.log(fetcher.events());
self.sendSocketNotification("CALENDAR_EVENTS", { self.sendSocketNotification("CALENDAR_EVENTS", {
id: identifier,
url: fetcher.url(), url: fetcher.url(),
events: fetcher.events() events: fetcher.events()
}); });
@ -61,15 +62,16 @@ module.exports = NodeHelper.create({
fetcher.onError(function (fetcher, error) { fetcher.onError(function (fetcher, error) {
console.error("Calendar Error. Could not fetch calendar: ", fetcher.url(), error); console.error("Calendar Error. Could not fetch calendar: ", fetcher.url(), error);
self.sendSocketNotification("FETCH_ERROR", { self.sendSocketNotification("FETCH_ERROR", {
id: identifier,
url: fetcher.url(), url: fetcher.url(),
error: error error: error
}); });
}); });
self.fetchers[url] = fetcher; self.fetchers[identifier + url] = fetcher;
} else { } else {
//console.log('Use existing news fetcher for url: ' + url); //console.log('Use existing news fetcher for url: ' + url);
fetcher = self.fetchers[url]; fetcher = self.fetchers[identifier + url];
fetcher.broadcastEvents(); fetcher.broadcastEvents();
} }