[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

+40 -30
View File
@@ -45,40 +45,47 @@ void pilightCallback(const String& protocol, const String& message, int status,
size_t repeats, const String& deviceID) {
if (status == VALID) {
Log.trace(F("Creating RF PiLight buffer" CR));
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
JsonObject RFPiLightdata = jsonBuffer.to<JsonObject>();
StaticJsonDocument<JSON_MSG_BUFFER> RFPiLightdataBuffer;
JsonObject RFPiLightdata = RFPiLightdataBuffer.to<JsonObject>();
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer2;
JsonObject msg = jsonBuffer2.to<JsonObject>();
auto error = deserializeJson(jsonBuffer2, message);
if (error) {
Log.error(F("deserializeJson() failed: %s" CR), error.c_str());
return;
if (message.length() > 0) {
auto error = deserializeJson(jsonBuffer2, message);
if (error) {
Log.error(F("deserializeJson() failed: %s" CR), error.c_str());
return;
}
RFPiLightdata["message"] = msg;
}
RFPiLightdata["message"] = msg;
RFPiLightdata["protocol"] = (const char*)protocol.c_str();
RFPiLightdata["length"] = (const char*)deviceID.c_str();
if (protocol.length() > 0) {
RFPiLightdata["protocol"] = protocol;
}
if (deviceID.length() > 0) {
RFPiLightdata["value"] = deviceID;
const char* device_id = deviceID.c_str();
if (!strlen(device_id) && !msg.isNull()) {
// deviceID returned from Pilight is only extracted from id field
// but some device may use another name as unique identifier
char* choices[] = {"key", "unit", "device_id", "systemcode", "unitcode", "programcode"};
const char* device_id = deviceID.c_str();
if (!strlen(device_id)) {
// deviceID returned from Pilight is only extracted from id field
// but some device may use another name as unique identifier
char* choices[] = {"key", "unit", "device_id", "systemcode", "unitcode", "programcode"};
for (uint8_t i = 0; i < 6; i++) {
if (msg[choices[i]]) {
device_id = (const char*)msg[choices[i]];
break;
for (uint8_t i = 0; i < 6; i++) {
if (msg[choices[i]]) {
device_id = (const char*)msg[choices[i]];
break;
}
}
}
RFPiLightdata["value"] = device_id;
}
RFPiLightdata["value"] = device_id;
RFPiLightdata["repeats"] = (int)repeats;
RFPiLightdata["status"] = (int)status;
pub(subjectPilighttoMQTT, RFPiLightdata);
RFPiLightdata["origin"] = subjectPilighttoMQTT;
enqueueJsonObject(RFPiLightdata);
if (repeatPilightwMQTT) {
Log.trace(F("Pub Pilight for rpt" CR));
pub(subjectMQTTtoPilight, RFPiLightdata);
RFPiLightdata["origin"] = subjectMQTTtoPilight;
enqueueJsonObject(RFPiLightdata);
}
}
}
@@ -92,16 +99,16 @@ void pilightRawCallback(const uint16_t* pulses, size_t length) {
return;
}
Log.trace(F("Creating RF PiLight buffer" CR));
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
JsonObject RFPiLightdata = jsonBuffer.to<JsonObject>();
StaticJsonDocument<JSON_MSG_BUFFER> RFPiLightdataBuffer;
JsonObject RFPiLightdata = RFPiLightdataBuffer.to<JsonObject>();
RFPiLightdata["format"] = "RAW";
RFPiLightdata["rawlen"] = length;
RFPiLightdata["pulsesString"] = rf.pulseTrainToString(pulses, length); // c=pulse_array_key;p=pulse_types
// publish data
pub(subjectPilighttoMQTT, RFPiLightdata);
// Enqueue data
RFPiLightdata["origin"] = subjectPilighttoMQTT;
enqueueJsonObject(RFPiLightdata);
}
# endif
@@ -196,7 +203,8 @@ void MQTTtoPilight(char* topicOri, JsonObject& Pilightdata) {
# endif
if (success) {
pub(subjectGTWPilighttoMQTT, Pilightdata); // 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(subjectGTWPilighttoMQTT, Pilightdata);
} else {
pub(subjectGTWPilighttoMQTT, "{\"Status\": \"Error\"}"); // Fail feedback
Log.error(F("MQTTtoPilightProtocol Fail json" CR));
@@ -264,7 +272,8 @@ void MQTTtoPilight(char* topicOri, JsonObject& Pilightdata) {
int msgLength = rf.send(protocol, message);
if (msgLength > 0) {
Log.trace(F("Adv data MQTTtoPilight push state via PilighttoMQTT" CR));
pub(subjectGTWPilighttoMQTT, Pilightdata);
Pilightdata["origin"] = subjectGTWPilighttoMQTT;
enqueueJsonObject(Pilightdata);
success = true;
} else {
switch (msgLength) {
@@ -298,7 +307,8 @@ void MQTTtoPilight(char* topicOri, JsonObject& Pilightdata) {
}
# endif
if (success) {
pub(subjectGTWPilighttoMQTT, Pilightdata); // 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(subjectGTWPilighttoMQTT, Pilightdata);
} else {
pub(subjectGTWPilighttoMQTT, "{\"Status\": \"Error\"}"); // Fail feedback
Log.error(F("MQTTtoPilight Fail json" CR));