mirror of
https://github.com/KevinMidboe/fanController.git
synced 2025-10-29 17:40:22 +00:00
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:
51
server.js
51
server.js
@@ -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 () {
|
||||
|
||||
Reference in New Issue
Block a user