Changed the fanToggling to only be done by one script, changed server.js to reflect these changes and also needed to do some changes to index.html. These being checking the return text and some linting.

This commit is contained in:
2017-02-19 11:55:48 +01:00
parent 5c4cf195e0
commit 0cb52876a1
13 changed files with 110 additions and 541 deletions

View File

@@ -4,8 +4,6 @@ var path = require('path');
var PythonShell = require('python-shell');
app.use(express.static('public'));
var fs = require('fs');
// This responds with "Hello World" on the homepage
@@ -16,31 +14,52 @@ app.get('/', function (req, res) {
})
app.get('/state', function(req, res) {
var contents = fs.readFileSync('public/state.txt', 'utf8');
console.log(contents);
res.send(contents);
var options = {
pythonOptions: ['-u'],
args: 'get'
};
PythonShell.run('scripts/fanController.py', options, function (err, results) {
if (err) throw err;
if (results[0] == true)
res.send('on')
else
res.send('off')
});
})
// This responds a POST request for the homepage
app.post('/on', function (req, res) {
console.log("Got a POST request for ON");
var options = {
pythonOptions: ['-u'],
args: 'on'
};
PythonShell.run('pyscripts/led-csid0-on.py', function (err) {
if (err) throw err;
console.log('finished');
PythonShell.run('scripts/fanController.py', options, function (err, results) {
if (err) throw err;
if (results[0] == true)
res.send('on')
else
res.send('none')
});
res.send('on');
})
app.post('/off', function (req, res) {
console.log("Got a POST request for OFF");
var options = {
pythonOptions: ['-u'],
args: 'off'
};
PythonShell.run('pyscripts/led-csid0-off.py', function (err) {
if (err) throw err;
console.log('finished');
PythonShell.run('scripts/fanController.py', options, function (err, results) {
if (err) throw err;
if (results[0] == true)
res.send('off')
else
res.send('none')
});
res.send('off');
})
var server = app.listen(3000, function () {