OpenDTU-old/lib/Hoymiles/src/Hoymiles.cpp
helgeerbe d494810975
merge V23.12.16 (#556)
* 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>
2023-12-27 11:49:57 +01:00

295 lines
8.9 KiB
C++

// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2022-2023 Thomas Basler and others
*/
#include "Hoymiles.h"
#include "Utils.h"
#include "inverters/HMS_1CH.h"
#include "inverters/HMS_1CHv2.h"
#include "inverters/HMS_2CH.h"
#include "inverters/HMS_4CH.h"
#include "inverters/HMT_4CH.h"
#include "inverters/HMT_6CH.h"
#include "inverters/HM_1CH.h"
#include "inverters/HM_2CH.h"
#include "inverters/HM_4CH.h"
#include <Arduino.h>
HoymilesClass Hoymiles;
void HoymilesClass::init()
{
_pollInterval = 0;
_radioNrf.reset(new HoymilesRadio_NRF());
_radioCmt.reset(new HoymilesRadio_CMT());
}
void HoymilesClass::initNRF(SPIClass* initialisedSpiBus, const uint8_t pinCE, const uint8_t pinIRQ)
{
_radioNrf->init(initialisedSpiBus, pinCE, pinIRQ);
}
void HoymilesClass::initCMT(const int8_t pin_sdio, const int8_t pin_clk, const int8_t pin_cs, const int8_t pin_fcs, const int8_t pin_gpio2, const int8_t pin_gpio3)
{
_radioCmt->init(pin_sdio, pin_clk, pin_cs, pin_fcs, pin_gpio2, pin_gpio3);
}
void HoymilesClass::loop()
{
std::lock_guard<std::mutex> lock(_mutex);
_radioNrf->loop();
_radioCmt->loop();
if (getNumInverters() == 0) {
return;
}
if (millis() - _lastPoll > (_pollInterval * 1000)) {
static uint8_t inverterPos = 0;
std::shared_ptr<InverterAbstract> iv = getInverterByPos(inverterPos);
if ((iv == nullptr) || ((iv != nullptr) && (!iv->getRadio()->isInitialized()))) {
if (++inverterPos >= getNumInverters()) {
inverterPos = 0;
}
}
if (iv != nullptr && iv->getRadio()->isInitialized() && iv->getRadio()->isQueueEmpty()) {
if (iv->getZeroValuesIfUnreachable() && !iv->isReachable()) {
iv->Statistics()->zeroRuntimeData();
}
if (iv->getEnablePolling() || iv->getEnableCommands()) {
_messageOutput->print("Fetch inverter: ");
_messageOutput->println(iv->serial(), HEX);
if (!iv->isReachable()) {
iv->sendChangeChannelRequest();
}
iv->sendStatsRequest();
// Fetch event log
const bool force = iv->EventLog()->getLastAlarmRequestSuccess() == CMD_NOK;
iv->sendAlarmLogRequest(force);
// Fetch limit
if (((millis() - iv->SystemConfigPara()->getLastUpdateRequest() > HOY_SYSTEM_CONFIG_PARA_POLL_INTERVAL)
&& (millis() - iv->SystemConfigPara()->getLastUpdateCommand() > HOY_SYSTEM_CONFIG_PARA_POLL_MIN_DURATION))) {
_messageOutput->println("Request SystemConfigPara");
iv->sendSystemConfigParaRequest();
}
// Set limit if required
if (iv->SystemConfigPara()->getLastLimitCommandSuccess() == CMD_NOK) {
_messageOutput->println("Resend ActivePowerControl");
iv->resendActivePowerControlRequest();
}
// Set power status if required
if (iv->PowerCommand()->getLastPowerCommandSuccess() == CMD_NOK) {
_messageOutput->println("Resend PowerCommand");
iv->resendPowerControlRequest();
}
// Fetch dev info (but first fetch stats)
if (iv->Statistics()->getLastUpdate() > 0) {
const bool invalidDevInfo = !iv->DevInfo()->containsValidData()
&& iv->DevInfo()->getLastUpdateAll() > 0
&& iv->DevInfo()->getLastUpdateSimple() > 0;
if (invalidDevInfo) {
_messageOutput->println("DevInfo: No Valid Data");
}
if ((iv->DevInfo()->getLastUpdateAll() == 0)
|| (iv->DevInfo()->getLastUpdateSimple() == 0)
|| invalidDevInfo) {
_messageOutput->println("Request device info");
iv->sendDevInfoRequest();
}
}
// Fetch grid profile
if (iv->Statistics()->getLastUpdate() > 0 && iv->GridProfile()->getLastUpdate() == 0) {
iv->sendGridOnProFileParaRequest();
}
_lastPoll = millis();
}
if (++inverterPos >= getNumInverters()) {
inverterPos = 0;
}
}
// Perform housekeeping of all inverters on day change
const int8_t currentWeekDay = Utils::getWeekDay();
static int8_t lastWeekDay = -1;
if (lastWeekDay == -1) {
lastWeekDay = currentWeekDay;
} else {
if (currentWeekDay != lastWeekDay) {
for (auto& inv : _inverters) {
// Have to reset the offets first, otherwise it will
// Substract the offset from zero which leads to a high value
inv->Statistics()->resetYieldDayCorrection();
if (inv->getZeroYieldDayOnMidnight()) {
inv->Statistics()->zeroDailyData();
}
}
lastWeekDay = currentWeekDay;
}
}
}
}
std::shared_ptr<InverterAbstract> HoymilesClass::addInverter(const char* name, const uint64_t serial)
{
std::shared_ptr<InverterAbstract> i = nullptr;
if (HMT_4CH::isValidSerial(serial)) {
i = std::make_shared<HMT_4CH>(_radioCmt.get(), serial);
} else if (HMT_6CH::isValidSerial(serial)) {
i = std::make_shared<HMT_6CH>(_radioCmt.get(), serial);
} else if (HMS_4CH::isValidSerial(serial)) {
i = std::make_shared<HMS_4CH>(_radioCmt.get(), serial);
} else if (HMS_2CH::isValidSerial(serial)) {
i = std::make_shared<HMS_2CH>(_radioCmt.get(), serial);
} else if (HMS_1CH::isValidSerial(serial)) {
i = std::make_shared<HMS_1CH>(_radioCmt.get(), serial);
} else if (HMS_1CHv2::isValidSerial(serial)) {
i = std::make_shared<HMS_1CHv2>(_radioCmt.get(), serial);
} else if (HM_4CH::isValidSerial(serial)) {
i = std::make_shared<HM_4CH>(_radioNrf.get(), serial);
} else if (HM_2CH::isValidSerial(serial)) {
i = std::make_shared<HM_2CH>(_radioNrf.get(), serial);
} else if (HM_1CH::isValidSerial(serial)) {
i = std::make_shared<HM_1CH>(_radioNrf.get(), serial);
}
if (i) {
i->setName(name);
i->init();
_inverters.push_back(std::move(i));
return _inverters.back();
}
return nullptr;
}
std::shared_ptr<InverterAbstract> HoymilesClass::getInverterByPos(const uint8_t pos)
{
if (pos >= _inverters.size()) {
return nullptr;
} else {
return _inverters[pos];
}
}
std::shared_ptr<InverterAbstract> HoymilesClass::getInverterBySerial(const uint64_t serial)
{
for (uint8_t i = 0; i < _inverters.size(); i++) {
if (_inverters[i]->serial() == serial) {
return _inverters[i];
}
}
return nullptr;
}
std::shared_ptr<InverterAbstract> HoymilesClass::getInverterByFragment(const fragment_t& fragment)
{
if (fragment.len <= 4) {
return nullptr;
}
std::shared_ptr<InverterAbstract> inv;
for (uint8_t i = 0; i < _inverters.size(); i++) {
inv = _inverters[i];
serial_u p;
p.u64 = inv->serial();
if ((p.b[3] == fragment.fragment[1])
&& (p.b[2] == fragment.fragment[2])
&& (p.b[1] == fragment.fragment[3])
&& (p.b[0] == fragment.fragment[4])) {
return inv;
}
}
return nullptr;
}
void HoymilesClass::removeInverterBySerial(const uint64_t serial)
{
for (uint8_t i = 0; i < _inverters.size(); i++) {
if (_inverters[i]->serial() == serial) {
std::lock_guard<std::mutex> lock(_mutex);
_inverters.erase(_inverters.begin() + i);
return;
}
}
}
size_t HoymilesClass::getNumInverters() const
{
return _inverters.size();
}
HoymilesRadio_NRF* HoymilesClass::getRadioNrf()
{
return _radioNrf.get();
}
HoymilesRadio_CMT* HoymilesClass::getRadioCmt()
{
return _radioCmt.get();
}
bool HoymilesClass::isAllRadioIdle() const
{
return _radioNrf.get()->isIdle() && _radioCmt.get()->isIdle();
}
uint32_t HoymilesClass::PollInterval() const
{
return _pollInterval;
}
void HoymilesClass::setPollInterval(const uint32_t interval)
{
_pollInterval = interval;
}
void HoymilesClass::setVerboseLogging(bool verboseLogging)
{
_verboseLogging = verboseLogging;
}
void HoymilesClass::setMessageOutput(Print* output)
{
_messageOutput = output;
}
Print* HoymilesClass::getMessageOutput()
{
return _messageOutput;
}
class Silent : public Print {
public:
size_t write(uint8_t c) final { return 0; }
};
Silent Dummy;
Print* HoymilesClass::getVerboseMessageOutput()
{
if (_verboseLogging) {
return _messageOutput;
}
return &Dummy;
}