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;
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.`)
};
};
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}`);
}
});
}); });
const { client } = this;
});
// Handle when the Broker sends us a message client.on('connect', () => {
client.on('message', (topic, message) => { console.log('Connected to MQTT broker');
const msgStr = message.toString(); // Subscribe to status topics
const msgJson = JSON.parse(msgStr); config.states.elements.forEach(element => {
console.log(`Message received on topic ${topic}: ${msgStr}`); client.subscribe(state[element].topic, (err) => {
console.log(msgJson); if (!err) {
state[msgJson.name].on = msgJson.on; console.log(`Subscribed to ${state[element].topic}`);
window.refreshState(window.document, state); }
}); });
} });
});
// Publish a message to the MQTT Broker // Handle when the Broker sends us a message
send(topic, message) { client.on('message', (topic, message) => {
// Publish with retain flag set to true const msgStr = message.toString();
this.client.publish(topic, message, { retain: true }, (err) => { const msgJson = JSON.parse(msgStr);
if (err) { console.log(`Message received on topic ${topic}: ${msgStr}`);
console.error('Failed to publish message:', err); console.log(msgJson);
} else { state[msgJson.name].on = msgJson.on;
console.log('Message published and retained on topic:', topic); window.refreshState(window.document, state);
} });
}); }
// 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);
}
});
}
} }
} };