From 8c8e14f60c85ae8de350a5b73ac6429e2fe226d2 Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Sat, 13 Apr 2024 11:29:56 +0200 Subject: [PATCH] Some changes for cloud --- lib/CloudConnector/include/CloudConnector.h | 13 ++++--- lib/CloudConnector/src/CloudConnector.cpp | 38 +++++++++++++++++---- src/AmsToMqttBridge.cpp | 23 +++++++++---- 3 files changed, 57 insertions(+), 17 deletions(-) diff --git a/lib/CloudConnector/include/CloudConnector.h b/lib/CloudConnector/include/CloudConnector.h index 4af2b01f..b10b0efd 100644 --- a/lib/CloudConnector/include/CloudConnector.h +++ b/lib/CloudConnector/include/CloudConnector.h @@ -42,7 +42,7 @@ static const char CC_JSON_POWER_LIST3[] PROGMEM = ",\"%s\":{\"P\":%lu,\"Q\":%lu, static const char CC_JSON_PHASE[] PROGMEM = "%s\"%d\":{\"u\":%.2f,\"i\":%s}"; static const char CC_JSON_PHASE_LIST4[] PROGMEM = "%s\"%d\":{\"u\":%.2f,\"i\":%s,\"Pim\":%lu,\"Pex\":%lu,\"pf\":%.2f}"; static const char CC_JSON_STATUS[] PROGMEM = ",\"status\":{\"esp\":{\"state\":%d,\"error\":%d},\"han\":{\"state\":%d,\"error\":%d},\"wifi\":{\"state\":%d,\"error\":%d},\"mqtt\":{\"state\":%d,\"error\":%d}}"; -static const char CC_JSON_INIT[] PROGMEM = ",\"init\":{\"mac\":\"%s\",\"apmac\":\"%s\",\"version\":\"%s\",\"boardType\":%d,\"bootReason\":%d,\"bootCause\":%d,\"utcOffset\":%d},\"meter\":{\"manufacturerId\":%d,\"manufacturer\":\"%s\",\"model\":\"%s\",\"id\":\"%s\",\"system\":\"%s\",\"fuse\":%d,\"import\":%d,\"export\":%d},\"network\":{\"ip\":\"%s\",\"mask\":\"%s\",\"gw\":\"%s\",\"dns1\":\"%s\",\"dns2\":\"%s\"}"; +static const char CC_JSON_INIT[] PROGMEM = ",\"init\":{\"mac\":\"%s\",\"apmac\":\"%s\",\"version\":\"%s\",\"boardType\":%d,\"bootReason\":%d,\"bootCause\":%d,\"tz\":\"%s\"},\"meter\":{\"manufacturerId\":%d,\"manufacturer\":\"%s\",\"model\":\"%s\",\"id\":\"%s\",\"system\":\"%s\",\"fuse\":%d,\"import\":%d,\"export\":%d},\"network\":{\"ip\":\"%s\",\"mask\":\"%s\",\"gw\":\"%s\",\"dns1\":\"%s\",\"dns2\":\"%s\"}"; struct CloudData { uint8_t type; @@ -52,11 +52,12 @@ struct CloudData { class CloudConnector { public: CloudConnector(RemoteDebug*); - bool setup(CloudConfig& config, MeterConfig& meter, SystemConfig& system, HwTools* hw, ResetDataContainer* rdc); + bool setup(CloudConfig& config, MeterConfig& meter, SystemConfig& system, NtpConfig& ntp, HwTools* hw, ResetDataContainer* rdc); void setMqttHandler(AmsMqttHandler* mqttHandler); void update(AmsData& data, EnergyAccounting& ea); + void setPriceConfig(PriceServiceConfig&); + void setEnergyAccountingConfig(EnergyAccountingConfig&); void forceUpdate(); - void setTimezone(Timezone* tz); void setConnectionHandler(ConnectionHandler* ch); private: @@ -64,13 +65,17 @@ class CloudConnector { HwTools* hw = NULL; ConnectionHandler* ch = NULL; ResetDataContainer* rdc = NULL; - Timezone* tz = NULL; AmsMqttHandler* mqttHandler = NULL; CloudConfig config; + PriceServiceConfig priceConfig; + unsigned long lastPriceConfig = 0; + EnergyAccountingConfig eac; + unsigned long lastEac = 0; HTTPClient http; WiFiUDP udp; int maxPwr = 0; uint8_t boardType = 0; + char timezone[32]; uint8_t distributionSystem = 0; uint16_t mainFuse = 0, productionCapacity = 0; diff --git a/lib/CloudConnector/src/CloudConnector.cpp b/lib/CloudConnector/src/CloudConnector.cpp index f131b59f..274f9626 100644 --- a/lib/CloudConnector/src/CloudConnector.cpp +++ b/lib/CloudConnector/src/CloudConnector.cpp @@ -46,7 +46,7 @@ CloudConnector::CloudConnector(RemoteDebug* debugger) { sprintf_P(this->apmac, PSTR("%02X:%02X:%02X:%02X:%02X:%02X"), apmac[0], apmac[1], apmac[2], apmac[3], apmac[4], apmac[5]); } -bool CloudConnector::setup(CloudConfig& config, MeterConfig& meter, SystemConfig& system, HwTools* hw, ResetDataContainer* rdc) { +bool CloudConnector::setup(CloudConfig& config, MeterConfig& meter, SystemConfig& system, NtpConfig& ntp, HwTools* hw, ResetDataContainer* rdc) { bool ret = false; #if defined(ESP32) if(!ESPRandom::isValidV4Uuid(config.clientId)) { @@ -61,6 +61,7 @@ bool CloudConnector::setup(CloudConfig& config, MeterConfig& meter, SystemConfig this->rdc = rdc; this->boardType = system.boardType; + strcpy(this->timezone, ntp.timezone); this->maxPwr = 0; this->distributionSystem = meter.distributionSystem; @@ -204,7 +205,7 @@ void CloudConnector::update(AmsData& data, EnergyAccounting& ea) { boardType, rtc_get_reset_reason(0), rdc == NULL ? 0 : rdc->last_cause, - tz == NULL ? 0 : (tz->toLocal(now)-now)/3600, + timezone, data.getMeterType(), meterManufacturer(data.getMeterType()).c_str(), data.getMeterModel().c_str(), @@ -219,6 +220,23 @@ void CloudConnector::update(AmsData& data, EnergyAccounting& ea) { dns1.toString().c_str(), dns2.toString().c_str() ); + } else if(lastPriceConfig == 0) { + pos += snprintf_P(clearBuffer+pos, CC_BUF_SIZE-pos, PSTR(",\"price\":{\"area\":\"%s\",\"currency\":\"%s\"}"), priceConfig.area, priceConfig.currency); + lastPriceConfig = now; + } else if(lastEac == 0) { + pos += snprintf_P(clearBuffer+pos, CC_BUF_SIZE-pos, PSTR(",\"accounting\":{\"hours\":%d,\"thresholds\":[%d,%d,%d,%d,%d,%d,%d,%d,%d]}"), + eac.hours, + eac.thresholds[0], + eac.thresholds[1], + eac.thresholds[2], + eac.thresholds[3], + eac.thresholds[4], + eac.thresholds[5], + eac.thresholds[6], + eac.thresholds[7], + eac.thresholds[8] + ); + lastEac = now; } float vcc = 0.0; @@ -382,16 +400,24 @@ void CloudConnector::update(AmsData& data, EnergyAccounting& ea) { void CloudConnector::forceUpdate() { lastUpdate = 0; -} - -void CloudConnector::setTimezone(Timezone* tz) { - this->tz = tz; + lastPriceConfig = 0; + lastEac = 0; } void CloudConnector::setConnectionHandler(ConnectionHandler* ch) { this->ch = ch; } +void CloudConnector::setPriceConfig(PriceServiceConfig& priceConfig) { + this->priceConfig = priceConfig; + this->lastPriceConfig = 0; +} + +void CloudConnector::setEnergyAccountingConfig(EnergyAccountingConfig& eac) { + this->eac = eac; + this->lastEac = 0; +} + void CloudConnector::debugPrint(byte *buffer, int start, int length) { for (int i = start; i < start + length; i++) { if (buffer[i] < 0x10) diff --git a/src/AmsToMqttBridge.cpp b/src/AmsToMqttBridge.cpp index b7c7b9c0..881b5d4f 100644 --- a/src/AmsToMqttBridge.cpp +++ b/src/AmsToMqttBridge.cpp @@ -674,11 +674,19 @@ void loop() { if(cloud == NULL) { cloud = new CloudConnector(&Debug); } - if(cloud->setup(cc, meterConfig, sysConfig, &hw, &rdc)) { + NtpConfig ntp; + config.getNtpConfig(ntp); + if(cloud->setup(cc, meterConfig, sysConfig, ntp, &hw, &rdc)) { config.setCloudConfig(cc); } - cloud->setTimezone(tz); cloud->setConnectionHandler(ch); + + PriceServiceConfig price; + config.getPriceServiceConfig(price); + cloud->setPriceConfig(price); + + EnergyAccountingConfig *eac = ea.getConfig(); + cloud->setEnergyAccountingConfig(*eac); } config.ackCloudConfig(); } @@ -908,6 +916,9 @@ void handleEnergyAccountingChanged() { config.getEnergyAccountingConfig(*eac); ea.setup(&ds, eac); config.ackEnergyAccountingChange(); + if(cloud != NULL) { + cloud->setEnergyAccountingConfig(*eac); + } } char ntpServerName[64] = ""; @@ -931,11 +942,6 @@ void handleNtpChange() { ws.setTimezone(tz); ds.setTimezone(tz); ea.setTimezone(tz); - #if defined(ESP32) - if(cloud != NULL) { - cloud->setTimezone(tz); - } - #endif } config.ackNtpChange(); @@ -1049,6 +1055,9 @@ void handlePriceService(unsigned long now) { ps = new PriceService(&Debug); ea.setPriceService(ps); ws.setPriceService(ps); + if(cloud != NULL) { + cloud->setPriceConfig(price); + } } ps->setup(price); } else if(ps != NULL) {