* Optimize Sun data calculation * Remove not required enum * Split config struct into different sub structs * Feature: Allow configuration of LWT QoS * Made resetreason methods static * Feature: Implement offset cache for "YieldDay" Thanks to @broth-itk for the idea! Fix: #1258 #1397 * Add Esp32-Stick-PoE-A * remove broken LilyGO_T_ETH_POE config, use device profile instead * Feature: High resolution Icon and PWA (Progressive Web App) functionality Fix: #1289 * webapp: Update dependencies * Initialize TaskScheduler * Migrate SunPosition to TaskScheduler * Migrate Datastore to TaskScheduler * Migrate MqttHandleInverterTotal to TaskSchedule * Migrate MqttHandleHass to TaskScheduler * Migrate MqttHandleDtu to TaskScheduler * Migrate MqttHandleInverter to TaskScheduler * Migrate LedSingle to TaskScheduler * Migrate NetworkSettings to TaskScheduler * Migrate InverterSettings to TaskScheduler * Migrate MessageOutput to TaskScheduler * Migrate Display_Graphic to TaskScheduler * Migrate WebApi to TaskScheduler * Split InverterSettings into multiple tasks * Calculate SunPosition only every 5 seconds * Split LedSingle into multiple tasks * Upgrade espMqttClient from 1.4.5 to 1.5.0 * Doc: Correct amount of MPP-Tracker * Added HMT-1600-4T and HMT-1800-4T to DevInfoParser Fix #1524 * Adjusted inverter names for HMS-1600/1800/2000-4T * Add channel count to description of detected inverter type (DevInfoParser) * Adjust device web api endpoint for dynamic led count * Feature: Added ability to change the brightness of the LEDs Based on the idea of @moritzlerch with several modifications like pwmTable and structure * webapp: Update dependencies * Update olikraus/U8g2 from 2.35.7 to 2.35.8 * Remove not required onWebsocketEvent * Remove code nesting * Introduce several const statements * Remove not required AsyncEventSource * Doc: Added byte specification to each command * Feature: Added basic Grid Profile parser which shows the used profile and version Other values are still outstanding. * Optimize AlarmLogParser to save memory * Add libfrozen to project to create constexpr maps * Feature: First version of GridProfile Parser which shows all values contained in the profile. * webapp: Update dependencies * Apply better variable names * Remove not required casts * Add additional compiler flags to prevent errors * Add const statement to several variables * Replace NULL by nullptr * Update bblanchon/ArduinoJson from 6.21.3 to 6.21.4 * Add const keyword to method parameters * Add const keyword to methods * Use references instead of pointers whenver possible * Adjust member variable names in MqttSettings * Adjust member variable names in NetworkSettings * webapp: Update timezone database to latest version * webapp: Beautify and unify form footers * Feature: Allow setting of an inverter limit of 0% and 0W Thanks to @madmartin in #1270 * Feature: Allow links in device profiles These links will be shown on the hardware settings page. * Doc: Added hint regarding HMS-xxxx-xT-NA inverters * Feature: Added DeviceProfile for CASmo-DTU Based on #1565 * Upgrade actions/upload-artifact from v3 to v4 * Upgrade actions/download-artifact from v3 to v4 * webapp: add app.js.gz * Gridprofileparser: Added latest known values Thanks to @stefan123t and @noone2k * webapp: Fix lint errors * Feature: Add DTU to Home Assistant Auto Discovery This is based on PR 1365 from @CFenner with several fixes and optimizations * Fix: Remove debug output as it floods the console * Fix: Gridprofileparser: Add additional error handling if profile is unknown * webapp: add app.js.gz * Fix: Offset cache for "YieldDay" did not work correctly * webapp: update dependencies * webapp: add app.js.gz * Fix: yarn.lock was outdated * Fix: yarn build error * Fix: Reset Yield day correction in combination with Zero Yield Day on Midnight lead to wrong values. * Fix: Allow negative values in GridProfileParser * Correct variable name * Fix #1579: Static IP in Ethernet mode did not work correctly * Feature: Added diagram to display This is based on the idea of @Henrik-Ingenieur and was discussed in #1504 * webapp: update dependencies * webapp: add app.js.gz --------- Co-authored-by: Thomas Basler <thomas@familie-basler.net> Co-authored-by: Pierre Kancir <pierre.kancir.emn@gmail.com>
249 lines
10 KiB
C++
249 lines
10 KiB
C++
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
/*
|
|
* Copyright (C) 2022-2023 Thomas Basler and others
|
|
*/
|
|
#include "MqttHandleInverter.h"
|
|
#include "MessageOutput.h"
|
|
#include "MqttSettings.h"
|
|
#include <ctime>
|
|
|
|
#define TOPIC_SUB_LIMIT_PERSISTENT_RELATIVE "limit_persistent_relative"
|
|
#define TOPIC_SUB_LIMIT_PERSISTENT_ABSOLUTE "limit_persistent_absolute"
|
|
#define TOPIC_SUB_LIMIT_NONPERSISTENT_RELATIVE "limit_nonpersistent_relative"
|
|
#define TOPIC_SUB_LIMIT_NONPERSISTENT_ABSOLUTE "limit_nonpersistent_absolute"
|
|
#define TOPIC_SUB_POWER "power"
|
|
#define TOPIC_SUB_RESTART "restart"
|
|
|
|
#define PUBLISH_MAX_INTERVAL 60000
|
|
|
|
MqttHandleInverterClass MqttHandleInverter;
|
|
|
|
void MqttHandleInverterClass::init(Scheduler& scheduler)
|
|
{
|
|
using std::placeholders::_1;
|
|
using std::placeholders::_2;
|
|
using std::placeholders::_3;
|
|
using std::placeholders::_4;
|
|
using std::placeholders::_5;
|
|
using std::placeholders::_6;
|
|
|
|
const String topic = MqttSettings.getPrefix();
|
|
MqttSettings.subscribe(String(topic + "+/cmd/" + TOPIC_SUB_LIMIT_PERSISTENT_RELATIVE).c_str(), 0, std::bind(&MqttHandleInverterClass::onMqttMessage, this, _1, _2, _3, _4, _5, _6));
|
|
MqttSettings.subscribe(String(topic + "+/cmd/" + TOPIC_SUB_LIMIT_PERSISTENT_ABSOLUTE).c_str(), 0, std::bind(&MqttHandleInverterClass::onMqttMessage, this, _1, _2, _3, _4, _5, _6));
|
|
MqttSettings.subscribe(String(topic + "+/cmd/" + TOPIC_SUB_LIMIT_NONPERSISTENT_RELATIVE).c_str(), 0, std::bind(&MqttHandleInverterClass::onMqttMessage, this, _1, _2, _3, _4, _5, _6));
|
|
MqttSettings.subscribe(String(topic + "+/cmd/" + TOPIC_SUB_LIMIT_NONPERSISTENT_ABSOLUTE).c_str(), 0, std::bind(&MqttHandleInverterClass::onMqttMessage, this, _1, _2, _3, _4, _5, _6));
|
|
MqttSettings.subscribe(String(topic + "+/cmd/" + TOPIC_SUB_POWER).c_str(), 0, std::bind(&MqttHandleInverterClass::onMqttMessage, this, _1, _2, _3, _4, _5, _6));
|
|
MqttSettings.subscribe(String(topic + "+/cmd/" + TOPIC_SUB_RESTART).c_str(), 0, std::bind(&MqttHandleInverterClass::onMqttMessage, this, _1, _2, _3, _4, _5, _6));
|
|
|
|
scheduler.addTask(_loopTask);
|
|
_loopTask.setCallback(std::bind(&MqttHandleInverterClass::loop, this));
|
|
_loopTask.setIterations(TASK_FOREVER);
|
|
_loopTask.setInterval(Configuration.get().Mqtt.PublishInterval * TASK_SECOND);
|
|
_loopTask.enable();
|
|
}
|
|
|
|
void MqttHandleInverterClass::loop()
|
|
{
|
|
_loopTask.setInterval(Configuration.get().Mqtt.PublishInterval * TASK_SECOND);
|
|
|
|
if (!MqttSettings.getConnected() || !Hoymiles.isAllRadioIdle()) {
|
|
_loopTask.forceNextIteration();
|
|
return;
|
|
}
|
|
|
|
// Loop all inverters
|
|
for (uint8_t i = 0; i < Hoymiles.getNumInverters(); i++) {
|
|
auto inv = Hoymiles.getInverterByPos(i);
|
|
|
|
const String subtopic = inv->serialString();
|
|
|
|
// Name
|
|
MqttSettings.publish(subtopic + "/name", inv->name());
|
|
|
|
if (inv->DevInfo()->getLastUpdate() > 0) {
|
|
// Bootloader Version
|
|
MqttSettings.publish(subtopic + "/device/bootloaderversion", String(inv->DevInfo()->getFwBootloaderVersion()));
|
|
|
|
// Firmware Version
|
|
MqttSettings.publish(subtopic + "/device/fwbuildversion", String(inv->DevInfo()->getFwBuildVersion()));
|
|
|
|
// Firmware Build DateTime
|
|
char timebuffer[32];
|
|
const time_t t = inv->DevInfo()->getFwBuildDateTime();
|
|
std::strftime(timebuffer, sizeof(timebuffer), "%Y-%m-%d %H:%M:%S", gmtime(&t));
|
|
MqttSettings.publish(subtopic + "/device/fwbuilddatetime", String(timebuffer));
|
|
|
|
// Hardware part number
|
|
MqttSettings.publish(subtopic + "/device/hwpartnumber", String(inv->DevInfo()->getHwPartNumber()));
|
|
|
|
// Hardware version
|
|
MqttSettings.publish(subtopic + "/device/hwversion", inv->DevInfo()->getHwVersion());
|
|
}
|
|
|
|
if (inv->SystemConfigPara()->getLastUpdate() > 0) {
|
|
// Limit
|
|
MqttSettings.publish(subtopic + "/status/limit_relative", String(inv->SystemConfigPara()->getLimitPercent()));
|
|
|
|
uint16_t maxpower = inv->DevInfo()->getMaxPower();
|
|
if (maxpower > 0) {
|
|
MqttSettings.publish(subtopic + "/status/limit_absolute", String(inv->SystemConfigPara()->getLimitPercent() * maxpower / 100));
|
|
}
|
|
}
|
|
|
|
MqttSettings.publish(subtopic + "/status/reachable", String(inv->isReachable()));
|
|
MqttSettings.publish(subtopic + "/status/producing", String(inv->isProducing()));
|
|
|
|
if (inv->Statistics()->getLastUpdate() > 0) {
|
|
MqttSettings.publish(subtopic + "/status/last_update", String(std::time(0) - (millis() - inv->Statistics()->getLastUpdate()) / 1000));
|
|
} else {
|
|
MqttSettings.publish(subtopic + "/status/last_update", String(0));
|
|
}
|
|
|
|
const uint32_t lastUpdateInternal = inv->Statistics()->getLastUpdateFromInternal();
|
|
if (inv->Statistics()->getLastUpdate() > 0 && (lastUpdateInternal != _lastPublishStats[i])) {
|
|
_lastPublishStats[i] = lastUpdateInternal;
|
|
|
|
// Loop all channels
|
|
for (auto& t : inv->Statistics()->getChannelTypes()) {
|
|
for (auto& c : inv->Statistics()->getChannelsByType(t)) {
|
|
if (t == TYPE_DC) {
|
|
INVERTER_CONFIG_T* inv_cfg = Configuration.getInverterConfig(inv->serial());
|
|
if (inv_cfg != nullptr) {
|
|
// TODO(tbnobody)
|
|
MqttSettings.publish(inv->serialString() + "/" + String(static_cast<uint8_t>(c) + 1) + "/name", inv_cfg->channel[c].Name);
|
|
}
|
|
}
|
|
for (uint8_t f = 0; f < sizeof(_publishFields) / sizeof(FieldId_t); f++) {
|
|
publishField(inv, t, c, _publishFields[f]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
yield();
|
|
}
|
|
}
|
|
|
|
void MqttHandleInverterClass::publishField(std::shared_ptr<InverterAbstract> inv, const ChannelType_t type, const ChannelNum_t channel, const FieldId_t fieldId)
|
|
{
|
|
const String topic = getTopic(inv, type, channel, fieldId);
|
|
if (topic == "") {
|
|
return;
|
|
}
|
|
|
|
MqttSettings.publish(topic, inv->Statistics()->getChannelFieldValueString(type, channel, fieldId));
|
|
}
|
|
|
|
String MqttHandleInverterClass::getTopic(std::shared_ptr<InverterAbstract> inv, const ChannelType_t type, const ChannelNum_t channel, const FieldId_t fieldId)
|
|
{
|
|
if (!inv->Statistics()->hasChannelFieldValue(type, channel, fieldId)) {
|
|
return "";
|
|
}
|
|
|
|
String chanName;
|
|
if (type == TYPE_AC && fieldId == FLD_PDC) {
|
|
chanName = "powerdc";
|
|
} else {
|
|
chanName = inv->Statistics()->getChannelFieldName(type, channel, fieldId);
|
|
chanName.toLowerCase();
|
|
}
|
|
|
|
String chanNum;
|
|
if (type == TYPE_DC) {
|
|
// TODO(tbnobody)
|
|
chanNum = static_cast<uint8_t>(channel) + 1;
|
|
} else {
|
|
chanNum = channel;
|
|
}
|
|
|
|
return inv->serialString() + "/" + chanNum + "/" + chanName;
|
|
}
|
|
|
|
void MqttHandleInverterClass::onMqttMessage(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, const size_t len, const size_t index, const size_t total)
|
|
{
|
|
const CONFIG_T& config = Configuration.get();
|
|
|
|
char token_topic[MQTT_MAX_TOPIC_STRLEN + 40]; // respect all subtopics
|
|
strncpy(token_topic, topic, MQTT_MAX_TOPIC_STRLEN + 40); // convert const char* to char*
|
|
|
|
char* serial_str;
|
|
char* subtopic;
|
|
char* setting;
|
|
char* rest = &token_topic[strlen(config.Mqtt.Topic)];
|
|
|
|
serial_str = strtok_r(rest, "/", &rest);
|
|
subtopic = strtok_r(rest, "/", &rest);
|
|
setting = strtok_r(rest, "/", &rest);
|
|
|
|
if (serial_str == NULL || subtopic == NULL || setting == NULL) {
|
|
return;
|
|
}
|
|
|
|
const uint64_t serial = strtoull(serial_str, 0, 16);
|
|
|
|
auto inv = Hoymiles.getInverterBySerial(serial);
|
|
|
|
if (inv == nullptr) {
|
|
MessageOutput.println("Inverter not found");
|
|
return;
|
|
}
|
|
|
|
// check if subtopic is unequal cmd
|
|
if (strcmp(subtopic, "cmd")) {
|
|
return;
|
|
}
|
|
|
|
char* strlimit = new char[len + 1];
|
|
memcpy(strlimit, payload, len);
|
|
strlimit[len] = '\0';
|
|
const int32_t payload_val = strtol(strlimit, NULL, 10);
|
|
delete[] strlimit;
|
|
|
|
if (payload_val < 0) {
|
|
MessageOutput.printf("MQTT payload < 0 received --> ignoring\r\n");
|
|
return;
|
|
}
|
|
|
|
if (!strcmp(setting, TOPIC_SUB_LIMIT_PERSISTENT_RELATIVE)) {
|
|
// Set inverter limit relative persistent
|
|
MessageOutput.printf("Limit Persistent: %d %%\r\n", payload_val);
|
|
inv->sendActivePowerControlRequest(payload_val, PowerLimitControlType::RelativPersistent);
|
|
|
|
} else if (!strcmp(setting, TOPIC_SUB_LIMIT_PERSISTENT_ABSOLUTE)) {
|
|
// Set inverter limit absolute persistent
|
|
MessageOutput.printf("Limit Persistent: %d W\r\n", payload_val);
|
|
inv->sendActivePowerControlRequest(payload_val, PowerLimitControlType::AbsolutPersistent);
|
|
|
|
} else if (!strcmp(setting, TOPIC_SUB_LIMIT_NONPERSISTENT_RELATIVE)) {
|
|
// Set inverter limit relative non persistent
|
|
MessageOutput.printf("Limit Non-Persistent: %d %%\r\n", payload_val);
|
|
if (!properties.retain) {
|
|
inv->sendActivePowerControlRequest(payload_val, PowerLimitControlType::RelativNonPersistent);
|
|
} else {
|
|
MessageOutput.println("Ignored because retained");
|
|
}
|
|
|
|
} else if (!strcmp(setting, TOPIC_SUB_LIMIT_NONPERSISTENT_ABSOLUTE)) {
|
|
// Set inverter limit absolute non persistent
|
|
MessageOutput.printf("Limit Non-Persistent: %d W\r\n", payload_val);
|
|
if (!properties.retain) {
|
|
inv->sendActivePowerControlRequest(payload_val, PowerLimitControlType::AbsolutNonPersistent);
|
|
} else {
|
|
MessageOutput.println("Ignored because retained");
|
|
}
|
|
|
|
} else if (!strcmp(setting, TOPIC_SUB_POWER)) {
|
|
// Turn inverter on or off
|
|
MessageOutput.printf("Set inverter power to: %d\r\n", payload_val);
|
|
inv->sendPowerControlRequest(payload_val > 0);
|
|
|
|
} else if (!strcmp(setting, TOPIC_SUB_RESTART)) {
|
|
// Restart inverter
|
|
MessageOutput.printf("Restart inverter\r\n");
|
|
if (!properties.retain && payload_val == 1) {
|
|
inv->sendRestartControlRequest();
|
|
} else {
|
|
MessageOutput.println("Ignored because retained");
|
|
}
|
|
}
|
|
} |