From 9279257ce94333ff3b011defbca61065581ae5c5 Mon Sep 17 00:00:00 2001 From: technyon Date: Sun, 27 Mar 2022 19:55:42 +0200 Subject: [PATCH] show pairing and mqtt status in configuration page --- Network.cpp | 9 ++++++++- Network.h | 4 ++++ Nuki.cpp | 5 +++++ Nuki.h | 2 ++ WebCfgServer.cpp | 16 ++++++++++++++-- WebCfgServer.h | 6 +++++- main.cpp | 2 +- 7 files changed, 39 insertions(+), 5 deletions(-) diff --git a/Network.cpp b/Network.cpp index 01789162..1352e3bc 100644 --- a/Network.cpp +++ b/Network.cpp @@ -68,6 +68,7 @@ bool Network::reconnect() // Attempt to connect if (_mqttClient.connect("nukiHub")) { Serial.println(F("MQTT connected")); + _mqttConnected = true; // ... and resubscribe _mqttClient.subscribe(mqtt_topic_lockstate_action); @@ -76,12 +77,13 @@ bool Network::reconnect() { Serial.print(F("MQTT connect failed, rc=")); Serial.println(_mqttClient.state()); + _mqttConnected = false; _nextReconnect = millis() + 5000; } } + return _mqttConnected; } - void Network::update() { if(!WiFi.isConnected()) @@ -167,3 +169,8 @@ void Network::publishInt(const char *topic, const int value) itoa(value, str, 10); _mqttClient.publish(topic, str); } + +bool Network::isMqttConnected() +{ + return _mqttConnected; +} diff --git a/Network.h b/Network.h index 2f3929d4..c9e37788 100644 --- a/Network.h +++ b/Network.h @@ -14,6 +14,8 @@ class Network void initialize(); void update(); + bool isMqttConnected(); + void publishKeyTurnerState(const char* state, const char* trigger, const char* completionStatus); void publishBatteryReport(const BatteryReport& batteryReport); @@ -32,6 +34,8 @@ class Network WiFiClient _wifiClient; Preferences* _preferences; + bool _mqttConnected = false; + unsigned long _nextReconnect = 0; char _mqttBrokerAddr[100] = {0}; diff --git a/Nuki.cpp b/Nuki.cpp index 7936f850..d5f37d6c 100644 --- a/Nuki.cpp +++ b/Nuki.cpp @@ -251,3 +251,8 @@ void Nuki::onLockActionReceived(const char *value) Serial.print(F("Action: ")); Serial.println((int)nukiInst->_nextLockAction); } + +const bool Nuki::isPaired() +{ + return _paired; +} diff --git a/Nuki.h b/Nuki.h index 7cf97420..576ab8c5 100644 --- a/Nuki.h +++ b/Nuki.h @@ -12,6 +12,8 @@ class Nuki void initialize(); void update(); + const bool isPaired(); + private: static void onLockActionReceived(const char* value); diff --git a/WebCfgServer.cpp b/WebCfgServer.cpp index 9cbafb7f..a644f86c 100644 --- a/WebCfgServer.cpp +++ b/WebCfgServer.cpp @@ -2,8 +2,10 @@ #include #include "PreferencesKeys.h" -WebCfgServer::WebCfgServer(Preferences* preferences) +WebCfgServer::WebCfgServer(Nuki* nuki, Network* network, Preferences* preferences) : _wifiServer(80), + _nuki(nuki), + _network(network), _preferences(preferences) {} @@ -112,6 +114,16 @@ void WebCfgServer::serveHtml(WiFiClient &client) client.println(""); client.println(""); + + client.println("

"); + client.print("Paired: "); + client.println(_nuki->isPaired() ? "Yes" : "No"); + client.println("

"); + client.println("

"); + client.print("MQTT Connected: "); + client.println(_network->isMqttConnected() ? "Yes" : "No"); + client.println("



"); + client.println("
"); client.print("MQTT Broker: getInt(preference_query_interval_battery)); client.println("\" NAME=\"BATINT\" SIZE=\"25\" MAXLENGTH=\"16\">
"); - client.println(""); + client.println("
"); client.println("
"); diff --git a/WebCfgServer.h b/WebCfgServer.h index c9374f03..9cc632d8 100644 --- a/WebCfgServer.h +++ b/WebCfgServer.h @@ -2,6 +2,8 @@ #include #include +#include "Nuki.h" +#include "Network.h" enum class TokenType { @@ -15,7 +17,7 @@ enum class TokenType class WebCfgServer { public: - WebCfgServer(Preferences* preferences); + WebCfgServer(Nuki* nuki, Network* network, Preferences* preferences); ~WebCfgServer() = default; void initialize(); @@ -28,6 +30,8 @@ class WebCfgServer TokenType getParameterType(char*& token); WiFiServer _wifiServer; + Nuki* _nuki; + Network* _network; Preferences* _preferences; bool _enabled = true; diff --git a/main.cpp b/main.cpp index 008b2bf3..2657c3c0 100644 --- a/main.cpp +++ b/main.cpp @@ -39,8 +39,8 @@ void setup() preferences = new Preferences(); preferences->begin("nukihub", false); network = new Network(preferences); - webCfgServer = new WebCfgServer(preferences); nuki = new Nuki("Main Door", 2020001, network, preferences); + webCfgServer = new WebCfgServer(nuki, network, preferences); network->initialize(); webCfgServer->initialize();