From b1f9f2965b692c7857857cc0c2cc41e9a4c7c463 Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Mon, 4 Sep 2017 19:35:58 +0200 Subject: [PATCH] Now the toggleing is done withing the first python call because it is a async call. --- server.js | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/server.js b/server.js index aad25a1..cf9af31 100644 --- a/server.js +++ b/server.js @@ -62,32 +62,44 @@ app.post('/off', function (req, res) { }) app.post('/toggle', function (req, res) { - var relayState; var options = { pythonOptions: ['-u'], args: 'get' }; PythonShell.run('scripts/fanController.py', options, function (err, results) { - if (err) throw err; - if (results[0] == true) - relayState = true; - else - relayState = false; - }); + if (err) throw err; + + if (results[0] == true) { + var toggleOptions = { + pythonOptions: ['-u'], + args: 'off' + }; - options = { - pythonOptions: ['-u'], - args: relayState - }; + PythonShell.run('scripts/fanController.py', toggleOptions, function (err, results) { + if (err) throw err; + + if (results[0] == true) + res.send('off') + else + res.send('none') + }); + } + else { + var toggleOptions = { + pythonOptions: ['-u'], + args: 'on' + }; - PythonShell.run('scripts/fanController.py', options, function (err, results) { - if (err) throw err; - - if (results[0] == true) - res.send('off') - else - res.send('on') + PythonShell.run('scripts/fanController.py', toggleOptions, function (err, results) { + if (err) throw err; + + if (results[0] == true) + res.send('on') + else + res.send('none') + }); + } }); })