From 283f963e380b76a60dfc0a32a085a9cc81115be4 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Fri, 6 Jan 2023 17:04:46 -0500 Subject: [PATCH] bug fixes --- .vscode/pssnippets.code-snippets | 6 +++ config.json | 2 +- functions.js | 75 ++++++++++++++++++-------------- websvr.js | 23 ++++++---- www/public/main.css | 8 +++- www/public/main.js | 32 ++++++-------- www/views/index.html | 13 +++--- 7 files changed, 91 insertions(+), 68 deletions(-) diff --git a/.vscode/pssnippets.code-snippets b/.vscode/pssnippets.code-snippets index 9b3d49f..7e3c4da 100644 --- a/.vscode/pssnippets.code-snippets +++ b/.vscode/pssnippets.code-snippets @@ -27,5 +27,11 @@ "prefix": "onpi", "body": "if (process.env.ONPI == 'true') {\n\t$1\n} else {\n\t$2\n}$0", "description": "Run something only if the ONPI env var is set" + }, + "Error": { + "scope": "javascript", + "prefix": "err", + "body": "console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] E: $1`);\n$0", + "description": "Log an error to the console with timestamp" } } \ No newline at end of file diff --git a/config.json b/config.json index 77c3ae1..867039a 100644 --- a/config.json +++ b/config.json @@ -1 +1 @@ -{"debugMode":true,"status":{"igniter":0,"blower":0,"auger":0,"igniterFinished":false,"shutdown":0,"vacuum":0,"pof":0},"timestamps":{"procStart":1673037430589,"blowerOn":0,"blowerOff":0,"igniterOn":0,"igniterOff":0},"intervals":{"augerOn":"700","augerOff":"1300","igniterStart":5000,"blowerStop":5000},"web":{"port":8080,"ip":"0.0.0.0"}} \ No newline at end of file +{"debugMode":true,"status":{"igniter":0,"blower":0,"auger":0,"igniterFinished":false,"shutdown":0,"vacuum":0,"pof":0},"timestamps":{"procStart":1673042241248,"blowerOn":0,"blowerOff":0,"igniterOn":0,"igniterOff":0},"intervals":{"augerOn":"500","augerOff":"1500","pause":"5000","igniterStart":5000,"blowerStop":5000},"web":{"port":8080,"ip":"0.0.0.0"}} \ No newline at end of file diff --git a/functions.js b/functions.js index 651f2e3..96d76f7 100644 --- a/functions.js +++ b/functions.js @@ -17,25 +17,30 @@ const fs = require('fs'); const { time } = require('console'); const main = (gpio) => { - // If the auger is enabled - if (config.status.auger == 1) { - // Run a cycle of the auger - functions.auger.cycle(gpio).then(res => { - if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`); - // Recursion ecursion cursion ursion rsion sion ion on n - main(gpio); - }).catch(err => { - if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] E: ${err}`); - }); - } else { - // If the auger is disabled - functions.commands.pause().then(res => { - main(gpio); - }).catch(err => { - if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] E: ${err}`); - main(gpio); - }); - } + functions.commands.refreshConfig().then(res => { + // If the auger is enabled + if (config.status.auger == 1) { + // Run a cycle of the auger + functions.auger.cycle(gpio).then(res => { + if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`); + // Recursion ecursion cursion ursion rsion sion ion on n + main(gpio); + }).catch(err => { + if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] E: ${err}`); + }); + } else { + // If the auger is disabled + functions.commands.pause().then(res => { + main(gpio); + }).catch(err => { + if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] E: ${err}`); + main(gpio); + }); + } + }).catch(rej => { + console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] E: Problem refreshing the config file.`); + main(gpio); + }); } // The functions we'll export to be used in other files @@ -144,20 +149,24 @@ const functions = { }, refreshConfig(newSettings) { - // When the reload button is pressed, the call to this function will contain new config values - // { - // augerOff: 500, - // augerOn: 1500, - // pause: 5000 - // } - if (newSettings != undefined) { - config.intervals.augerOff = newSettings.augerOff; - config.intervals.augerOn = newSettings.augerOn; - config.intervals.pause = newSettings.pause; - } - fs.writeFile('./config.json', JSON.stringify(config), (err) => { - if (err) throw err; - }); + return new Promise((resolve, reject) => { + // When the reload button is pressed, the call to this function will contain new config values + // { + // augerOff: 500, + // augerOn: 1500, + // pause: 5000 + // } + if (newSettings != undefined) { + config.intervals.augerOff = newSettings.augerOff; + config.intervals.augerOn = newSettings.augerOn; + config.intervals.pause = newSettings.pause; + } + fs.writeFile('./config.json', JSON.stringify(config), (err) => { + if (err) reject(err); + resolve(); + }); + }) + } }, // Sleeps for any given milliseconds diff --git a/websvr.js b/websvr.js index 2a077e9..ae08071 100644 --- a/websvr.js +++ b/websvr.js @@ -11,10 +11,21 @@ const express = require('express'); const app = express(); const http = require('http'); const server = http.createServer(app); -const config = require('./config.json'); const fs = require('fs'); // const bodyParser = require('body-parser'); -const fn = require('./functions.js').functions; +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); + }); +}); app.use(express.urlencoded()); @@ -45,12 +56,8 @@ app.post('/', (req, res) => { fn.commands.refreshConfig({ augerOff: req.body.augerOff, augerOn: req.body.augerOn, - pause: req.body.pause + pause: req.body.pauseInt }); } - // res.send(200); }); - // console.log(req.body); -}); - -server.listen(config.web.port, config.web.ip); \ No newline at end of file +}); \ No newline at end of file diff --git a/www/public/main.css b/www/public/main.css index c3e602b..08b18b9 100644 --- a/www/public/main.css +++ b/www/public/main.css @@ -9,9 +9,15 @@ html, body { #title { text-align: center; font-size: 35px; + margin-top: 10px; padding-bottom: 15px; } +#title a { + text-decoration: none; + color: inherit; +} + #status { text-align: center; } @@ -71,7 +77,7 @@ html, body { font-family: times; font-size: 20px; height: 35px; - width: 100px; + width: 150px; border-top-left-radius: 0px; border-bottom-left-radius: 0px; border-top-right-radius: 0px; diff --git a/www/public/main.js b/www/public/main.js index 9565ea6..581efa0 100644 --- a/www/public/main.js +++ b/www/public/main.js @@ -32,30 +32,26 @@ function refreshData() { const log = document.getElementById('log-area'); log.contentWindow.location.reload(); sleep(100).then(() => { - document.getElementById('log-area').contentWindow.scrollTo(0, 9999999); + document.getElementById('log-area').contentWindow.scrollTo(0, 9999999999); }); + // Get the elements we need to update const augerStatus = document.getElementById('auger-status'); - // const augerOn = document.getElementById('auger-on'); - // const augerOff = document.getElementById('auger-off'); - const igniterStatus = document.getElementById('igniter-status'); - const blowerStatus = document.getElementById('blower-status'); - // const pauseInt = document.getElementById('pause-int'); - const vacuumStatus = document.getElementById('vacuum-status'); - const pofStatus = document.getElementById('pof-status'); - + const augerOn = document.getElementById('auger-on'); + const augerOff = document.getElementById('auger-off'); + const pauseInt = document.getElementById('pause-int'); + + // Get the config file const config = readJSON('./config.json'); + // console.log(config); augerStatus.innerHTML = parseStatus(config.status.auger); - // augerOn.innerHTML = parseStatus(config.intervals.augerOn); - // augerOff.innerHTML = parseStatus(config.intervals.augerOff); - igniterStatus.innerHTML = parseStatus(config.status.igniter); - blowerStatus.innerHTML = parseStatus(config.status.blower); - // pauseInt.innerHTML = parseStatus(config.intervals.pause); - vacuumStatus.innerHTML = parseStatus(config.status.vacuum); - pofStatus.innerHTML = parseStatus(config.status.pof); - - sleep(2000).then(() => { + augerOn.value = config.intervals.augerOn; + augerOff.value = config.intervals.augerOff; + pauseInt.value = config.intervals.pause; + + // Run this again after 2 seconds + sleep(5000).then(() => { refreshData(); }); }; \ No newline at end of file diff --git a/www/views/index.html b/www/views/index.html index 80c9ef8..8e9cdf3 100644 --- a/www/views/index.html +++ b/www/views/index.html @@ -6,14 +6,13 @@ <%- include('trial.html') -%> -
Hestia Web Portal
-
Auger: | Igniter: | Combustion Blower:
-
Vacuum: | Proof of Fire:
+
Hestia Web Portal
+
Auger:
-
+
ms
@@ -21,17 +20,17 @@ ms
-
+
-
+