More migration, ready for testing

This commit is contained in:
Skylar Grant 2023-01-22 11:27:58 -05:00
parent 299a8b2efa
commit 9f5811d90f
8 changed files with 91 additions and 114 deletions

1
.gitignore vendored
View File

@ -11,6 +11,7 @@ lerna-debug.log*
.vscode/*
config.json
log.txt
nohup.out
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

BIN
data/config.db Normal file

Binary file not shown.

View File

@ -54,7 +54,8 @@ case "$opt" in
# Launch Hestia Web Portal
clear
echo "Launching Hestia Web Portal"
nohup node websvr.js > log.txt &
nohup node main.js > log.txt &
nohup node modules/_server.js &
;;
2)
# Quit Hestia Web Portal

60
modules/_server.js Normal file
View File

@ -0,0 +1,60 @@
/* 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
*/
const express = require('express');
const http = require('http');
const fn = require('./functions.js').functions;
var config;
fn.commands.refreshConfig().then(newConfig => {
config = newConfig.config;
});
const { dbfn } = require('./functions.js');
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) => {
res.render('index', config);
});
// A POST form submission to the root page
app.post('/', (req, res) => {
res.render('index', config);
if (req.body.start != undefined) {
fn.commands.startup();
}
if (req.body.shutdown != undefined) {
fn.commands.shutdown();
}
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}`);
}).catch(err => console.log(`E: ${err}`));
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}`);
}).catch(err => console.log(`E: ${err}`));
}
if (req.body.quit != undefined) {
fn.commands.quit();
}
});
server.listen(8080, "0.0.0.0");

View File

@ -1,10 +1,4 @@
const dbfn = require('../modules/database.js');
const sqlite3 = require('sqlite3');
// Connect to or create the database.
let db = new sqlite3.Database('../data/config.db', (err) => {
if (err) console.error("DB Connect: " + err);
console.log("Connected to database.");
});
// Create `status` table
/*
@ -33,7 +27,7 @@ CREATE TABLE IF NOT EXISTS status (
*/
const createStatusTableQuery = "CREATE TABLE IF NOT EXISTS status (key varchar(100) NOT NULL,value varchar(1000) NOT NULL);";
dbfn.run(db, createStatusTableQuery).then(res => {
dbfn.run(createStatusTableQuery).then(res => {
console.log(res.status);
const statusEntries = {
igniter: 0,
@ -47,16 +41,13 @@ dbfn.run(db, createStatusTableQuery).then(res => {
};
for ( key in statusEntries ){
const insertStatusEntryQuery = `INSERT INTO status (key, value) VALUES ("${key}", "${statusEntries[key]}")`;
dbfn.run(db, insertStatusEntryQuery).then(res => {
dbfn.run(insertStatusEntryQuery).then(res => {
console.log(`${res.status}: ${res.data.lastID}: ${res.data.changes} changes`);
}).catch(err => console.error(err));
}
const selectAllStatusEntriesQuery = "SELECT * FROM status";
dbfn.all(db, selectAllStatusEntriesQuery).then(res => {
dbfn.all(selectAllStatusEntriesQuery).then(res => {
console.log(res.status);
res.rows.forEach(row => {
console.log(`${row.key} | ${row.value}`);
});
}).catch(err => console.error(err));
}).catch(err => {
console.error(err);
@ -86,7 +77,7 @@ CREATE TABLE IF NOT EXISTS timestamps (
*/
const createTimestampsTableQuery = "CREATE TABLE IF NOT EXISTS timestamps (key varchar(100) NOT NULL,value varchar(1000) NOT NULL);";
dbfn.run(db, createTimestampsTableQuery).then(res => {
dbfn.run(createTimestampsTableQuery).then(res => {
console.log(res.status);
const timestampsEntries = {
process_start: 0,
@ -97,16 +88,13 @@ dbfn.run(db, createTimestampsTableQuery).then(res => {
};
for ( key in timestampsEntries ){
const insertTimestampsEntryQuery = `INSERT INTO timestamps (key, value) VALUES ("${key}", "${timestampsEntries[key]}")`;
dbfn.run(db, insertTimestampsEntryQuery).then(res => {
dbfn.run(insertTimestampsEntryQuery).then(res => {
console.log(`${res.status}: ${res.data.lastID}: ${res.data.changes} changes`);
}).catch(err => console.error(err));
}
const selectAllTimestampsEntriesQuery = "SELECT * FROM timestamps";
dbfn.all(db, selectAllTimestampsEntriesQuery).then(res => {
dbfn.all(selectAllTimestampsEntriesQuery).then(res => {
console.log(res.status);
res.rows.forEach(row => {
console.log(`${row.key} | ${row.value}`);
});
}).catch(err => console.error(err));
}).catch(err => {
console.error(err);
@ -136,27 +124,24 @@ CREATE TABLE IF NOT EXISTS intervals (
*/
const createIntervalsTableQuery = "CREATE TABLE IF NOT EXISTS intervals (key varchar(100) NOT NULL,value varchar(1000) NOT NULL);";
dbfn.run(db, createIntervalsTableQuery).then(res => {
dbfn.run(createIntervalsTableQuery).then(res => {
console.log(res.status);
const intervalsEntries = {
process_start: 0,
blower_on: 0,
blower_off: 0,
igniter_on: 0,
igniter_off: 0
auger_on: 600,
auger_off: 1400,
pause: 5000,
igniter_start: 420000,
blower_stop: 600000
};
for ( key in intervalsEntries ){
const insertIntervalsEntryQuery = `INSERT INTO intervals (key, value) VALUES ("${key}", "${intervalsEntries[key]}")`;
dbfn.run(db, insertIntervalsEntryQuery).then(res => {
dbfn.run(insertIntervalsEntryQuery).then(res => {
console.log(`${res.status}: ${res.data.lastID}: ${res.data.changes} changes`);
}).catch(err => console.error(err));
}
const selectAllIntervalsEntriesQuery = "SELECT * FROM intervals";
dbfn.all(db, selectAllIntervalsEntriesQuery).then(res => {
dbfn.all(selectAllIntervalsEntriesQuery).then(res => {
console.log(res.status);
res.rows.forEach(row => {
console.log(`${row.key} | ${row.value}`);
});
}).catch(err => console.error(err));
}).catch(err => {
console.error(err);
@ -164,7 +149,7 @@ dbfn.run(db, createIntervalsTableQuery).then(res => {
// Show the tables to confirm they were created properly:
dbfn.showTables(db).then(res => {
dbfn.showTables().then(res => {
res.rows.forEach(row => {
console.log("Table: " + JSON.stringify(row));
});

View File

@ -85,14 +85,20 @@ const functions = {
// Prepare the stove for starting
startup() {
// Basic startup just enables the auger
config.status.auger = 1;
const enableAugerQuery = "UPDATE status SET value = 1 WHERE key = 'auger'";
dbfn.run(enableAugerQuery).then(res => {
console.log(`[${(Date.now() - config.timestamps.procStart) / 1000}] I: Auger enabled.`);
return;
}).catch(err => console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] E: ${err}`));
},
shutdown() {
// Basic shutdown only needs to disable the auger
config.status.auger = 0;
const disableAugerQuery = "UPDATE status SET value = 0 WHERE key = 'auger'";
dbfn.run(disableAugerQuery).then(res => {
if (process.env.DEBUG) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res.status}`);
console.log(`[${(Date.now() - config.timestamps.procStart) / 1000}] I: Auger disabled.`);
return;
}).catch(err => console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] E: ${err}`));
},
// Pauses the script for the time defined in env variables
pause() {
@ -146,7 +152,6 @@ const functions = {
// Get status
const selectStatusQuery = "SELECT * FROM status";
dbfn.all(selectStatusQuery).then(res => {
console.log(JSON.stringify(res));
let { status } = config;
let { rows } = res;
status.auger = rows.auger;
@ -160,7 +165,6 @@ const functions = {
// Get timestamps
const selectTimestampsQuery = "SELECT * FROM timestamps";
dbfn.all(selectTimestampsQuery).then(res => {
console.log(JSON.stringify(res));
let { timestamps } = config;
let { rows } = res;
timestamps.blowerOff = rows.blower_off;
@ -172,7 +176,6 @@ const functions = {
// Get intervals
const selectIntervalsQuery = "SELECT * FROM intervals";
dbfn.all(selectIntervalsQuery).then(res => {
console.log(JSON.stringify(res));
let { intervals } = config;
let { rows } = res;
intervals.augerOff = rows.auger_off;
@ -282,7 +285,5 @@ const functions = {
}
}
// Export the above object, functions, as a module
module.exports = { functions };
module.exports = { functions, dbfn };

View File

@ -1,71 +0,0 @@
/* 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
*/
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const fs = require('fs');
// const bodyParser = require('body-parser');
var config = require('./templates/config.json');
var fn;
const sqlite3 = require('sqlite3');
const db = new sqlite3.Database('./data/config.db', (err) => {
if (err) throw `E: DB Connection: ${err.message}`;
console.log(`I: Connected to the database.`);
});
// 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('./modules/functions.js').functions;
server.listen(config.web.port, config.web.ip);
});
});
app.use(express.urlencoded());
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);
});
});
app.post('/', (req, res) => {
fs.readFile(__dirname + '/config.json', (err, data) => {
// console.log(JSON.parse(data));
res.render('index', JSON.parse(data));
if (req.body.start != undefined) {
fn.commands.startup();
}
if (req.body.shutdown != undefined) {
fn.commands.shutdown();
}
if (req.body.reload != undefined) {
fn.commands.refreshConfig({
augerOff: 2000 - req.body.feedRate,
augerOn: req.body.feedRate
});
}
if (req.body.quit != undefined) {
fn.commands.quit();
}
});
});

View File

@ -55,7 +55,7 @@
</select>
</div>
<div class="button-container d-flex justify-content-end">
<input class="btn btn-outline-secondary" type="submit" id="reload" value="Reload" name="reload">
<input class="btn btn-outline-secondary" type="submit" id="reload" value="Set Feed Rate" name="reload">
</div>
</form>
</div>