require('dotenv').config();
const PythonShell = require('python-shell');
const fs = require('fs');
const express = require('express');
const bodyParser= require('body-parser');
const path = require('path')
const app = express();
// 1.Switch states held in memory
const switches = [];
// 2.Read state from saveState.json, populate switches array
var readableStream = fs.createReadStream('saveState.json');
var data = ''
readableStream.on('data', function(chunk) {
data+=chunk;
});
readableStream.on('end', function() {
var parsed = JSON.parse(data);
for (i=0;i<parsed.switches.length;i++){
switches.push(new Switch(parsed.switches[i]))
}
});
// 3.Switch Model
// Expects an object:{
// id:"sw" + number,
// state: "on" or "off",
// name: any name you want to display. Defaults to "switch"
// }
function Switch(switchValues){
this.state = switchValues.state || "off"
this.toggle = function(){
if(this.state === "on"){
this.setState("off")
}
else{
this.setState("on");
}
}
this.setState = function(state){
var str = state === "on" ? onString(this.id[2]) : offString(this.id[2]); PythonShell.run(str, function (err) {
if (err) throw err;
}
});
this.state = state
}
// Invokes setState on init to set the switch to its last recalled state.
this.setState(this.state);
}
// 4.needed due to a quirk with PythonShell
function onString(number){
return './public/python/sw' + number + '_on.py'
}
function offString(number){
return './public/python/sw' + number + '_off.py'
}
// 5.Switch Lookup
function getSwitch(string){
return switches.filter(function(element){
})[0]
}
// 6.Updates saveState.json
function saveState (){
var formattedState = {
switches: switches
}
fs.writeFile('./saveState.json', JSON.stringify(formattedState) )
}
//7.Server Configuration
app.use(bodyParser.urlencoded({ extended: true }))
app.use(express.static(__dirname + '/public'));
// If you have a frontend, drop it in the Public folder with an entry point of index.html
app.get('/', function(req, res){
res.sendFile('index');
})
// Switch Routes for API
app.get('/api/switches', function(req, res){
res.send(switches);
})
app.get('/api/switches/:id', function(req, res){
res.json(found);
})
app.post('/api/switches/:id', function(req, res){
// 8.For now, uses a simple password query in the url string.
// Example: POST to localhost:8000/API/switches/sw1?password=test
if (req.query.password === process.env.PASS){
// Optional On / Off command. If not included, defaults to a toggle.
if(!(req.query.command === "on" || req.query.command === "off")){
foundSwitch.toggle();
}
else {
foundSwitch.setState(req.query.command)
}
saveState();
console.log("postSwitch "+JSON.stringify(foundSwitch));
res.json(foundSwitch);
}
else {
console.log("invalid password")
res.send("try again")
}
})
app.listen(process.env.PORT, function(){
console.log('Listening on port ' + process.env.PORT);
})