-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode_helper.js
107 lines (87 loc) · 3.02 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* Magic Mirror
* Node Helper: MMM-Pomodoro
*
* By Toan Le
* MIT Licensed.
*/
var NodeHelper = require("node_helper");
var fs = require('fs');
var defaultData = { today: [] };
var dataPath = 'modules/MMM-Pomodoro/data.json';
module.exports = NodeHelper.create({
// Override socketNotificationReceived method.
/* socketNotificationReceived(notification, payload)
* This method is called when a socket notification arrives.
*
* argument notification string - The identifier of the noitication.
* argument payload mixed - The payload of the notification.
*/
socketNotificationReceived: function(notification, payload) {
if (notification === "MMM-Pomodoro-SAVEDATA") {
this.addSuccessPomodoro(payload);
// console.log("Working notification system. Notification:", notification, "payload: ", payload);
// Send notification
this.sendSocketNotification("MMM-Pomodoro-SAVEDATA", this.returnPayload());
} else if (notification === "MMM-Pomodoro-UPDATEDOM") {
let data = this.readData();
let todayCycles = this.filterTodayPomodoro(this.cycleByType(data.today, "pomodoro"));
let lastCompleted = todayCycles.reverse()[0];
this.sendSocketNotification("MMM-Pomodoro-UPDATEDOM", {
todayPomodoro: todayCycles.length,
lastCompleted: lastCompleted && (new Date(lastCompleted.time)).toLocaleString()
});
}
},
readData: function(){
var jsonData;
try {
jsonData = JSON.parse(fs.readFileSync(dataPath));
} catch (err) {
console.log(err);
jsonData = defaultData;
}
return jsonData;
},
filterTodayPomodoro: function(array) {
let midnightToday = new Date;
midnightToday.setHours(0,0,0,0);
return array.filter( x => Date.parse(x.time) >= midnightToday );
},
addSuccessPomodoro: function(payload){
let jsonData = this.readData();
jsonData.today = this.filterTodayPomodoro(jsonData.today);
jsonData.today.push(payload);
let data = JSON.stringify(jsonData, defaultData, 2);
fs.writeFileSync(dataPath, data, (err) => {
if (err) throw err;
});
},
detectNextCycleType: function(){
let todayCycles = this.readData().today.reverse();
if (todayCycles[0].type != "pomodoro") {
return "pomodoro";
} else {
let recentPomodoroCycles = todayCycles.filter( x => x.type == "pomodoro" || x.type == "long-relax" ).slice(0, 4);
for (var i = 0; i < recentPomodoroCycles.length - 1; i++) {
let diffTime = Math.abs(Date.parse(recentPomodoroCycles[i].time) - Date.parse(recentPomodoroCycles[i+1].time));
if (diffTime > 15*60*1000) {
return "short-relax";
}
}
if (recentPomodoroCycles.filter( x => x.type == "pomodoro").length == 4) {
return "long-relax";
} else {
return "short-relax";
}
}
},
cycleByType: function(array, type) {
return array.filter( x => x.type == type );
},
returnPayload: function() {
let data = this.readData();
return {
next_type: this.detectNextCycleType()
}
},
});