Skip to content

Commit

Permalink
Some changes for cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
gskjold committed Apr 13, 2024
1 parent 3b93897 commit 8c8e14f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 17 deletions.
13 changes: 9 additions & 4 deletions lib/CloudConnector/include/CloudConnector.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -52,25 +52,30 @@ 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:
RemoteDebug* debugger = NULL;
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;

Expand Down
38 changes: 32 additions & 6 deletions lib/CloudConnector/src/CloudConnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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;
Expand Down Expand Up @@ -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(),
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 16 additions & 7 deletions src/AmsToMqttBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -908,6 +916,9 @@ void handleEnergyAccountingChanged() {
config.getEnergyAccountingConfig(*eac);
ea.setup(&ds, eac);
config.ackEnergyAccountingChange();
if(cloud != NULL) {
cloud->setEnergyAccountingConfig(*eac);
}
}

char ntpServerName[64] = "";
Expand All @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 8c8e14f

Please sign in to comment.