diff --git a/webasto/integrationpluginwebasto.cpp b/webasto/integrationpluginwebasto.cpp index 79a1d83..b9eef61 100644 --- a/webasto/integrationpluginwebasto.cpp +++ b/webasto/integrationpluginwebasto.cpp @@ -149,7 +149,8 @@ void IntegrationPluginWebasto::postSetupThing(Thing *thing) m_pluginTimer = hardwareManager()->pluginTimerManager()->registerTimer(1); connect(m_pluginTimer, &PluginTimer::timeout, this, [this] { Q_FOREACH(Webasto *connection, m_webastoConnections) { - update(connection); + if (connection->connected()) + update(connection); } }); } @@ -159,6 +160,7 @@ void IntegrationPluginWebasto::postSetupThing(Thing *thing) if (!connection) { qCWarning(dcWebasto()) << "Can't find connection to thing"; } + update(connection); } else { Q_ASSERT_X(false, "postSetupThing", QString("Unhandled thingClassId: %1").arg(thing->thingClassId().toString()).toUtf8()); diff --git a/webasto/webasto.cpp b/webasto/webasto.cpp index b4709f6..9c980ad 100644 --- a/webasto/webasto.cpp +++ b/webasto/webasto.cpp @@ -38,7 +38,7 @@ Webasto::Webasto(const QHostAddress &address, uint port, QObject *parent) : m_modbusConnection = new ModbusTCPMaster(address, port, this); m_modbusConnection->setNumberOfRetries(3); m_modbusConnection->setTimeout(1000); - connect(m_modbusConnection, &ModbusTCPMaster::connectionStateChanged, this, &Webasto::connectionStateChanged); + connect(m_modbusConnection, &ModbusTCPMaster::receivedHoldingRegister, this, &Webasto::onReceivedHoldingRegister); connect(m_modbusConnection, &ModbusTCPMaster::writeRequestExecuted, this, &Webasto::writeRequestExecuted); connect(m_modbusConnection, &ModbusTCPMaster::writeRequestError, this, &Webasto::writeRequestError); @@ -115,10 +115,27 @@ void Webasto::setLiveBit() { qCDebug(dcWebasto()) << "Webasto: Set live bit"; m_modbusConnection->writeHoldingRegister(m_unitId, TqLifeBit, 0x0001); + if (m_awaitingLiveBitResponse) { + // Live bit response has not been received, setting connection as disconnected + // The live bit acts as heartbeat for both sides, client and server + if (m_connected) { + m_connected = false; + emit connectionStateChanged(false); + } + } else { + m_awaitingLiveBitResponse = true; + } } void Webasto::onReceivedHoldingRegister(uint slaveAddress, uint modbusRegister, const QVector &values) { Q_UNUSED(slaveAddress) + if (modbusRegister == TqLifeBit) { + m_awaitingLiveBitResponse = false; + if (!m_connected) { + m_connected = true; + emit connectionStateChanged(true); + } + } emit receivedRegister(TqModbusRegister(modbusRegister), values); } diff --git a/webasto/webasto.h b/webasto/webasto.h index 16a852a..49a093f 100644 --- a/webasto/webasto.h +++ b/webasto/webasto.h @@ -137,7 +137,8 @@ private: private: QTimer *m_lifeBitTimer = nullptr; - + bool m_connected = false; + bool m_awaitingLiveBitResponse = false; signals: void connectionStateChanged(bool state); void writeRequestExecuted(const QUuid &requestId, bool success);