Testing improved init logic
This commit is contained in:
parent
8a68c00f01
commit
594d177348
@ -9,10 +9,10 @@ module.exports = {
|
||||
on: false,
|
||||
name: "igniter",
|
||||
topic: config.mqtt.topics.igniter,
|
||||
publisher: 'front',
|
||||
power: (communicator) => {
|
||||
// This *should* toggle the state, asks if state is true, if it is set it false, otherwise set it true
|
||||
this.igniter.on ? this.igniter.on = false : this.igniter.on = true;
|
||||
publisher: 'backend',
|
||||
power: (communicator, 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));
|
||||
}
|
||||
};
|
||||
@ -21,10 +21,10 @@ module.exports = {
|
||||
on: false,
|
||||
name: "exhaust",
|
||||
topic: config.mqtt.topics.exhaust,
|
||||
publisher: 'front',
|
||||
power: (communicator) => {
|
||||
// This *should* toggle the state, asks if state is true, if it is set it false, otherwise set it true
|
||||
this.exhaust.on ? this.exhaust.on = false : this.exhaust.on = true;
|
||||
publisher: 'backend',
|
||||
power: (communicator, 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));
|
||||
}
|
||||
};
|
||||
@ -34,10 +34,10 @@ module.exports = {
|
||||
name: "auger",
|
||||
feedRate: 500,
|
||||
topic: config.mqtt.topics.auger,
|
||||
publisher: 'front',
|
||||
power: (communicator) => {
|
||||
// This *should* toggle the state, asks if state is true, if it is set it false, otherwise set it true
|
||||
this.auger.on ? this.auger.on = false : this.auger.on = true;
|
||||
publisher: 'backend',
|
||||
power: (communicator, 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));
|
||||
}
|
||||
};
|
||||
@ -75,16 +75,24 @@ module.exports = {
|
||||
|
||||
// Handle when the Broker sends us a message
|
||||
client.on('message', (topic, message) => {
|
||||
// Save the existing state
|
||||
const oldState = { ...state };
|
||||
// The message is a buffer which will need to be converted to string
|
||||
const msgStr = message.toString();
|
||||
// Since the message is a JSON object, we can parse it
|
||||
const msgJson = JSON.parse(msgStr);
|
||||
console.log(`Message received on topic ${topic}: ${msgStr}`);
|
||||
// Log the message
|
||||
console.log(`Message received on topic ${topic}:`);
|
||||
console.log(msgJson);
|
||||
// Check if the message is from the backend
|
||||
if (msgJson.publisher === this.publisher) {
|
||||
console.log('Message is from the backend, ignoring');
|
||||
return;
|
||||
}
|
||||
// Update the state
|
||||
state[msgJson.name].on = msgJson.on;
|
||||
const change = {
|
||||
name: msgJson.name,
|
||||
on: msgJson.on
|
||||
};
|
||||
this.emit('stateChange', change);
|
||||
// Emit the state change
|
||||
this.emit('stateChange', oldState, state);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -19,7 +19,7 @@ module.exports = {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
},
|
||||
gpio: {
|
||||
setDefaults() {
|
||||
setDefaults(communicator, state) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let stateChanges = [];
|
||||
|
||||
@ -27,6 +27,7 @@ module.exports = {
|
||||
if (pin.mode === 'OUT') {
|
||||
return gpio.setPin(pin.board, pin.defaultState).then(() => {
|
||||
stateChanges.push(`Set ${pin.key} pin to ${pin.defaultState}.`);
|
||||
state[pin.key].power(communicator, pin.defaultState);
|
||||
}).catch(e => console.error(e));
|
||||
}
|
||||
});
|
||||
@ -147,10 +148,19 @@ module.exports = {
|
||||
}
|
||||
},
|
||||
handlers: {
|
||||
stateChange(change) {
|
||||
gpio.togglePin(pinMap.get(change.name).board).then(() => {
|
||||
console.log(`${change.name} toggled.`);
|
||||
stateChange(oldState, state) {
|
||||
const promises = pins.map(pin => {
|
||||
if (oldState[pin.key].on !== state[pin.key].on) {
|
||||
if (state[pin.key].on) {
|
||||
return gpio.setPin(pin.board, 1).then(() => {
|
||||
console.log(`${pin.key} powered on.`);
|
||||
}).catch(e => console.error(e));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Promise.all(promises).then(() => {
|
||||
console.log('State change complete.');
|
||||
}).catch(e => console.error(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ setInterval(() => {
|
||||
}
|
||||
}, 1000);
|
||||
|
||||
comms.on('stateChange', (change) => {
|
||||
console.log(`State change detected: ${change.name}`);
|
||||
fn.handlers.stateChange(change);
|
||||
comms.on('stateChange', (oldState, state) => {
|
||||
console.log(`State change detected.`);
|
||||
fn.handlers.stateChange(oldState, state);
|
||||
});
|
Loading…
Reference in New Issue
Block a user