mirror of
https://github.com/lumapu/ahoy.git
synced 2025-05-20 20:36:11 +02:00
another MQTT optimization (reduced lines and duplicates)
This commit is contained in:
parent
71da175fc0
commit
0ab382986b
3 changed files with 25 additions and 32 deletions
|
@ -578,7 +578,6 @@ void app::sendMqttDiscoveryConfig(void) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
void app::sendMqtt(void) {
|
void app::sendMqtt(void) {
|
||||||
mMqtt.isConnected(true); // really needed? See comment from HorstG-57 #176
|
mMqtt.isConnected(true); // really needed? See comment from HorstG-57 #176
|
||||||
|
@ -594,34 +593,32 @@ void app::sendMqtt(void) {
|
||||||
Inverter<> *iv = mSys->getInverterByPos(id);
|
Inverter<> *iv = mSys->getInverterByPos(id);
|
||||||
if (NULL != iv) {
|
if (NULL != iv) {
|
||||||
record_t<> *rec = iv->getRecordStruct(RealTimeRunData_Debug);
|
record_t<> *rec = iv->getRecordStruct(RealTimeRunData_Debug);
|
||||||
if (!iv->isAvailable(mUtcTimestamp, rec) && !iv->isProducing(mUtcTimestamp, rec)) {
|
|
||||||
|
uint8_t status = MQTT_STATUS_AVAIL_PROD;
|
||||||
|
if (!iv->isAvailable(mUtcTimestamp, rec))
|
||||||
|
status = MQTT_STATUS_NOT_AVAIL_NOT_PROD;
|
||||||
|
if (!iv->isProducing(mUtcTimestamp, rec)) {
|
||||||
|
if (MQTT_STATUS_AVAIL_PROD == status)
|
||||||
|
status = MQTT_STATUS_AVAIL_NOT_PROD;
|
||||||
|
}
|
||||||
|
|
||||||
snprintf(topic, 32 + MAX_NAME_LENGTH, "%s/available_text", iv->name);
|
snprintf(topic, 32 + MAX_NAME_LENGTH, "%s/available_text", iv->name);
|
||||||
snprintf(val, 32, DEF_MQTT_IV_MESSAGE_NOT_AVAIL_AND_NOT_PRODUCED);
|
snprintf(val, 32, "%s%s%s%s",
|
||||||
|
(MQTT_STATUS_NOT_AVAIL_NOT_PROD) ? "not " : "",
|
||||||
|
"available and ",
|
||||||
|
(MQTT_STATUS_NOT_AVAIL_NOT_PROD || MQTT_STATUS_AVAIL_NOT_PROD) ? "not " : "",
|
||||||
|
"producing"
|
||||||
|
);
|
||||||
mMqtt.sendMsg(topic, val);
|
mMqtt.sendMsg(topic, val);
|
||||||
|
|
||||||
snprintf(topic, 32 + MAX_NAME_LENGTH, "%s/available", iv->name);
|
snprintf(topic, 32 + MAX_NAME_LENGTH, "%s/available", iv->name);
|
||||||
snprintf(val, 32, "0");
|
snprintf(val, 32, "%d", status);
|
||||||
mMqtt.sendMsg(topic, val);
|
|
||||||
} else if (!iv->isProducing(mUtcTimestamp, rec)) {
|
|
||||||
snprintf(topic, 32 + MAX_NAME_LENGTH, "%s/available_text", iv->name);
|
|
||||||
snprintf(val, 32, DEF_MQTT_IV_MESSAGE_INVERTER_AVAIL_AND_NOT_PRODUCED);
|
|
||||||
mMqtt.sendMsg(topic, val);
|
|
||||||
|
|
||||||
snprintf(topic, 32 + MAX_NAME_LENGTH, "%s/available", iv->name);
|
|
||||||
snprintf(val, 32, "1");
|
|
||||||
mMqtt.sendMsg(topic, val);
|
mMqtt.sendMsg(topic, val);
|
||||||
|
|
||||||
|
if (iv->isAvailable(mUtcTimestamp, rec)) {
|
||||||
snprintf(topic, 32 + MAX_NAME_LENGTH, "%s/last_success", iv->name);
|
snprintf(topic, 32 + MAX_NAME_LENGTH, "%s/last_success", iv->name);
|
||||||
snprintf(val, 48, "%i", iv->getLastTs(rec) * 1000);
|
snprintf(val, 48, "%i", iv->getLastTs(rec) * 1000);
|
||||||
mMqtt.sendMsg(topic, val);
|
mMqtt.sendMsg(topic, val);
|
||||||
} else { // is producing and is available
|
|
||||||
snprintf(topic, 32 + MAX_NAME_LENGTH, "%s/available_text", iv->name);
|
|
||||||
snprintf(val, 32, DEF_MQTT_IV_MESSAGE_INVERTER_AVAIL_AND_PRODUCED);
|
|
||||||
mMqtt.sendMsg(topic, val);
|
|
||||||
|
|
||||||
snprintf(topic, 32 + MAX_NAME_LENGTH, "%s/available", iv->name);
|
|
||||||
snprintf(val, 32, "2");
|
|
||||||
mMqtt.sendMsg(topic, val);
|
|
||||||
|
|
||||||
for (uint8_t i = 0; i < rec->length; i++) {
|
for (uint8_t i = 0; i < rec->length; i++) {
|
||||||
snprintf(topic, 32 + MAX_NAME_LENGTH, "%s/ch%d/%s", iv->name, rec->assign[i].ch, fields[rec->assign[i].fieldId]);
|
snprintf(topic, 32 + MAX_NAME_LENGTH, "%s/ch%d/%s", iv->name, rec->assign[i].ch, fields[rec->assign[i].fieldId]);
|
||||||
|
@ -675,7 +672,6 @@ void app::sendMqtt(void) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
const char *app::getFieldDeviceClass(uint8_t fieldId) {
|
const char *app::getFieldDeviceClass(uint8_t fieldId) {
|
||||||
uint8_t pos = 0;
|
uint8_t pos = 0;
|
||||||
|
@ -686,7 +682,6 @@ const char *app::getFieldDeviceClass(uint8_t fieldId) {
|
||||||
return (pos >= DEVICE_CLS_ASSIGN_LIST_LEN) ? NULL : deviceClasses[deviceFieldAssignment[pos].deviceClsId];
|
return (pos >= DEVICE_CLS_ASSIGN_LIST_LEN) ? NULL : deviceClasses[deviceFieldAssignment[pos].deviceClsId];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
const char *app::getFieldStateClass(uint8_t fieldId) {
|
const char *app::getFieldStateClass(uint8_t fieldId) {
|
||||||
uint8_t pos = 0;
|
uint8_t pos = 0;
|
||||||
|
@ -697,7 +692,6 @@ const char *app::getFieldStateClass(uint8_t fieldId) {
|
||||||
return (pos >= DEVICE_CLS_ASSIGN_LIST_LEN) ? NULL : stateClasses[deviceFieldAssignment[pos].stateClsId];
|
return (pos >= DEVICE_CLS_ASSIGN_LIST_LEN) ? NULL : stateClasses[deviceFieldAssignment[pos].stateClsId];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
void app::resetSystem(void) {
|
void app::resetSystem(void) {
|
||||||
mUptimeSecs = 0;
|
mUptimeSecs = 0;
|
||||||
|
@ -869,7 +863,6 @@ void app::setupMqtt(void) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
void app::resetPayload(Inverter<> *iv) {
|
void app::resetPayload(Inverter<> *iv) {
|
||||||
DPRINTLN(DBG_INFO, "resetPayload: id: " + String(iv->id));
|
DPRINTLN(DBG_INFO, "resetPayload: id: " + String(iv->id));
|
||||||
|
@ -882,7 +875,6 @@ void app::resetPayload(Inverter<> *iv) {
|
||||||
mPayload[iv->id].ts = mUtcTimestamp;
|
mPayload[iv->id].ts = mUtcTimestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
void app::calculateSunriseSunset() {
|
void app::calculateSunriseSunset() {
|
||||||
// Source: https://en.wikipedia.org/wiki/Sunrise_equation#Complete_calculation_on_Earth
|
// Source: https://en.wikipedia.org/wiki/Sunrise_equation#Complete_calculation_on_Earth
|
||||||
|
|
|
@ -108,10 +108,6 @@
|
||||||
// default MQTT topic
|
// default MQTT topic
|
||||||
#define DEF_MQTT_TOPIC "inverter"
|
#define DEF_MQTT_TOPIC "inverter"
|
||||||
|
|
||||||
//default MQTT Message Inverter Status
|
|
||||||
#define DEF_MQTT_IV_MESSAGE_NOT_AVAIL_AND_NOT_PRODUCED "not available and not producing" // STATUS 0
|
|
||||||
#define DEF_MQTT_IV_MESSAGE_INVERTER_AVAIL_AND_NOT_PRODUCED "available and not producing" // STATUS 1
|
|
||||||
#define DEF_MQTT_IV_MESSAGE_INVERTER_AVAIL_AND_PRODUCED "available and producing" // STATUS 2
|
|
||||||
|
|
||||||
#if __has_include("config_override.h")
|
#if __has_include("config_override.h")
|
||||||
#include "config_override.h"
|
#include "config_override.h"
|
||||||
|
|
|
@ -68,6 +68,11 @@ typedef enum {
|
||||||
#define MIN_SEND_INTERVAL 15
|
#define MIN_SEND_INTERVAL 15
|
||||||
#define MIN_MQTT_INTERVAL 60
|
#define MIN_MQTT_INTERVAL 60
|
||||||
|
|
||||||
|
|
||||||
|
#define MQTT_STATUS_NOT_AVAIL_NOT_PROD 0
|
||||||
|
#define MQTT_STATUS_AVAIL_NOT_PROD 1
|
||||||
|
#define MQTT_STATUS_AVAIL_PROD 2
|
||||||
|
|
||||||
//-------------------------------------
|
//-------------------------------------
|
||||||
// EEPROM
|
// EEPROM
|
||||||
//-------------------------------------
|
//-------------------------------------
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue