From c5ba6b8621f576db2fc869acc36ac74fae10913d Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Mon, 4 Sep 2017 18:54:24 +0200 Subject: [PATCH] Added a toggle endpoint that checks the db for button state then uses that to set it to the opposite. --- server.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/server.js b/server.js index 62da6b0..46ed9ac 100644 --- a/server.js +++ b/server.js @@ -5,6 +5,7 @@ var PythonShell = require('python-shell'); app.use(express.static('public')); +var relayState = false; // This responds with "Hello World" on the homepage app.get('/', function (req, res) { @@ -61,6 +62,23 @@ app.post('/off', function (req, res) { }); }) +app.post('/toggle', function (req, res) { + relayState = !relayState + var options = { + pythonOptions: ['-u'], + args: relayState + }; + + PythonShell.run('scripts/fanController.py', options, function (err, results) { + if (err) throw err; + + if (results[0] == true) + res.send('off') + else + res.send('none') + }); +}) + var server = app.listen(3000, function () { var host = server.address().address