Initial changes for v2.3

This commit is contained in:
Gunnar Skjold
2023-11-16 18:21:56 +01:00
parent 8cada69aaf
commit ae82914795
121 changed files with 6175 additions and 3080 deletions

View File

@@ -1,3 +1,9 @@
/**
* @copyright Utilitech AS 2023
* License: Fair Source
*
*/
#include "AmsConfiguration.h"
#include "hexutils.h"
@@ -13,12 +19,22 @@ bool AmsConfiguration::getSystemConfig(SystemConfig& config) {
config.vendorConfigured = false;
config.userConfigured = false;
config.dataCollectionConsent = 0;
config.energyspeedometer = 0;
strcpy(config.country, "");
return false;
}
}
bool AmsConfiguration::setSystemConfig(SystemConfig& config) {
SystemConfig existing;
if(getSystemConfig(existing)) {
sysChanged |= config.boardType != existing.boardType;
sysChanged |= config.vendorConfigured != existing.vendorConfigured;
sysChanged |= config.userConfigured != existing.userConfigured;
sysChanged |= config.dataCollectionConsent != existing.dataCollectionConsent;
sysChanged |= strcmp(config.country, existing.country) != 0;
sysChanged |= config.energyspeedometer != existing.energyspeedometer;
}
EEPROM.begin(EEPROM_SIZE);
stripNonAscii((uint8_t*) config.country, 2);
EEPROM.put(CONFIG_SYSTEM_START, config);
@@ -27,39 +43,48 @@ bool AmsConfiguration::setSystemConfig(SystemConfig& config) {
return ret;
}
bool AmsConfiguration::getWiFiConfig(WiFiConfig& config) {
bool AmsConfiguration::isSystemConfigChanged() {
return sysChanged;
}
void AmsConfiguration::ackSystemConfigChanged() {
sysChanged = false;
}
bool AmsConfiguration::getNetworkConfig(NetworkConfig& config) {
if(hasConfig()) {
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_WIFI_START, config);
EEPROM.get(CONFIG_NETWORK_START, config);
EEPROM.end();
if(config.sleep > 2) config.sleep = 1;
return true;
} else {
clearWifi(config);
clearNetworkConfig(config);
return false;
}
}
bool AmsConfiguration::setWiFiConfig(WiFiConfig& config) {
WiFiConfig existing;
bool AmsConfiguration::setNetworkConfig(NetworkConfig& config) {
NetworkConfig existing;
if(config.sleep > 2) config.sleep = 1;
if(getWiFiConfig(existing)) {
wifiChanged |= strcmp(config.ssid, existing.ssid) != 0;
wifiChanged |= strcmp(config.psk, existing.psk) != 0;
wifiChanged |= strcmp(config.ip, existing.ip) != 0;
if(getNetworkConfig(existing)) {
networkChanged |= strcmp(config.ssid, existing.ssid) != 0;
networkChanged |= strcmp(config.psk, existing.psk) != 0;
networkChanged |= strcmp(config.ip, existing.ip) != 0;
if(strlen(config.ip) > 0) {
wifiChanged |= strcmp(config.gateway, existing.gateway) != 0;
wifiChanged |= strcmp(config.subnet, existing.subnet) != 0;
wifiChanged |= strcmp(config.dns1, existing.dns1) != 0;
wifiChanged |= strcmp(config.dns2, existing.dns2) != 0;
networkChanged |= strcmp(config.gateway, existing.gateway) != 0;
networkChanged |= strcmp(config.subnet, existing.subnet) != 0;
networkChanged |= strcmp(config.dns1, existing.dns1) != 0;
networkChanged |= strcmp(config.dns2, existing.dns2) != 0;
}
wifiChanged |= strcmp(config.hostname, existing.hostname) != 0;
wifiChanged |= config.power != existing.power;
wifiChanged |= config.sleep != existing.sleep;
wifiChanged |= config.use11b != existing.use11b;
wifiChanged |= config.autoreboot != existing.autoreboot;
networkChanged |= strcmp(config.hostname, existing.hostname) != 0;
networkChanged |= config.power != existing.power;
networkChanged |= config.sleep != existing.sleep;
networkChanged |= config.use11b != existing.use11b;
networkChanged |= config.autoreboot != existing.autoreboot;
networkChanged |= config.mode != existing.mode;
} else {
wifiChanged = true;
networkChanged = true;
}
stripNonAscii((uint8_t*) config.ssid, 32, true);
@@ -72,20 +97,20 @@ bool AmsConfiguration::setWiFiConfig(WiFiConfig& config) {
stripNonAscii((uint8_t*) config.hostname, 32);
EEPROM.begin(EEPROM_SIZE);
EEPROM.put(CONFIG_WIFI_START, config);
EEPROM.put(CONFIG_NETWORK_START, config);
bool ret = EEPROM.commit();
EEPROM.end();
return ret;
}
void AmsConfiguration::clearWifi(WiFiConfig& config) {
void AmsConfiguration::clearNetworkConfig(NetworkConfig& config) {
strcpy(config.ssid, "");
strcpy(config.psk, "");
clearWifiIp(config);
clearNetworkConfigIp(config);
uint16_t chipId;
#if defined(ESP32)
chipId = ESP.getEfuseMac();
chipId = ( ESP.getEfuseMac() >> 32 ) % 0xFFFFFFFF;
config.power = 195;
#else
chipId = ESP.getChipId();
@@ -97,7 +122,7 @@ void AmsConfiguration::clearWifi(WiFiConfig& config) {
config.use11b = 1;
}
void AmsConfiguration::clearWifiIp(WiFiConfig& config) {
void AmsConfiguration::clearNetworkConfigIp(NetworkConfig& config) {
strcpy(config.ip, "");
strcpy(config.gateway, "");
strcpy(config.subnet, "");
@@ -105,12 +130,12 @@ void AmsConfiguration::clearWifiIp(WiFiConfig& config) {
strcpy(config.dns2, "");
}
bool AmsConfiguration::isWifiChanged() {
return wifiChanged;
bool AmsConfiguration::isNetworkConfigChanged() {
return networkChanged;
}
void AmsConfiguration::ackWifiChange() {
wifiChanged = false;
void AmsConfiguration::ackNetworkConfigChange() {
networkChanged = false;
}
bool AmsConfiguration::getMqttConfig(MqttConfig& config) {
@@ -234,6 +259,9 @@ bool AmsConfiguration::setMeterConfig(MeterConfig& config) {
meterChanged |= strcmp((char*) config.encryptionKey, (char*) existing.encryptionKey);
meterChanged |= strcmp((char*) config.authenticationKey, (char*) existing.authenticationKey);
meterChanged |= config.bufferSize != existing.bufferSize;
meterChanged |= config.rxPin != existing.rxPin;
meterChanged |= config.rxPinPullup != existing.rxPinPullup;
meterChanged |= config.txPin != existing.txPin;
} else {
meterChanged = true;
}
@@ -245,6 +273,9 @@ bool AmsConfiguration::setMeterConfig(MeterConfig& config) {
}
void AmsConfiguration::clearMeter(MeterConfig& config) {
config.rxPin = 0xFF;
config.txPin = 0xFF;
config.rxPinPullup = true;
config.baud = 0;
config.parity = 0;
config.invert = false;
@@ -386,7 +417,6 @@ bool AmsConfiguration::pinUsed(uint8_t pin, GpioConfig& config) {
if(pin == 0xFF)
return false;
return
pin == config.hanPin ||
pin == config.apPin ||
pin == config.ledPin ||
pin == config.ledPinRed ||
@@ -394,7 +424,8 @@ bool AmsConfiguration::pinUsed(uint8_t pin, GpioConfig& config) {
pin == config.ledPinBlue ||
pin == config.tempSensorPin ||
pin == config.tempAnalogSensorPin ||
pin == config.vccPin
pin == config.vccPin ||
pin == config.ledDisablePin
;
}
@@ -413,10 +444,6 @@ bool AmsConfiguration::getGpioConfig(GpioConfig& config) {
bool AmsConfiguration::setGpioConfig(GpioConfig& config) {
GpioConfig existing;
if(getGpioConfig(existing)) {
meterChanged |= config.hanPin != existing.hanPin;
meterChanged |= config.hanPinPullup != existing.hanPinPullup;
}
/* This currently does not work, as it checks its own pin
if(pinUsed(config.hanPin, config)) {
debugger->println(F("HAN pin already used"));
@@ -454,6 +481,10 @@ bool AmsConfiguration::setGpioConfig(GpioConfig& config) {
debugger->println(F("Vcc pin already used"));
return false;
}
if(pinUsed(config.ledDisablePin, config)) {
debugger->println(F("ledDisablePin already used"));
return false;
}
*/
if(config.apPin >= 0)
pinMode(config.apPin, INPUT_PULLUP);
@@ -466,8 +497,6 @@ bool AmsConfiguration::setGpioConfig(GpioConfig& config) {
}
void AmsConfiguration::clearGpio(GpioConfig& config) {
config.hanPin = 3;
config.hanPinPullup = true;
config.apPin = 0xFF;
config.ledPin = 0xFF;
config.ledInverted = true;
@@ -483,6 +512,8 @@ void AmsConfiguration::clearGpio(GpioConfig& config) {
config.vccBootLimit = 0;
config.vccResistorGnd = 0;
config.vccResistorVcc = 0;
config.ledDisablePin = 0xFF;
config.ledBehaviour = LED_BEHAVIOUR_DEFAULT;
}
bool AmsConfiguration::getNtpConfig(NtpConfig& config) {
@@ -502,7 +533,7 @@ bool AmsConfiguration::setNtpConfig(NtpConfig& config) {
if(getNtpConfig(existing)) {
if(config.enable != existing.enable) {
if(!existing.enable) {
wifiChanged = true;
networkChanged = true;
} else {
ntpChanged = true;
}
@@ -660,6 +691,7 @@ bool AmsConfiguration::getUiConfig(UiConfig& config) {
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_UI_START, config);
if(config.showImport > 2) clearUiConfig(config); // Must be wrong
if(config.showRealtimePlot > 2) config.showRealtimePlot = 1; // TODO: Move to new config version for v2.3
EEPROM.end();
return true;
} else {
@@ -677,7 +709,7 @@ bool AmsConfiguration::setUiConfig(UiConfig& config) {
}
void AmsConfiguration::clearUiConfig(UiConfig& config) {
// 1 = Always, 2 = If value present, 0 = Hidden
// 1 = Enable, 2 = Auto, 0 = Disable
config.showImport = 1;
config.showExport = 2;
config.showVoltage = 2;
@@ -689,6 +721,8 @@ void AmsConfiguration::clearUiConfig(UiConfig& config) {
config.showDayPlot = 1;
config.showMonthPlot = 1;
config.showTemperaturePlot = 2;
config.showRealtimePlot = 1;
config.darkMode = 2;
}
bool AmsConfiguration::setUpgradeInformation(int16_t exitCode, int16_t errorCode, const char* currentVersion, const char* nextVersion) {
@@ -730,6 +764,58 @@ void AmsConfiguration::clearUpgradeInformation(UpgradeInformation& upinfo) {
memset(upinfo.toVersion, 0, 8);
}
bool AmsConfiguration::getCloudConfig(CloudConfig& config) {
if(hasConfig()) {
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_CLOUD_START, config);
EEPROM.end();
return true;
} else {
clearCloudConfig(config);
return false;
}
}
bool AmsConfiguration::setCloudConfig(CloudConfig& config) {
CloudConfig existing;
if(getCloudConfig(existing)) {
cloudChanged |= config.enabled != existing.enabled;
cloudChanged |= config.interval!= existing.interval;
cloudChanged |= config.port!= existing.port;
cloudChanged |= strcmp(config.hostname, existing.hostname) != 0;
cloudChanged |= strcmp(config.clientId, existing.clientId) != 0;
cloudChanged |= strcmp(config.clientSecret, existing.clientSecret) != 0;
} else {
cloudChanged = true;
}
stripNonAscii((uint8_t*) config.hostname, 64);
stripNonAscii((uint8_t*) config.clientId, 17);
stripNonAscii((uint8_t*) config.clientSecret, 17);
EEPROM.begin(EEPROM_SIZE);
EEPROM.put(CONFIG_CLOUD_START, config);
bool ret = EEPROM.commit();
EEPROM.end();
return ret;
}
void AmsConfiguration::clearCloudConfig(CloudConfig& config) {
config.enabled = false;
strcpy(config.hostname, "cloud.amsleser.no");
config.port = 7443;
config.interval = 10;
strcpy(config.clientId, "");
strcpy(config.clientSecret, "");
}
bool AmsConfiguration::isCloudChanged() {
return cloudChanged;
}
void AmsConfiguration::ackCloudConfig() {
cloudChanged = false;
}
void AmsConfiguration::clear() {
EEPROM.begin(EEPROM_SIZE);
@@ -738,6 +824,7 @@ void AmsConfiguration::clear() {
EEPROM.get(CONFIG_SYSTEM_START, sys);
sys.userConfigured = false;
sys.dataCollectionConsent = 0;
sys.energyspeedometer = 0;
strcpy(sys.country, "");
EEPROM.put(CONFIG_SYSTEM_START, sys);
@@ -745,9 +832,9 @@ void AmsConfiguration::clear() {
clearMeter(meter);
EEPROM.put(CONFIG_METER_START, meter);
WiFiConfig wifi;
clearWifi(wifi);
EEPROM.put(CONFIG_WIFI_START, wifi);
NetworkConfig network;
clearNetworkConfig(network);
EEPROM.put(CONFIG_NETWORK_START, network);
MqttConfig mqtt;
clearMqtt(mqtt);
@@ -789,6 +876,10 @@ void AmsConfiguration::clear() {
clearUpgradeInformation(upinfo);
EEPROM.put(CONFIG_UPGRADE_INFO_START, upinfo);
CloudConfig cloud;
clearCloudConfig(cloud);
EEPROM.put(CONFIG_CLOUD_START, cloud);
EEPROM.put(EEPROM_CONFIG_ADDRESS, EEPROM_CLEARED_INDICATOR);
EEPROM.commit();
EEPROM.end();
@@ -808,46 +899,6 @@ bool AmsConfiguration::hasConfig() {
}
} else {
switch(configVersion) {
case 93:
configVersion = -1; // Prevent loop
if(relocateConfig93()) {
configVersion = 94;
} else {
configVersion = 0;
return false;
}
case 94:
configVersion = -1; // Prevent loop
if(relocateConfig94()) {
configVersion = 95;
} else {
configVersion = 0;
return false;
}
case 95:
configVersion = -1; // Prevent loop
if(relocateConfig95()) {
configVersion = 96;
} else {
configVersion = 0;
return false;
}
case 96:
configVersion = -1; // Prevent loop
if(relocateConfig96()) {
configVersion = 100;
} else {
configVersion = 0;
return false;
}
case 100:
configVersion = -1; // Prevent loop
if(relocateConfig100()) {
configVersion = 101;
} else {
configVersion = 0;
return false;
}
case 101:
configVersion = -1; // Prevent loop
if(relocateConfig101()) {
@@ -864,6 +915,14 @@ bool AmsConfiguration::hasConfig() {
configVersion = 0;
return false;
}
case 103:
configVersion = -1; // Prevent loop
if(relocateConfig103()) {
configVersion = 104;
} else {
configVersion = 0;
return false;
}
case EEPROM_CHECK_SUM:
return true;
default:
@@ -878,204 +937,18 @@ int AmsConfiguration::getConfigVersion() {
return configVersion;
}
void AmsConfiguration::loadTempSensors() {
EEPROM.begin(EEPROM_SIZE);
TempSensorConfig* tempSensors[32];
int address = EEPROM_TEMP_CONFIG_ADDRESS;
int c = 0;
int storedCount = EEPROM.read(address++);
if(storedCount > 0 && storedCount <= 32) {
for(int i = 0; i < storedCount; i++) {
TempSensorConfig* tsc = new TempSensorConfig();
EEPROM.get(address, *tsc);
if(tsc->address[0] != 0xFF) {
tempSensors[c++] = tsc;
}
address += sizeof(*tsc);
}
}
this->tempSensors = new TempSensorConfig*[c];
for(int i = 0; i < c; i++) {
this->tempSensors[i] = tempSensors[i];
}
tempSensorCount = c;
EEPROM.end();
}
void AmsConfiguration::saveTempSensors() {
int address = EEPROM_TEMP_CONFIG_ADDRESS;
EEPROM.put(address++, tempSensorCount);
for(int i = 0; i < tempSensorCount; i++) {
TempSensorConfig* tsc = tempSensors[i];
if(tsc->address[0] != 0xFF) {
EEPROM.put(address, *tsc);
address += sizeof(*tsc);
}
}
}
bool AmsConfiguration::relocateConfig93() {
MeterConfig95 meter;
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_METER_START_93, meter);
meter.wattageMultiplier = 0;
meter.voltageMultiplier = 0;
meter.amperageMultiplier = 0;
meter.accumulatedMultiplier = 0;
EEPROM.put(CONFIG_METER_START, meter);
EEPROM.put(EEPROM_CONFIG_ADDRESS, 94);
bool ret = EEPROM.commit();
EEPROM.end();
return ret;
}
bool AmsConfiguration::relocateConfig94() {
EnergyAccountingConfig eac;
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_ENERGYACCOUNTING_START, eac);
eac.hours = 1;
EEPROM.put(CONFIG_ENERGYACCOUNTING_START, eac);
EEPROM.put(EEPROM_CONFIG_ADDRESS, 95);
bool ret = EEPROM.commit();
EEPROM.end();
return ret;
}
bool AmsConfiguration::relocateConfig95() {
MeterConfig95 meter;
MeterConfig95 meter95;
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_METER_START, meter);
EEPROM.get(CONFIG_METER_START, meter95);
meter.wattageMultiplier = meter95.wattageMultiplier;
meter.voltageMultiplier = meter95.voltageMultiplier;
meter.amperageMultiplier = meter95.amperageMultiplier;
meter.accumulatedMultiplier = meter95.accumulatedMultiplier;
EEPROM.put(CONFIG_METER_START, meter);
EEPROM.put(EEPROM_CONFIG_ADDRESS, 96);
bool ret = EEPROM.commit();
EEPROM.end();
return ret;
}
bool AmsConfiguration::relocateConfig96() {
EEPROM.begin(EEPROM_SIZE);
SystemConfig sys;
EEPROM.get(CONFIG_SYSTEM_START, sys);
MeterConfig100 meter;
EEPROM.get(CONFIG_METER_START, meter);
meter.source = 1; // Serial
meter.parser = 0; // Auto
EEPROM.put(CONFIG_METER_START, meter);
#if defined(ESP8266)
GpioConfig gpio;
EEPROM.get(CONFIG_GPIO_START, gpio);
switch(sys.boardType) {
case 3: // Pow UART0 -- Now Pow-K UART0
case 4: // Pow GPIO12 -- Now Pow-U UART0
case 5: // Pow-K+ -- Now also Pow-K GPIO12
case 7: // Pow-U+ -- Now also Pow-U GPIO12
if(meter.baud == 2400 && meter.parity == 3) { // 3 == 8N1, assuming Pow-K
if(gpio.hanPin == 3) { // UART0
sys.boardType = 3;
} else if(gpio.hanPin == 12) {
sys.boardType = 5;
}
} else { // Assuming Pow-U
if(gpio.hanPin == 3) { // UART0
sys.boardType = 4;
} else if(gpio.hanPin == 12) {
sys.boardType = 7;
}
}
break;
}
#endif
sys.vendorConfigured = true;
sys.userConfigured = true;
sys.dataCollectionConsent = 0;
strcpy(sys.country, "");
EEPROM.put(CONFIG_SYSTEM_START, sys);
WiFiConfig wifi;
EEPROM.get(CONFIG_WIFI_START, wifi);
wifi.use11b = 1;
wifi.autoreboot = true;
EEPROM.put(CONFIG_WIFI_START, wifi);
NtpConfig ntp;
NtpConfig96 ntp96;
EEPROM.get(CONFIG_NTP_START, ntp96);
ntp.enable = ntp96.enable;
ntp.dhcp = ntp96.dhcp;
if(ntp96.offset == 360 && ntp96.summerOffset == 360) {
strcpy(ntp.timezone, "Europe/Oslo");
} else {
strcpy(ntp.timezone, "GMT");
}
strcpy(ntp.server, ntp96.server);
EEPROM.put(CONFIG_NTP_START, ntp);
EntsoeConfig entsoe;
EEPROM.get(CONFIG_ENTSOE_START, entsoe);
entsoe.enabled = strlen(entsoe.token) > 0;
EEPROM.put(CONFIG_ENTSOE_START, entsoe);
EEPROM.put(EEPROM_CONFIG_ADDRESS, 100);
bool ret = EEPROM.commit();
EEPROM.end();
return ret;
}
bool AmsConfiguration::relocateConfig100() {
EEPROM.begin(EEPROM_SIZE);
MeterConfig100 meter100;
EEPROM.get(CONFIG_METER_START, meter100);
MeterConfig meter;
meter.baud = meter100.baud;
meter.parity = meter100.parity;
meter.invert = meter100.invert;
meter.distributionSystem = meter100.distributionSystem;
meter.mainFuse = meter100.mainFuse;
meter.productionCapacity = meter100.productionCapacity;
memcpy(meter.encryptionKey, meter100.encryptionKey, 16);
memcpy(meter.authenticationKey, meter100.authenticationKey, 16);
meter.wattageMultiplier = meter100.wattageMultiplier;
meter.voltageMultiplier = meter100.voltageMultiplier;
meter.amperageMultiplier = meter100.amperageMultiplier;
meter.accumulatedMultiplier = meter100.accumulatedMultiplier;
meter.source = meter100.source;
meter.parser = meter100.parser;
EEPROM.put(CONFIG_METER_START, meter);
UiConfig ui;
clearUiConfig(ui);
EEPROM.put(CONFIG_UI_START, ui);
EEPROM.put(EEPROM_CONFIG_ADDRESS, 101);
bool ret = EEPROM.commit();
EEPROM.end();
return ret;
}
bool AmsConfiguration::relocateConfig101() {
EEPROM.begin(EEPROM_SIZE);
EnergyAccountingConfig config;
EnergyAccountingConfig101 config101;
EEPROM.get(CONFIG_ENERGYACCOUNTING_START, config101);
EEPROM.get(CONFIG_ENERGYACCOUNTING_START_103, config101);
for(uint8_t i = 0; i < 9; i++) {
config.thresholds[i] = config101.thresholds[i];
}
config.thresholds[9] = 0xFFFF;
config.hours = config101.hours;
EEPROM.put(CONFIG_ENERGYACCOUNTING_START, config);
EEPROM.put(CONFIG_ENERGYACCOUNTING_START_103, config);
EEPROM.put(EEPROM_CONFIG_ADDRESS, 102);
bool ret = EEPROM.commit();
@@ -1086,19 +959,19 @@ bool AmsConfiguration::relocateConfig101() {
bool AmsConfiguration::relocateConfig102() {
EEPROM.begin(EEPROM_SIZE);
GpioConfig gpioConfig;
EEPROM.get(CONFIG_GPIO_START, gpioConfig);
GpioConfig103 gpioConfig;
EEPROM.get(CONFIG_GPIO_START_103, gpioConfig);
gpioConfig.hanPinPullup = true;
EEPROM.put(CONFIG_GPIO_START, gpioConfig);
EEPROM.put(CONFIG_GPIO_START_103, gpioConfig);
HomeAssistantConfig haconf;
clearHomeAssistantConfig(haconf);
EEPROM.put(CONFIG_HA_START, haconf);
EEPROM.put(CONFIG_HA_START_103, haconf);
EntsoeConfig entsoe;
EEPROM.get(CONFIG_ENTSOE_START, entsoe);
EEPROM.get(CONFIG_ENTSOE_START_103, entsoe);
entsoe.fixedPrice = 0;
EEPROM.put(CONFIG_ENTSOE_START, entsoe);
EEPROM.put(CONFIG_ENTSOE_START_103, entsoe);
EEPROM.put(EEPROM_CONFIG_ADDRESS, 103);
bool ret = EEPROM.commit();
@@ -1106,10 +979,89 @@ bool AmsConfiguration::relocateConfig102() {
return ret;
}
bool AmsConfiguration::relocateConfig103() {
EEPROM.begin(EEPROM_SIZE);
MeterConfig meter;
UpgradeInformation upinfo;
UiConfig ui;
GpioConfig103 gpio103;
EntsoeConfig entsoe;
NetworkConfig wifi;
EnergyAccountingConfig eac;
WebConfig web;
DebugConfig debug;
DomoticzConfig domo;
NtpConfig ntp;
MqttConfig mqtt;
HomeAssistantConfig ha;
EEPROM.get(CONFIG_METER_START_103, meter);
EEPROM.get(CONFIG_UPGRADE_INFO_START_103, upinfo);
EEPROM.get(CONFIG_UI_START_103, ui);
EEPROM.get(CONFIG_GPIO_START_103, gpio103);
EEPROM.get(CONFIG_ENTSOE_START_103, entsoe);
EEPROM.get(CONFIG_WIFI_START_103, wifi);
EEPROM.get(CONFIG_ENERGYACCOUNTING_START_103, eac);
EEPROM.get(CONFIG_WEB_START_103, web);
EEPROM.get(CONFIG_DEBUG_START_103, debug);
EEPROM.get(CONFIG_DOMOTICZ_START_103, domo);
EEPROM.get(CONFIG_NTP_START_103, ntp);
EEPROM.get(CONFIG_MQTT_START_103, mqtt);
EEPROM.get(CONFIG_HA_START_103, ha);
meter.rxPin = gpio103.hanPin;
meter.txPin = 0xFF;
meter.rxPinPullup = gpio103.hanPinPullup;
wifi.mode = 1; // 1 == WiFi client
GpioConfig gpio = {
gpio103.apPin,
gpio103.ledPin,
gpio103.ledInverted,
gpio103.ledPinRed,
gpio103.ledPinGreen,
gpio103.ledPinBlue,
gpio103.ledRgbInverted,
gpio103.tempSensorPin,
gpio103.tempAnalogSensorPin,
gpio103.vccPin,
gpio103.vccOffset,
gpio103.vccMultiplier,
gpio103.vccBootLimit,
gpio103.vccResistorGnd,
gpio103.vccResistorVcc,
gpio103.ledDisablePin,
gpio103.ledBehaviour
};
EEPROM.put(CONFIG_UPGRADE_INFO_START, upinfo);
EEPROM.put(CONFIG_NETWORK_START, wifi);
EEPROM.put(CONFIG_METER_START, meter);
EEPROM.put(CONFIG_GPIO_START, gpio);
EEPROM.put(CONFIG_ENTSOE_START, entsoe);
EEPROM.put(CONFIG_ENERGYACCOUNTING_START, eac);
EEPROM.put(CONFIG_WEB_START, web);
EEPROM.put(CONFIG_DEBUG_START, debug);
EEPROM.put(CONFIG_NTP_START, ntp);
EEPROM.put(CONFIG_MQTT_START, mqtt);
EEPROM.put(CONFIG_DOMOTICZ_START, domo);
EEPROM.put(CONFIG_HA_START, ha);
EEPROM.put(CONFIG_UI_START, ui);
CloudConfig cloud;
clearCloudConfig(cloud);
EEPROM.put(CONFIG_CLOUD_START, cloud);
EEPROM.put(EEPROM_CONFIG_ADDRESS, 104);
bool ret = EEPROM.commit();
EEPROM.end();
return ret;
}
bool AmsConfiguration::save() {
EEPROM.begin(EEPROM_SIZE);
EEPROM.put(EEPROM_CONFIG_ADDRESS, EEPROM_CHECK_SUM);
saveTempSensors();
bool success = EEPROM.commit();
EEPROM.end();
@@ -1117,60 +1069,6 @@ bool AmsConfiguration::save() {
return success;
}
uint8_t AmsConfiguration::getTempSensorCount() {
return tempSensorCount;
}
TempSensorConfig* AmsConfiguration::getTempSensorConfig(uint8_t address[8]) {
if(tempSensors == NULL)
return NULL;
for(int x = 0; x < tempSensorCount; x++) {
TempSensorConfig *conf = tempSensors[x];
if(isSensorAddressEqual(conf->address, address)) {
return conf;
}
}
return NULL;
}
void AmsConfiguration::updateTempSensorConfig(uint8_t address[8], const char name[32], bool common) {
bool found = false;
if(tempSensors != NULL) {
for(int x = 0; x < tempSensorCount; x++) {
TempSensorConfig *data = tempSensors[x];
if(isSensorAddressEqual(data->address, address)) {
found = true;
strcpy(data->name, name);
data->common = common;
}
}
}
if(!found) {
TempSensorConfig** tempSensors = new TempSensorConfig*[tempSensorCount+1];
if(this->tempSensors != NULL) {
for(int i = 0;i < tempSensorCount; i++) {
tempSensors[i] = this->tempSensors[i];
}
}
TempSensorConfig *data = new TempSensorConfig();
memcpy(data->address, address, 8);
strcpy(data->name, name);
data->common = common;
tempSensors[tempSensorCount++] = data;
if(this->tempSensors != NULL) {
delete this->tempSensors;
}
this->tempSensors = tempSensors;
}
}
bool AmsConfiguration::isSensorAddressEqual(uint8_t a[8], uint8_t b[8]) {
for(int i = 0; i < 8; i++) {
if(a[i] != b[i]) return false;
}
return true;
}
void AmsConfiguration::saveToFs() {
}
@@ -1186,21 +1084,32 @@ void AmsConfiguration::deleteFromFs(uint8_t version) {
void AmsConfiguration::print(Print* debugger)
{
debugger->println(F("-----------------------------------------------"));
WiFiConfig wifi;
if(getWiFiConfig(wifi)) {
debugger->println(F("--WiFi configuration--"));
debugger->printf_P(PSTR("SSID: '%s'\r\n"), wifi.ssid);
debugger->printf_P(PSTR("Psk: '%s'\r\n"), wifi.psk);
if(strlen(wifi.ip) > 0) {
debugger->printf_P(PSTR("IP: '%s'\r\n"), wifi.ip);
debugger->printf_P(PSTR("Gateway: '%s'\r\n"), wifi.gateway);
debugger->printf_P(PSTR("Subnet: '%s'\r\n"), wifi.subnet);
debugger->printf_P(PSTR("DNS1: '%s'\r\n"), wifi.dns1);
debugger->printf_P(PSTR("DNS2: '%s'\r\n"), wifi.dns2);
NetworkConfig network;
if(getNetworkConfig(network)) {
debugger->println(F("--Network configuration--"));
switch(network.mode) {
case 1:
debugger->printf_P(PSTR("Mode: 'WiFi client'\r\n"));
break;
case 2:
debugger->printf_P(PSTR("Mode: 'WiFi AP'\r\n"));
break;
case 3:
debugger->printf_P(PSTR("Mode: 'Ethernet'\r\n"));
break;
}
debugger->printf_P(PSTR("Hostname: '%s'\r\n"), wifi.hostname);
debugger->printf_P(PSTR("mDNS: '%s'\r\n"), wifi.mdns ? "Yes" : "No");
debugger->printf_P(PSTR("802.11b: '%s'\r\n"), wifi.use11b ? "Yes" : "No");
debugger->printf_P(PSTR("SSID: '%s'\r\n"), network.ssid);
debugger->printf_P(PSTR("Psk: '%s'\r\n"), network.psk);
if(strlen(network.ip) > 0) {
debugger->printf_P(PSTR("IP: '%s'\r\n"), network.ip);
debugger->printf_P(PSTR("Gateway: '%s'\r\n"), network.gateway);
debugger->printf_P(PSTR("Subnet: '%s'\r\n"), network.subnet);
debugger->printf_P(PSTR("DNS1: '%s'\r\n"), network.dns1);
debugger->printf_P(PSTR("DNS2: '%s'\r\n"), network.dns2);
}
debugger->printf_P(PSTR("Hostname: '%s'\r\n"), network.hostname);
debugger->printf_P(PSTR("mDNS: '%s'\r\n"), network.mdns ? "Yes" : "No");
debugger->printf_P(PSTR("802.11b: '%s'\r\n"), network.use11b ? "Yes" : "No");
debugger->println(F(""));
delay(10);
debugger->flush();
@@ -1246,6 +1155,8 @@ void AmsConfiguration::print(Print* debugger)
MeterConfig meter;
if(getMeterConfig(meter)) {
debugger->println(F("--Meter configuration--"));
debugger->printf_P(PSTR("HAN RX: %i\r\n"), meter.rxPin);
debugger->printf_P(PSTR("HAN RX pullup %s\r\n"), meter.rxPinPullup ? "Yes" : "No");
debugger->printf_P(PSTR("Baud: %i\r\n"), meter.baud);
debugger->printf_P(PSTR("Parity: %i\r\n"), meter.parity);
debugger->printf_P(PSTR("Invert serial: %s\r\n"), meter.invert ? "Yes" : "No");
@@ -1261,8 +1172,6 @@ void AmsConfiguration::print(Print* debugger)
GpioConfig gpio;
if(getGpioConfig(gpio)) {
debugger->println(F("--GPIO configuration--"));
debugger->printf_P(PSTR("HAN pin: %i\r\n"), gpio.hanPin);
debugger->printf_P(PSTR("HAN pin pullup %s\r\n"), gpio.hanPinPullup ? "Yes" : "No");
debugger->printf_P(PSTR("LED pin: %i\r\n"), gpio.ledPin);
debugger->printf_P(PSTR("LED inverted: %s\r\n"), gpio.ledInverted ? "Yes" : "No");
debugger->printf_P(PSTR("LED red pin: %i\r\n"), gpio.ledPinRed);
@@ -1273,6 +1182,8 @@ void AmsConfiguration::print(Print* debugger)
debugger->printf_P(PSTR("Temperature pin: %i\r\n"), gpio.tempSensorPin);
debugger->printf_P(PSTR("Temp analog pin: %i\r\n"), gpio.tempAnalogSensorPin);
debugger->printf_P(PSTR("Vcc pin: %i\r\n"), gpio.vccPin);
debugger->printf_P(PSTR("LED disable pin: %i\r\n"), gpio.ledDisablePin);
debugger->printf_P(PSTR("LED behaviour: %i\r\n"), gpio.ledBehaviour);
if(gpio.vccMultiplier > 0) {
debugger->printf_P(PSTR("Vcc multiplier: %f\r\n"), gpio.vccMultiplier / 1000.0);
}
@@ -1335,7 +1246,5 @@ void AmsConfiguration::print(Print* debugger)
debugger->flush();
}
debugger->printf_P(PSTR("Temp sensor count: %i\r\n"), this->getTempSensorCount());
debugger->println(F("-----------------------------------------------"));
}

View File

@@ -1,3 +1,9 @@
/**
* @copyright Utilitech AS 2023
* License: Fair Source
*
*/
#include "hexutils.h"
String toHex(uint8_t* in) {