Skip to content

Commit

Permalink
Merge pull request #5 from john3300/modify-notifications
Browse files Browse the repository at this point in the history
Add modify notification functionality
  • Loading branch information
john3300 authored Mar 27, 2021
2 parents 3ed34c2 + 140d580 commit 2efc831
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 10 deletions.
40 changes: 36 additions & 4 deletions MMM-HomeAutomationNotifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Module.register("MMM-HomeAutomationNotifications", {
requiresVersion: "2.12.0",

notifications: [],
id: 1,

defaults: {
max: 5,
Expand Down Expand Up @@ -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() {
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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://<SERVER-ADDRESS>:<PORT>/MMM-HomeAutomationNotifications?id=<ID>&type=<TYPE>&message=<MESSAGE>`

A notificaiton can be deleted with an HTTP DELETE request to this URL:
`DELETE http://<SERVER-ADDRESS>:<PORT>/MMM-HomeAutomationNotifications?id=<ID>`

## Configuration options

The following properties can be configured:
Expand Down
52 changes: 46 additions & 6 deletions node_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});
Expand Down

0 comments on commit 2efc831

Please sign in to comment.