Better logging and naming

This commit is contained in:
Skylar Grant 2024-11-24 15:17:23 -05:00
parent 3f0be2d972
commit e9c54210aa
2 changed files with 31 additions and 18 deletions

View File

@ -12,10 +12,10 @@ module.exports = {
name: "igniter", name: "igniter",
topic: config.mqtt.topics.igniter, topic: config.mqtt.topics.igniter,
publisher: this.publisher, publisher: this.publisher,
power: (communicator, pinState) => { power: (comlink, pinState) => {
// Set the power based on the desired pinState // Set the power based on the desired pinState
this.igniter.on = pinState === 1 ? true : false; this.igniter.on = pinState === 1 ? true : false;
communicator.send(config.mqtt.topics.igniter, JSON.stringify(this.igniter)); comlink.send(config.mqtt.topics.igniter, JSON.stringify(this.igniter));
} }
}; };
@ -24,10 +24,10 @@ module.exports = {
name: "exhaust", name: "exhaust",
topic: config.mqtt.topics.exhaust, topic: config.mqtt.topics.exhaust,
publisher: this.publisher, publisher: this.publisher,
power: (communicator, pinState) => { power: (comlink, pinState) => {
// Set the power based on the desired pinState // Set the power based on the desired pinState
this.exhaust.on = pinState === 1 ? true : false; this.exhaust.on = pinState === 1 ? true : false;
communicator.send(config.mqtt.topics.exhaust, JSON.stringify(this.exhaust)); comlink.send(config.mqtt.topics.exhaust, JSON.stringify(this.exhaust));
} }
}; };
@ -37,10 +37,10 @@ module.exports = {
feedRate: 500, feedRate: 500,
topic: config.mqtt.topics.auger, topic: config.mqtt.topics.auger,
publisher: this.publisher, publisher: this.publisher,
power: (communicator, pinState) => { power: (comlink, pinState) => {
// Set the power based on the desired pinState // Set the power based on the desired pinState
this.auger.on = pinState === 1 ? true : false; this.auger.on = pinState === 1 ? true : false;
communicator.send(config.mqtt.topics.auger, JSON.stringify(this.auger)); comlink.send(config.mqtt.topics.auger, JSON.stringify(this.auger));
} }
}; };
@ -49,10 +49,10 @@ module.exports = {
name: "pof", name: "pof",
topic: config.mqtt.topics.pof, topic: config.mqtt.topics.pof,
publisher: this.publisher, publisher: this.publisher,
power: (communicator, pinState) => { power: (comlink, pinState) => {
// Set the power based on the desired pinState // Set the power based on the desired pinState
this.pof.on = pinState === 1 ? true : false; this.pof.on = pinState === 1 ? true : false;
communicator.send(config.mqtt.topics.pof, JSON.stringify(this.pof)); comlink.send(config.mqtt.topics.pof, JSON.stringify(this.pof));
} }
}; };
@ -61,10 +61,10 @@ module.exports = {
name: "vacuum", name: "vacuum",
topic: config.mqtt.topics.vacuum, topic: config.mqtt.topics.vacuum,
publisher: this.publisher, publisher: this.publisher,
power: (communicator, pinState) => { power: (comlink, pinState) => {
// Set the power based on the desired pinState // Set the power based on the desired pinState
this.vacuum.on = pinState === 1 ? true : false; this.vacuum.on = pinState === 1 ? true : false;
communicator.send(config.mqtt.topics.vacuum, JSON.stringify(this.vacuum)); comlink.send(config.mqtt.topics.vacuum, JSON.stringify(this.vacuum));
} }
}; };

View File

@ -1,28 +1,41 @@
// Variables
process.debug = true;
// Import modules // Import modules
const gpio = require('./custom_modules/VoidGPIO.js'); const gpio = require('./custom_modules/VoidGPIO.js');
const config = require('./custom_modules/config.json'); const config = require('./custom_modules/config.json');
const fn = require('./custom_modules/functions.js'); const fn = require('./custom_modules/functions.js');
const { State, Communicator } = require('./custom_modules/HestiaClasses.js'); const { State, Communicator } = require('./custom_modules/HestiaClasses.js');
// Initialize state and comlink
process.psState = new State(config); process.psState = new State(config);
const comms = new Communicator(process.psState); const comms = new Communicator(process.psState);
comms.init(process.psState, config); comms.init(process.psState, config);
// Initialize GPIO
fn.gpio.init(comms, process.psState); fn.gpio.init(comms, process.psState);
// Sensor detection loop // Sensor detection loop
setInterval(() => { setInterval(() => {
// Iterate through pins
for (const pin of config.pins) { for (const pin of config.pins) {
// If pin is an input, read it
if (pin.mode === 'IN') { if (pin.mode === 'IN') {
fn.log(`I: Sensor Detection Loop: Reading pin ${pin.board}`);
// Read pin
gpio.readPin(pin.board).then(state => { gpio.readPin(pin.board).then(state => {
fn.log(`I: Sensor Detection Loop: Pin ${pin.board} is ${state}`);
// Convert the state from string to boolean
const boolState = state === '1' ? true : false; const boolState = state === '1' ? true : false;
// Compare the state to the last known state
if (boolState !== process.psState[pin.key].on) { if (boolState !== process.psState[pin.key].on) {
// Update the state
process.psState[pin.key].on = boolState; process.psState[pin.key].on = boolState;
// Send the state to the MQTT broker
comms.send(config.mqtt.topics[pin.key], JSON.stringify(process.psState[pin.key])); comms.send(config.mqtt.topics[pin.key], JSON.stringify(process.psState[pin.key]));
fn.log(`${pin.key}: ${state}`); fn.log(`I: Sensor Detection Loop: ${pin.key}: ${state}`);
} }
}).catch(e => console.error(e)); }).catch(e => console.error(`E: Sensor Detection Loop: ${e}`));
} }
} }
}, 1000); }, 1000);
@ -31,16 +44,16 @@ setInterval(() => {
setInterval(fn.routines.cycleAuger, config.augerTotalCycleTime); setInterval(fn.routines.cycleAuger, config.augerTotalCycleTime);
comms.on('stateChange', (oldState, state) => { comms.on('stateChange', (oldState, state) => {
console.log(`State change detected.`); fn.log(`Event: State change detected.`);
fn.handlers.stateChange(oldState, state); fn.handlers.stateChange(oldState, state);
}); });
comms.on('startup', () => { comms.on('startup', () => {
console.log(`Startup detected.`); fn.log(`I: Event: Startup detected.`);
fn.power.start.init(comms, process.psState).catch(e => console.error(e)); fn.power.start.init(comms, process.psState).catch(e => console.error(`E: Power Start Init: ${e}`));
}); });
comms.on('shutdown', () => { comms.on('shutdown', () => {
console.log(`Shutdown detected.`); fn.log(`I: Event: Shutdown detected.`);
fn.power.stop.init(comms, process.psState).catch(e => console.error(e)); fn.power.stop.init(comms, process.psState).catch(e => console.error(`E: Power Stop Init: ${e}`));
}); });