hestia/websvr.js

63 lines
1.8 KiB
JavaScript
Raw Normal View History

2022-12-19 18:07:36 +00:00
/* Pellet Stove Control Panel
* Web Configuration Server
* v0.0.0 by Skylar Grant
2022-12-20 02:55:50 +00:00
*
* TODOs:
* Implement Express to make it easier
* Add actual data into the responses
2022-12-19 18:07:36 +00:00
*/
2022-12-20 19:18:24 +00:00
const express = require('express');
const app = express();
2022-12-19 18:07:36 +00:00
const http = require('http');
2022-12-20 19:18:24 +00:00
const server = http.createServer(app);
2022-12-20 20:17:38 +00:00
const fs = require('fs');
2022-12-21 02:12:22 +00:00
// const bodyParser = require('body-parser');
2023-01-06 22:04:46 +00:00
var config;
var fn;
// First thing is to copy the template config to main config file
fs.readFile('./templates/config.json', (err, data) => {
fs.writeFile('./config.json', data, (err) => {
if (err) throw err;
console.log(`I: Config Template copied.`);
config = require('./config.json');
fn = require('./functions.js').functions;
server.listen(config.web.port, config.web.ip);
});
});
2022-12-21 02:12:22 +00:00
app.use(express.urlencoded());
2022-12-20 20:17:38 +00:00
app.use(express.static(__dirname + '/www/public'));
app.set('views', __dirname + '/www/views');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.get('/', (req, res) => {
fs.readFile(__dirname + '/config.json', (err, data) => {
// console.log(JSON.parse(data));
res.render('index', JSON.parse(data));
// res.send(200);
});
});
2022-12-19 18:07:36 +00:00
2022-12-20 19:18:24 +00:00
app.post('/', (req, res) => {
2022-12-21 02:12:22 +00:00
fs.readFile(__dirname + '/config.json', (err, data) => {
// console.log(JSON.parse(data));
res.render('index', JSON.parse(data));
if (req.body.start != undefined) {
2023-01-06 20:58:42 +00:00
fn.commands.startup();
2022-12-21 18:50:09 +00:00
}
if (req.body.shutdown != undefined) {
2023-01-06 20:58:42 +00:00
fn.commands.shutdown();
}
if (req.body.reload != undefined) {
fn.commands.refreshConfig({
2023-01-06 22:12:27 +00:00
augerOff: 2000 - req.body.augerOn,
2023-01-06 20:58:42 +00:00
augerOn: req.body.augerOn,
2023-01-06 22:04:46 +00:00
pause: req.body.pauseInt
2023-01-06 20:58:42 +00:00
});
2022-12-21 02:12:22 +00:00
}
});
2023-01-06 22:04:46 +00:00
});