From 140d580b788f514b9e8f1c155bacdf71ae913fb5 Mon Sep 17 00:00:00 2001 From: Brian Johnson Date: Fri, 26 Mar 2021 20:51:30 -0500 Subject: [PATCH] Add modify notification functionality And ID value is now returned when a notification is first created. That ID value can then be used to update or delete the notification. Closes #4 --- MMM-HomeAutomationNotifications.js | 40 ++++++++++++++++++++--- README.md | 8 +++++ node_helper.js | 52 ++++++++++++++++++++++++++---- 3 files changed, 90 insertions(+), 10 deletions(-) diff --git a/MMM-HomeAutomationNotifications.js b/MMM-HomeAutomationNotifications.js index a5a7e6b..d9ee690 100644 --- a/MMM-HomeAutomationNotifications.js +++ b/MMM-HomeAutomationNotifications.js @@ -10,6 +10,7 @@ Module.register("MMM-HomeAutomationNotifications", { requiresVersion: "2.12.0", notifications: [], + id: 1, defaults: { max: 5, @@ -48,23 +49,54 @@ Module.register("MMM-HomeAutomationNotifications", { socketNotificationReceived: function(notification, payload) { var self = this; + var timestamp = moment(); + var duration = moment.duration(this.config.duration, "m"); if (notification === "HOME_AUTOMATION_NOTIFICATION") { - var timestamp = moment(); - var duration = moment.duration(this.config.duration, "m"); + var id = self.generateId(); + self.notifications.push({ + id: id, type: payload.type, message: payload.message, timestamp: timestamp.toISOString(), expiration: timestamp.add(duration).toISOString() }); - + while (self.notifications.length > self.config.max) { self.notifications.shift(); } - self.updateDom(self.config.animationSpeed); + self.sendSocketNotification("HOME_AUTOMATION_NOTIFICATION_ID", id); + } else if (notification === "HOME_AUTOMATION_NOTIFICATION_UPDATE") { + var i = self.notifications.findIndex(x => x.id === payload.id); + self.notifications[i] = { + id: payload.id, + type: payload.type, + message: payload.message, + timestamp: timestamp.toISOString(), + expiration: timestamp.add(duration).toISOString() + }; + } else if (notification === "HOME_AUTOMATION_NOTIFICATION_DELETE") { + for (i = self.notifications.length - 1; i >= 0; i--) { + if (self.notifications[i].id == payload.id) { + self.notifications.splice(i, 1); + } + } + } + + self.updateDom(self.config.animationSpeed); + }, + + generateId: function() { + var self = this; + + var id = self.id++; + if (self.id > 100) { + self.id = 1; } + + return id; }, getDom: function() { diff --git a/README.md b/README.md index 7593af2..91b2927 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,14 @@ Notifications can be sent to the module with an HTTP POST request to this URL: Example request: `POST http://magicmirror:8080/MMM-HomeAutomationNotifications?type=WARNING&message=Front%20door%20unlocked` +The server will respond to the request with an ID value for the notification that was created. This value can be used in subsequent requests to update or delete the notification. + +A notification can be updated with an HTTP PUT request to this URL: +`PUT http://:/MMM-HomeAutomationNotifications?id=&type=&message=` + +A notificaiton can be deleted with an HTTP DELETE request to this URL: +`DELETE http://:/MMM-HomeAutomationNotifications?id=` + ## Configuration options The following properties can be configured: diff --git a/node_helper.js b/node_helper.js index 4a00b45..7b5dbb3 100644 --- a/node_helper.js +++ b/node_helper.js @@ -8,28 +8,68 @@ const NodeHelper = require("node_helper"); module.exports = NodeHelper.create({ + idPromise: null, socketNotificationReceived: function(notification, payload) { - this.sendSocketNotification("CONNECTED"); + 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', function (req, res) { + self.expressApp.post("/MMM-HomeAutomationNotifications", (req, res) => { if (!req.query.type) { - res.status(400).send("Query parameter type is required!"); + res.status(400).json({ error: "Query parameter type is required!" }); } else if (!types.includes(req.query.type)) { - res.status(400).send("Query parameter type value is invalid!"); + res.status(400).json({ error: "Query parameter type value is invalid!" }); } else if (!req.query.message) { - res.status(400).send("Query parameter message is required!"); + res.status(400).json({ error: "Query parameter message is required!" }); } else { - self.sendSocketNotification("HOME_AUTOMATION_NOTIFICATION", { + 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(); } });