Sma: Fix setup of monitor based sma devices

master
Simon Stürz 2025-05-22 13:43:46 +02:00
parent 8f47c12b23
commit 59f15b8fdb
1 changed files with 94 additions and 113 deletions

View File

@ -594,19 +594,27 @@ void IntegrationPluginSma::setupThing(ThingSetupInfo *info)
}
});
// Wait for the monitor to be ready
// If this is the initial setup, wait for the monitor to be reachable and make sure
// we have an IP address, otherwise let the monitor do his work
if (info->isInitialSetup()) {
// Continue with setup only if we know that the network device is reachable
if (monitor->reachable()) {
// Thing already reachable...let's continue with the setup
setupModbusSolarInverterConnection(info);
} else {
qCDebug(dcSma()) << "Waiting for the network monitor to get reachable before continue to set up the connection" << thing->name() << address.toString() << "...";
// otherwise wait until we reach the networkdevice before setting up the device
qCDebug(dcSma()) << "Network device" << thing->name() << "is not reachable yet. Continue with the setup once reachable.";
connect(monitor, &NetworkDeviceMonitor::reachableChanged, info, [=](bool reachable){
if (reachable) {
qCDebug(dcSma()) << "The monitor for thing setup" << thing->name() << "is now reachable. Continue setup...";
qCDebug(dcSma()) << "Network device" << thing->name() << "is now reachable. Continue with the setup...";
setupModbusSolarInverterConnection(info);
}
});
}
} else {
// Continue setup with monitor...
setupModbusSolarInverterConnection(info);
}
} else if (thing->thingClassId() == modbusBatteryInverterThingClassId) {
@ -645,19 +653,26 @@ void IntegrationPluginSma::setupThing(ThingSetupInfo *info)
}
});
// Wait for the monitor to be ready
// If this is the initial setup, wait for the monitor to be reachable and make sure
// we have an IP address, otherwise let the monitor do his work
if (info->isInitialSetup()) {
// Continue with setup only if we know that the network device is reachable
if (monitor->reachable()) {
// Thing already reachable...let's continue with the setup
setupModbusBatteryInverterConnection(info);
} else {
qCDebug(dcSma()) << "Waiting for the network monitor to become reachable before continue to set up the connection" << thing->name() << address.toString() << "...";
// otherwise wait until we reach the networkdevice before setting up the device
qCDebug(dcSma()) << "Network device" << thing->name() << "is not reachable yet. Continue with the setup once reachable.";
connect(monitor, &NetworkDeviceMonitor::reachableChanged, info, [=](bool reachable){
if (reachable) {
qCDebug(dcSma()) << "The monitor for thing setup" << thing->name() << "is now reachable. Continuing with setup...";
qCDebug(dcSma()) << "Network device" << thing->name() << "is now reachable. Continue with the setup...";
setupModbusBatteryInverterConnection(info);
}
});
}
} else {
// Continue setup with monitor...
setupModbusBatteryInverterConnection(info);
}
} else {
Q_ASSERT_X(false, "setupThing", QString("Unhandled thingClassId: %1").arg(thing->thingClassId().toString()).toUtf8());
@ -810,12 +825,12 @@ void IntegrationPluginSma::setupRefreshTimer()
inverter->refresh();
}
foreach (SmaSolarInverterModbusTcpConnection *connection, m_modbusSolarInverters) {
foreach (SmaSolarInverterModbusTcpConnection *connection, m_modbusSolarInverters)
connection->update();
}
foreach (SmaBatteryInverterModbusTcpConnection *connection, m_modbusBatteryInverters) {
foreach (SmaBatteryInverterModbusTcpConnection *connection, m_modbusBatteryInverters)
connection->update();
}
});
m_refreshTimer->start();
@ -881,25 +896,6 @@ void IntegrationPluginSma::setupModbusSolarInverterConnection(ThingSetupInfo *in
}
});
connect(connection, &SmaSolarInverterModbusTcpConnection::initializationFinished, info, [=](bool success){
if (!success) {
qCWarning(dcSma()) << "Connection init finished with errors" << thing->name() << connection->modbusTcpMaster()->hostAddress().toString();
hardwareManager()->networkDeviceDiscovery()->unregisterMonitor(monitor);
connection->deleteLater();
info->finish(Thing::ThingErrorHardwareFailure, QT_TR_NOOP("Could not initialize the communication with the inverter."));
return;
}
qCDebug(dcSma()) << "Connection init finished successfully" << connection;
m_modbusSolarInverters.insert(thing, connection);
info->finish(Thing::ThingErrorNoError);
// Set connected true
thing->setStateValue("connected", true);
foreach (Thing *childThing, myThings().filterByParentId(thing->id())) {
childThing->setStateValue("connected", true);
}
connect(connection, &SmaSolarInverterModbusTcpConnection::updateFinished, thing, [=](){
qCDebug(dcSma()) << "Updated" << connection;
@ -948,10 +944,10 @@ void IntegrationPluginSma::setupModbusSolarInverterConnection(ThingSetupInfo *in
thing->setStateValue(modbusSolarInverterFirmwareVersionStateTypeId, Sma::buildSoftwareVersionString(connection->softwarePackage()));
});
// Update registers
connection->update();
});
m_modbusSolarInverters.insert(thing, connection);
info->finish(Thing::ThingErrorNoError);
if (monitor->reachable())
connection->connectDevice();
}
@ -1012,21 +1008,6 @@ void IntegrationPluginSma::setupModbusBatteryInverterConnection(ThingSetupInfo *
}
});
connect(connection, &SmaBatteryInverterModbusTcpConnection::initializationFinished, info, [=](bool success){
if (!success) {
qCWarning(dcSma()) << "Connection init finished with errors" << thing->name() << connection->modbusTcpMaster()->hostAddress().toString();
hardwareManager()->networkDeviceDiscovery()->unregisterMonitor(monitor);
connection->deleteLater();
info->finish(Thing::ThingErrorHardwareFailure, QT_TR_NOOP("Could not initialize the communication with the battery inverter."));
return;
}
qCDebug(dcSma()) << "Connection init finished successfully" << connection;
m_modbusBatteryInverters.insert(thing, connection);
info->finish(Thing::ThingErrorNoError);
thing->setStateValue("connected", true);
connect(connection, &SmaBatteryInverterModbusTcpConnection::updateFinished, thing, [=](){
qCDebug(dcSma()) << "Updated" << connection;
thing->setStateValue(modbusBatteryInverterFirmwareVersionStateTypeId, Sma::buildSoftwareVersionString(connection->softwarePackage()));
@ -1038,10 +1019,10 @@ void IntegrationPluginSma::setupModbusBatteryInverterConnection(ThingSetupInfo *
});
// Update registers
connection->update();
});
m_modbusBatteryInverters.insert(thing, connection);
info->finish(Thing::ThingErrorNoError);
if (monitor->reachable())
connection->connectDevice();
}