mirror of
https://github.com/lumapu/ahoy.git
synced 2025-05-03 12:15:54 +02:00
refactored sendMqttDiscoveryConfig
This commit is contained in:
parent
2ac8aadec2
commit
43e8714526
5 changed files with 81 additions and 83 deletions
76
src/app.cpp
76
src/app.cpp
|
@ -85,7 +85,7 @@ void app::loop(void) {
|
||||||
|
|
||||||
if (mFlagSendDiscoveryConfig) {
|
if (mFlagSendDiscoveryConfig) {
|
||||||
mFlagSendDiscoveryConfig = false;
|
mFlagSendDiscoveryConfig = false;
|
||||||
sendMqttDiscoveryConfig();
|
mMqtt.sendMqttDiscoveryConfig(mSys, mConfig.mqtt.topic, mMqttInterval);
|
||||||
}
|
}
|
||||||
|
|
||||||
mSys->Radio.loop();
|
mSys->Radio.loop();
|
||||||
|
@ -524,60 +524,6 @@ void app::getAvailNetworks(JsonObject obj) {
|
||||||
mWifi->getAvailNetworks(obj);
|
mWifi->getAvailNetworks(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
void app::sendMqttDiscoveryConfig(void) {
|
|
||||||
DPRINTLN(DBG_VERBOSE, F("app::sendMqttDiscoveryConfig"));
|
|
||||||
|
|
||||||
char stateTopic[64], discoveryTopic[64], buffer[512], name[32], uniq_id[32];
|
|
||||||
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["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", mConfig.mqtt.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"] = mMqttInterval + 5; // add 5 sec if connection is bad or ESP too slow
|
|
||||||
if (devCls != NULL)
|
|
||||||
doc["dev_cla"] = devCls;
|
|
||||||
if (stateCls != NULL)
|
|
||||||
doc["stat_cla"] = stateCls;
|
|
||||||
|
|
||||||
serializeJson(doc, buffer);
|
|
||||||
mMqtt.sendMsg2(discoveryTopic, buffer, true);
|
|
||||||
// DPRINTLN(DBG_INFO, F("mqtt sent"));
|
|
||||||
doc.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: remove this field, obsolete?
|
|
||||||
mMqttConfigSendState[id] = true;
|
|
||||||
|
|
||||||
yield();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
void app::sendMqtt(void) {
|
void app::sendMqtt(void) {
|
||||||
|
@ -691,26 +637,6 @@ void app::sendMqtt(void) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
const char *app::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 *app::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];
|
|
||||||
}
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
void app::resetSystem(void) {
|
void app::resetSystem(void) {
|
||||||
mUptimeSecs = 0;
|
mUptimeSecs = 0;
|
||||||
|
|
|
@ -32,6 +32,7 @@
|
||||||
#define ACOS(x) (degrees(acos(x)))
|
#define ACOS(x) (degrees(acos(x)))
|
||||||
|
|
||||||
typedef HmSystem<MAX_NUM_INVERTERS> HmSystemType;
|
typedef HmSystem<MAX_NUM_INVERTERS> HmSystemType;
|
||||||
|
typedef mqtt<HmSystemType> MqttType;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint8_t txCmd;
|
uint8_t txCmd;
|
||||||
|
@ -177,7 +178,6 @@ class app {
|
||||||
void loadEEpconfig(void);
|
void loadEEpconfig(void);
|
||||||
|
|
||||||
void setupMqtt(void);
|
void setupMqtt(void);
|
||||||
void sendMqttDiscoveryConfig(void);
|
|
||||||
void sendMqtt(void);
|
void sendMqtt(void);
|
||||||
|
|
||||||
void setupLed(void);
|
void setupLed(void);
|
||||||
|
@ -186,9 +186,6 @@ class app {
|
||||||
bool buildPayload(uint8_t id);
|
bool buildPayload(uint8_t id);
|
||||||
void processPayload(bool retransmit);
|
void processPayload(bool retransmit);
|
||||||
|
|
||||||
const char* getFieldDeviceClass(uint8_t fieldId);
|
|
||||||
const char* getFieldStateClass(uint8_t fieldId);
|
|
||||||
|
|
||||||
inline uint16_t buildEEpCrc(uint32_t start, uint32_t length) {
|
inline uint16_t buildEEpCrc(uint32_t start, uint32_t length) {
|
||||||
DPRINTLN(DBG_VERBOSE, F("main.h:buildEEpCrc"));
|
DPRINTLN(DBG_VERBOSE, F("main.h:buildEEpCrc"));
|
||||||
uint8_t buf[32];
|
uint8_t buf[32];
|
||||||
|
@ -286,11 +283,10 @@ class app {
|
||||||
uint32_t mRxTicker;
|
uint32_t mRxTicker;
|
||||||
|
|
||||||
// mqtt
|
// mqtt
|
||||||
mqtt mMqtt;
|
MqttType mMqtt;
|
||||||
uint16_t mMqttTicker;
|
uint16_t mMqttTicker;
|
||||||
uint16_t mMqttInterval;
|
uint16_t mMqttInterval;
|
||||||
bool mMqttActive;
|
bool mMqttActive;
|
||||||
bool mMqttConfigSendState[MAX_NUM_INVERTERS];
|
|
||||||
std::queue<uint8_t> mMqttSendList;
|
std::queue<uint8_t> mMqttSendList;
|
||||||
|
|
||||||
// serial
|
// serial
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
//-------------------------------------
|
//-------------------------------------
|
||||||
#define VERSION_MAJOR 0
|
#define VERSION_MAJOR 0
|
||||||
#define VERSION_MINOR 5
|
#define VERSION_MINOR 5
|
||||||
#define VERSION_PATCH 32
|
#define VERSION_PATCH 33
|
||||||
|
|
||||||
//-------------------------------------
|
//-------------------------------------
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
|
@ -65,7 +65,6 @@ def convert2Header(inFile, compress):
|
||||||
|
|
||||||
# delete all files in the 'h' dir, but ignore 'favicon_ico_gz.h'
|
# delete all files in the 'h' dir, but ignore 'favicon_ico_gz.h'
|
||||||
dir = 'h'
|
dir = 'h'
|
||||||
print(os.getcwd())
|
|
||||||
if os.getcwd()[-4:] != "html":
|
if os.getcwd()[-4:] != "html":
|
||||||
dir = "web/html/" + dir
|
dir = "web/html/" + dir
|
||||||
|
|
||||||
|
|
|
@ -16,9 +16,14 @@
|
||||||
#undef F
|
#undef F
|
||||||
#define F(sl) (sl)
|
#define F(sl) (sl)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include "../utils/dbg.h"
|
||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
#include "../defines.h"
|
#include "../defines.h"
|
||||||
|
#include "../hm/hmSystem.h"
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
|
template<class HMSYSTEM>
|
||||||
class mqtt {
|
class mqtt {
|
||||||
public:
|
public:
|
||||||
mqtt() {
|
mqtt() {
|
||||||
|
@ -89,6 +94,78 @@ class mqtt {
|
||||||
return mTxCnt;
|
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:
|
private:
|
||||||
void reconnect(void) {
|
void reconnect(void) {
|
||||||
DPRINTLN(DBG_DEBUG, F("mqtt.h:reconnect"));
|
DPRINTLN(DBG_DEBUG, F("mqtt.h:reconnect"));
|
||||||
|
|
Loading…
Add table
Reference in a new issue