Fix #76: Null Pointer exception when adding a inverter with unkonwn serial number

This commit is contained in:
Thomas Basler 2022-08-16 19:47:51 +02:00
parent 6d9d5a0747
commit 1456974616
3 changed files with 16 additions and 8 deletions

View File

@ -49,7 +49,7 @@ void HoymilesClass::loop()
std::shared_ptr<InverterAbstract> HoymilesClass::addInverter(const char* name, uint64_t serial) std::shared_ptr<InverterAbstract> HoymilesClass::addInverter(const char* name, uint64_t serial)
{ {
std::shared_ptr<InverterAbstract> i; std::shared_ptr<InverterAbstract> i = nullptr;
if (HM_4CH::isValidSerial(serial)) { if (HM_4CH::isValidSerial(serial)) {
i = std::make_shared<HM_4CH>(serial); i = std::make_shared<HM_4CH>(serial);
} else if (HM_2CH::isValidSerial(serial)) { } else if (HM_2CH::isValidSerial(serial)) {

View File

@ -141,9 +141,11 @@ void WebApiInverterClass::onInverterAdd(AsyncWebServerRequest* request)
auto inv = Hoymiles.addInverter(inverter->Name, inverter->Serial); auto inv = Hoymiles.addInverter(inverter->Name, inverter->Serial);
if (inv != nullptr) {
for (uint8_t c = 0; c < INV_MAX_CHAN_COUNT; c++) { for (uint8_t c = 0; c < INV_MAX_CHAN_COUNT; c++) {
inv->Statistics()->setChannelMaxPower(c, inverter->MaxChannelPower[c]); inv->Statistics()->setChannelMaxPower(c, inverter->MaxChannelPower[c]);
} }
}
MqttHassPublishing.forceUpdate(); MqttHassPublishing.forceUpdate();
} }
@ -243,15 +245,19 @@ void WebApiInverterClass::onInverterEdit(AsyncWebServerRequest* request)
std::shared_ptr<InverterAbstract> inv = Hoymiles.getInverterByPos(root[F("id")].as<uint64_t>()); std::shared_ptr<InverterAbstract> inv = Hoymiles.getInverterByPos(root[F("id")].as<uint64_t>());
if (inv->serial() != inverter.Serial) { if (inv != nullptr && inv->serial() != inverter.Serial) {
// Serial was changed // Serial was changed
Hoymiles.removeInverterByPos(root[F("id")].as<uint64_t>()); Hoymiles.removeInverterByPos(root[F("id")].as<uint64_t>());
inv = Hoymiles.addInverter(inverter.Name, inverter.Serial); inv = Hoymiles.addInverter(inverter.Name, inverter.Serial);
} else if (inv == nullptr) {
inv = Hoymiles.addInverter(inverter.Name, inverter.Serial);
} }
if (inv != nullptr) {
for (uint8_t c = 0; c < INV_MAX_CHAN_COUNT; c++) { for (uint8_t c = 0; c < INV_MAX_CHAN_COUNT; c++) {
inv->Statistics()->setChannelMaxPower(c, inverter.MaxChannelPower[c]); inv->Statistics()->setChannelMaxPower(c, inverter.MaxChannelPower[c]);
} }
}
MqttHassPublishing.forceUpdate(); MqttHassPublishing.forceUpdate();
} }

View File

@ -100,9 +100,11 @@ void setup()
config.Inverter[i].Name, config.Inverter[i].Name,
config.Inverter[i].Serial); config.Inverter[i].Serial);
if (inv != nullptr) {
for (uint8_t c = 0; c < INV_MAX_CHAN_COUNT; c++) { for (uint8_t c = 0; c < INV_MAX_CHAN_COUNT; c++) {
inv->Statistics()->setChannelMaxPower(c, config.Inverter[i].MaxChannelPower[c]); inv->Statistics()->setChannelMaxPower(c, config.Inverter[i].MaxChannelPower[c]);
} }
}
Serial.println(F(" done")); Serial.println(F(" done"));
} }
} }