2022-12-03 23:03:57 +00:00
|
|
|
// Get environment variables
|
|
|
|
const dotenv = require('dotenv').config();
|
|
|
|
// Module for working with files
|
|
|
|
const fs = require('fs');
|
2022-12-04 00:42:47 +00:00
|
|
|
const { resolve } = require('path');
|
2022-12-04 00:55:47 +00:00
|
|
|
|
2022-12-03 23:03:57 +00:00
|
|
|
|
|
|
|
// The functions we'll export to be used in other files
|
|
|
|
const functions = {
|
|
|
|
auger: {
|
|
|
|
ready(err) {
|
|
|
|
if (err) throw err;
|
|
|
|
console.log('Auger GPIO Ready');
|
|
|
|
return;
|
2022-12-03 23:20:38 +00:00
|
|
|
},
|
2022-12-03 23:03:57 +00:00
|
|
|
// Turns the auger on (Pin 7 high)
|
2022-12-04 00:55:47 +00:00
|
|
|
on(gpio) {
|
2022-12-04 00:42:47 +00:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
if (process.env.ONPI == 'true') {
|
|
|
|
gpio.write(7, true, function(err) {
|
|
|
|
if (err) throw err;
|
2022-12-04 01:22:43 +00:00
|
|
|
resolve('Auger turned on.');
|
2022-12-04 00:42:47 +00:00
|
|
|
});
|
|
|
|
} else {
|
2022-12-04 01:16:54 +00:00
|
|
|
console.log('NOPI Auger turned on.');
|
|
|
|
resolve('NOPI Auger turned on.');
|
2022-12-04 00:42:47 +00:00
|
|
|
}
|
2022-12-03 23:03:57 +00:00
|
|
|
});
|
2022-12-04 00:42:47 +00:00
|
|
|
|
2022-12-03 23:03:57 +00:00
|
|
|
},
|
|
|
|
// Turns the auger off (pin 7 low)
|
2022-12-04 01:25:18 +00:00
|
|
|
off(gpio) {
|
2022-12-04 00:42:47 +00:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
if (process.env.ONPI == 'true') {
|
|
|
|
gpio.write(7, false, function(err) {
|
|
|
|
if (err) throw err;
|
2022-12-04 01:22:43 +00:00
|
|
|
resolve('Auger turned on.');
|
|
|
|
|
2022-12-04 00:42:47 +00:00
|
|
|
});
|
|
|
|
} else {
|
2022-12-04 01:16:54 +00:00
|
|
|
console.log('NOPI Auger turned off.');
|
|
|
|
resolve('NOPI Auger turned off.');
|
2022-12-04 00:42:47 +00:00
|
|
|
}
|
2022-12-03 23:03:57 +00:00
|
|
|
});
|
2022-12-04 00:42:47 +00:00
|
|
|
|
2022-12-03 23:03:57 +00:00
|
|
|
},
|
|
|
|
// Cycles the auger using the two functions above this one (functions.auger.on() and functions.auger.off())
|
|
|
|
// Sleeps in between cycles using functions.sleep()
|
2022-12-04 00:59:12 +00:00
|
|
|
cycle(gpio) {
|
2022-12-04 00:42:47 +00:00
|
|
|
return new Promise((resolve) => {
|
2022-12-04 01:22:43 +00:00
|
|
|
this.on(gpio).then((res) => {
|
|
|
|
if (process.env.DEBUG == 'true') console.log(res);
|
|
|
|
functions.sleep(process.env.ONTIME).then((res) => {
|
|
|
|
if (process.env.DEBUG == 'true') console.log(res);
|
|
|
|
this.off(gpio).then((res) => {
|
|
|
|
if (process.env.DEBUG == 'true') console.log(res);
|
|
|
|
functions.sleep(process.env.OFFTIME).then((res) => {
|
|
|
|
if (process.env.DEBUG == 'true') console.log(res);
|
2022-12-04 00:42:47 +00:00
|
|
|
resolve("Cycle complete.");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-12-03 23:03:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
files: {
|
2022-12-04 00:42:47 +00:00
|
|
|
check() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// TODO this code needs to be finished from migration
|
|
|
|
// Check for pause file existing, then sleep for preset time, then run the function again.
|
|
|
|
if (fs.existsSync('./pause')) {
|
|
|
|
resolve("pause");
|
|
|
|
}
|
2022-12-03 23:03:57 +00:00
|
|
|
|
2022-12-04 00:42:47 +00:00
|
|
|
// Check for reload file existing, then reload environment variables, then delete the file.
|
|
|
|
if (fs.existsSync('./reload')) {
|
|
|
|
resolve("reload");
|
|
|
|
}
|
2022-12-03 23:03:57 +00:00
|
|
|
|
2022-12-04 00:42:47 +00:00
|
|
|
// Check for quit file existing, then delete it, then quit the program
|
|
|
|
if (fs.existsSync('./quit')) {
|
|
|
|
resolve("quit");
|
|
|
|
}
|
|
|
|
resolve("none");
|
|
|
|
});
|
2022-12-03 23:03:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
commands: {
|
|
|
|
pause() {
|
2022-12-04 00:42:47 +00:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
functions.sleep(process.env.PAUSETIME).then(() => { resolve(); });
|
|
|
|
});
|
2022-12-03 23:03:57 +00:00
|
|
|
},
|
2022-12-04 00:42:47 +00:00
|
|
|
reload(envs) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
const dotenv = require('dotenv').config({ override: true });
|
|
|
|
fs.unlink('./reload', (err) => {
|
|
|
|
if (err) throw err;
|
|
|
|
console.log('Deleted reload file.');
|
|
|
|
});
|
|
|
|
console.log('Reloaded environment variables.');
|
|
|
|
console.log(`ONTIME=${process.env.ONTIME}\nOFFTIME=${process.env.OFFTIME}\nPAUSETIME=${process.env.PAUSETIME}\nDEBUG=${process.env.DEBUG}\nONPI=${process.env.ONPI}`);
|
|
|
|
resolve();
|
2022-12-03 23:03:57 +00:00
|
|
|
});
|
2022-12-04 00:42:47 +00:00
|
|
|
|
2022-12-03 23:03:57 +00:00
|
|
|
},
|
|
|
|
quit() {
|
|
|
|
fs.unlink('./quit', (err) => {
|
|
|
|
if (err) throw err;
|
|
|
|
console.log('Removed quit file.');
|
|
|
|
});
|
|
|
|
console.log('Quitting...');
|
|
|
|
process.exit();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Sleeps for any given milliseconds, call with await
|
|
|
|
sleep(ms) {
|
|
|
|
return new Promise((resolve) => {
|
2022-12-04 00:42:47 +00:00
|
|
|
if (process.env.DEBUG == "true") console.log(`Sleeping for ${ms}ms`);
|
|
|
|
const finish = () => {
|
2022-12-04 01:22:43 +00:00
|
|
|
resolve(`Slept for ${ms}ms`);
|
2022-12-04 00:42:47 +00:00
|
|
|
}
|
|
|
|
setTimeout(finish, ms);
|
2022-12-03 23:03:57 +00:00
|
|
|
});
|
|
|
|
},
|
2022-12-04 00:55:47 +00:00
|
|
|
init(gpio) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// Write the current env vars to console
|
|
|
|
console.log('Environment variables:');
|
|
|
|
console.log(`ONTIME=${process.env.ONTIME}\nOFFTIME=${process.env.OFFTIME}\nPAUSETIME=${process.env.PAUSETIME}\nDEBUG=${process.env.DEBUG}\nONPI=${process.env.ONPI}`);
|
|
|
|
// Set up GPIO 4 (pysical pin 7) as output, then call functions.auger.ready()
|
|
|
|
if (process.env.ONPI == 'true') {
|
|
|
|
gpio.setup(7, gpio.DIR_OUT, (err) => {
|
|
|
|
if (err) reject(err);
|
|
|
|
resolve('GPIO Initialized');
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
resolve('GPIO Not Available');
|
|
|
|
}
|
|
|
|
});
|
2022-12-03 23:20:38 +00:00
|
|
|
},
|
2022-12-03 23:03:57 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 00:42:47 +00:00
|
|
|
module.exports = { functions };
|