Reconfigure module

This commit is contained in:
Skylar Grant 2024-08-21 21:18:43 -04:00
parent 9e543989b8
commit 24470fe6f2

View File

@ -1,93 +1,96 @@
export class State { module.exports = {
constructor(config) { // State class
this.igniter = { State: class State {
on: false, constructor(config) {
name: "igniter", this.igniter = {
topic: config.mqtt.topics.igniter, on: false,
publisher: 'front', name: "igniter",
power: (communicator) => { topic: config.mqtt.topics.igniter,
// This *should* toggle the state, asks if state is true, if it is set it false, otherwise set it true publisher: 'front',
this.igniter.on ? this.igniter.on = false : this.igniter.on = true; power: (communicator) => {
communicator.send(config.mqtt.topics.igniter, JSON.stringify(this.igniter)); // 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;
communicator.send(config.mqtt.topics.igniter, JSON.stringify(this.igniter));
}
};
this.exhaust = {
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;
communicator.send(config.mqtt.topics.exhaust, JSON.stringify(this.exhaust));
}
};
this.auger = {
on: false,
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;
communicator.send(config.mqtt.topics.auger, JSON.stringify(this.auger));
}
};
console.log(`State initialized.`)
}; };
},
// Communicator class
Communicator: class Communicator {
constructor(state) {
this.publisher = state.publisher;
return this;
}
this.exhaust = { init(state, config) {
on: false, // Connect to the MQTT Broker
name: "exhaust", console.log(`Attempting MQTT connection to broker: ${config.mqtt.address}, with username: ${config.mqtt.username}`);
topic: config.mqtt.topics.exhaust, this.client = mqtt.connect(config.mqtt.address, {
publisher: 'front', username: config.mqtt.username,
power: (communicator) => { password: config.mqtt.password
// 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; const { client } = this;
communicator.send(config.mqtt.topics.exhaust, JSON.stringify(this.exhaust));
}
};
this.auger = { client.on('connect', () => {
on: false, console.log('Connected to MQTT broker');
name: "auger", // Subscribe to status topics
feedRate: 500, config.states.elements.forEach(element => {
topic: config.mqtt.topics.auger, client.subscribe(state[element].topic, (err) => {
publisher: 'front', if (!err) {
power: (communicator) => { console.log(`Subscribed to ${state[element].topic}`);
// 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; });
communicator.send(config.mqtt.topics.auger, JSON.stringify(this.auger));
}
};
console.log(`State initialized.`)
};
};
export class Communicator {
constructor(state) {
this.publisher = state.publisher;
return this;
}
init(state, config) {
// Connect to the MQTT Broker
console.log(`Attempting MQTT connection to broker: ${config.mqtt.address}, with username: ${config.mqtt.username}`);
this.client = mqtt.connect(config.mqtt.address, {
username: config.mqtt.username,
password: config.mqtt.password
});
const { client } = this;
client.on('connect', () => {
console.log('Connected to MQTT broker');
// Subscribe to status topics
config.states.elements.forEach(element => {
client.subscribe(state[element].topic, (err) => {
if (!err) {
console.log(`Subscribed to ${state[element].topic}`);
}
}); });
}); });
}); // Handle when the Broker sends us a message
client.on('message', (topic, message) => {
const msgStr = message.toString();
const msgJson = JSON.parse(msgStr);
console.log(`Message received on topic ${topic}: ${msgStr}`);
console.log(msgJson);
state[msgJson.name].on = msgJson.on;
window.refreshState(window.document, state);
});
}
// Handle when the Broker sends us a message // Publish a message to the MQTT Broker
client.on('message', (topic, message) => { send(topic, message) {
const msgStr = message.toString(); // Publish with retain flag set to true
const msgJson = JSON.parse(msgStr); this.client.publish(topic, message, { retain: true }, (err) => {
console.log(`Message received on topic ${topic}: ${msgStr}`); if (err) {
console.log(msgJson); console.error('Failed to publish message:', err);
state[msgJson.name].on = msgJson.on; } else {
window.refreshState(window.document, state); console.log('Message published and retained on topic:', topic);
}); }
});
}
} }
};
// Publish a message to the MQTT Broker
send(topic, message) {
// Publish with retain flag set to true
this.client.publish(topic, message, { retain: true }, (err) => {
if (err) {
console.error('Failed to publish message:', err);
} else {
console.log('Message published and retained on topic:', topic);
}
});
}
}