diff --git a/lib/post.js b/lib/post.js index 75f0b79..ca8c391 100644 --- a/lib/post.js +++ b/lib/post.js @@ -2,18 +2,18 @@ const got = require('got') const token = require('basic-auth-token'); module.exports = { - postRequest: async () => { + postRequest: async (body, url) => { const authToken = token(process.env.username, process.env.password) const options = { method: 'POST', - json: {}, + json: JSON.parse(body), headers: { "User-Agent-Wiser": "iPhoneTestTool;iOS6;WiserApp2.0.0", "Authorization": "Basic " + authToken } } try { - const response = await got('https://public.wcs.schneider-electric.ws/rpc/public_genie/poll', options); + const response = await got('https://public.wcs.schneider-electric.ws/rpc/public_genie/' + url, options); const res = JSON.parse(response.body) return res } catch (error) { diff --git a/package-lock.json b/package-lock.json index 1e8bc17..8a86b90 100644 --- a/package-lock.json +++ b/package-lock.json @@ -419,11 +419,6 @@ "resolved": "https://registry.npmjs.org/normalize-url/-/normalize-url-4.5.0.tgz", "integrity": "sha512-2s47yzUxdexf1OhyRi4Em83iQk0aPvwTddtFz4hnSSw9dCEsLEGf6SwIO8ss/19S9iBb5sJaOuTvTGDeZI00BQ==" }, - "numeral": { - "version": "2.0.6", - "resolved": "https://registry.npmjs.org/numeral/-/numeral-2.0.6.tgz", - "integrity": "sha1-StCAk21EPCVhrtnyGX7//iX05QY=" - }, "on-finished": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz", diff --git a/package.json b/package.json index c0eb5d6..6d88356 100644 --- a/package.json +++ b/package.json @@ -14,8 +14,9 @@ "license": "ISC", "dependencies": { "basic-auth-token": "^0.4.2", + "body-parser": "^1.19.0", "express": "^4.17.1", "fs": "0.0.1-security", "got": "^11.1.1" - } + } } diff --git a/routes/heating.js b/routes/heating_get.js similarity index 85% rename from routes/heating.js rename to routes/heating_get.js index 2bb8ef5..79d60b3 100644 --- a/routes/heating.js +++ b/routes/heating_get.js @@ -3,13 +3,13 @@ const common = require('../lib/common') module.exports = function (app) { app.get("/heating", async function (req, res, next) { - const response = await post.postRequest() + const response = await post.postRequest('{}', 'poll') const heating = response.updateData.zones[0] res.json(heating) }); app.get("/heating/status", async function (req, res, next) { - const response = await post.postRequest() + const response = await post.postRequest('{}', 'poll') const heating = response.updateData.zones[0].status const mrt = await common.updateTemp(heating) const on = await common.heatingOn(heating) @@ -27,7 +27,7 @@ module.exports = function (app) { }); app.get("/heating/config", async function (req, res, next) { - const response = await post.postRequest() + const response = await post.postRequest('{}', 'poll') const heating = response.updateData.zones[0].config res.json(heating) }); diff --git a/routes/heating_post.js b/routes/heating_post.js new file mode 100644 index 0000000..57526eb --- /dev/null +++ b/routes/heating_post.js @@ -0,0 +1,11 @@ +const post = require('../lib/post') +const common = require('../lib/common') + +module.exports = function (app) { + app.post("/heating", async function (req, res, next) { + const response = await post.postRequest('{}') + const heating = response.updateData.zones[0] + res.json(heating) + }); + +} \ No newline at end of file diff --git a/routes/water.js b/routes/water_get.js similarity index 83% rename from routes/water.js rename to routes/water_get.js index 55176a1..8b8577d 100644 --- a/routes/water.js +++ b/routes/water_get.js @@ -3,13 +3,13 @@ const common = require('../lib/common') module.exports = function (app) { app.get("/water", async function (req, res, next) { - const response = await post.postRequest() + const response = await post.postRequest('{}', 'poll') const water = response.updateData.zones[1] res.json(water) }); app.get("/water/status", async function (req, res, next) { - const response = await post.postRequest() + const response = await post.postRequest('{}', 'poll') const water = response.updateData.zones[1].status const on = await common.heatingOn(water) @@ -23,7 +23,7 @@ module.exports = function (app) { }); app.get("/water/config", async function (req, res, next) { - const response = await post.postRequest() + const response = await post.postRequest('{}', 'poll') const water = response.updateData.zones[1].config res.json(water) }); diff --git a/routes/water_post.js b/routes/water_post.js new file mode 100644 index 0000000..0678648 --- /dev/null +++ b/routes/water_post.js @@ -0,0 +1,19 @@ +const post = require('../lib/post') +const common = require('../lib/common') +const bodyParser = require('body-parser'); + +module.exports = function (app) { + app.use(bodyParser.urlencoded({ extended: true })); + + app.post("/water", async function (req, res, next) { + if (req.query.switch === '1') { + await post.postRequest('{"zoneId":[1],"setPoint": 255,"durationMinutes": 90}', 'apply_timer') + } else if (req.query.switch === '0') { + await post.postRequest('{"zoneId":[1]}', 'cancel_timer') + } else { + throw new Error + } + res.json(req.query) + }); + +} \ No newline at end of file