Gonna go testing
This commit is contained in:
parent
5633c2340a
commit
63053d6a87
@ -1,29 +1,48 @@
|
||||
export class State {
|
||||
constructor(config) {
|
||||
config.mqtt.subscriptions.forEach(subscription => {
|
||||
this[subscription.name] = {
|
||||
constructor() {
|
||||
this.publisher = 'front';
|
||||
return this;
|
||||
};
|
||||
|
||||
init(config) {
|
||||
config.mqtt.subscriptions.forEach(sub => {
|
||||
this[sub.name] = {
|
||||
on: false,
|
||||
topic: subscription.topic,
|
||||
topic: sub.topic,
|
||||
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[subscription.name].on ? this[subscription.name].on = false : this[subscription.name].on = true;
|
||||
communicator.send(subscription.name, JSON.stringify(this));
|
||||
this[sub.name].on ? this[sub.name].on = false : this[sub.name].on = true;
|
||||
communicator.send(sub.topic, JSON.stringify(this));
|
||||
}
|
||||
};
|
||||
});
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
export class Communicator {
|
||||
constructor(state) {
|
||||
this.publisher = state.publisher;
|
||||
return this;
|
||||
}
|
||||
|
||||
init(state) {
|
||||
// Connect to the MQTT Broker
|
||||
this.client = mqtt.connect(config.mqtt.address);
|
||||
const { client } = this;
|
||||
|
||||
// Subscribe to status topics
|
||||
config.mqtt.subscriptions.forEach(subscription => {
|
||||
this.client.subscribe(subscription.topic);
|
||||
state[subscription.name].topic = subscription.topic;
|
||||
client.on('connect', () => {
|
||||
console.log('Connected to MQTT broker');
|
||||
// Subscribe to status topics
|
||||
config.mqtt.subscriptions.forEach(sub => {
|
||||
client.subscribe(sub.topic, (err) => {
|
||||
if (!err) {
|
||||
console.log(`Subscribed to ${sub.topic}`);
|
||||
state[sub.name].topic = sub.topic;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"mqtt": {
|
||||
"address": "wd://broker-url:port",
|
||||
"address": "ws://192.168.0.3:1883",
|
||||
"subscriptions": [
|
||||
{
|
||||
"name": "igniter",
|
||||
|
@ -1,4 +1,7 @@
|
||||
import { Communicator,State } from './HestiaClasses.js';
|
||||
import { Communicator, State } from './HestiaClasses.js';
|
||||
let config;
|
||||
const psState = new State();
|
||||
const comms = new Communicator(state);
|
||||
|
||||
window.onload = async function() {
|
||||
await fetch('/assets/config.json')
|
||||
@ -9,9 +12,10 @@ window.onload = async function() {
|
||||
return response.json(); // Parse the JSON data from the response
|
||||
})
|
||||
.then(data => {
|
||||
const config = data;
|
||||
config = data;
|
||||
console.log(config);
|
||||
const psState = new State(config);
|
||||
psState.init(config);
|
||||
comms.init(psState);
|
||||
refreshState(window.document, psState);
|
||||
})
|
||||
.catch(error => {
|
||||
@ -36,4 +40,8 @@ function refreshState(doc, state) {
|
||||
statusString = '';
|
||||
if (state.auger.on) statusString = "On"; else statusString = "Off";
|
||||
augerStatus.innerHTML = statusString;
|
||||
}
|
||||
|
||||
function power(element) {
|
||||
psState[element].power(comms);
|
||||
}
|
Loading…
Reference in New Issue
Block a user