bugfix ExternalMqtt

This commit is contained in:
Mit4el
2023-10-02 23:16:29 +03:00
parent 34c6213100
commit 137a5a43b8
2 changed files with 18 additions and 6 deletions

View File

@@ -121,13 +121,16 @@ void mqttSubscribe() {
} }
void mqttSubscribeExternal(String topic, bool usePrefix) { void mqttSubscribeExternal(String topic, bool usePrefix) {
SerialPrint("i", F("MQTT"), ("subscribed external" + topic).c_str());
// SerialPrint("i", F("MQTT"), mqttRootDevice); // SerialPrint("i", F("MQTT"), mqttRootDevice);
String _sb_topic = topic;
if (usePrefix) if (usePrefix)
{ {
mqtt.subscribe((mqttPrefix + topic).c_str()); _sb_topic = mqttPrefix + "/" + topic;
} }
mqtt.subscribe(topic.c_str()); mqtt.subscribe(_sb_topic.c_str());
SerialPrint("i", F("MQTT"), ("subscribed external " + _sb_topic).c_str());
} }
void mqttCallback(char* topic, uint8_t* payload, size_t length) { void mqttCallback(char* topic, uint8_t* payload, size_t length) {

View File

@@ -17,6 +17,7 @@ private:
bool _isJson; bool _isJson;
bool _addPrefix; bool _addPrefix;
bool _debug; bool _debug;
bool sendOk = false;
public: public:
ExternalMQTT(String parameters) : IoTItem(parameters) ExternalMQTT(String parameters) : IoTItem(parameters)
@@ -26,10 +27,12 @@ public:
jsonRead(parameters, F("red"), red); jsonRead(parameters, F("red"), red);
jsonRead(parameters, F("offline"), offline); jsonRead(parameters, F("offline"), offline);
_topic = jsonReadStr(parameters, "topic"); _topic = jsonReadStr(parameters, "topic");
_isJson = jsonReadBool(parameters, "isJson"); jsonRead(parameters, "isJson", _isJson);
_addPrefix = jsonReadBool(parameters, "addPrefix"); jsonRead(parameters, "addPrefix", _addPrefix);
_debug = jsonReadBool(parameters, "debug"); jsonRead(parameters, "debug", _debug);
dataFromNode = false; dataFromNode = false;
if (mqttIsConnect())
sendOk = true;
mqttSubscribeExternal(_topic, _addPrefix); mqttSubscribeExternal(_topic, _addPrefix);
} }
char *TimeToString(unsigned long t) char *TimeToString(unsigned long t)
@@ -58,6 +61,7 @@ public:
{ {
return; return;
} }
if (_isJson) if (_isJson)
{ {
DynamicJsonDocument doc(JSON_BUFFER_SIZE); DynamicJsonDocument doc(JSON_BUFFER_SIZE);
@@ -106,6 +110,11 @@ public:
{ {
_minutesPassed++; _minutesPassed++;
setNewWidgetAttributes(); setNewWidgetAttributes();
if (mqttIsConnect() && !sendOk)
{
sendOk = true;
mqttSubscribeExternal(_topic, _addPrefix);
}
} }
void onMqttWsAppConnectEvent() void onMqttWsAppConnectEvent()
{ {