diff --git a/lib/common.js b/lib/common.js index 70cac8d..f43b9fd 100644 --- a/lib/common.js +++ b/lib/common.js @@ -29,6 +29,10 @@ module.exports = { value let heatMap = [ + { + "miGenie": 0, + "realTemp": 0 + }, { "miGenie": 84, "realTemp": 22 diff --git a/lib/post.js b/lib/post.js index ca8c391..729378b 100644 --- a/lib/post.js +++ b/lib/post.js @@ -17,7 +17,7 @@ module.exports = { const res = JSON.parse(response.body) return res } catch (error) { - console.log(error.response.body); + console.log('post error'); } } } \ No newline at end of file diff --git a/routes/heating_get.js b/routes/heating_get.js index 79d60b3..6f33b6c 100644 --- a/routes/heating_get.js +++ b/routes/heating_get.js @@ -10,6 +10,7 @@ module.exports = function (app) { app.get("/heating/status", async function (req, res, next) { const response = await post.postRequest('{}', 'poll') + // console.log(response.updateData) const heating = response.updateData.zones[0].status const mrt = await common.updateTemp(heating) const on = await common.heatingOn(heating) diff --git a/routes/heating_post.js b/routes/heating_post.js index 57526eb..e274163 100644 --- a/routes/heating_post.js +++ b/routes/heating_post.js @@ -8,4 +8,22 @@ module.exports = function (app) { res.json(heating) }); + app.post("/heating/status", async function (req, res, next) { + 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) + const csp = await common.heatMap(heating.currentSetpoint) + const lsp = await common.heatMap(heating.lastTimerSetPoint) + + let heatingStatus = { + "heatingOn": JSON.stringify(on), + "measuredRoomTemp": mrt, + "currentSetpoint": csp, + "lastTimerSetPoint": lsp, + "lastTimerDurationMinutes": JSON.stringify(heating.lastTimerDurationMinutes) + } + res.json(heatingStatus) + }); + } \ No newline at end of file diff --git a/routes/water_get.js b/routes/water_get.js index 8b8577d..ec8f10b 100644 --- a/routes/water_get.js +++ b/routes/water_get.js @@ -28,4 +28,23 @@ module.exports = function (app) { res.json(water) }); + app.get("/water/switch", async function (req, res, next) { + const response = await post.postRequest('{}', 'poll') + console.log('switch status start') + const water = response.updateData.zones[1].status + let on = await common.heatingOn(water) + if (on == true) { + on = '0' + } else { + on = '1' + } + + let switchStatus = { + "switch": on + } + console.log(switchStatus) + console.log('switch status end') + res.json(switchStatus) + }); + } \ No newline at end of file diff --git a/routes/water_post.js b/routes/water_post.js index 0678648..331161b 100644 --- a/routes/water_post.js +++ b/routes/water_post.js @@ -1,19 +1,48 @@ 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/switch", async function (req, res, next) { + if (req.body.switch === 1) { + try { + await post.postRequest('{"zoneId":[1],"setPoint": 255,"durationMinutes": 90}', 'apply_timer') + const response = await post.postRequest('{}', 'poll') + const water = response.updateData.zones[1].status + const on = await common.heatingOn(water) - 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') { + let waterStatus = { + "waterOn": JSON.stringify(on), + } + res.json(waterStatus) + } catch (error) { + + } + } else if (req.body.switch === 0) { await post.postRequest('{"zoneId":[1]}', 'cancel_timer') + const response = await post.postRequest('{}', 'poll') + const water = response.updateData.zones[1].status + const on = await common.heatingOn(water) + + let waterStatus = { + "waterOn": JSON.stringify(on), + } + res.json(waterStatus) } else { - throw new Error + res.json({ "switch": "break" }) } - res.json(req.query) }); + app.post("/water/status", async function (req, res, next) { + const response = await post.postRequest('{}', 'poll') + const water = response.updateData.zones[1].status + const on = await common.heatingOn(water) + + let waterStatus = { + "waterOn": JSON.stringify(on), + "currentSetpoint": JSON.stringify(water.currentSetpoint), + "lastTimerSetPoint": JSON.stringify(water.lastTimerSetPoint), + "lastTimerDurationMinutes": JSON.stringify(water.lastTimerDurationMinutes) + } + res.json(waterStatus) + }); } \ No newline at end of file diff --git a/server.js b/server.js index d50124e..b7f1eef 100644 --- a/server.js +++ b/server.js @@ -1,5 +1,10 @@ var express = require("express"); var app = express(); +const bodyParser = require('body-parser'); + +app.use(bodyParser.urlencoded({ extended: true })); + +app.use(bodyParser.json()) require('./routes')(app); diff --git a/test.js b/test.js new file mode 100644 index 0000000..5673f9f --- /dev/null +++ b/test.js @@ -0,0 +1,20 @@ +var express = require("express"); +var app = express(); + +const bodyParser = require('body-parser') + +app.use( + bodyParser.urlencoded({ + extended: true + }) +) + +app.use(bodyParser.json()) + +app.post('/endpoint', (req, res) => { + console.log(req.body) +}) + +app.listen(2020, function () { + console.log("Server running on port 2020"); +}); \ No newline at end of file