Browse Source

Bugfix für Mqtt unsubcribe/subscribe Fremdtopic

pull/1703/head
Patrick Amrhein 7 months ago
parent
commit
e9ae34a0eb
  1. 8
      src/app.cpp
  2. 4
      src/app.h
  3. 4
      src/appInterface.h
  4. 12
      src/publisher/pubMqtt.h
  5. 11
      src/web/RestApi.h

8
src/app.cpp

@ -831,10 +831,10 @@ void app::updateLed(void) {
void app::subscribe(const char *subTopic, uint8_t qos) { void app::subscribeExtern(const char *subTopic, uint8_t qos) {
mMqtt.subscribe(subTopic, qos); mMqtt.subscribeExtern(subTopic, qos);
} }
void app::unsubscribe(const char *subTopic) { void app::unsubscribeExtern(const char *subTopic) {
mMqtt.unsubscribe(subTopic); mMqtt.unsubscribeExtern(subTopic);
} }

4
src/app.h

@ -360,8 +360,8 @@ class app : public IApp, public ah::Scheduler {
} }
#endif #endif
void subscribe(const char *subTopic, uint8_t qos = QOS_0); void subscribeExtern(const char *subTopic, uint8_t qos = QOS_0);
void unsubscribe(const char *subTopic); void unsubscribeExtern(const char *subTopic);
private: private:
#define CHECK_AVAIL true #define CHECK_AVAIL true

4
src/appInterface.h

@ -74,8 +74,8 @@ class IApp {
#endif #endif
virtual void* getRadioObj(bool nrf) = 0; virtual void* getRadioObj(bool nrf) = 0;
virtual void subscribe(const char *subTopic, uint8_t qos) = 0; virtual void subscribeExtern(const char *subTopic, uint8_t qos) = 0;
virtual void unsubscribe(const char *subTopic) = 0; virtual void unsubscribeExtern(const char *subTopic) = 0;
}; };
#endif /*__IAPP_H__*/ #endif /*__IAPP_H__*/

12
src/publisher/pubMqtt.h

@ -251,18 +251,18 @@ class PubMqtt {
mClient.subscribe(topic, qos); 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) { void subscribeExtern(const char *subTopic, uint8_t qos = QOS_0) {
char topic[MQTT_TOPIC_LEN + 20]; char topic[MQTT_TOPIC_LEN + 20];
snprintf(topic, (MQTT_TOPIC_LEN + 20), "%s", subTopic); snprintf(topic, (MQTT_TOPIC_LEN + 20), "%s", subTopic);
mClient.subscribe(topic, qos); 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) { void setConnectionCb(connectionCb cb) {
mConnectionCb = cb; mConnectionCb = cb;
} }

11
src/web/RestApi.h

@ -1207,19 +1207,18 @@ class RestApi {
// pm_src // pm_src
const char *neu = jsonIn[F("pm_src")].as<const char*>(); const char *neu = jsonIn[F("pm_src")].as<const char*>();
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 // unsubscribe
if(mConfig->plugin.zeroExport.groups[group].pm_type == zeroExportPowermeterType_t::Mqtt) 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 // save
snprintf(mConfig->plugin.zeroExport.groups[group].pm_src, ZEROEXPORT_GROUP_MAX_LEN_PM_SRC, "%s", jsonIn[F("pm_src")].as<const char*>()); snprintf(mConfig->plugin.zeroExport.groups[group].pm_src, ZEROEXPORT_GROUP_MAX_LEN_PM_SRC, "%s", jsonIn[F("pm_src")].as<const char*>());
// subsrcribe // subsrcribe
if(mConfig->plugin.zeroExport.groups[group].pm_type == zeroExportPowermeterType_t::Mqtt) 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 || if(mConfig->plugin.zeroExport.groups[group].pm_type == zeroExportBatteryCfg::mqttSoC ||
mConfig->plugin.zeroExport.groups[group].pm_type == zeroExportBatteryCfg::mqttU ) 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 // save
@ -1261,7 +1260,7 @@ class RestApi {
if(mConfig->plugin.zeroExport.groups[group].pm_type == zeroExportBatteryCfg::mqttSoC || if(mConfig->plugin.zeroExport.groups[group].pm_type == zeroExportBatteryCfg::mqttSoC ||
mConfig->plugin.zeroExport.groups[group].pm_type == zeroExportBatteryCfg::mqttU) 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);
} }
} }

Loading…
Cancel
Save