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",
topic: config.mqtt.topics.igniter,
publisher: this.publisher,
power: (communicator, pinState) => {
power: (comlink, pinState) => {
// Set the power based on the desired pinState
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",
topic: config.mqtt.topics.exhaust,
publisher: this.publisher,
power: (communicator, pinState) => {
power: (comlink, pinState) => {
// Set the power based on the desired pinState
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,
topic: config.mqtt.topics.auger,
publisher: this.publisher,
power: (communicator, pinState) => {
power: (comlink, pinState) => {
// Set the power based on the desired pinState
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",
topic: config.mqtt.topics.pof,
publisher: this.publisher,
power: (communicator, pinState) => {
power: (comlink, pinState) => {
// Set the power based on the desired pinState
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",
topic: config.mqtt.topics.vacuum,
publisher: this.publisher,
power: (communicator, pinState) => {
power: (comlink, pinState) => {
// Set the power based on the desired pinState
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
const gpio = require('./custom_modules/VoidGPIO.js');
const config = require('./custom_modules/config.json');
const fn = require('./custom_modules/functions.js');
const { State, Communicator } = require('./custom_modules/HestiaClasses.js');
// Initialize state and comlink
process.psState = new State(config);
const comms = new Communicator(process.psState);
comms.init(process.psState, config);
// Initialize GPIO
fn.gpio.init(comms, process.psState);
// Sensor detection loop
setInterval(() => {
// Iterate through pins
for (const pin of config.pins) {
// If pin is an input, read it
if (pin.mode === 'IN') {
fn.log(`I: Sensor Detection Loop: Reading pin ${pin.board}`);
// Read pin
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;
// Compare the state to the last known state
if (boolState !== process.psState[pin.key].on) {
// Update the state
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]));
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);
@ -31,16 +44,16 @@ setInterval(() => {
setInterval(fn.routines.cycleAuger, config.augerTotalCycleTime);
comms.on('stateChange', (oldState, state) => {
console.log(`State change detected.`);
fn.log(`Event: State change detected.`);
fn.handlers.stateChange(oldState, state);
});
comms.on('startup', () => {
console.log(`Startup detected.`);
fn.power.start.init(comms, process.psState).catch(e => console.error(e));
fn.log(`I: Event: Startup detected.`);
fn.power.start.init(comms, process.psState).catch(e => console.error(`E: Power Start Init: ${e}`));
});
comms.on('shutdown', () => {
console.log(`Shutdown detected.`);
fn.power.stop.init(comms, process.psState).catch(e => console.error(e));
fn.log(`I: Event: Shutdown detected.`);
fn.power.stop.init(comms, process.psState).catch(e => console.error(`E: Power Stop Init: ${e}`));
});