From 634ef1e511f9524fab97b42e56630ad744711b46 Mon Sep 17 00:00:00 2001 From: cpu Date: Mon, 12 May 2025 23:58:46 +0200 Subject: [PATCH] state of the mqtt --- src/App.vue | 13 +++--- src/services/MqttService.js | 32 +++++++++++---- src/store/index.js | 19 +++++++-- src/views/SetupView.vue | 82 ++++++++++++++++++++++++++----------- vite.config.js | 28 +++++-------- 5 files changed, 116 insertions(+), 58 deletions(-) diff --git a/src/App.vue b/src/App.vue index 2397c5f..37098ce 100644 --- a/src/App.vue +++ b/src/App.vue @@ -178,16 +178,19 @@ onMounted(() => { // Example: Sync with OS theme only if no theme saved (requires store modification) // syncWithOsThemeIfNeeded(); - // *** Auto-connect MQTT if URL is stored and not already connected/connecting *** - const storedBrokerUrl = store.getters.mqttBrokerUrl; + // Auto-connect MQTT only if URL is stored AND user desires connection + const storedBrokerUrl = store.getters.mqttBrokerUrl; + const connectDesired = store.getters.mqttConnectDesired; // Get the flag + if (storedBrokerUrl && + connectDesired && // Check the flag MqttService.connectionStatus.value !== 'connected' && MqttService.connectionStatus.value !== 'connecting') { - console.log("App.vue: Auto-connecting to stored MQTT broker:", storedBrokerUrl); + console.log("App.vue: Auto-connecting to stored MQTT broker (user desired):", storedBrokerUrl); MqttService.connect(storedBrokerUrl); + } else if (storedBrokerUrl && !connectDesired) { + console.log("App.vue: MQTT Broker URL is stored, but user previously disconnected. Not auto-connecting."); } - // *** End Auto-connect MQTT *** - }).catch(error => { console.error("App.vue: Error during store.dispatch('loadState'):", error); }); diff --git a/src/services/MqttService.js b/src/services/MqttService.js index 5ac8599..46bbce4 100644 --- a/src/services/MqttService.js +++ b/src/services/MqttService.js @@ -114,15 +114,33 @@ const connect = async (brokerUrl = 'ws://localhost:9001') => { } }; -const disconnect = () => { /* ... existing disconnect logic ... */ - if (client.value && typeof client.value.end === 'function') { - console.log('MQTT: Disconnecting...'); - connectionStatus.value = 'disconnected'; - client.value.end(true, () => { - console.log('MQTT: Disconnected callback triggered.'); +const disconnect = () => { + if (client.value) { + console.log('MQTT: Disconnecting/Stopping connection attempt...'); + // Set status immediately to give user feedback, 'close' event will confirm + // but if it was 'connecting', it might not emit 'close' if it never truly connected. + const wasConnecting = connectionStatus.value === 'connecting'; + + client.value.end(true, () => { // true forces close and stops reconnect attempts + console.log('MQTT: client.end() callback executed.'); + // The 'close' event listener on the client should handle final cleanup + // like setting client.value = null and connectionStatus.value = 'disconnected'. + // If it was just 'connecting' and never connected, 'close' might not fire reliably. + if (wasConnecting && connectionStatus.value !== 'disconnected') { + connectionStatus.value = 'disconnected'; + client.value = null; // Ensure cleanup if 'close' doesn't fire from 'connecting' state + } }); + // If it was 'connecting', we might want to immediately reflect disconnected state + // as 'end(true)' stops further attempts. + if (wasConnecting) { + connectionStatus.value = 'disconnected'; + // Note: client.value will be fully nulled on the 'close' event or after end() callback. + } + } else { - connectionStatus.value = 'disconnected'; + console.log('MQTT: No active client to disconnect.'); + connectionStatus.value = 'disconnected'; // Ensure status is correct } }; diff --git a/src/store/index.js b/src/store/index.js index 3d8a111..9a057d1 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -36,6 +36,7 @@ const initialState = { globalMqttStopPause: null, // New globalMqttRunAll: null, // New mqttBrokerUrl: 'ws://localhost:9001', // Default, user can change + mqttConnectDesired: false, currentPlayerIndex: 0, gameMode: 'normal', isMuted: false, @@ -80,6 +81,7 @@ export default createStore({ globalMqttStopPause: persistedState.globalMqttStopPause || initialState.globalMqttStopPause, globalMqttRunAll: persistedState.globalMqttRunAll || initialState.globalMqttRunAll, mqttBrokerUrl: persistedState.mqttBrokerUrl || initialState.mqttBrokerUrl, + mqttConnectDesired: persistedState.hasOwnProperty('mqttConnectDesired') ? persistedState.mqttConnectDesired : initialState.mqttConnectDesired, gameRunning: false, // Always start non-running currentPlayerIndex: persistedState.currentPlayerIndex !== undefined && persistedState.currentPlayerIndex < playersToUse.length ? persistedState.currentPlayerIndex : 0, }; @@ -113,6 +115,10 @@ export default createStore({ }, SET_MQTT_BROKER_URL(state, url) { state.mqttBrokerUrl = url; + state.mqttConnectDesired = true; + }, + SET_MQTT_CONNECT_DESIRED(state, desired) { + state.mqttConnectDesired = desired; }, SET_GLOBAL_MQTT_STOP_PAUSE(state, char) { state.globalMqttStopPause = char; @@ -251,9 +257,10 @@ export default createStore({ })), globalHotkeyStopPause: state.globalHotkeyStopPause, globalHotkeyRunAll: state.globalHotkeyRunAll, - globalMqttStopPause: state.globalMqttStopPause, // Save - globalMqttRunAll: state.globalMqttRunAll, // Save - mqttBrokerUrl: state.mqttBrokerUrl, // Save + globalMqttStopPause: state.globalMqttStopPause, + globalMqttRunAll: state.globalMqttRunAll, + mqttBrokerUrl: state.mqttBrokerUrl, + mqttConnectDesired: state.mqttConnectDesired, currentPlayerIndex: state.currentPlayerIndex, gameMode: state.gameMode, isMuted: state.isMuted, @@ -268,6 +275,10 @@ export default createStore({ commit('SET_MQTT_BROKER_URL', url); dispatch('saveState'); }, + setMqttConnectDesired({ commit, dispatch }, desired) { + commit('SET_MQTT_CONNECT_DESIRED', desired); + dispatch('saveState'); + }, setGlobalMqttStopPause({ commit, dispatch }, char) { commit('SET_GLOBAL_MQTT_STOP_PAUSE', char); dispatch('saveState'); @@ -333,6 +344,7 @@ export default createStore({ commit('SET_GLOBAL_HOTKEY_STOP_PAUSE', freshInitialState.globalHotkeyStopPause); commit('SET_GLOBAL_HOTKEY_RUN_ALL', freshInitialState.globalHotkeyRunAll); commit('SET_MQTT_BROKER_URL', freshInitialState.mqttBrokerUrl); // Reset + commit('SET_MQTT_CONNECT_DESIRED', freshInitialState.mqttConnectDesired); // Reset commit('SET_GLOBAL_MQTT_STOP_PAUSE', freshInitialState.globalMqttStopPause); // Reset commit('SET_GLOBAL_MQTT_RUN_ALL', freshInitialState.globalMqttRunAll); // Reset @@ -486,6 +498,7 @@ export default createStore({ globalHotkeyStopPause: state => state.globalHotkeyStopPause, globalHotkeyRunAll: state => state.globalHotkeyRunAll, mqttBrokerUrl: state => state.mqttBrokerUrl, + mqttConnectDesired: state => state.mqttConnectDesired, globalMqttStopPause: state => state.globalMqttStopPause, globalMqttRunAll: state => state.globalMqttRunAll, totalPlayers: state => state.players.length, diff --git a/src/views/SetupView.vue b/src/views/SetupView.vue index 8fbcb8d..2941cc4 100644 --- a/src/views/SetupView.vue +++ b/src/views/SetupView.vue @@ -4,10 +4,11 @@

Nexus Timer Setup

- +
+
-

Players ({{ players.length }})

+

Players ({{ players.length }})

{{ mqttError }}

-

Connected to {{ store.getters.mqttBrokerUrl }}. Subscribed to '{{ MqttService.MQTT_TOPIC_GAME }}'.

-

Not connected to MQTT broker.

+

+ Connected to {{ store.getters.mqttBrokerUrl }}. Subscribed to '{{ MqttService.MQTT_TOPIC_GAME }}'. +

+

+ Connecting to {{ localMqttBrokerUrl }}... (Click "Stop" to cancel) +

+

+ Not connected. +

+

Global "Stop/Pause All" Trigger:

@@ -87,7 +106,6 @@
-

Global "Run All Timers" Trigger:

@@ -141,6 +159,12 @@
+ +