Skip to content

Commit

Permalink
reviewed sendDiscoveryConfig #565
Browse files Browse the repository at this point in the history
  • Loading branch information
lumapu committed Jan 16, 2023
1 parent c157134 commit 3f0cfd4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 40 deletions.
1 change: 1 addition & 0 deletions src/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* improved wifi handling and tickers, many thanks to @beegee3 #571
* fixed YieldTotal correction calculation #589
* fixed serial output of power limit acknowledge #569
* reviewed `sendDiscoveryConfig` #565

## 0.5.70
* corrected MQTT `comm_disabled` #529
Expand Down
81 changes: 41 additions & 40 deletions src/publisher/pubMqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,50 +182,51 @@ class PubMqtt {
void sendDiscoveryConfig(void) {
DPRINTLN(DBG_VERBOSE, F("sendMqttDiscoveryConfig"));

char stateTopic[64], discoveryTopic[64], buffer[512], name[32], uniq_id[32];
char topic[64], buffer[512], name[32], uniq_id[32];

This comment has been minimized.

Copy link
@stefan123t

stefan123t Jan 18, 2023

Collaborator

Both buffer[512] and doc(512) are 512 bytes in size.
Does this fit all the Json Payload we are constructing below based on the maximum field lenght(s) of all the device name, ids, etc. fields ?
What is the DynamicJsonDocument anyway, I would assume this is actually allocating some more memory if needed ?
Also the deviceObj which is assigned the current DynamicJsonDocument doc should be dynamically allocated.
What does the serializeJson and publish Methods do as they operate on the fixed size buffer ?
Do we need to allocate another object before serialization and publishing the second instance of the doc in lines L212-222 which include the deviceObj as an element ?

DynamicJsonDocument doc(512);
for (uint8_t id = 0; id < mSys->getNumInverters(); id++) {
Inverter<> *iv = mSys->getInverterByPos(id);
if (NULL != iv) {
record_t<> *rec = iv->getRecordStruct(RealTimeRunData_Debug);
DynamicJsonDocument deviceDoc(128);
deviceDoc[F("name")] = iv->config->name;
deviceDoc[F("ids")] = String(iv->config->serial.u64, HEX);
deviceDoc[F("cu")] = F("http://") + String(WiFi.localIP().toString());
deviceDoc[F("mf")] = F("Hoymiles");
deviceDoc[F("mdl")] = iv->config->name;
JsonObject deviceObj = deviceDoc.as<JsonObject>();
DynamicJsonDocument doc(384);

for (uint8_t i = 0; i < rec->length; i++) {
if (rec->assign[i].ch == CH0) {
snprintf(name, 32, "%s %s", iv->config->name, iv->getFieldName(i, rec));
} else {
snprintf(name, 32, "%s CH%d %s", iv->config->name, rec->assign[i].ch, iv->getFieldName(i, rec));
}
snprintf(stateTopic, 64, "/ch%d/%s", rec->assign[i].ch, iv->getFieldName(i, rec));
snprintf(discoveryTopic, 64, "%s/sensor/%s/ch%d_%s/config", MQTT_DISCOVERY_PREFIX, iv->config->name, rec->assign[i].ch, iv->getFieldName(i, rec));
snprintf(uniq_id, 32, "ch%d_%s", rec->assign[i].ch, iv->getFieldName(i, rec));
const char *devCls = getFieldDeviceClass(rec->assign[i].fieldId);
const char *stateCls = getFieldStateClass(rec->assign[i].fieldId);

doc[F("name")] = name;
doc[F("stat_t")] = String(mCfgMqtt->topic) + "/" + String(iv->config->name) + String(stateTopic);
doc[F("unit_of_meas")] = iv->getUnit(i, rec);
doc[F("uniq_id")] = String(iv->config->serial.u64, HEX) + "_" + uniq_id;
doc[F("dev")] = deviceObj;
doc[F("exp_aft")] = MQTT_INTERVAL + 5; // add 5 sec if connection is bad or ESP too slow @TODO: stimmt das wirklich als expire!?
if (devCls != NULL)
doc[F("dev_cla")] = devCls;
if (stateCls != NULL)
doc[F("stat_cla")] = stateCls;

serializeJson(doc, buffer);
publish(discoveryTopic, buffer, true, false);
doc.clear();
}
if (NULL == iv)
continue;

yield();
record_t<> *rec = iv->getRecordStruct(RealTimeRunData_Debug);
doc.clear();
doc[F("name")] = iv->config->name;
doc[F("ids")] = String(iv->config->serial.u64, HEX);
doc[F("cu")] = F("http://") + String(WiFi.localIP().toString());
doc[F("mf")] = F("Hoymiles");
doc[F("mdl")] = iv->config->name;
JsonObject deviceObj = doc.as<JsonObject>();

for (uint8_t i = 0; i < rec->length; i++) {
if (rec->assign[i].ch == CH0)
snprintf(name, 32, "%s %s", iv->config->name, iv->getFieldName(i, rec));
else
snprintf(name, 32, "%s CH%d %s", iv->config->name, rec->assign[i].ch, iv->getFieldName(i, rec));
snprintf(topic, 64, "/ch%d/%s", rec->assign[i].ch, iv->getFieldName(i, rec));
snprintf(uniq_id, 32, "ch%d_%s", rec->assign[i].ch, iv->getFieldName(i, rec));

const char *devCls = getFieldDeviceClass(rec->assign[i].fieldId);
const char *stateCls = getFieldStateClass(rec->assign[i].fieldId);

doc.clear();
doc[F("name")] = name;
doc[F("stat_t")] = String(mCfgMqtt->topic) + "/" + String(iv->config->name) + String(topic);
doc[F("unit_of_meas")] = iv->getUnit(i, rec);
doc[F("uniq_id")] = String(iv->config->serial.u64, HEX) + "_" + uniq_id;
doc[F("dev")] = deviceObj;
doc[F("exp_aft")] = MQTT_INTERVAL + 5; // add 5 sec if connection is bad or ESP too slow @TODO: stimmt das wirklich als expire!?
if (devCls != NULL)
doc[F("dev_cla")] = String(devCls);
if (stateCls != NULL)
doc[F("stat_cla")] = String(stateCls);

snprintf(topic, 64, "%s/sensor/%s/ch%d_%s/config", MQTT_DISCOVERY_PREFIX, iv->config->name, rec->assign[i].ch, iv->getFieldName(i, rec));
serializeJson(doc, buffer);
publish(topic, buffer, true, false);
}

yield();
}
}

Expand Down

0 comments on commit 3f0cfd4

Please sign in to comment.