From c5e80c2944fe0211c173c73db4e452114c9a38b2 Mon Sep 17 00:00:00 2001 From: Bas van Rooten Date: Mon, 2 Sep 2019 14:33:22 +0200 Subject: [PATCH 1/2] Added getSmartPlug Endpoint for getting smartplug details --- controllers/plug_controller.js | 48 ++++++++++++++++++++++++++++++++++ package-lock.json | 41 +++++++++++++++++++++-------- routes/plug_routes.js | 1 + 3 files changed, 79 insertions(+), 11 deletions(-) diff --git a/controllers/plug_controller.js b/controllers/plug_controller.js index 58512cc..86ee572 100644 --- a/controllers/plug_controller.js +++ b/controllers/plug_controller.js @@ -152,5 +152,53 @@ module.exports = { "is_active": localStorage.getItem(req.params.plugID) }); } + }, + + getSmartPlug(req, res, next) { + // Return Smartplug details + + AuthenticationManager.getSessionKey().then(sessionkey => { + + // Check if session key is valid + if (sessionkey != "ERROR") { + + axios({ + method: 'get', + url: 'https://plug.homewizard.com/plugs', + headers: { + "x-session-token": sessionkey + } + }) + .then(response => { + response.data.map((smartplug) => { + + // Check if no smartplugs exist + if (smartplug.length == 0) { + res.status(200).send(new ApiResponse("No smartplugs found", 200)); + } else { + + // Create custom response based on response data + res.status(200).send({ + "id": smartplug.id, + "name": smartplug.name, + "online": smartplug.online, + "latitude": smartplug.latitude, + "longtitude": smartplug.longtitude, + "timeZone": smartplug.timeZone, + "firmwareUpdateAvailable": smartplug.firmwareUpdateAvailable + }); + } + }) + }) + .catch(e => { + // Cannot communicate with HWL, returning error + logger.error("Failed to communicate with HWL! ERROR: ", e.message); + res.status(503).send(new ApiResponse(e.message, 503)); + }); + } else { + // Session key is invalid + res.status(503).send(new ApiResponse("Invalid session key! Check the logs for more details about this problem ", 503)); + } + }); } } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 2564861..7178c40 100644 --- a/package-lock.json +++ b/package-lock.json @@ -824,7 +824,8 @@ }, "ansi-regex": { "version": "2.1.1", - "bundled": true + "bundled": true, + "optional": true }, "aproba": { "version": "1.2.0", @@ -842,11 +843,13 @@ }, "balanced-match": { "version": "1.0.0", - "bundled": true + "bundled": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -859,15 +862,18 @@ }, "code-point-at": { "version": "1.1.0", - "bundled": true + "bundled": true, + "optional": true }, "concat-map": { "version": "0.0.1", - "bundled": true + "bundled": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", - "bundled": true + "bundled": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -970,7 +976,8 @@ }, "inherits": { "version": "2.0.3", - "bundled": true + "bundled": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -980,6 +987,7 @@ "is-fullwidth-code-point": { "version": "1.0.0", "bundled": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -992,17 +1000,20 @@ "minimatch": { "version": "3.0.4", "bundled": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } }, "minimist": { "version": "0.0.8", - "bundled": true + "bundled": true, + "optional": true }, "minipass": { "version": "2.3.5", "bundled": true, + "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -1019,6 +1030,7 @@ "mkdirp": { "version": "0.5.1", "bundled": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -1091,7 +1103,8 @@ }, "number-is-nan": { "version": "1.0.1", - "bundled": true + "bundled": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -1101,6 +1114,7 @@ "once": { "version": "1.4.0", "bundled": true, + "optional": true, "requires": { "wrappy": "1" } @@ -1176,7 +1190,8 @@ }, "safe-buffer": { "version": "5.1.2", - "bundled": true + "bundled": true, + "optional": true }, "safer-buffer": { "version": "2.1.2", @@ -1206,6 +1221,7 @@ "string-width": { "version": "1.0.2", "bundled": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -1223,6 +1239,7 @@ "strip-ansi": { "version": "3.0.1", "bundled": true, + "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -1261,11 +1278,13 @@ }, "wrappy": { "version": "1.0.2", - "bundled": true + "bundled": true, + "optional": true }, "yallist": { "version": "3.0.3", - "bundled": true + "bundled": true, + "optional": true } } }, diff --git a/routes/plug_routes.js b/routes/plug_routes.js index 1a99408..78ac2bb 100644 --- a/routes/plug_routes.js +++ b/routes/plug_routes.js @@ -3,6 +3,7 @@ const PlugController = require('../controllers/plug_controller'); // Plug routes routes.get('/plug', PlugController.getAllPlugs); +routes.get('/smartplug', PlugController.getSmartPlug); routes.post('/act/smartplug/:smartPlugID/plug/:plugID', PlugController.switchPlug); routes.get('/act/smartplug/:smartPlugID/plug/:plugID', PlugController.plugState); From 269670dce5a60056d2db492c62b0f0f074433a3c Mon Sep 17 00:00:00 2001 From: Bas van Rooten <33634284+basvanrooten@users.noreply.github.com> Date: Sun, 16 Aug 2020 21:28:08 +0200 Subject: [PATCH 2/2] Create Dockerfile --- Dockerfile | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4bc76fa --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM node:10 + +# Create directory +WORKDIR /usr/src/app + +# Copy Package.json +COPY package*.json ./ + +RUN npm install + +# Copy app source +COPY . . + +# !! MAKE SURE THIS IS THE RIGHT PORT YOU WANT TO USE !! +EXPOSE 3000 + +CMD ["node", "index.js"]