From 9f5811d90fbe02ed91eeba3ab4d40d2dfe81be50 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sun, 22 Jan 2023 11:27:58 -0500 Subject: [PATCH] More migration, ready for testing --- .gitignore | 1 + data/config.db | Bin 0 -> 16384 bytes hestia.sh | 3 +- modules/_server.js | 60 ++++++++++++++++++++++++++++++++++++ modules/_setupdb.js | 45 +++++++++------------------ modules/functions.js | 23 +++++++------- websvr.js | 71 ------------------------------------------- www/views/index.html | 2 +- 8 files changed, 91 insertions(+), 114 deletions(-) create mode 100644 data/config.db create mode 100644 modules/_server.js delete mode 100644 websvr.js diff --git a/.gitignore b/.gitignore index a83b968..e50cc6c 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/data/config.db b/data/config.db new file mode 100644 index 0000000000000000000000000000000000000000..4cf319b3e6b1f943481919917d73f5de332806f9 GIT binary patch literal 16384 zcmeI(zi-n(6bJBgE+wVVzM|5a2vv23+O&eAOMkObP`9WE2y^7R=hh;zgMCiv*cqu( zM-~P~Bv!=4z#oA40}v|{5)%IaFKL~$3yRwHYsIneo$sFW+1{P<{AndH+VP{f;TmU2 zj0(#V4;@DcF~et`&+L$VbGB_|9sjw`i2I%x<_F}+CqZv%mM=IU009U<00Izz00bZa z0SG_<0(}aEvS3e5S%-IxyIt2oXms3i>q++}|M=PFMs?GvZak`NI^D1{_fj7?EjRY| z+<306)V#Cxr0Q(Fs8sIp_*9?mJJUJ;Dp#(<PG{2OKCCO6&Had{}m6K&U{eI2W7 zmb^`;1Jp4676Wu7=!kyOH~K;!FY(hb6ao-{00bZa0SG_<0uX=z1Rwx`J_IH$St{`o zfTxqB#yk75DX*?AFRd=Ftv^^%D>NYQWR*Lw5%_8(FQo1#pOpEX{^6XQ>b670 zZ!p$gVdzcI_5UBP|BnS7bN&CFKGXX?UK5W%00Izz00bZa0SG_<0uX=z1R!uZ0kY)e zcxThT7KQv51$C2){^Ds(pO~n*$}y!T^0HLScKtgm1K}Q0>|Fo= literal 0 HcmV?d00001 diff --git a/hestia.sh b/hestia.sh index 1b01a86..f1a0c91 100755 --- a/hestia.sh +++ b/hestia.sh @@ -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 diff --git a/modules/_server.js b/modules/_server.js new file mode 100644 index 0000000..215fa7c --- /dev/null +++ b/modules/_server.js @@ -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"); \ No newline at end of file diff --git a/modules/_setupdb.js b/modules/_setupdb.js index fc566ef..956921b 100644 --- a/modules/_setupdb.js +++ b/modules/_setupdb.js @@ -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)); }); diff --git a/modules/functions.js b/modules/functions.js index 1abc04a..f1f7f03 100644 --- a/modules/functions.js +++ b/modules/functions.js @@ -85,14 +85,20 @@ const functions = { // Prepare the stove for starting startup() { // Basic startup just enables the auger - config.status.auger = 1; - console.log(`[${(Date.now() - config.timestamps.procStart) / 1000}] I: Auger enabled.`); - return; + 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; - console.log(`[${(Date.now() - config.timestamps.procStart) / 1000}] I: Auger disabled.`); + 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 }; \ No newline at end of file +module.exports = { functions, dbfn }; \ No newline at end of file diff --git a/websvr.js b/websvr.js deleted file mode 100644 index 80d1bc0..0000000 --- a/websvr.js +++ /dev/null @@ -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(); - } - }); -}); \ No newline at end of file diff --git a/www/views/index.html b/www/views/index.html index 062caf9..013b729 100644 --- a/www/views/index.html +++ b/www/views/index.html @@ -55,7 +55,7 @@
- +