164 lines
6.0 KiB
C
Raw Normal View History

2020-11-27 15:25:19 +01:00
#include <stdint.h>
2020-11-27 16:23:14 +01:00
#include <stdbool.h>
2020-11-27 13:27:24 +01:00
2020-11-27 15:25:19 +01:00
#include <config.h>
#include <eeprom.h>
2020-11-27 15:54:16 +01:00
#include <logger.h>
2020-11-30 18:48:48 +01:00
#include <mbusComm.h>
2020-11-27 13:27:24 +01:00
2020-11-30 19:01:53 +01:00
#define NUM_OF_DEFAULT_DEVICES 8
2020-11-30 18:29:53 +01:00
t_deviceBlock defaultDeviceBlock[] = {
{
.deviceMagic = DEVICE_MAGIC,
.deviceName = "Total",
.address = 80,
.consideredField = { 0, 17, -1, -1 },
.period = 10,
.filler = { 0, 0, 0 }
},
{
.deviceMagic = DEVICE_MAGIC,
.deviceName = "Computer",
.address = 85,
.consideredField = { 0, 4, 2, 3 },
.period = 10,
.filler = { 0, 0, 0 }
2020-11-30 18:52:56 +01:00
},
{
2020-11-30 19:07:26 +01:00
.deviceMagic = DEVICE_MAGIC,
2020-11-30 18:52:56 +01:00
.deviceName = "Dryer",
.address = 81,
.consideredField = { 0, 4, 2, 3 },
.period = 10,
.filler = { 0, 0, 0 }
},
{
.deviceMagic = DEVICE_MAGIC,
.deviceName = "Laundry",
.address = 82,
.consideredField = { 0, 4, 2, 3 },
.period = 10,
.filler = { 0, 0, 0 }
},
{
.deviceMagic = DEVICE_MAGIC,
.deviceName = "Dishwasher",
.address = 83,
.consideredField = { 0, 4, 2, 3 },
.period = 10,
.filler = { 0, 0, 0 }
},
{
.deviceMagic = DEVICE_MAGIC,
.deviceName = "Light",
.address = 84,
.consideredField = { 0, 4, 2, 3 },
.period = 10,
.filler = { 0, 0, 0 }
},
{
.deviceMagic = DEVICE_MAGIC,
.deviceName = "Freezer",
.address = 86,
.consideredField = { 0, 4, 2, 3 },
.period = 10,
.filler = { 0, 0, 0 }
},
{
.deviceMagic = DEVICE_MAGIC,
.deviceName = "Fridge",
.address = 87,
.consideredField = { 0, 4, 2, 3 },
.period = 10,
.filler = { 0, 0, 0 }
2020-11-30 18:29:53 +01:00
}
};
2020-11-27 13:27:24 +01:00
2020-11-30 18:57:19 +01:00
t_configBlock defaultConfigBlock = {
.configMagic = CONFIG_MAGIC,
.deviceName = "MBGW3",
.macAddress = { 0x00, 0xA0, 0x57, 0x05, 0x3E, 0x0D },
.frontendThreshold = 240,
.brokerName = "mqttbroker",
.watchdogTopic = "IoT/Watchdog",
.startupTopic = "IoT/MBGW3/Startup",
.statusTopic = "IoT/MBGW3/Status",
.mbusDataTopic = "IoT/MBGW3/Measurement",
.syslogServerName = "syslogserver",
2020-11-30 19:01:53 +01:00
.numOfDeviceBlocks = NUM_OF_DEFAULT_DEVICES,
2020-11-30 18:57:19 +01:00
.filler = { 0 }
};
2020-11-27 15:25:19 +01:00
t_configBlock mainConfigBlock;
2020-11-27 13:27:24 +01:00
t_configBlock* getConfig() {
2020-11-27 16:36:46 +01:00
return &mainConfigBlock;
2020-11-27 15:25:19 +01:00
}
void configInit() {
2020-11-27 16:33:03 +01:00
coloredMsg(LOG_BLUE, false, "cfg ci Reading configuration block from eeprom");
2020-11-30 18:29:53 +01:00
eepromReadConfigBlock(&mainConfigBlock);
2020-11-27 15:58:17 +01:00
2020-11-27 16:33:03 +01:00
if (mainConfigBlock.configMagic != CONFIG_MAGIC) {
coloredMsg(LOG_BLUE, false, "cfg ci Invalid configuration block read from eeprom");
2020-11-27 16:16:40 +01:00
2020-11-30 18:29:53 +01:00
eepromWriteConfigBlock(&defaultConfigBlock);
2020-11-27 16:33:03 +01:00
coloredMsg(LOG_BLUE, false, "cfg ci Default configuration block written to eeprom");
2020-11-27 16:23:14 +01:00
2020-11-30 19:01:53 +01:00
for (uint8_t i = 0; i < NUM_OF_DEFAULT_DEVICES; i++) {
2020-11-30 18:55:40 +01:00
eepromWriteDeviceBlock(i, &defaultDeviceBlock[i]);
}
2020-11-30 18:29:53 +01:00
coloredMsg(LOG_BLUE, false, "cfg ci Default device blocks written to eeprom");
2020-11-27 16:33:03 +01:00
coloredMsg(LOG_BLUE, false, "cfg ci Reading configuration block from eeprom again");
2020-11-30 18:29:53 +01:00
eepromReadConfigBlock(&mainConfigBlock);
2020-11-27 16:33:03 +01:00
}
coloredMsg(LOG_BLUE, false, "cfg ci configMagic: %lx", mainConfigBlock.configMagic);
coloredMsg(LOG_BLUE, false, "cfg ci deviceName: %s", mainConfigBlock.deviceName);
coloredMsg(LOG_BLUE, false, "cfg ci MAC address: %02x:%02x:%02x:%02x:%02x:%02x", mainConfigBlock.macAddress[0],
mainConfigBlock.macAddress[1],
mainConfigBlock.macAddress[2],
mainConfigBlock.macAddress[3],
mainConfigBlock.macAddress[4],
mainConfigBlock.macAddress[5]);
coloredMsg(LOG_BLUE, false, "cfg ci frontend threshold: %ld", mainConfigBlock.frontendThreshold);
coloredMsg(LOG_BLUE, false, "cfg ci broker: %s", mainConfigBlock.brokerName);
coloredMsg(LOG_BLUE, false, "cfg ci watchdogTopic: %s", mainConfigBlock.watchdogTopic);
coloredMsg(LOG_BLUE, false, "cfg ci startupTopic: %s", mainConfigBlock.startupTopic);
coloredMsg(LOG_BLUE, false, "cfg ci statusTopic: %s", mainConfigBlock.statusTopic);
coloredMsg(LOG_BLUE, false, "cfg ci mbusDataTopic: %s", mainConfigBlock.mbusDataTopic);
coloredMsg(LOG_BLUE, false, "cfg ci syslog server: %s", mainConfigBlock.syslogServerName);
2020-11-27 16:55:09 +01:00
coloredMsg(LOG_BLUE, false, "cfg ci device block cnt: %d", mainConfigBlock.numOfDeviceBlocks);
2020-11-30 18:29:53 +01:00
for (uint8_t i = 0; i < mainConfigBlock.numOfDeviceBlocks; i++) {
t_deviceBlock tmpDeviceBlock;
eepromReadDeviceBlock(i, &tmpDeviceBlock);
2020-11-30 18:48:48 +01:00
if (tmpDeviceBlock.deviceMagic == DEVICE_MAGIC) {
coloredMsg(LOG_BLUE, false, "cfg ci device %d: ", i);
coloredMsg(LOG_BLUE, false, " Name: %s, Address: %d, Period: %d",
tmpDeviceBlock.deviceName, tmpDeviceBlock.address, tmpDeviceBlock.period);
coloredMsg(LOG_BLUE, false, " Considered Fields: %d %d %d %d",
tmpDeviceBlock.consideredField[0],
tmpDeviceBlock.consideredField[1],
tmpDeviceBlock.consideredField[2],
tmpDeviceBlock.consideredField[3]);
2020-12-17 16:11:11 +01:00
if (tmpDeviceBlock.period == 0) {
coloredMsg(LOG_BLUE, false, " device is marked as inactive");
2020-12-17 16:14:15 +01:00
} else if (tmpDeviceBlock.period == -1) {
coloredMsg(LOG_BLUE, false, " device is marked as deleted");
2020-12-17 16:11:11 +01:00
} else {
mbusCommAddDevice(&tmpDeviceBlock);
}
2020-11-30 18:48:48 +01:00
} else {
2020-11-30 19:06:37 +01:00
coloredMsg(LOG_BLUE, false, "magic of device %d does not match, ignored", i);
2020-11-30 18:48:48 +01:00
}
2020-11-30 18:29:53 +01:00
}
2020-11-27 13:27:24 +01:00
}