-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
78 lines (63 loc) · 2.12 KB
/
node_helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* Magic Mirror
* Node Helper: Calendar
*
* By Michael Teeuw http://michaelteeuw.nl
* MIT Licensed.
*/
var NodeHelper = require("node_helper");
var validUrl = require("valid-url");
var CalendarFetcher = require("./calendarfetcher.js");
module.exports = NodeHelper.create({
// Override start method.
start: function() {
var events = [];
this.fetchers = [];
console.log("Starting node helper for: " + this.name);
},
// Override socketNotificationReceived method.
socketNotificationReceived: function(notification, payload) {
if (notification === "ADD_CALENDAR") {
//console.log('ADD_CALENDAR: ');
this.createFetcher(payload.url, payload.fetchInterval, payload.excludedEvents, payload.maximumEntries, payload.maximumNumberOfDays, payload.auth);
}
},
/* createFetcher(url, reloadInterval)
* Creates a fetcher for a new url if it doesn't exist yet.
* Otherwise it reuses the existing one.
*
* attribute url string - URL of the news feed.
* attribute reloadInterval number - Reload interval in milliseconds.
*/
createFetcher: function(url, fetchInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth) {
var self = this;
if (!validUrl.isUri(url)) {
self.sendSocketNotification("INCORRECT_URL", {url: url});
return;
}
var fetcher;
if (typeof self.fetchers[url] === "undefined") {
console.log("Create new calendar fetcher for url: " + url + " - Interval: " + fetchInterval);
fetcher = new CalendarFetcher(url, fetchInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth);
fetcher.onReceive(function(fetcher) {
//console.log('Broadcast events.');
//console.log(fetcher.events());
self.sendSocketNotification("CALENDAR_EVENTS", {
url: fetcher.url(),
events: fetcher.events()
});
});
fetcher.onError(function(fetcher, error) {
self.sendSocketNotification("FETCH_ERROR", {
url: fetcher.url(),
error: error
});
});
self.fetchers[url] = fetcher;
} else {
//console.log('Use existing news fetcher for url: ' + url);
fetcher = self.fetchers[url];
fetcher.broadcastEvents();
}
fetcher.startFetch();
}
});