mirror of
https://github.com/lumapu/ahoy.git
synced 2025-05-29 00:36:11 +02:00
refactored sendMqttDiscoveryConfig
This commit is contained in:
parent
2ac8aadec2
commit
43e8714526
5 changed files with 81 additions and 83 deletions
|
@ -16,9 +16,14 @@
|
|||
#undef F
|
||||
#define F(sl) (sl)
|
||||
#endif
|
||||
|
||||
#include "../utils/dbg.h"
|
||||
#include <PubSubClient.h>
|
||||
#include "../defines.h"
|
||||
#include "../hm/hmSystem.h"
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
template<class HMSYSTEM>
|
||||
class mqtt {
|
||||
public:
|
||||
mqtt() {
|
||||
|
@ -89,6 +94,78 @@ class mqtt {
|
|||
return mTxCnt;
|
||||
}
|
||||
|
||||
void sendMqttDiscoveryConfig(HMSYSTEM *sys, const char *topic, uint32_t invertval) {
|
||||
DPRINTLN(DBG_VERBOSE, F("app::sendMqttDiscoveryConfig"));
|
||||
|
||||
char stateTopic[64], discoveryTopic[64], buffer[512], name[32], uniq_id[32];
|
||||
for (uint8_t id = 0; id < sys->getNumInverters(); id++) {
|
||||
Inverter<> *iv = sys->getInverterByPos(id);
|
||||
if (NULL != iv) {
|
||||
record_t<> *rec = iv->getRecordStruct(RealTimeRunData_Debug);
|
||||
DynamicJsonDocument deviceDoc(128);
|
||||
deviceDoc["name"] = iv->name;
|
||||
deviceDoc["ids"] = String(iv->serial.u64, HEX);
|
||||
deviceDoc["cu"] = F("http://") + String(WiFi.localIP().toString());
|
||||
deviceDoc["mf"] = "Hoymiles";
|
||||
deviceDoc["mdl"] = iv->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->name, iv->getFieldName(i, rec));
|
||||
} else {
|
||||
snprintf(name, 32, "%s CH%d %s", iv->name, rec->assign[i].ch, iv->getFieldName(i, rec));
|
||||
}
|
||||
snprintf(stateTopic, 64, "%s/%s/ch%d/%s", topic, iv->name, rec->assign[i].ch, iv->getFieldName(i, rec));
|
||||
snprintf(discoveryTopic, 64, "%s/sensor/%s/ch%d_%s/config", MQTT_DISCOVERY_PREFIX, iv->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["name"] = name;
|
||||
doc["stat_t"] = stateTopic;
|
||||
doc["unit_of_meas"] = iv->getUnit(i, rec);
|
||||
doc["uniq_id"] = String(iv->serial.u64, HEX) + "_" + uniq_id;
|
||||
doc["dev"] = deviceObj;
|
||||
doc["exp_aft"] = invertval + 5; // add 5 sec if connection is bad or ESP too slow @TODO: stimmt das wirklich als expire!?
|
||||
if (devCls != NULL)
|
||||
doc["dev_cla"] = devCls;
|
||||
if (stateCls != NULL)
|
||||
doc["stat_cla"] = stateCls;
|
||||
|
||||
serializeJson(doc, buffer);
|
||||
sendMsg2(discoveryTopic, buffer, true);
|
||||
// DPRINTLN(DBG_INFO, F("mqtt sent"));
|
||||
doc.clear();
|
||||
}
|
||||
|
||||
yield();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
const char *getFieldDeviceClass(uint8_t fieldId) {
|
||||
uint8_t pos = 0;
|
||||
for (; pos < DEVICE_CLS_ASSIGN_LIST_LEN; pos++) {
|
||||
if (deviceFieldAssignment[pos].fieldId == fieldId)
|
||||
break;
|
||||
}
|
||||
return (pos >= DEVICE_CLS_ASSIGN_LIST_LEN) ? NULL : deviceClasses[deviceFieldAssignment[pos].deviceClsId];
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
const char *getFieldStateClass(uint8_t fieldId) {
|
||||
uint8_t pos = 0;
|
||||
for (; pos < DEVICE_CLS_ASSIGN_LIST_LEN; pos++) {
|
||||
if (deviceFieldAssignment[pos].fieldId == fieldId)
|
||||
break;
|
||||
}
|
||||
return (pos >= DEVICE_CLS_ASSIGN_LIST_LEN) ? NULL : stateClasses[deviceFieldAssignment[pos].stateClsId];
|
||||
}
|
||||
|
||||
private:
|
||||
void reconnect(void) {
|
||||
DPRINTLN(DBG_DEBUG, F("mqtt.h:reconnect"));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue