Begin db migration

This commit is contained in:
Skylar Grant 2023-01-21 10:48:16 -05:00
parent 53e3042940
commit 6ee01cc214
7 changed files with 101 additions and 17 deletions

View File

@ -54,3 +54,53 @@ For ease of adaption, connection, and prototyping I've decided to use Cat 5 ethe
* Vacuum Switch OPEN after igniter start.
4. Test manipulation of feed rates.
5. Test shutdown sequence.
# SQLite Database Tables
## status
| Field | Type | Null | Key | Default | Extra |
| ----- | ---- | ---- | --- | ------- | ----- |
| id | int(10) | No | PRI | NULL | auto_increment |
| key | varchar(100) | No | | |
| value | varchar(1000) | No | | |
| id | key | value |
| -- | --- | ----- |
0 | igniter | 0
1 | blower | 0
2 | auger | 0
3 | igniter_finished | false
4 | shutdown_initiated | 0
5 | vacuum | 0
6 | proof_of_fire | 0
7 | shutdown_next_cycle | 0
## timestamps
| Field | Type | Null | Key | Default | Extra |
| ----- | ---- | ---- | --- | ------- | ----- |
| id | int(10) | No | PRI | NULL | auto_increment |
| key | varchar(100) | No | | |
| value | varchar(1000) | No | | |
| id | key | value |
| -- | --- | ----- |
0 | process_start | 0
1 | blower_on | 0
2 | blower_off | 0
3 | igniter_on | 0
4 | igniter_off | 0
## intervals
| Field | Type | Null | Key | Default | Extra |
| ----- | ---- | ---- | --- | ------- | ----- |
| id | int(10) | No | PRI | NULL | auto_increment |
| key | varchar(100) | No | | |
| value | varchar(1000) | No | | |
| id | key | value |
| -- | --- | ----- |
0 | auger_on | 600
1 | auger_off | 1400
2 | pause | 5000
3 | igniter_start | 420000
4 | blower_stop | 600000

5
data/strings.json Normal file
View File

@ -0,0 +1,5 @@
{
"database": {
"createConfigTable": ""
}
}

View File

@ -22,13 +22,13 @@ const main = (gpio) => {
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}`);
if (process.env.DEBUG) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
// Recursion ecursion cursion ursion rsion sion ion on n
functions.checkForQuit().then(n => {
main(gpio);
});
}).catch(err => {
if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] E: ${err}`);
if (process.env.DEBUG) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] E: ${err}`);
});
} else {
// If the auger is disabled
@ -37,7 +37,7 @@ const main = (gpio) => {
main(gpio);
});
}).catch(err => {
if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] E: ${err}`);
if (process.env.DEBUG) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] E: ${err}`);
main(gpio);
});
}
@ -92,19 +92,19 @@ const functions = {
// Turn the auger on
this.on(gpio).then((res) => {
// Log action if in debug mode
// if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
// if (process.env.DEBUG) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
// Sleep for the time set in env variables
functions.sleep(config.intervals.augerOn).then((res) => {
// Log action if in debug mode
// if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
// if (process.env.DEBUG) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
// Turn the auger off
this.off(gpio).then((res) => {
// Log action if in debug mode
// if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
// if (process.env.DEBUG) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
// Sleep for the time set in env variables
functions.sleep(config.intervals.augerOff).then((res) => {
// Log action if in debug mode
// if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
// if (process.env.DEBUG) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
// Resolve the promise, letting the main script know the cycle is complete
resolve(`Auger cycled (${config.intervals.augerOn}/${config.intervals.augerOff})`);
});
@ -130,10 +130,10 @@ const functions = {
// Pauses the script for the time defined in env variables
pause() {
return new Promise((resolve) => {
if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: Pausing for ${config.intervals.pause}ms`);
if (process.env.DEBUG) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: Pausing for ${config.intervals.pause}ms`);
functions.sleep(config.intervals.pause).then((res) => {
if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: Pause finished.`);
if (process.env.DEBUG) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: Pause finished.`);
resolve();
});
});
@ -146,12 +146,12 @@ const functions = {
// Delete the reload file
fs.unlink('./reload', (err) => {
if (err) throw err;
if (config.debugMode) console.log('Deleted reload file.');
if (process.env.DEBUG) console.log('Deleted reload file.');
});
// Print out the new environment variables
// This should be printed regardless of debug status, maybe prettied up TODO?
console.log('Reloaded environment variables.');
console.log(`ONTIME=${config.intervals.augerOn}\nOFFTIME=${config.intervals.augerOff}\nPAUSETIME=${config.intervals.pause}\nDEBUG=${config.debugMode}\nONPI=${process.env.ONPI}`);
console.log(`ONTIME=${config.intervals.augerOn}\nOFFTIME=${config.intervals.augerOff}\nPAUSETIME=${config.intervals.pause}\nDEBUG=${process.env.DEBUG}\nONPI=${process.env.ONPI}`);
// Resolve the promise, letting the main script know we're done reloading the variables and the cycle can continue
resolve();
});
@ -192,7 +192,7 @@ const functions = {
// Sleeps for any given milliseconds
sleep(ms) {
return new Promise((resolve) => {
// if (config.debugMode) console.log(`Sleeping for ${ms}ms`);
// if (process.env.DEBUG) console.log(`Sleeping for ${ms}ms`);
// Function to be called when setTimeout finishes
const finish = () => {
// Resolve the promise
@ -224,14 +224,14 @@ const functions = {
== == ONTIME=${config.intervals.augerOn}
== == OFFTIME=${config.intervals.augerOff}
== == PAUSETIME=${config.intervals.pause}
== == DEBUG=${config.debugMode}
== == DEBUG=${process.env.DEBUG}
== == ONPI=${process.env.ONPI}`);
// Set up GPIO 4 (pysical pin 7) as output, then call functions.auger.ready()
if (process.env.ONPI == 'true') {
// Init the Auger pin
gpio.setup(augerPin, gpio.DIR_OUT, (err) => {
if (err) reject(err);
if (config.debugMode) console.log('== Auger pin initialized.');
if (process.env.DEBUG) console.log('== Auger pin initialized.');
// Resolve the promise now that all pins have been initialized
resolve('== GPIO Initialized.');
});

23
modules/database.js Normal file
View File

@ -0,0 +1,23 @@
// Get autocomplete
module.exports = {
query(db) {
db.serialize(() => {
db.each(`SELECT PlaylistId as id,
Name as name
FROM playlists`, (err, row) => {
if (err) {
console.error(err.message);
}
console.log(row.id + "\t" + row.name);
});
});
db.close((err) => {
if (err) {
console.error(err.message);
}
console.log('Close the database connection.');
});
}
};

7
setupdb.js Normal file
View File

@ -0,0 +1,7 @@
const sqlite3 = require('sqlite3').verbose();
// Connect to or create the database.
let db = new sqlite3.Database('./config.db', (err) => {
if (err) console.error("DB Connect: " + err);
console.log("Connected to database.");
});

View File

@ -1,5 +1,4 @@
{
"debugMode": true,
"status": {
"igniter": 0,
"blower": 0,

View File

@ -67,7 +67,7 @@ function refreshData() {
feedRate.value = config.intervals.augerOn;
// Run this again after 2 seconds
sleep(1000).then(() => {
sleep(5000).then(() => {
refreshData();
});
};