forked from thobach/MMM-MicrosoftToDo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_helper.js
More file actions
76 lines (67 loc) · 2.38 KB
/
node_helper.js
File metadata and controls
76 lines (67 loc) · 2.38 KB
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
/*
Node Helper module for MMM-MicrosoftToDo
Purpose: Microsoft's OAutht 2.0 Token API endpoint does not support CORS,
therefore we cannot make AJAX calls from the browser without disabling
webSecurity in Electron.
*/
var NodeHelper = require("node_helper");
const Log = require("logger");
const Client = require("./MicrosoftToDoClient");
const clients = {};
const intervals = {};
const todos = {};
module.exports = NodeHelper.create({
init: function () {
Log.info(`[MMM-MicrosoftToDo] node_helper init ...`);
},
start: function () {
Log.info(`[${this.name}] node_helper started ...`);
},
stop: function () {
Object.keys(intervals).forEach(clearInterval);
Log.info(`[${this.name}] node_helper shutting down ...`);
},
socketNotificationReceived: function (notification, payload) {
Log.debug(`server --> ${notification} ${payload.id}`);
if (notification === "FETCH_DATA") {
if (!intervals[payload.id]) {
var self = this;
this.fetchData(payload);
intervals[payload.id] = setInterval(() => self.fetchData(payload), payload.refreshSeconds * 1000);
Log.debug(`[${payload.id}] interval started with the id ${intervals[payload.id]}`);
} else {
if (todos[payload.id] && todos[payload.id].length) {
self.sendSocketNotification(`DATA_FETCHED_${payload.id}`, todos[payload.id]);
}
Log.debug(`[${payload.id}] interval exists with the id ${intervals[payload.id]}`);
}
} else if (notification === "COMPLETE_TASK") {
this.completeTask(payload.listId, payload.taskId, payload.config);
} else {
Log.warn(`[${config.id}] - did not process event: ${notification}`);
}
},
getClient: function (config) {
if (!clients[config.id]) {
clients[config.id] = new Client(config);
}
return clients[config.id];
},
completeTask: function (listId, taskId, config) {
Log.error(`[${config.id}] completeTask are not implemented yet`);
},
fetchData: function (config) {
const self = this;
const client = this.getClient(config);
client
.getTodos(config)
.then(tasks => {
todos[config.id] = tasks;
self.sendSocketNotification(`DATA_FETCHED_${config.id}`, tasks);
})
.catch(error => {
Log.error(`[${config.id}] - ${error}`);
self.sendSocketNotification(`FETCH_INFO_ERROR_${config.id}`, error);
});
},
});