Merge PR #168: SunSpec: Improve discovery and handle blocking connections

pull/174/head
jenkins 2024-05-14 15:04:05 +02:00
commit 2d7c868242
3 changed files with 41 additions and 4 deletions

2
debian/compat vendored
View File

@ -1 +1 @@
9
10

View File

@ -36,9 +36,9 @@
SunSpecDiscovery::SunSpecDiscovery(NetworkDeviceDiscovery *networkDeviceDiscovery, const QList<quint16> &slaveIds, SunSpecDataPoint::ByteOrder byteOrder, QObject *parent)
: QObject{parent},
m_networkDeviceDiscovery{networkDeviceDiscovery},
m_slaveIds{slaveIds},
m_byteOrder{byteOrder}
m_networkDeviceDiscovery{networkDeviceDiscovery},
m_slaveIds{slaveIds},
m_byteOrder{byteOrder}
{
m_scanPorts.append(502);
m_scanPorts.append(1502);
@ -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;