replace VICTRON_MAX_COUNT
determine the amount of controllers actually in use dynamically, especially to avoid indices which are invalid, causing an error to be printed, even though the user did not do anything wrong.
This commit is contained in:
parent
8e5e8d169d
commit
b449dd1196
@ -30,8 +30,6 @@
|
|||||||
|
|
||||||
#define DEV_MAX_MAPPING_NAME_STRLEN 63
|
#define DEV_MAX_MAPPING_NAME_STRLEN 63
|
||||||
|
|
||||||
#define VICTRON_MAX_COUNT 2
|
|
||||||
|
|
||||||
#define POWERMETER_MAX_PHASES 3
|
#define POWERMETER_MAX_PHASES 3
|
||||||
#define POWERMETER_MAX_HTTP_URL_STRLEN 1024
|
#define POWERMETER_MAX_HTTP_URL_STRLEN 1024
|
||||||
#define POWERMETER_MAX_USERNAME_STRLEN 64
|
#define POWERMETER_MAX_USERNAME_STRLEN 64
|
||||||
|
|||||||
@ -24,6 +24,7 @@ public:
|
|||||||
uint32_t getDataAgeMillis() const;
|
uint32_t getDataAgeMillis() const;
|
||||||
uint32_t getDataAgeMillis(size_t idx) const;
|
uint32_t getDataAgeMillis(size_t idx) const;
|
||||||
|
|
||||||
|
size_t controllerAmount() const { return _controllers.size(); }
|
||||||
std::optional<VeDirectMpptController::spData_t> getData(size_t idx = 0) const;
|
std::optional<VeDirectMpptController::spData_t> getData(size_t idx = 0) const;
|
||||||
|
|
||||||
// total output of all MPPT charge controllers in Watts
|
// total output of all MPPT charge controllers in Watts
|
||||||
|
|||||||
@ -25,7 +25,7 @@ private:
|
|||||||
|
|
||||||
uint32_t _lastFullPublish = 0;
|
uint32_t _lastFullPublish = 0;
|
||||||
uint32_t _lastPublish = 0;
|
uint32_t _lastPublish = 0;
|
||||||
static constexpr uint16_t _responseSize = VICTRON_MAX_COUNT * (1024 + 128);
|
uint16_t responseSize() const;
|
||||||
|
|
||||||
std::mutex _mutex;
|
std::mutex _mutex;
|
||||||
|
|
||||||
|
|||||||
@ -57,7 +57,7 @@ void MqttHandleVedirectHassClass::publishConfig()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// device info
|
// device info
|
||||||
for (int idx = 0; idx < VICTRON_MAX_COUNT; ++idx) {
|
for (int idx = 0; idx < VictronMppt.controllerAmount(); ++idx) {
|
||||||
// ensure data is received from victron
|
// ensure data is received from victron
|
||||||
if (!VictronMppt.isDataValid(idx)) {
|
if (!VictronMppt.isDataValid(idx)) {
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@ -58,7 +58,7 @@ void MqttHandleVedirectClass::loop()
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
for (int idx = 0; idx < VICTRON_MAX_COUNT; ++idx) {
|
for (int idx = 0; idx < VictronMppt.controllerAmount(); ++idx) {
|
||||||
if (!VictronMppt.isDataValid(idx)) {
|
if (!VictronMppt.isDataValid(idx)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -60,6 +60,11 @@ bool WebApiWsVedirectLiveClass::hasUpdate(size_t idx)
|
|||||||
return dataAgeMillis < publishAgeMillis;
|
return dataAgeMillis < publishAgeMillis;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint16_t WebApiWsVedirectLiveClass::responseSize() const
|
||||||
|
{
|
||||||
|
return VictronMppt.controllerAmount() * (1024 + 128);
|
||||||
|
}
|
||||||
|
|
||||||
void WebApiWsVedirectLiveClass::sendDataTaskCb()
|
void WebApiWsVedirectLiveClass::sendDataTaskCb()
|
||||||
{
|
{
|
||||||
// do nothing if no WS client is connected
|
// do nothing if no WS client is connected
|
||||||
@ -69,7 +74,7 @@ void WebApiWsVedirectLiveClass::sendDataTaskCb()
|
|||||||
bool fullUpdate = (millis() - _lastFullPublish > (10 * 1000));
|
bool fullUpdate = (millis() - _lastFullPublish > (10 * 1000));
|
||||||
bool updateAvailable = false;
|
bool updateAvailable = false;
|
||||||
if (!fullUpdate) {
|
if (!fullUpdate) {
|
||||||
for (int idx = 0; idx < VICTRON_MAX_COUNT; ++idx) {
|
for (size_t idx = 0; idx < VictronMppt.controllerAmount(); ++idx) {
|
||||||
if (hasUpdate(idx)) {
|
if (hasUpdate(idx)) {
|
||||||
updateAvailable = true;
|
updateAvailable = true;
|
||||||
break;
|
break;
|
||||||
@ -80,7 +85,7 @@ void WebApiWsVedirectLiveClass::sendDataTaskCb()
|
|||||||
if (fullUpdate || updateAvailable) {
|
if (fullUpdate || updateAvailable) {
|
||||||
try {
|
try {
|
||||||
std::lock_guard<std::mutex> lock(_mutex);
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
DynamicJsonDocument root(_responseSize);
|
DynamicJsonDocument root(responseSize());
|
||||||
if (Utils::checkJsonAlloc(root, __FUNCTION__, __LINE__)) {
|
if (Utils::checkJsonAlloc(root, __FUNCTION__, __LINE__)) {
|
||||||
JsonVariant var = root;
|
JsonVariant var = root;
|
||||||
generateJsonResponse(var, fullUpdate);
|
generateJsonResponse(var, fullUpdate);
|
||||||
@ -114,7 +119,7 @@ void WebApiWsVedirectLiveClass::generateJsonResponse(JsonVariant& root, bool ful
|
|||||||
const JsonObject &array = root["vedirect"].createNestedObject("instances");
|
const JsonObject &array = root["vedirect"].createNestedObject("instances");
|
||||||
root["vedirect"]["full_update"] = fullUpdate;
|
root["vedirect"]["full_update"] = fullUpdate;
|
||||||
|
|
||||||
for (int idx = 0; idx < VICTRON_MAX_COUNT; ++idx) {
|
for (size_t idx = 0; idx < VictronMppt.controllerAmount(); ++idx) {
|
||||||
std::optional<VeDirectMpptController::spData_t> spOptMpptData = VictronMppt.getData(idx);
|
std::optional<VeDirectMpptController::spData_t> spOptMpptData = VictronMppt.getData(idx);
|
||||||
if (!spOptMpptData.has_value()) {
|
if (!spOptMpptData.has_value()) {
|
||||||
continue;
|
continue;
|
||||||
@ -213,7 +218,7 @@ void WebApiWsVedirectLiveClass::onLivedataStatus(AsyncWebServerRequest* request)
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
std::lock_guard<std::mutex> lock(_mutex);
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
AsyncJsonResponse* response = new AsyncJsonResponse(false, _responseSize);
|
AsyncJsonResponse* response = new AsyncJsonResponse(false, responseSize());
|
||||||
auto& root = response->getRoot();
|
auto& root = response->getRoot();
|
||||||
|
|
||||||
generateJsonResponse(root, true/*fullUpdate*/);
|
generateJsonResponse(root, true/*fullUpdate*/);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user