configuration

This commit is contained in:
2020-11-27 13:27:24 +01:00
parent 3c25a7d9e1
commit 0b1c6217c4
6 changed files with 68 additions and 16 deletions

View File

@ -9,6 +9,7 @@
#include <mbusComm.h>
#include <oled.h>
#include <eeprom.h>
#include <config.h>
#include <stdint.h>
#include <stdlib.h>
@ -19,16 +20,19 @@
extern const uint8_t MQTT_SOCK;
static t_configBlock *config;
client_t client;
mqttClient_t mqttClient;
uint8_t brokerAddress[] = { 172, 16, 2, 16 };
uint8_t brokerAddress[4];
uint16_t brokerPort = 1883;
/*
const static char WatchdogTopic[] = "IoT/Watchdog";
const static char StartupTopic[] = "IoT/MBGW3/Startup";
const static char StatusTopic[] = "IoT/MBGW3/Status";
*/
static uint32_t watchdogCounter = 0;
@ -44,7 +48,7 @@ void watchdogHandler(void *handle) {
// typedef void (*callback_t)(char*, uint8_t*, uint16_t);
static void mqttCallback(char *topic, uint8_t *payload, uint16_t payloadLength) {
if (0 == strcmp(topic, WatchdogTopic)) {
if (0 == strcmp(topic, config->watchdogTopic)) {
watchdogCounter++;
} else {
coloredMsg(LOG_GREEN, false, "mqcb: %s : %.*s", topic, payloadLength, payload);
@ -61,7 +65,7 @@ static void mqttStatusPublisher(void *handle) {
snprintf(buf, sizeof(buf), "{\"uptime\":\"%ld\", \"runningHours\":\"%ld\", \"powerCycles\":\"%ld\", \"tasks\":\"%d\", \"requests\":\"%ld\", \"errors\":\"%ld\", \"octets\":\"%ld\", \"overrun\":\"%ld\", \"framing\":\"%ld\", \"parity\":\"%ld\", \"noise\":\"%ld\"}",
uptime, globalDeviceStats->totalRunningHours, globalDeviceStats->totalPowercycles, schTaskCnt(), mbusCommStats->mbusRequestCnt, mbusCommStats->mbusErrorCnt,
mbusCommStats->uartOctetCnt, mbusCommStats->uartOverrunCnt, mbusCommStats->uartFramingErrCnt, mbusCommStats->uartParityErrCnt, mbusCommStats->uartNoiseErrCnt);
bool res = publish(&mqttClient, StatusTopic, (const uint8_t*)buf, strlen(buf), false);
bool res = publish(&mqttClient, config->statusTopic, (const uint8_t*)buf, strlen(buf), false);
coloredMsg(LOG_GREEN, false, "mqch, publish returned %d", res);
oledSetActiveScreen(OLED_SCREEN1);
@ -93,7 +97,7 @@ void mqttCommHandler(void *handle) {
case 1:
coloredMsg(LOG_GREEN, false, "mqch, resolving broker name");
if (! wizDnsQuery("mqttbroker", brokerAddress)) {
if (! wizDnsQuery(config->brokerName, brokerAddress)) {
coloredMsg(LOG_GREEN, false, "mqch, query for broker address failed, going to error state");
state = 255;
}
@ -111,7 +115,7 @@ void mqttCommHandler(void *handle) {
case 2:
coloredMsg(LOG_GREEN, false, "mqch, publish start-up");
res = publish(&mqttClient, StartupTopic, (const uint8_t*)message, strlen((char*)message), false);
res = publish(&mqttClient, config->startupTopic, (const uint8_t*)message, strlen((char*)message), false);
coloredMsg(LOG_GREEN, false, "mqch, publish returned %d", res);
schAdd(mqttStatusPublisher, NULL, 0, 60000);
coloredMsg(LOG_GREEN, false, "mqch, status publisher scheduled");
@ -120,7 +124,7 @@ void mqttCommHandler(void *handle) {
case 3:
coloredMsg(LOG_GREEN, false, "mqch, subscribe watchdog");
res = subscribe(&mqttClient, WatchdogTopic, MQTTQOS0);
res = subscribe(&mqttClient, config->watchdogTopic, MQTTQOS0);
coloredMsg(LOG_GREEN, false, "mqch, subscribe returned %d", res);
schAdd(watchdogHandler, NULL, 60000, 60000);
coloredMsg(LOG_GREEN, false, "mqch, watchdogHandler scheduled");
@ -150,6 +154,7 @@ void mqttCommHandler(void *handle) {
}
void mqttCommInit() {
config = getConfig();
schAdd(mqttCommHandler, NULL, 0, 100);
}