hestia/modules/_server.js

139 lines
4.6 KiB
JavaScript
Raw Normal View History

2023-01-22 16:27:58 +00:00
/* Pellet Stove Control Panel
* Web Configuration Server
* v0.0.0 by Skylar Grant
*
* TODOs:
* Implement Express to make it easier
* Add actual data into the responses
*/
2023-11-10 22:57:59 +00:00
// Modules
2023-01-22 16:27:58 +00:00
const express = require('express');
const http = require('http');
2023-11-14 23:06:09 +00:00
const fs = require('fs');
2023-01-22 16:27:58 +00:00
const fn = require('./functions.js').functions;
2023-11-10 22:57:59 +00:00
const { dbfn } = require('./functions.js');
// Grab the current configuration settings from the database to display
2023-01-22 16:27:58 +00:00
var config;
fn.commands.refreshConfig().then(newConfig => {
config = newConfig.config;
});
2023-11-10 22:57:59 +00:00
// Create the server
2023-01-22 16:27:58 +00:00
const app = express();
const server = http.createServer(app);
app.use(express.urlencoded());
// Our root directory for the public web files
app.use(express.static(__dirname + '/../www/public'));
// Our directory for views used to render the pages
app.set('views', __dirname + '/../www/views');
// Set .html as the file extension for views
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
// A normal load of the root page
app.get('/', (req, res) => {
2023-11-10 22:57:59 +00:00
// Render the page, passing the config along for the front end to use
2023-01-23 03:42:23 +00:00
res.render('index', { config: JSON.stringify(config) });
2023-01-22 16:27:58 +00:00
});
// A POST form submission to the root page
app.post('/', (req, response) => {
2023-11-14 23:06:09 +00:00
if (req.body.augerOn != undefined) {
fn.commands.augerOn();
2023-01-23 04:12:21 +00:00
fn.commands.refreshConfig().then(res => {
config = res.config;
response.render('index', { config: JSON.stringify(config) });
return;
});
2023-11-14 23:06:09 +00:00
return;
2023-01-22 16:27:58 +00:00
}
2023-11-14 23:06:09 +00:00
if (req.body.augerOff != undefined) {
fn.commands.augerOff();
2023-01-23 04:12:21 +00:00
fn.commands.refreshConfig().then(res => {
config = res.config;
response.render('index', { config: JSON.stringify(config) });
return;
});
2023-11-14 23:06:09 +00:00
return;
2023-01-22 16:27:58 +00:00
}
2023-11-17 15:06:22 +00:00
if (req.body.igniterOn != undefined) {
fn.commands.igniterOn();
fn.commands.refreshConfig().then(res => {
config = res.config;
response.render('index', { config: JSON.stringify(config) });
return;
});
return;
}
if (req.body.igniterOff != undefined) {
fn.commands.igniterOff();
fn.commands.refreshConfig().then(res => {
config = res.config;
response.render('index', { config: JSON.stringify(config) });
return;
});
return;
}
if (req.body.exhaustOn != undefined) {
fn.commands.exhaustOn();
fn.commands.refreshConfig().then(res => {
config = res.config;
response.render('index', { config: JSON.stringify(config) });
return;
});
return;
}
if (req.body.exhaustOff != undefined) {
fn.commands.exhaustOff();
fn.commands.refreshConfig().then(res => {
config = res.config;
response.render('index', { config: JSON.stringify(config) });
return;
});
return;
}
2023-01-22 16:27:58 +00:00
if (req.body.reload != undefined) {
const updateAugerOffIntervalQuery = `UPDATE intervals SET value = '${2000 - req.body.feedRate}' WHERE key = 'auger_off'`;
const updateAugerOnIntervalQuery = `UPDATE intervals SET value = '${req.body.feedRate}' WHERE key = 'auger_on'`;
dbfn.run(updateAugerOffIntervalQuery).then(res => {
if (process.env.DEBUG) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: Auger off interval updated: ${res.data.changes}`);
dbfn.run(updateAugerOnIntervalQuery).then(res => {
if (process.env.DEBUG) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: Auger on interval updated: ${res.data.changes}`);
fn.commands.refreshConfig().then(res => {
config = res.config;
response.render('index', { config: JSON.stringify(config) });
return;
});
}).catch(err => console.log(`E: ${err}`));
2023-01-22 16:27:58 +00:00
}).catch(err => console.log(`E: ${err}`));
2023-11-14 23:06:09 +00:00
return;
2023-01-22 16:27:58 +00:00
}
if (req.body.quit != undefined) {
2023-11-14 23:06:09 +00:00
fs.appendFile('quit', ".", err => {
if (err) console.error(err);
});
2023-01-23 04:12:21 +00:00
fn.commands.refreshConfig().then(res => {
config = res.config;
response.render('index', { config: JSON.stringify(config) });
return;
});
2023-11-14 23:06:09 +00:00
return;
2023-01-22 16:27:58 +00:00
}
2023-11-14 23:06:09 +00:00
fn.commands.refreshConfig().then(res => {
config = res.config;
response.render('index', { config: JSON.stringify(config) });
return;
});
2023-01-22 16:27:58 +00:00
});
2023-01-23 03:42:23 +00:00
module.exports = {
start: () => {
server.listen(8080, "0.0.0.0");
}
};