From 13f0bc329664c6fb6abb6a66a5676be744f8388a Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Mon, 3 Oct 2022 08:08:51 +0200 Subject: [PATCH] mqtt status shows queue and reconnects --- .../src/framework/mqtt/MqttStatusForm.tsx | 69 +++++++++++-------- interface/src/types/mqtt.ts | 2 + lib/framework/MqttStatus.cpp | 4 +- src/emsdevice.cpp | 8 ++- src/emsdevicevalue.cpp | 2 +- src/emsdevicevalue.h | 2 +- src/mqtt.cpp | 4 +- src/mqtt.h | 12 +++- 8 files changed, 66 insertions(+), 37 deletions(-) diff --git a/interface/src/framework/mqtt/MqttStatusForm.tsx b/interface/src/framework/mqtt/MqttStatusForm.tsx index a889ec0fa..d844dbb8b 100644 --- a/interface/src/framework/mqtt/MqttStatusForm.tsx +++ b/interface/src/framework/mqtt/MqttStatusForm.tsx @@ -5,6 +5,7 @@ import DeviceHubIcon from '@mui/icons-material/DeviceHub'; import RefreshIcon from '@mui/icons-material/Refresh'; import ReportIcon from '@mui/icons-material/Report'; import SpeakerNotesOffIcon from '@mui/icons-material/SpeakerNotesOff'; +import AutoAwesomeMotionIcon from '@mui/icons-material/AutoAwesomeMotion'; import { ButtonRow, FormLoader, SectionContent } from '../../components'; import { MqttStatus, MqttDisconnectReason } from '../../types'; @@ -31,6 +32,12 @@ export const mqttPublishHighlight = ({ mqtt_fails }: MqttStatus, theme: Theme) = return theme.palette.error.main; }; +export const mqttQueueHighlight = ({ mqtt_queued }: MqttStatus, theme: Theme) => { + if (mqtt_queued <= 1) return theme.palette.success.main; + + return theme.palette.warning.main; +}; + const MqttStatusForm: FC = () => { const { loadData, data, errorMessage } = useRest({ read: MqttApi.readMqttStatus }); @@ -38,14 +45,14 @@ const MqttStatusForm: FC = () => { const theme = useTheme(); - const mqttStatus = ({ enabled, connected }: MqttStatus) => { + const mqttStatus = ({ enabled, connected, connect_count }: MqttStatus) => { if (!enabled) { return LL.NOT_ENABLED(); } if (connected) { - return LL.CONNECTED(); + return (LL.CONNECTED() + (connect_count > 1 ? ' (' + connect_count + ')' : '')); } - return LL.DISCONNECTED(); + return (LL.DISCONNECTED() + (connect_count > 1 ? ' (' + connect_count + ')' : '')); }; const disconnectReason = ({ disconnect_reason }: MqttStatus) => { @@ -77,36 +84,44 @@ const MqttStatusForm: FC = () => { } const renderConnectionStatus = () => { - if (data.connected) { - return ( - <> - - - # - - - - - - - - - - - - - - ); - } return ( <> + {!data.connected && ( + <> + + + + + + + + + + + )} - - + # + + + + + + + + - + + + + + + + + + + diff --git a/interface/src/types/mqtt.ts b/interface/src/types/mqtt.ts index 1e2b50a00..0e8a10eec 100644 --- a/interface/src/types/mqtt.ts +++ b/interface/src/types/mqtt.ts @@ -15,6 +15,8 @@ export interface MqttStatus { client_id: string; disconnect_reason: MqttDisconnectReason; mqtt_fails: number; + mqtt_queued: number; + connect_count: number; } export interface MqttSettings { diff --git a/lib/framework/MqttStatus.cpp b/lib/framework/MqttStatus.cpp index dc75cc9fc..c5ff2a05c 100644 --- a/lib/framework/MqttStatus.cpp +++ b/lib/framework/MqttStatus.cpp @@ -20,7 +20,9 @@ void MqttStatus::mqttStatus(AsyncWebServerRequest * request) { root["client_id"] = _mqttSettingsService->getClientId(); root["disconnect_reason"] = (uint8_t)_mqttSettingsService->getDisconnectReason(); - root["mqtt_fails"] = emsesp::Mqtt::publish_fails(); // proddy added + root["mqtt_queued"] = emsesp::Mqtt::publish_queued(); // mdvp added + root["mqtt_fails"] = emsesp::Mqtt::publish_fails(); // proddy added + root["connect_count"] = emsesp::Mqtt::connect_count(); // mdvp added response->setLength(); request->send(response); diff --git a/src/emsdevice.cpp b/src/emsdevice.cpp index 7db14612b..c156cf3f1 100644 --- a/src/emsdevice.cpp +++ b/src/emsdevice.cpp @@ -860,7 +860,8 @@ void EMSdevice::generate_values_web(JsonObject & output) { obj["s"] = Helpers::render_value(s, (float)(-1) * dv.numeric_operator, 0); } - int16_t dv_set_min, dv_set_max; + int16_t dv_set_min; + uint16_t dv_set_max; if (dv.get_min_max(dv_set_min, dv_set_max)) { obj["m"] = Helpers::render_value(s, dv_set_min, 0); obj["x"] = Helpers::render_value(s, dv_set_max, 0); @@ -1042,7 +1043,7 @@ void EMSdevice::setCustomEntity(const std::string & entity_id) { auto min = dv.min; auto max = dv.max; dv.set_custom_minmax(); - if (dv.short_name == FL_(seltemp)[0] && (min != dv.min || max != dv.max)) { + if (Mqtt::ha_enabled() && dv.short_name == FL_(seltemp)[0] && (min != dv.min || max != dv.max)) { set_climate_minmax(dv.tag, dv.min, dv.max); } return; @@ -1214,7 +1215,8 @@ bool EMSdevice::get_value_info(JsonObject & output, const char * cmd, const int8 // set the min and max only for commands if (dv.has_cmd) { - int16_t dv_set_min, dv_set_max; + int16_t dv_set_min; + uint16_t dv_set_max; if (dv.get_min_max(dv_set_min, dv_set_max)) { json["min"] = dv_set_min; json["max"] = dv_set_max; diff --git a/src/emsdevicevalue.cpp b/src/emsdevicevalue.cpp index f436e96b3..4fa65e074 100644 --- a/src/emsdevicevalue.cpp +++ b/src/emsdevicevalue.cpp @@ -266,7 +266,7 @@ bool DeviceValue::hasValue() const { // converts to signed int, which means rounding to an whole integer // returns false if there is no min/max needed // Types BOOL, ENUM, STRING and CMD are not used -bool DeviceValue::get_min_max(int16_t & dv_set_min, int16_t & dv_set_max) { +bool DeviceValue::get_min_max(int16_t & dv_set_min, uint16_t & dv_set_max) { uint8_t fahrenheit = !EMSESP::system_.fahrenheit() ? 0 : (uom == DeviceValueUOM::DEGREES) ? 2 : (uom == DeviceValueUOM::DEGREES_R) ? 1 : 0; // if we have individual limits set already, just do the conversion diff --git a/src/emsdevicevalue.h b/src/emsdevicevalue.h index d3ac85ce5..1aac8ee17 100644 --- a/src/emsdevicevalue.h +++ b/src/emsdevicevalue.h @@ -177,7 +177,7 @@ class DeviceValue { uint8_t state); bool hasValue() const; - bool get_min_max(int16_t & dv_set_min, int16_t & dv_set_max); + bool get_min_max(int16_t & dv_set_min, uint16_t & dv_set_max); void set_custom_minmax(); bool get_custom_min(int16_t & val); diff --git a/src/mqtt.cpp b/src/mqtt.cpp index a0e6aae0a..12113214d 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -917,8 +917,8 @@ void Mqtt::publish_ha_sensor_config(DeviceValue & dv, const std::string & model, } // calculate the min and max - int16_t dv_set_min; - int16_t dv_set_max; + int16_t dv_set_min; + uint16_t dv_set_max; (void)dv.get_min_max(dv_set_min, dv_set_max); // determine if we're creating the command topics which we use special HA configs diff --git a/src/mqtt.h b/src/mqtt.h index ffc93ab34..d9a1288e1 100644 --- a/src/mqtt.h +++ b/src/mqtt.h @@ -167,6 +167,14 @@ class Mqtt { return mqtt_publish_fails_; } + static uint32_t publish_queued() { + return mqtt_messages_.size(); + } + + static uint8_t connect_count() { + return connectcount_; + } + static void reset_mqtt(); static bool is_nested() { @@ -178,7 +186,7 @@ class Mqtt { } static bool publish_single() { - return publish_single_; + return mqtt_enabled_ && publish_single_; } static bool publish_single2cmd() { @@ -190,7 +198,7 @@ class Mqtt { } static bool ha_enabled() { - return ha_enabled_; + return mqtt_enabled_ && ha_enabled_; } static void ha_enabled(bool ha_enabled) {