[SYS] Implement a central queue (#1767)

to avoid concurrency issues when modules or/and gateways want to publish data
This commit is contained in:
Florian authored and GitHub committed 2023-11-08 07:26:21 -06:00
1 parent ca9ef664ab
commit 029409c0c3
39 files changed
+491 -407

No files matched your search

+18 -11
View File
@@ -133,8 +133,8 @@ void setupRF() {
void RFtoMQTT() {
if (mySwitch.available()) {
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
JsonObject RFdata = jsonBuffer.to<JsonObject>();
StaticJsonDocument<JSON_MSG_BUFFER> RFdataBuffer;
JsonObject RFdata = RFdataBuffer.to<JsonObject>();
Log.trace(F("Rcv. RF" CR));
# ifdef ESP32
Log.trace(F("RF Task running on core :%d" CR), xPortGetCoreID());
@@ -150,13 +150,16 @@ void RFtoMQTT() {
RFdata["tre_state"] = bin2tristate(binary);
RFdata["binary"] = binary;
# if defined(ESP32) || defined(ESP8266)
unsigned int* raw = mySwitch.getReceivedRawdata();
String rawDump = "";
for (unsigned int i = 0; i <= length * 2; i++) {
rawDump = rawDump + String(raw[i]) + ",";
std::string rawDump;
for (unsigned int i = 0; i < length * 2; i++) {
if (i != 0)
rawDump += ",";
rawDump += std::to_string(raw[i]);
}
RFdata["raw"] = rawDump.c_str();
RFdata["raw"] = rawDump;
# endif
# ifdef ZradioCC1101 // set Receive off and Transmitt on
RFdata["mhz"] = receiveMhz;
# endif
@@ -167,13 +170,15 @@ void RFtoMQTT() {
if (SYSConfig.discovery)
RFtoMQTTdiscovery(MQTTvalue);
# endif
pub(subjectRFtoMQTT, RFdata);
RFdata["origin"] = subjectRFtoMQTT;
enqueueJsonObject(RFdata);
// Casting "receivedSignal[o].value" to (unsigned long) because ArduinoLog doesn't support uint64_t for ESP's
Log.trace(F("Store val: %u" CR), (unsigned long)MQTTvalue);
storeSignalValue(MQTTvalue);
if (repeatRFwMQTT) {
Log.trace(F("Pub RF for rpt" CR));
pub(subjectMQTTtoRF, RFdata);
RFdata["origin"] = subjectMQTTtoRF;
enqueueJsonObject(RFdata);
}
}
}
@@ -274,7 +279,8 @@ void MQTTtoRF(char* topicOri, JsonObject& RFdata) { // json object decoding
mySwitch.setProtocol(valuePRT, valuePLSL);
mySwitch.send(data, valueBITS);
Log.notice(F("MQTTtoRF OK" CR));
pub(subjectGTWRFtoMQTT, RFdata); // we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
pub(subjectGTWRFtoMQTT, RFdata);
mySwitch.setRepeatTransmit(RF_EMITTER_REPEAT); // Restore the default value
} else {
bool success = false;
@@ -292,7 +298,8 @@ void MQTTtoRF(char* topicOri, JsonObject& RFdata) { // json object decoding
}
# endif
if (success) {
pub(subjectGTWRFtoMQTT, RFdata); // we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
pub(subjectGTWRFtoMQTT, RFdata);
} else {
# ifndef ARDUINO_AVR_UNO // Space issues with the UNO
pub(subjectGTWRFtoMQTT, "{\"Status\": \"Error\"}"); // Fail feedback