forked from schnibel/MMM-Memo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_helper.js
executable file
·174 lines (147 loc) · 5.23 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* Magic Mirror
* Module: MMM-Memo
*
* By Schnibel @schnibel
* March 2017
* MIT Licensed.
*
*/
const NodeHelper = require("node_helper");
const url = require("url");
const fs = require("fs");
module.exports = NodeHelper.create({
start: function() {
this.expressApp.get('/AddMemo', (req, res) => {
var query = url.parse(req.url, true).query;
var level = query.level;
var memoTitle = query.memoTitle;
var item = query.item;
if (typeof level === "undefined") {
level = "INFO";
console.log("automatically set level to INFO");
}
if (memoTitle == null && item == null && level == null) {
res.send({"status": "failed", "error": "No 'memoTitle', 'level' and 'item' given."});
}
else if (memoTitle == null) {
res.send({"status": "failed", "error": "No 'memoTitle' given."});
}
else if (item == null){
res.send({"status": "failed", "error": "No 'item' given."});
}
else if (level == null) {
res.send({"status": "failed", "error": "No 'level' given."});
}
else {
var new_item = {"memoTitle": memoTitle.toLowerCase(), "level": level, "item": item, "timestamp": new Date()};
res.send({"status": "success", "item": new_item});
this.sendSocketNotification("ADD", new_item);
this.storeMemos(new_item);
}
}),
this.expressApp.get('/RemoveMemo', (req, res) => {
var query = url.parse(req.url, true).query;
var memoTitle = query.memoTitle;
var item = query.item;
if (memoTitle == null && item == null){
res.send({"status": "failed", "error": "No 'memoTitle' and 'item' given."});
}
else if (memoTitle == null) {
res.send({"status": "failed", "error": "No 'memoTitle' given."});
}
else if (item == null) {
res.send({"status": "failed", "error": "No 'item' given."});
}
else {
var old_item = {"memoTitle": memoTitle.toLowerCase(), "item": item};
res.send({"status": "success", "payload": old_item});
this.removeMemos(old_item);
this.sendSocketNotification("INIT", this.memos);
}
});
this.expressApp.get('/DisplayMemo', (req, res) => {
var query = url.parse(req.url, true).query;
var memoTitle = query.memoTitle;
var item = query.item;
if (memoTitle == null && item == null){
res.send({"status": "failed", "error": "No 'memoTitle' and 'item' given."});
}
else if (memoTitle == null) {
res.send({"status": "failed", "error": "No 'memoTitle' given."});
}
else if (item == null) {
res.send({"status": "failed", "error": "No 'item' given."});
}
else {
var display_item = {"memoTitle": memoTitle.toLowerCase(), "item": item};
res.send({"status": "success", "payload": display_item});
this.displayMemos(display_item);
}
});
},
socketNotificationReceived: function(notification, payload) {
if(notification === "LOAD_MEMOS"){
this.memoTitle = payload.memoTitle.toLowerCase();
this.memoMaxItems = payload.memoMaxItems;
this.memoFilename = payload.memoFilename;
this.loadMemos();
}
},
storeMemos: function(item){
this.memos.push(item);
fs.writeFileSync(this.memoFilename, JSON.stringify({"memos": this.memos}), 'utf8');
},
loadMemos: function(){
if(this.fileExists(this.memoFilename)){
this.memos = JSON.parse(fs.readFileSync(this.memoFilename, 'utf8')).memos;
this.sendSocketNotification("INIT", this.memos);
} else {
this.memos = [];
}
},
removeMemos: function(old_item){
var j = 0;
for (var i = this.memos.length - 1; i >= 0; i--) {
if (this.memos[i].memoTitle.toLowerCase() == old_item.memoTitle.toLowerCase()) {
if (old_item.item == 'ALL') this.memos.splice(i, 1);
else {
j = j+1;
if (j == old_item.item) this.memos.splice(i, 1);
}
}
}
if (j == 0) console.log("The memoTitle '"+old_item.memoTitle.toLowerCase()+"' doesn't exist");
fs.writeFileSync(this.memoFilename, JSON.stringify({"memos": this.memos}), 'utf8');
},
displayMemos: function(item){
// Create a temporary array to deal with expected memos
var tempMemos = [];
for (var i = 0; i < this.memos.length ; i++ ) {
if (this.memos[i].memoTitle.toLowerCase() == item.memoTitle.toLowerCase()) {
tempMemos.push(this.memos[i]);
}
}
var j = 0;
// If ALL is specified, send the whole array
if (item.item == 'ALL') {
j = -1;
this.sendSocketNotification("DISPLAY", tempMemos);
}
// else, find the expected item
else {
for (var i = tempMemos.length - 1; i >= 0; i--) {
j = j+1;
if (j == item.item) this.sendSocketNotification("DISPLAY", tempMemos[i]);
}
}
if (j == 0) console.log("The memoTitle '"+item.memoTitle.toLowerCase()+"' doesn't exist");
},
fileExists: function(path){
try {
return fs.statSync(path).isFile();
} catch(e) {
console.log("No memo file found.");
return false;
}
}
});