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);
} }
@ -501,7 +504,7 @@ static e_mbusCommRequestResult mbusCommRequest(t_mbusDevice *mbusDevice) {
static uint8_t numOfDevices = 8; static uint8_t numOfDevices = 8;
static t_mbusDevice devices[] = { static t_mbusDevice devices[] = {
{ {
.deviceName = "Total Power", .deviceName = "TotalPower",
.address = 80, .address = 80,
.consideredField = { 0, 17, -1, -1 }, .consideredField = { 0, 17, -1, -1 },
.requests = 0, .requests = 0,
@ -511,7 +514,7 @@ static t_mbusDevice devices[] = {
.waiting = false .waiting = false
}, },
{ {
.deviceName = "Computer Power", .deviceName = "ComputerPower",
.address = 85, .address = 85,
.consideredField = { 0, 4, 2, 3 }, .consideredField = { 0, 4, 2, 3 },
.requests = 0, .requests = 0,
@ -521,7 +524,7 @@ static t_mbusDevice devices[] = {
.waiting = false .waiting = false
}, },
{ {
.deviceName = "Dryer Power", .deviceName = "DryerPower",
.address = 81, .address = 81,
.consideredField = { 0, 4, 2, 3 }, .consideredField = { 0, 4, 2, 3 },
.requests = 0, .requests = 0,
@ -531,7 +534,7 @@ static t_mbusDevice devices[] = {
.waiting = false .waiting = false
}, },
{ {
.deviceName = "Laundry Power", .deviceName = "LaundryPower",
.address = 82, .address = 82,
.consideredField = { 0, 4, 2, 3 }, .consideredField = { 0, 4, 2, 3 },
.requests = 0, .requests = 0,
@ -541,7 +544,7 @@ static t_mbusDevice devices[] = {
.waiting = false .waiting = false
}, },
{ {
.deviceName = "Dishwasher Power", .deviceName = "DishwasherPower",
.address = 83, .address = 83,
.consideredField = { 0, 4, 2, 3 }, .consideredField = { 0, 4, 2, 3 },
.requests = 0, .requests = 0,
@ -551,7 +554,7 @@ static t_mbusDevice devices[] = {
.waiting = false .waiting = false
}, },
{ {
.deviceName = "Light Power", .deviceName = "LightPower",
.address = 84, .address = 84,
.consideredField = { 0, 4, 2, 3 }, .consideredField = { 0, 4, 2, 3 },
.requests = 0, .requests = 0,
@ -561,7 +564,7 @@ static t_mbusDevice devices[] = {
.waiting = false .waiting = false
}, },
{ {
.deviceName = "Freezer Power", .deviceName = "FreezerPower",
.address = 86, .address = 86,
.consideredField = { 0, 4, 2, 3 }, .consideredField = { 0, 4, 2, 3 },
.requests = 0, .requests = 0,
@ -571,7 +574,7 @@ static t_mbusDevice devices[] = {
.waiting = false .waiting = false
}, },
{ {
.deviceName = "Fridge Power", .deviceName = "FridgePower",
.address = 87, .address = 87,
.consideredField = { 0, 4, 2, 3 }, .consideredField = { 0, 4, 2, 3 },
.requests = 0, .requests = 0,

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");
}
}