SunSpec: Improve discovery and handle blocking connections

master
Simon Stürz 2024-03-22 15:31:35 +01:00
parent cb2f5d94b9
commit 329b9ac864
3 changed files with 41 additions and 4 deletions

2
debian/compat vendored
View File

@ -1 +1 @@
9
10

View File

@ -94,7 +94,31 @@ void SunSpecDiscovery::testNextConnection(const QHostAddress &address)
if (!connection->connectDevice()) {
qCDebug(dcSunSpec()) << "Discovery: Failed to connect to" << QString("%1:%2").arg(address.toString()).arg(connection->port()) << "slave ID:" << connection->slaveId() << "Continue...";;
cleanupConnection(connection);
return;
}
/* Some connection will block the connection process and the connection will pending in the
* connecting state for over a minute before failing.
* This behavior blocks further connection tests for during the disdovery process and the discovery failes to detect the device.
* This timer will make sure the connection can be established within 5 seconds, otherwise we continue with the next connection.
*/
QTimer *connectionTimer = new QTimer(connection);
connectionTimer->setInterval(5000);
connectionTimer->setSingleShot(true);
m_connectionTimers.insert(connection, connectionTimer);
connect(connectionTimer, &QTimer::timeout, connection, [this, connection, connectionTimer](){
qCDebug(dcSunSpec()) << "Discovery: Could not establish a connection within" << connectionTimer->interval() << "ms. Continue...";
m_connectionTimers.remove(connection);
connection->disconnectDevice();
// Note: since the connection is the parent of the timer, it will be deleted with the connection
cleanupConnection(connection);
});
// Once connected, the timer will be stopped.
connectionTimer->start();
}
void SunSpecDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo)
@ -121,6 +145,13 @@ void SunSpecDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDevice
return;
}
// Successfully connected, we can stop the connection timer which takes care about blocking connection attempts
if (m_connectionTimers.contains(connection)) {
QTimer *connectionTimer = m_connectionTimers.take(connection);
connectionTimer->stop();
connectionTimer->deleteLater();
}
// Modbus TCP connected, try to discovery sunspec models...
connect(connection, &SunSpecConnection::discoveryFinished, this, [=](bool success){
if (!success) {
@ -180,6 +211,11 @@ void SunSpecDiscovery::cleanupConnection(SunSpecConnection *connection)
connection->disconnectDevice();
connection->deleteLater();
if (m_connectionTimers.contains(connection)) {
QTimer *connectionTimer = m_connectionTimers.take(connection);
connectionTimer->stop();
}
testNextConnection(connection->hostAddress());
}

View File

@ -66,6 +66,7 @@ private:
QDateTime m_startDateTime;
QHash<QHostAddress, QQueue<SunSpecConnection *>> m_pendingConnectionAttempts;
QHash<SunSpecConnection *, QTimer *> m_connectionTimers;
QList<SunSpecConnection *> m_connections;
QList<Result> m_results;