-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathnode_helper.js
58 lines (48 loc) · 1.99 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
var NodeHelper = require("node_helper")
const moment = require("moment")
const icalGenerator = require("ical-generator")
const apiHelper = require("./google-api-helper");
module.exports = NodeHelper.create({
requiresVersion: '2.6.0', // 2.6.0 got a fix for recurring events before 1970, which is pretty usefull for birthdays :D
start: function() {
this._refreshData();
this.expressApp.use("/" + this.name, (req, res) => {
this.ical.serve(res)
})
this._log('Server is running')
},
stop: function() {
this._log("Stopping helper");
},
ical: icalGenerator({name: 'MMM-GoogleBirthdaysProvider', domain: 'mmm-googlebirthdaysprovider.local'}),
_createIcalEvents: function(birthdays) {
birthdays.forEach(person => {
var date = moment({ day: person.birthday.day,
month: person.birthday.month - 1,
year: person.birthday.year,
hour: 12, minute: 0 , second: 0} );
this.ical.createEvent({
start: date,
repeating: person.birthday.year ? { freq: 'YEARLY' } : undefined, // repeat yearly if a year is set
summary: `${person.name}`,
allDay: true
});
});
},
_refreshData: function() {
this.ical.clear();
apiHelper.getBirthdays(this.path)
.then(birthdays => this._createIcalEvents(birthdays))
.catch(reason => {
// TODO: Show notification on the mirror ?
this._error(`API Helper error: ${reason.message}: ${reason.err || ''}`);
});
},
// custom logger utility to supress output in CI env
_log: function(message) {
if (process.env.NODE_ENV !== 'test') { console.log(`${this.name}: ${message}`) }
},
_error: function(message) {
if (process.env.NODE_ENV !== 'test') { console.error(`${this.name}: ${message}`) }
}
});