Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add modify notification functionality #5

Merged
merged 1 commit into from
Mar 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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