-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
74 lines (64 loc) · 2.34 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
/* 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_WEEKLY") {
this.createFetcher(payload.url, payload.auth, payload.fetchInterval, payload.maximumNumberOfDays, payload.previousDaysOfEvents);
}
},
/* createFetcher(url, auth, reloadInterval, maximumNumberOfDays, previousDaysOfEvents)
* 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 auth array - Authentication credentials for the calendar.
* attribute fetchInterval number - how oftent to reload the calendar data.
* attribute maximumNumberOfDays number - Maximum days from today to get events for.
* attribute previousDaysOfEvents number - Number of days previous to today to get events for..
*/
createFetcher: function(url, auth, fetchInterval, maximumNumberOfDays, previousDaysOfEvents) {
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, auth, fetchInterval, maximumNumberOfDays, previousDaysOfEvents);
fetcher.onReceive(function(fetcher) {
self.sendSocketNotification("WEEKLY_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 {
fetcher = self.fetchers[url];
fetcher.broadcastEvents();
}
fetcher.startFetch();
}
});