From 3995238fd559fa1507e3a5a2a3cdfed2bd246173 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Mon, 25 Nov 2024 14:25:11 +0100 Subject: [PATCH] EVerest: Fix shutdown crash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Simon Stürz --- everest/everest.cpp | 15 +++++++++++---- everest/everest.h | 2 ++ everest/everestclient.cpp | 10 +++++++++- everest/everestclient.h | 1 + 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/everest/everest.cpp b/everest/everest.cpp index 10705282..e4676cc1 100644 --- a/everest/everest.cpp +++ b/everest/everest.cpp @@ -77,6 +77,11 @@ Everest::~Everest() deinitialize(); } +Thing *Everest::thing() const +{ + return m_thing; +} + QString Everest::connector() const { return m_connector; @@ -192,11 +197,13 @@ void Everest::onPublishReceived(const QString &topic, const QByteArray &payload, */ QVariantMap dataMap = jsonDoc.toVariant().toMap(); + uint maxCurrent = dataMap.value("max_current_A_import").toUInt(); + uint minCurrent = dataMap.value("min_current_A_import").toUInt(); - m_thing->setStateMaxValue(everestMaxChargingCurrentStateTypeId, - dataMap.value("max_current_A_import").toUInt()); - m_thing->setStateMinValue(everestMaxChargingCurrentStateTypeId, - dataMap.value("min_current_A_import").toUInt()); + m_thing->setStateMaxValue(everestMaxChargingCurrentStateTypeId, maxCurrent); + m_thing->setStateMinValue(everestMaxChargingCurrentStateTypeId, minCurrent == 0 ? 6 : minCurrent); + + // FIXME: once we have a method for phase switching, we can re-enable the featre here // bool phaseSwitchingAvailable = dataMap.value("supports_changing_phases_during_charging", false).toBool(); // if (!phaseSwitchingAvailable) { diff --git a/everest/everest.h b/everest/everest.h index 2058aa82..4dc3101a 100644 --- a/everest/everest.h +++ b/everest/everest.h @@ -61,6 +61,8 @@ public: explicit Everest(MqttClient *client, Thing *thing, QObject *parent = nullptr); ~Everest(); + Thing *thing() const; + QString connector() const; void initialize(); diff --git a/everest/everestclient.cpp b/everest/everestclient.cpp index b7bc8b65..fb1999aa 100644 --- a/everest/everestclient.cpp +++ b/everest/everestclient.cpp @@ -36,6 +36,7 @@ EverestClient::EverestClient(QObject *parent) { m_client = new MqttClient("nymea-" + QUuid::createUuid().toString().left(8), 300, QString(), QByteArray(), Mqtt::QoS0, false, this); + connect(m_client, &MqttClient::disconnected, this, [this](){ qCDebug(dcEverest()) << "The MQTT client is now disconnected" << this; if (!m_address.isNull()) { @@ -71,6 +72,13 @@ EverestClient::EverestClient(QObject *parent) }); } +EverestClient::~EverestClient() +{ + foreach (Everest *everest, m_everests) { + removeThing(everest->thing()); + } +} + MqttClient *EverestClient::client() const { return m_client; @@ -86,7 +94,7 @@ void EverestClient::addThing(Thing *thing) if (m_everests.contains(thing)) { qCWarning(dcEverest()) << "The" << thing << "has already been added to the everest client. " "Please report a bug if you see this message."; - // FIXME: maybe cleanup and recreate the client due to reconfigure + // TODO: maybe cleanup and recreate the client due to reconfigure return; } diff --git a/everest/everestclient.h b/everest/everestclient.h index 4d650bd5..a74c5266 100644 --- a/everest/everestclient.h +++ b/everest/everestclient.h @@ -49,6 +49,7 @@ class EverestClient : public QObject Q_OBJECT public: explicit EverestClient(QObject *parent = nullptr); + ~EverestClient(); MqttClient *client() const;