publishing results

This commit is contained in:
Wolfgang Hottgenroth 2020-11-18 14:01:23 +01:00
parent 5714d3f6ed
commit 5e4f9063e1
Signed by: wn
GPG Key ID: 6C1E5E531E0D5D7F
3 changed files with 34 additions and 12 deletions

View File

@ -3,6 +3,9 @@
void mqttCommInit(); void mqttCommInit();
void mqttPublish(char *topic, char *message);
void mqttPublishf(char *topic, char *messageFormat, ...);
#endif // _MQTTCOMM_H_ #endif // _MQTTCOMM_H_

View File

@ -14,11 +14,12 @@
#include <frontend.h> #include <frontend.h>
#include <wizHelper.h> #include <wizHelper.h>
#include <mbusParserExt.h> #include <mbusParserExt.h>
#include <mqttComm.h>
#include <mbus/mbus-protocol.h> #include <mbus/mbus-protocol.h>
static const char MBUS_TOPIC[] = "IoT/MBGW3/Measurement";
static const uint8_t MBUS_QUERY_CMD = 0x5b; static const uint8_t MBUS_QUERY_CMD = 0x5b;
@ -201,6 +202,8 @@ static void parseAndPrintFrame(t_mbusCommHandle *localMbusCommHandle) {
coloredMsg(LOG_YELLOW, true, "mbc papf [%d] Error ratio is %.2f", coloredMsg(LOG_YELLOW, true, "mbc papf [%d] Error ratio is %.2f",
localMbusCommHandle->requestId, localMbusCommHandle->requestId,
errorRatio); errorRatio);
mqttPublishf(MBUS_TOPIC, "{\"Status\":\"Error\", \"RequestId\":\"%d\", \"Device\":\"%s\", \"ErrorRatio\":\"%.2f\"}",
localMbusCommHandle->requestId, localMbusCommHandle->device->deviceName, errorRatio);
} else { } else {
coloredMsg(LOG_RED, true, "mbc papf [%d] err: unable to parse frame", localMbusCommHandle->requestId); coloredMsg(LOG_RED, true, "mbc papf [%d] err: unable to parse frame", localMbusCommHandle->requestId);
} }

View File

@ -13,7 +13,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdarg.h>
extern const uint8_t MQTT_SOCK; extern const uint8_t MQTT_SOCK;
@ -133,10 +133,26 @@ void mqttCommInit() {
} }
void mqttPublish(char *topic, char *message) { void mqttPublish(char *topic, char *message) {
bool res = publish(&mqttClient, topic, message, strlen(message), false); bool res = false;
if (isNetworkAvailable()) {
res = publish(&mqttClient, topic, message, strlen(message), false);
}
if (res) { if (res) {
coloredMsg(LOG_GREEN, false, "mqp: %s -> %s successfully published", message, topic); coloredMsg(LOG_GREEN, false, "mqp: %s -> %s successfully published", message, topic);
} else { } else {
coloredMsg(LOG_RED, true, "mqp: %s -> %s failed to publish", message, topic); coloredMsg(LOG_RED, true, "mqp: %s -> %s failed to publish", message, topic);
} }
} }
void mqttPublishf(char *topic, char *messageFormat, ...) {
va_list vl;
va_start(vl, messageFormat);
char buf[2048];
int res = vsnprintf(buf, sizeof(buf), messageFormat, vl);
va_end(vl);
if (res < sizeof(buf)) {
mqttPublish(topic, buf);
} else {
coloredMsg(LOG_RED, true, "mqc mpf buffer overflow, truncated message not published");
}
}