forked from john3300/MMM-HomeAutomationNotifications
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
77 lines (68 loc) · 2.25 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
/* Magic Mirror
* Node Helper: MMM-HomeAutomationNotifications
*
* By Brian Johnson
* MIT Licensed
*/
const NodeHelper = require("node_helper");
module.exports = NodeHelper.create({
idPromise: null,
socketNotificationReceived: function(notification, payload) {
var self = this;
self.sendSocketNotification("CONNECTED");
if (notification === "HOME_AUTOMATION_NOTIFICATION_ID") {
self.idPromise(payload);
}
},
start: function() {
var self = this;
var types = ["INFO", "WARNING", "ERROR"];
self.expressApp.post("/MMM-HomeAutomationNotifications", (req, res) => {
if (!req.query.type) {
res.status(400).json({ error: "Query parameter type is required!" });
} else if (!types.includes(req.query.type)) {
res.status(400).json({ error: "Query parameter type value is invalid!" });
} else if (!req.query.message) {
res.status(400).json({ error: "Query parameter message is required!" });
} else {
new Promise((resolve, reject) => {
self.idPromise = resolve;
self.sendSocketNotification("HOME_AUTOMATION_NOTIFICATION", {
type: req.query.type,
message: req.query.message
});
}).then((id) => {
res.status(201).json({ id: id });
});
}
});
self.expressApp.put("/MMM-HomeAutomationNotifications", (req, res) => {
if (!req.query.id) {
res.status(400).json({ error: "Query parameter id is required!" });
} else if (!req.query.type) {
res.status(400).json({ error: "Query parameter type is required!" });
} else if (!types.includes(req.query.type)) {
res.status(400).json({ error: "Query parameter type value is invalid!" });
} else if (!req.query.message) {
res.status(400).json({ error: "Query parameter message is required!" });
} else {
self.sendSocketNotification("HOME_AUTOMATION_NOTIFICATION_UPDATE", {
id: req.query.id,
type: req.query.type,
message: req.query.message
});
res.status(204).end();
}
});
self.expressApp.delete("/MMM-HomeAutomationNotifications", (req, res) => {
if (!req.query.id) {
res.status(400).json({ error: "Query parameter id is required!" });
} else {
self.sendSocketNotification("HOME_AUTOMATION_NOTIFICATION_DELETE", {
id: req.query.id
});
res.status(204).end();
}
});
}
});