From 3c0c8eece32b45b553a4c08456d346f0d2078504 Mon Sep 17 00:00:00 2001 From: lumapu Date: Thu, 11 Jul 2024 22:20:11 +0200 Subject: [PATCH 1/2] 0.8.129 * sort alarms ascending #1471 * fix alarm counter for first alarm * prevent add inverter multiple times #1700 --- src/CHANGES.md | 7 ++++++- src/defines.h | 2 +- src/hm/hmInverter.h | 2 +- src/web/RestApi.h | 34 +++++++++++++++++++++++++++++----- 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/CHANGES.md b/src/CHANGES.md index dfd4ca2a..c60bd5c4 100644 --- a/src/CHANGES.md +++ b/src/CHANGES.md @@ -1,8 +1,13 @@ # Development Changes +## 0.8.129 - 2024-07-11 +* sort alarms ascending #1471 +* fix alarm counter for first alarm +* prevent add inverter multiple times #1700 + ## 0.8.128 - 2024-07-10 * add environments for 16MB flash size ESP32-S3 aka opendtufusion -* prevent duplicate alarms, update end time once it is received +* prevent duplicate alarms, update end time once it is received #1471 ## 0.8.127 - 2024-06-21 * add grid file #1677 diff --git a/src/defines.h b/src/defines.h index fae033d7..5914daa2 100644 --- a/src/defines.h +++ b/src/defines.h @@ -13,7 +13,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 8 -#define VERSION_PATCH 128 +#define VERSION_PATCH 129 //------------------------------------- typedef struct { uint8_t ch; diff --git a/src/hm/hmInverter.h b/src/hm/hmInverter.h index e1b99bc8..ff3ff9c5 100644 --- a/src/hm/hmInverter.h +++ b/src/hm/hmInverter.h @@ -834,7 +834,7 @@ class Inverter { } } - if(alarmCnt < 10 && alarmCnt < mAlarmNxtWrPos) + if(alarmCnt < 10 && alarmCnt <= mAlarmNxtWrPos) alarmCnt = mAlarmNxtWrPos + 1; lastAlarm[mAlarmNxtWrPos] = alarm_t(code, start, end); diff --git a/src/web/RestApi.h b/src/web/RestApi.h index d1f130d4..e02f186a 100644 --- a/src/web/RestApi.h +++ b/src/web/RestApi.h @@ -704,11 +704,23 @@ class RestApi { obj[F("last_id")] = iv->getChannelFieldValue(CH0, FLD_EVT, rec); JsonArray alarm = obj.createNestedArray(F("alarm")); + + // find oldest alarm + uint8_t offset = 0; + uint32_t oldestStart = 0xffffffff; + for(uint8_t i = 0; i < 10; i++) { + if((iv->lastAlarm[i].start != 0) && (iv->lastAlarm[i].start < oldestStart)) { + offset = i; + oldestStart = iv->lastAlarm[i].start; + } + } + for(uint8_t i = 0; i < 10; i++) { - alarm[i][F("code")] = iv->lastAlarm[i].code; - alarm[i][F("str")] = iv->getAlarmStr(iv->lastAlarm[i].code); - alarm[i][F("start")] = iv->lastAlarm[i].start; - alarm[i][F("end")] = iv->lastAlarm[i].end; + uint8_t pos = (i + offset) % 10; + alarm[pos][F("code")] = iv->lastAlarm[pos].code; + alarm[pos][F("str")] = iv->getAlarmStr(iv->lastAlarm[pos].code); + alarm[pos][F("start")] = iv->lastAlarm[pos].start; + alarm[pos][F("end")] = iv->lastAlarm[pos].end; } } @@ -1105,7 +1117,19 @@ class RestApi { } #endif /* !defined(ETHERNET */ else if(F("save_iv") == jsonIn[F("cmd")]) { - Inverter<> *iv = mSys->getInverterByPos(jsonIn[F("id")], false); + Inverter<> *iv; + + for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i++) { + iv = mSys->getInverterByPos(jsonIn[F("id")], true); + if(nullptr != iv) { + if((i != jsonIn[F("id")]) && (iv->config->serial.u64 == jsonIn[F("ser")])) { + jsonOut[F("error")] = F("ERR_DUPLICATE_INVERTER"); + return false; + } + } + } + + iv = mSys->getInverterByPos(jsonIn[F("id")], false); iv->config->enabled = jsonIn[F("en")]; iv->config->serial.u64 = jsonIn[F("ser")]; snprintf(iv->config->name, MAX_NAME_LENGTH, "%s", jsonIn[F("name")].as()); From e9ae34a0ebbda0716871411bb0b5ac06fddcfdc6 Mon Sep 17 00:00:00 2001 From: Patrick Amrhein Date: Fri, 12 Jul 2024 20:34:50 +0200 Subject: [PATCH 2/2] =?UTF-8?q?Bugfix=20f=C3=BCr=20Mqtt=20unsubcribe/subsc?= =?UTF-8?q?ribe=20Fremdtopic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.cpp | 8 ++++---- src/app.h | 4 ++-- src/appInterface.h | 4 ++-- src/publisher/pubMqtt.h | 12 ++++++------ src/web/RestApi.h | 11 +++++------ 5 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/app.cpp b/src/app.cpp index 5ebae6da..82f5c0b4 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -831,10 +831,10 @@ void app::updateLed(void) { -void app::subscribe(const char *subTopic, uint8_t qos) { - mMqtt.subscribe(subTopic, qos); +void app::subscribeExtern(const char *subTopic, uint8_t qos) { + mMqtt.subscribeExtern(subTopic, qos); } -void app::unsubscribe(const char *subTopic) { - mMqtt.unsubscribe(subTopic); +void app::unsubscribeExtern(const char *subTopic) { + mMqtt.unsubscribeExtern(subTopic); } \ No newline at end of file diff --git a/src/app.h b/src/app.h index 18d656c3..3eb0dd5c 100644 --- a/src/app.h +++ b/src/app.h @@ -360,8 +360,8 @@ class app : public IApp, public ah::Scheduler { } #endif - void subscribe(const char *subTopic, uint8_t qos = QOS_0); - void unsubscribe(const char *subTopic); + void subscribeExtern(const char *subTopic, uint8_t qos = QOS_0); + void unsubscribeExtern(const char *subTopic); private: #define CHECK_AVAIL true diff --git a/src/appInterface.h b/src/appInterface.h index e81580cf..ef728a73 100644 --- a/src/appInterface.h +++ b/src/appInterface.h @@ -74,8 +74,8 @@ class IApp { #endif virtual void* getRadioObj(bool nrf) = 0; - virtual void subscribe(const char *subTopic, uint8_t qos) = 0; - virtual void unsubscribe(const char *subTopic) = 0; + virtual void subscribeExtern(const char *subTopic, uint8_t qos) = 0; + virtual void unsubscribeExtern(const char *subTopic) = 0; }; #endif /*__IAPP_H__*/ diff --git a/src/publisher/pubMqtt.h b/src/publisher/pubMqtt.h index 0ba32b9a..480834f6 100644 --- a/src/publisher/pubMqtt.h +++ b/src/publisher/pubMqtt.h @@ -251,18 +251,18 @@ class PubMqtt { mClient.subscribe(topic, qos); } - // new - need to unsubscribe the topics. - void unsubscribe(const char *subTopic) - { - mClient.unsubscribe(subTopic); // add as many topics as you like - } - void subscribeExtern(const char *subTopic, uint8_t qos = QOS_0) { char topic[MQTT_TOPIC_LEN + 20]; snprintf(topic, (MQTT_TOPIC_LEN + 20), "%s", subTopic); mClient.subscribe(topic, qos); } + // new - need to unsubscribe the topics. + void unsubscribeExtern(const char *subTopic) + { + mClient.unsubscribe(subTopic); // add as many topics as you like + } + void setConnectionCb(connectionCb cb) { mConnectionCb = cb; } diff --git a/src/web/RestApi.h b/src/web/RestApi.h index 71a8a304..5fabbd4a 100644 --- a/src/web/RestApi.h +++ b/src/web/RestApi.h @@ -1207,19 +1207,18 @@ class RestApi { // pm_src const char *neu = jsonIn[F("pm_src")].as(); - if (strncmp(mConfig->plugin.zeroExport.groups[group].pm_src, neu, strlen(neu)) != 0) { + if (strcmp(mConfig->plugin.zeroExport.groups[group].pm_src, neu) != 0) { // unsubscribe if(mConfig->plugin.zeroExport.groups[group].pm_type == zeroExportPowermeterType_t::Mqtt) { - mApp->unsubscribe(mConfig->plugin.zeroExport.groups[group].pm_src); - + mApp->unsubscribeExtern(mConfig->plugin.zeroExport.groups[group].pm_src); } // save snprintf(mConfig->plugin.zeroExport.groups[group].pm_src, ZEROEXPORT_GROUP_MAX_LEN_PM_SRC, "%s", jsonIn[F("pm_src")].as()); // subsrcribe if(mConfig->plugin.zeroExport.groups[group].pm_type == zeroExportPowermeterType_t::Mqtt) { - mApp->subscribe(mConfig->plugin.zeroExport.groups[group].pm_src, QOS_2); + mApp->subscribeExtern(mConfig->plugin.zeroExport.groups[group].pm_src, QOS_2); } } @@ -1252,7 +1251,7 @@ class RestApi { if(mConfig->plugin.zeroExport.groups[group].pm_type == zeroExportBatteryCfg::mqttSoC || mConfig->plugin.zeroExport.groups[group].pm_type == zeroExportBatteryCfg::mqttU ) { - mApp->unsubscribe(mConfig->plugin.zeroExport.groups[group].battTopic); + mApp->unsubscribeExtern(mConfig->plugin.zeroExport.groups[group].battTopic); } // save @@ -1261,7 +1260,7 @@ class RestApi { if(mConfig->plugin.zeroExport.groups[group].pm_type == zeroExportBatteryCfg::mqttSoC || mConfig->plugin.zeroExport.groups[group].pm_type == zeroExportBatteryCfg::mqttU) { - mApp->subscribe(mConfig->plugin.zeroExport.groups[group].battTopic, QOS_2); + mApp->subscribeExtern(mConfig->plugin.zeroExport.groups[group].battTopic, QOS_2); } }