Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions lib/Hoymiles/src/Hoymiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ void HoymilesClass::initCMT(const int8_t pin_sdio, const int8_t pin_clk, const i

void HoymilesClass::loop()
{
std::lock_guard<std::mutex> lock(_mutex);
_radioNrf->loop();
_radioCmt->loop();

Expand Down Expand Up @@ -147,7 +146,15 @@ void HoymilesClass::loop()
} else {
if (currentWeekDay != lastWeekDay) {

for (auto& inv : _inverters) {
// Iterate over a snapshot: the inverter list may be modified
// concurrently (e.g. by a Web API request) while the daily
// tasks are being executed.
std::vector<std::shared_ptr<InverterAbstract>> inverters;
{
std::shared_lock<std::shared_mutex> lock(_mutex);
inverters = _inverters;
}
for (auto& inv : inverters) {
inv->performDailyTask();
}

Expand Down Expand Up @@ -188,6 +195,7 @@ std::shared_ptr<InverterAbstract> HoymilesClass::addInverter(const char* name, c
if (i) {
i->setName(name);
i->init();
std::lock_guard<std::shared_mutex> lock(_mutex);
_inverters.push_back(std::move(i));
return _inverters.back();
}
Expand All @@ -197,6 +205,7 @@ std::shared_ptr<InverterAbstract> HoymilesClass::addInverter(const char* name, c

std::shared_ptr<InverterAbstract> HoymilesClass::getInverterByPos(const uint8_t pos)
{
std::shared_lock<std::shared_mutex> lock(_mutex);
if (pos >= _inverters.size()) {
return nullptr;
} else {
Expand All @@ -206,6 +215,7 @@ std::shared_ptr<InverterAbstract> HoymilesClass::getInverterByPos(const uint8_t

std::shared_ptr<InverterAbstract> HoymilesClass::getInverterBySerial(const uint64_t serial)
{
std::shared_lock<std::shared_mutex> lock(_mutex);
for (auto& inv : _inverters) {
if (inv->serial() == serial) {
return inv;
Expand All @@ -220,6 +230,7 @@ std::shared_ptr<InverterAbstract> HoymilesClass::getInverterByFragment(const fra
return nullptr;
}

std::shared_lock<std::shared_mutex> lock(_mutex);
for (auto& inv : _inverters) {
serial_u p;
p.u64 = inv->serial();
Expand All @@ -237,9 +248,9 @@ std::shared_ptr<InverterAbstract> HoymilesClass::getInverterByFragment(const fra

void HoymilesClass::removeInverterBySerial(const uint64_t serial)
{
std::lock_guard<std::shared_mutex> lock(_mutex);
for (uint8_t i = 0; i < _inverters.size(); i++) {
if (_inverters[i]->serial() == serial) {
std::lock_guard<std::mutex> lock(_mutex);
_inverters[i]->getRadio()->removeCommands(_inverters[i].get());
_inverters.erase(_inverters.begin() + i);
return;
Expand All @@ -249,6 +260,7 @@ void HoymilesClass::removeInverterBySerial(const uint64_t serial)

size_t HoymilesClass::getNumInverters() const
{
std::shared_lock<std::shared_mutex> lock(_mutex);
return _inverters.size();
}

Expand Down
6 changes: 5 additions & 1 deletion lib/Hoymiles/src/Hoymiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <Print.h>
#include <SPI.h>
#include <memory>
#include <shared_mutex>
#include <vector>

#define HOY_SYSTEM_CONFIG_PARA_POLL_INTERVAL (2 * 60 * 1000) // 2 minutes
Expand Down Expand Up @@ -40,7 +41,10 @@ class HoymilesClass {
std::unique_ptr<HoymilesRadio_NRF> _radioNrf;
std::unique_ptr<HoymilesRadio_CMT> _radioCmt;

std::mutex _mutex;
// Protects _inverters. Writers (adding/removing inverters, e.g. from
// the Web API task) lock exclusively, readers lock shared. Mutable so
// const accessors can lock as well.
mutable std::shared_mutex _mutex;

uint32_t _pollInterval = 0;
uint32_t _lastPoll = 0;
Expand Down
49 changes: 38 additions & 11 deletions lib/Hoymiles/src/HoymilesRadio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,15 @@ bool HoymilesRadio::checkFragmentCrc(const fragment_t& fragment) const

void HoymilesRadio::sendRetransmitPacket(const uint8_t fragment_id)
{
CommandAbstract* cmd = _commandQueue.front().get();
auto front = _commandQueue.tryFront();
if (!front.has_value()) {
// The command was removed from the queue while its transaction was
// in flight (e.g. its inverter got deleted). Abort the transaction.
_busyFlag = false;
return;
}

CommandAbstract* requestCmd = cmd->getRequestFrameCommand(fragment_id);
CommandAbstract* requestCmd = front.value().get()->getRequestFrameCommand(fragment_id);

if (requestCmd != nullptr) {
sendEsbPacket(*requestCmd);
Expand All @@ -55,8 +61,14 @@ void HoymilesRadio::sendRetransmitPacket(const uint8_t fragment_id)

void HoymilesRadio::sendLastPacketAgain()
{
CommandAbstract* cmd = _commandQueue.front().get();
sendEsbPacket(*cmd);
auto front = _commandQueue.tryFront();
if (!front.has_value()) {
// The command was removed from the queue while its transaction was
// in flight (e.g. its inverter got deleted). Abort the transaction.
_busyFlag = false;
return;
}
sendEsbPacket(*front.value().get());
}

void HoymilesRadio::handleReceivedPackage()
Expand All @@ -70,21 +82,32 @@ void HoymilesRadio::handleReceivedPackage()
// The timeout below remains as fallback for incomplete responses.
bool earlyExit = false;
if (_busyFlag && !_rxTimeout.occured() && isRxBufferEmpty()) {
std::shared_ptr<InverterAbstract> inv = Hoymiles.getInverterBySerial(_commandQueue.front().get()->getTargetAddress());
if (nullptr != inv && inv->isTransactionComplete()) {
earlyExit = true;
ESP_LOGI(TAG, "RX Early Exit");
auto front = _commandQueue.tryFront();
if (front.has_value()) {
std::shared_ptr<InverterAbstract> inv = Hoymiles.getInverterBySerial(front.value().get()->getTargetAddress());
if (nullptr != inv && inv->isTransactionComplete()) {
earlyExit = true;
ESP_LOGI(TAG, "RX Early Exit");
}
}
}

if (_busyFlag && (earlyExit || _rxTimeout.occured())) {
if (!earlyExit) {
ESP_LOGI(TAG, "RX Period End");
}
std::shared_ptr<InverterAbstract> inv = Hoymiles.getInverterBySerial(_commandQueue.front().get()->getTargetAddress());
auto front = _commandQueue.tryFront();
if (!front.has_value()) {
// The command was removed from the queue while its transaction
// was in flight (e.g. its inverter got deleted). Abort the
// transaction instead of dereferencing an empty queue.
_busyFlag = false;
return;
}
std::shared_ptr<InverterAbstract> inv = Hoymiles.getInverterBySerial(front.value().get()->getTargetAddress());

if (nullptr != inv) {
CommandAbstract* cmd = _commandQueue.front().get();
CommandAbstract* cmd = front.value().get();
uint8_t verifyResult = inv->verifyAllFragments(*cmd);
if (verifyResult == FRAGMENT_ALL_MISSING_RESEND) {
ESP_LOGW(TAG, "Nothing received, resend whole request");
Expand Down Expand Up @@ -149,7 +172,11 @@ void HoymilesRadio::handleReceivedPackage()
} else if (!_busyFlag) {
// Currently in idle mode --> send packet if one is in the queue
if (!isQueueEmpty()) {
CommandAbstract* cmd = _commandQueue.front().get();
auto front = _commandQueue.tryFront();
if (!front.has_value()) {
return;
}
CommandAbstract* cmd = front.value().get();

auto inv = Hoymiles.getInverterBySerial(cmd->getTargetAddress());
if (nullptr != inv) {
Expand Down
9 changes: 8 additions & 1 deletion lib/ThreadSafeQueue/src/ThreadSafeQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,16 @@ class ThreadSafeQueue {
_queue.push_back(item);
}

T front()
// Returns the front element, or an empty optional if the queue is
// empty. Checking emptiness and fetching the element is atomic with
// respect to other queue operations, so callers cannot race with a
// concurrent pop()/erase() between the emptiness check and the fetch.
std::optional<T> tryFront()
{
std::lock_guard<std::mutex> lock(_mutex);
if (_queue.empty()) {
return {};
}
return _queue.front();
}

Expand Down
3 changes: 3 additions & 0 deletions src/MqttHandleHass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ void MqttHandleHassClass::publishConfig()
// Loop all inverters
for (uint8_t i = 0; i < Hoymiles.getNumInverters(); i++) {
auto inv = Hoymiles.getInverterByPos(i);
if (inv == nullptr) {
continue;
}
yield();

publishInverterButton(inv, "Turn Inverter Off", "cmd/power", "0", "mdi:power-plug-off", DEVICE_CLS_NONE, STATE_CLS_NONE, CATEGORY_CONFIG);
Expand Down
3 changes: 3 additions & 0 deletions src/MqttHandleInverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ void MqttHandleInverterClass::loop()
// Loop all inverters
for (uint8_t i = 0; i < Hoymiles.getNumInverters(); i++) {
auto inv = Hoymiles.getInverterByPos(i);
if (inv == nullptr) {
continue;
}

const String subtopic = inv->serialString();

Expand Down
3 changes: 3 additions & 0 deletions src/WebApi_limit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ void WebApiLimitClass::onLimitStatus(AsyncWebServerRequest* request)

for (uint8_t i = 0; i < Hoymiles.getNumInverters(); i++) {
auto inv = Hoymiles.getInverterByPos(i);
if (inv == nullptr) {
continue;
}

String serial = inv->serialString();

Expand Down
3 changes: 3 additions & 0 deletions src/WebApi_power.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ void WebApiPowerClass::onPowerStatus(AsyncWebServerRequest* request)

for (uint8_t i = 0; i < Hoymiles.getNumInverters(); i++) {
auto inv = Hoymiles.getInverterByPos(i);
if (inv == nullptr) {
continue;
}

LastCommandSuccess status = inv->PowerCommand()->getLastPowerCommandSuccess();
root[inv->serialString()]["power_set_status"] = WebApi.formatCommandStatus(status);
Expand Down
3 changes: 3 additions & 0 deletions src/WebApi_prometheus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ void WebApiPrometheusClass::onPrometheusMetricsGet(AsyncWebServerRequest* reques

for (uint8_t i = 0; i < Hoymiles.getNumInverters(); i++) {
auto inv = Hoymiles.getInverterByPos(i);
if (inv == nullptr) {
continue;
}

String serial = inv->serialString();
const char* name = inv->name();
Expand Down
Loading