publishing results

This commit is contained in:
2020-11-18 14:01:23 +01:00
parent 5714d3f6ed
commit 5e4f9063e1
3 changed files with 34 additions and 12 deletions

View File

@ -13,7 +13,7 @@
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
extern const uint8_t MQTT_SOCK;
@ -133,10 +133,26 @@ void mqttCommInit() {
}
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) {
coloredMsg(LOG_GREEN, false, "mqp: %s -> %s successfully published", message, topic);
} else {
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");
}
}