diff --git a/idm/integrationpluginidm.cpp b/idm/integrationpluginidm.cpp index e8be7c9..7f92ed1 100644 --- a/idm/integrationpluginidm.cpp +++ b/idm/integrationpluginidm.cpp @@ -39,39 +39,45 @@ IntegrationPluginIdm::IntegrationPluginIdm() void IntegrationPluginIdm::discoverThings(ThingDiscoveryInfo *info) { + if (!hardwareManager()->networkDeviceDiscovery()->available()) { + qCWarning(dcIdm()) << "Failed to discover network devices. The network device discovery is not available."; + info->finish(Thing::ThingErrorHardwareNotAvailable, QT_TR_NOOP("The discovery is not available.")); + return; + } + qCDebug(dcIdm()) << "Discovering network..."; NetworkDeviceDiscoveryReply *discoveryReply = hardwareManager()->networkDeviceDiscovery()->discover(); connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, this, [=](){ ThingDescriptors descriptors; - qCDebug(dcIdm()) << "Discovery finished. Found" << discoveryReply->networkDevices().count() << "devices"; - foreach (const NetworkDevice &networkDevice, discoveryReply->networkDevices()) { - qCDebug(dcIdm()) << networkDevice; + qCDebug(dcIdm()) << "Discovery finished. Found" << discoveryReply->networkDeviceInfos().count() << "devices"; + foreach (const NetworkDeviceInfo &networkDeviceInfo, discoveryReply->networkDeviceInfos()) { + qCDebug(dcIdm()) << networkDeviceInfo; QString title; - if (networkDevice.hostName().isEmpty()) { - title += networkDevice.address().toString(); + if (networkDeviceInfo.hostName().isEmpty()) { + title += networkDeviceInfo.address().toString(); } else { - title += networkDevice.address().toString() + " (" + networkDevice.hostName() + ")"; + title += networkDeviceInfo.address().toString() + " (" + networkDeviceInfo.hostName() + ")"; } QString description; - if (networkDevice.macAddressManufacturer().isEmpty()) { - description = networkDevice.macAddress(); + if (networkDeviceInfo.macAddressManufacturer().isEmpty()) { + description = networkDeviceInfo.macAddress(); } else { - description = networkDevice.macAddress() + " (" + networkDevice.macAddressManufacturer() + ")"; + description = networkDeviceInfo.macAddress() + " (" + networkDeviceInfo.macAddressManufacturer() + ")"; } ThingDescriptor descriptor(navigator2ThingClassId, title, description); // Check if we already have set up this device - Things existingThings = myThings().filterByParam(navigator2ThingMacAddressParamTypeId, networkDevice.macAddress()); + Things existingThings = myThings().filterByParam(navigator2ThingMacAddressParamTypeId, networkDeviceInfo.macAddress()); if (existingThings.count() == 1) { - qCDebug(dcIdm()) << "This thing already exists in the system." << existingThings.first() << networkDevice; + qCDebug(dcIdm()) << "This thing already exists in the system." << existingThings.first() << networkDeviceInfo; descriptor.setThingId(existingThings.first()->id()); } ParamList params; - params << Param(navigator2ThingMacAddressParamTypeId, networkDevice.macAddress()); - params << Param(navigator2ThingIpAddressParamTypeId, networkDevice.address().toString()); + params << Param(navigator2ThingMacAddressParamTypeId, networkDeviceInfo.macAddress()); + params << Param(navigator2ThingIpAddressParamTypeId, networkDeviceInfo.address().toString()); descriptor.setParams(params); info->addThingDescriptor(descriptor); } diff --git a/modbus/modbustcpmaster.cpp b/modbus/modbustcpmaster.cpp index bface2c..880f4c6 100644 --- a/modbus/modbustcpmaster.cpp +++ b/modbus/modbustcpmaster.cpp @@ -57,14 +57,6 @@ ModbusTCPMaster::~ModbusTCPMaster() { if (m_reconnectTimer) { m_reconnectTimer->stop(); - delete m_reconnectTimer; - m_reconnectTimer = nullptr; - } - - if (m_modbusTcpClient) { - m_modbusTcpClient->disconnectDevice(); - delete m_modbusTcpClient; - m_modbusTcpClient = nullptr; } if (m_modbusTcpClient) { @@ -92,40 +84,27 @@ void ModbusTCPMaster::setHostAddress(const QHostAddress &hostAddress) m_hostAddress = hostAddress; } -QHostAddress ModbusTCPMaster::hostAddress() const -{ - return m_hostAddress; -} - -uint ModbusTCPMaster::port() const -{ - return m_port; -} - -bool ModbusTCPMaster::setPort(uint port) -{ - m_port = port; - return connectDevice(); -} - -bool ModbusTCPMaster::setHostAddress(const QHostAddress &hostAddress) -{ - m_hostAddress = hostAddress; - return connectDevice(); -} - bool ModbusTCPMaster::connectDevice() { // TCP connection to target device - qCDebug(dcModbusTCP()) << "Setting up TCP connecion" << QString("%1:%2").arg(m_hostAddress.toString()).arg(m_port); if (!m_modbusTcpClient) return false; - m_modbusTcpClient->setConnectionParameter(QModbusDevice::NetworkPortParameter, m_port); - m_modbusTcpClient->setConnectionParameter(QModbusDevice::NetworkAddressParameter, m_hostAddress.toString()); - m_modbusTcpClient->setTimeout(m_timeout); - m_modbusTcpClient->setNumberOfRetries(m_numberOfRetries); + // Only connect if we are in the unconnected state + if (m_modbusTcpClient->state() == QModbusDevice::UnconnectedState) { + qCDebug(dcModbusTCP()) << "Connecting modbus TCP client to" << QString("%1:%2").arg(m_hostAddress.toString()).arg(m_port); + m_modbusTcpClient->setConnectionParameter(QModbusDevice::NetworkPortParameter, m_port); + m_modbusTcpClient->setConnectionParameter(QModbusDevice::NetworkAddressParameter, m_hostAddress.toString()); + m_modbusTcpClient->setTimeout(m_timeout); + m_modbusTcpClient->setNumberOfRetries(m_numberOfRetries); + return m_modbusTcpClient->connectDevice(); + } else if (m_modbusTcpClient->state() != QModbusDevice::ConnectedState) { + // Restart the timer in case of connecting not finished yet or closing + m_reconnectTimer->start(); + } else { + qCWarning(dcModbusTCP()) << "Connect modbus TCP device" << QString("%1:%2").arg(m_hostAddress.toString()).arg(m_port) << "called, but the socket is currently in the" << m_modbusTcpClient->state(); + } - return m_modbusTcpClient->connectDevice(); + return false; } void ModbusTCPMaster::disconnectDevice() @@ -133,9 +112,21 @@ void ModbusTCPMaster::disconnectDevice() if (!m_modbusTcpClient) return; + // Stop the reconnect timer since disconnect was explicitly called + m_reconnectTimer->stop(); m_modbusTcpClient->disconnectDevice(); } +bool ModbusTCPMaster::reconnectDevice() +{ + qCWarning(dcModbusTCP()) << "Reconnecting modbus TCP device" << QString("%1:%2").arg(m_hostAddress.toString()).arg(m_port); + if (!m_modbusTcpClient) + return false; + + disconnectDevice(); + return connectDevice(); +} + bool ModbusTCPMaster::connected() const { return m_connected; @@ -173,16 +164,6 @@ QModbusDevice::Error ModbusTCPMaster::error() const return m_modbusTcpClient->error(); } -QString ModbusTCPMaster::errorString() const -{ - return m_modbusTcpClient->errorString(); -} - -QModbusDevice::Error ModbusTCPMaster::error() const -{ - return m_modbusTcpClient->error(); -} - QUuid ModbusTCPMaster::readCoil(uint slaveAddress, uint registerAddress, uint size) { if (!m_modbusTcpClient) { @@ -499,6 +480,4 @@ void ModbusTCPMaster::onModbusStateChanged(QModbusDevice::State state) } else if (state == QModbusDevice::UnconnectedState) { m_reconnectTimer->start(); } - - emit connectionStateChanged(connected); } diff --git a/modbus/modbustcpmaster.h b/modbus/modbustcpmaster.h index 1ecdb51..7857922 100644 --- a/modbus/modbustcpmaster.h +++ b/modbus/modbustcpmaster.h @@ -59,7 +59,6 @@ public: int timeout() const; void setTimeout(int timeout); - int timeout(); QString errorString() const; QModbusDevice::Error error() const; @@ -105,9 +104,9 @@ signals: void writeRequestExecuted(const QUuid &requestId, bool success); void writeRequestError(const QUuid &requestId, const QString &error); - void readRequestError(const QUuid &requestId, const QString &error); void readRequestExecuted(const QUuid &requestId, bool success); + void readRequestError(const QUuid &requestId, const QString &error); void receivedCoil(uint slaveAddress, uint modbusRegister, const QVector &values); void receivedDiscreteInput(uint slaveAddress, uint modbusRegister, const QVector &values); diff --git a/modbuscommander/integrationpluginmodbuscommander.cpp b/modbuscommander/integrationpluginmodbuscommander.cpp index 00e8e8b..5889534 100644 --- a/modbuscommander/integrationpluginmodbuscommander.cpp +++ b/modbuscommander/integrationpluginmodbuscommander.cpp @@ -111,37 +111,43 @@ void IntegrationPluginModbusCommander::discoverThings(ThingDiscoveryInfo *info) info->finish(Thing::ThingErrorNoError); return; } else if (thingClassId == modbusTCPClientThingClassId) { + if (!hardwareManager()->networkDeviceDiscovery()->available()) { + qCWarning(dcModbusCommander()) << "Failed to discover network devices. The network device discovery is not available."; + info->finish(Thing::ThingErrorHardwareNotAvailable, QT_TR_NOOP("The discovery is not available.")); + return; + } + NetworkDeviceDiscoveryReply *discoveryReply = hardwareManager()->networkDeviceDiscovery()->discover(); connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, this, [=](){ ThingDescriptors descriptors; - qCDebug(dcModbusCommander()) << "Discovery finished. Found" << discoveryReply->networkDevices().count() << "devices"; - foreach (const NetworkDevice &networkDevice, discoveryReply->networkDevices()) { - qCDebug(dcModbusCommander()) << networkDevice; + qCDebug(dcModbusCommander()) << "Discovery finished. Found" << discoveryReply->networkDeviceInfos().count() << "devices"; + foreach (const NetworkDeviceInfo &networkDeviceInfo, discoveryReply->networkDeviceInfos()) { + qCDebug(dcModbusCommander()) << networkDeviceInfo; QString title; - if (networkDevice.hostName().isEmpty()) { - title += networkDevice.address().toString(); + if (networkDeviceInfo.hostName().isEmpty()) { + title += networkDeviceInfo.address().toString(); } else { - title += networkDevice.address().toString() + " (" + networkDevice.hostName() + ")"; + title += networkDeviceInfo.address().toString() + " (" + networkDeviceInfo.hostName() + ")"; } QString description; - if (networkDevice.macAddressManufacturer().isEmpty()) { - description = networkDevice.macAddress(); + if (networkDeviceInfo.macAddressManufacturer().isEmpty()) { + description = networkDeviceInfo.macAddress(); } else { - description = networkDevice.macAddress() + " (" + networkDevice.macAddressManufacturer() + ")"; + description = networkDeviceInfo.macAddress() + " (" + networkDeviceInfo.macAddressManufacturer() + ")"; } ThingDescriptor descriptor(modbusTCPClientThingClassId, title, description); // Check if we already have set up this device - Things existingThings = myThings().filterByParam(modbusTCPClientThingIpAddressParamTypeId, networkDevice.address().toString()); + Things existingThings = myThings().filterByParam(modbusTCPClientThingIpAddressParamTypeId, networkDeviceInfo.address().toString()); if (existingThings.count() == 1) { - qCDebug(dcModbusCommander()) << "This thing already exists in the system." << existingThings.first() << networkDevice; + qCDebug(dcModbusCommander()) << "This thing already exists in the system." << existingThings.first() << networkDeviceInfo; descriptor.setThingId(existingThings.first()->id()); } ParamList params; - params << Param(modbusTCPClientThingIpAddressParamTypeId, networkDevice.address().toString()); + params << Param(modbusTCPClientThingIpAddressParamTypeId, networkDeviceInfo.address().toString()); descriptor.setParams(params); info->addThingDescriptor(descriptor); } diff --git a/sunspec/integrationpluginsunspec.cpp b/sunspec/integrationpluginsunspec.cpp index e353d12..bb6e3de 100644 --- a/sunspec/integrationpluginsunspec.cpp +++ b/sunspec/integrationpluginsunspec.cpp @@ -101,37 +101,43 @@ void IntegrationPluginSunSpec::init() void IntegrationPluginSunSpec::discoverThings(ThingDiscoveryInfo *info) { + if (!hardwareManager()->networkDeviceDiscovery()->available()) { + qCWarning(dcSunSpec()) << "Failed to discover network devices. The network device discovery is not available."; + info->finish(Thing::ThingErrorHardwareNotAvailable, QT_TR_NOOP("The discovery is not available.")); + return; + } + NetworkDeviceDiscoveryReply *discoveryReply = hardwareManager()->networkDeviceDiscovery()->discover(); connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, this, [=](){ ThingDescriptors descriptors; - qCDebug(dcSunSpec()) << "Discovery finished. Found" << discoveryReply->networkDevices().count() << "devices"; - foreach (const NetworkDevice &networkDevice, discoveryReply->networkDevices()) { - qCDebug(dcSunSpec()) << networkDevice; + qCDebug(dcSunSpec()) << "Discovery finished. Found" << discoveryReply->networkDeviceInfos().count() << "devices"; + foreach (const NetworkDeviceInfo &networkDeviceInfo, discoveryReply->networkDeviceInfos()) { + qCDebug(dcSunSpec()) << networkDeviceInfo; QString title; - if (networkDevice.hostName().isEmpty()) { - title += networkDevice.address().toString(); + if (networkDeviceInfo.hostName().isEmpty()) { + title += networkDeviceInfo.address().toString(); } else { - title += networkDevice.address().toString() + " (" + networkDevice.hostName() + ")"; + title += networkDeviceInfo.address().toString() + " (" + networkDeviceInfo.hostName() + ")"; } QString description; - if (networkDevice.macAddressManufacturer().isEmpty()) { - description = networkDevice.macAddress(); + if (networkDeviceInfo.macAddressManufacturer().isEmpty()) { + description = networkDeviceInfo.macAddress(); } else { - description = networkDevice.macAddress() + " (" + networkDevice.macAddressManufacturer() + ")"; + description = networkDeviceInfo.macAddress() + " (" + networkDeviceInfo.macAddressManufacturer() + ")"; } ThingDescriptor descriptor(sunspecConnectionThingClassId, title, description); // Check if we already have set up this device - Things existingThings = myThings().filterByParam(sunspecConnectionThingIpAddressParamTypeId, networkDevice.address().toString()); + Things existingThings = myThings().filterByParam(sunspecConnectionThingIpAddressParamTypeId, networkDeviceInfo.address().toString()); if (existingThings.count() == 1) { - qCDebug(dcSunSpec()) << "This thing already exists in the system." << existingThings.first() << networkDevice; + qCDebug(dcSunSpec()) << "This thing already exists in the system." << existingThings.first() << networkDeviceInfo; descriptor.setThingId(existingThings.first()->id()); } ParamList params; - params << Param(sunspecConnectionThingIpAddressParamTypeId, networkDevice.address().toString()); + params << Param(sunspecConnectionThingIpAddressParamTypeId, networkDeviceInfo.address().toString()); descriptor.setParams(params); info->addThingDescriptor(descriptor); } diff --git a/wallbe/integrationpluginwallbe.cpp b/wallbe/integrationpluginwallbe.cpp index 9bd15dc..379dd6b 100644 --- a/wallbe/integrationpluginwallbe.cpp +++ b/wallbe/integrationpluginwallbe.cpp @@ -81,43 +81,49 @@ void IntegrationPluginWallbe::init() void IntegrationPluginWallbe::discoverThings(ThingDiscoveryInfo *info) { - if (info->thingClassId() == wallbeEcoThingClassId){ + if (info->thingClassId() == wallbeEcoThingClassId) { + if (!hardwareManager()->networkDeviceDiscovery()->available()) { + qCWarning(dcWallbe()) << "Failed to discover network devices. The network device discovery is not available."; + info->finish(Thing::ThingErrorHardwareNotAvailable, QT_TR_NOOP("The discovery is not available.")); + return; + } + qCDebug(dcWallbe()) << "Start Wallbe eco discovery"; NetworkDeviceDiscoveryReply *discoveryReply = hardwareManager()->networkDeviceDiscovery()->discover(); connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, this, [=](){ ThingDescriptors descriptors; - qCDebug(dcWallbe()) << "Discovery finished. Found" << discoveryReply->networkDevices().count() << "devices"; - foreach (const NetworkDevice &networkDevice, discoveryReply->networkDevices()) { - qCDebug(dcWallbe()) << networkDevice; - if (!networkDevice.macAddressManufacturer().contains("Phoenix", Qt::CaseSensitivity::CaseInsensitive)) + qCDebug(dcWallbe()) << "Discovery finished. Found" << discoveryReply->networkDeviceInfos().count() << "devices"; + foreach (const NetworkDeviceInfo &networkDeviceInfo, discoveryReply->networkDeviceInfos()) { + qCDebug(dcWallbe()) << networkDeviceInfo; + if (!networkDeviceInfo.macAddressManufacturer().contains("Phoenix", Qt::CaseSensitivity::CaseInsensitive)) continue; QString title; - if (networkDevice.hostName().isEmpty()) { - title += networkDevice.address().toString(); + if (networkDeviceInfo.hostName().isEmpty()) { + title += networkDeviceInfo.address().toString(); } else { - title += networkDevice.address().toString() + " (" + networkDevice.hostName() + ")"; + title += networkDeviceInfo.address().toString() + " (" + networkDeviceInfo.hostName() + ")"; } QString description; - if (networkDevice.macAddressManufacturer().isEmpty()) { - description = networkDevice.macAddress(); + if (networkDeviceInfo.macAddressManufacturer().isEmpty()) { + description = networkDeviceInfo.macAddress(); } else { - description = networkDevice.macAddress() + " (" + networkDevice.macAddressManufacturer() + ")"; + description = networkDeviceInfo.macAddress() + " (" + networkDeviceInfo.macAddressManufacturer() + ")"; } ThingDescriptor descriptor(wallbeEcoThingClassId, title, description); // Check if we already have set up this device - Things existingThings = myThings().filterByParam(wallbeEcoThingIpParamTypeId, networkDevice.address().toString()); + Things existingThings = myThings().filterByParam(wallbeEcoThingIpParamTypeId, networkDeviceInfo.address().toString()); if (existingThings.count() == 1) { - qCDebug(dcWallbe()) << "This thing already exists in the system." << existingThings.first() << networkDevice; + qCDebug(dcWallbe()) << "This thing already exists in the system." << existingThings.first() << networkDeviceInfo; descriptor.setThingId(existingThings.first()->id()); } ParamList params; - params << Param(wallbeEcoThingIpParamTypeId, networkDevice.address().toString()); - params << Param(wallbeEcoThingMacParamTypeId, networkDevice.macAddress()); + params << Param(wallbeEcoThingIpParamTypeId, networkDeviceInfo.address().toString()); + params << Param(wallbeEcoThingMacParamTypeId, networkDeviceInfo.macAddress()); descriptor.setParams(params); info->addThingDescriptor(descriptor); } diff --git a/webasto/integrationpluginwebasto.cpp b/webasto/integrationpluginwebasto.cpp index 6f720e5..5e25778 100644 --- a/webasto/integrationpluginwebasto.cpp +++ b/webasto/integrationpluginwebasto.cpp @@ -82,42 +82,48 @@ void IntegrationPluginWebasto::init() void IntegrationPluginWebasto::discoverThings(ThingDiscoveryInfo *info) { if (info->thingClassId() == liveWallboxThingClassId) { + if (!hardwareManager()->networkDeviceDiscovery()->available()) { + qCWarning(dcWebasto()) << "Failed to discover network devices. The network device discovery is not available."; + info->finish(Thing::ThingErrorHardwareNotAvailable, QT_TR_NOOP("The discovery is not available.")); + return; + } + qCDebug(dcWebasto()) << "Discover things"; NetworkDeviceDiscoveryReply *discoveryReply = hardwareManager()->networkDeviceDiscovery()->discover(); connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, this, [=](){ ThingDescriptors descriptors; - qCDebug(dcWebasto()) << "Discovery finished. Found" << discoveryReply->networkDevices().count() << "devices"; - foreach (const NetworkDevice &networkDevice, discoveryReply->networkDevices()) { - qCDebug(dcWebasto()) << networkDevice; - if (!networkDevice.hostName().contains("webasto", Qt::CaseSensitivity::CaseInsensitive)) + qCDebug(dcWebasto()) << "Discovery finished. Found" << discoveryReply->networkDeviceInfos().count() << "devices"; + foreach (const NetworkDeviceInfo &networkDeviceInfo, discoveryReply->networkDeviceInfos()) { + qCDebug(dcWebasto()) << networkDeviceInfo; + if (!networkDeviceInfo.hostName().contains("webasto", Qt::CaseSensitivity::CaseInsensitive)) continue; QString title = "Wallbox "; - if (networkDevice.hostName().isEmpty()) { - title += networkDevice.address().toString(); + if (networkDeviceInfo.hostName().isEmpty()) { + title += networkDeviceInfo.address().toString(); } else { - title += networkDevice.address().toString() + " (" + networkDevice.hostName() + ")"; + title += networkDeviceInfo.address().toString() + " (" + networkDeviceInfo.hostName() + ")"; } QString description; - if (networkDevice.macAddressManufacturer().isEmpty()) { - description = networkDevice.macAddress(); + if (networkDeviceInfo.macAddressManufacturer().isEmpty()) { + description = networkDeviceInfo.macAddress(); } else { - description = networkDevice.macAddress() + " (" + networkDevice.macAddressManufacturer() + ")"; + description = networkDeviceInfo.macAddress() + " (" + networkDeviceInfo.macAddressManufacturer() + ")"; } ThingDescriptor descriptor(liveWallboxThingClassId, title, description); // Check if we already have set up this device - Things existingThings = myThings().filterByParam(liveWallboxThingIpAddressParamTypeId, networkDevice.address().toString()); + Things existingThings = myThings().filterByParam(liveWallboxThingIpAddressParamTypeId, networkDeviceInfo.address().toString()); if (existingThings.count() == 1) { - qCDebug(dcWebasto()) << "This thing already exists in the system." << existingThings.first() << networkDevice; + qCDebug(dcWebasto()) << "This thing already exists in the system." << existingThings.first() << networkDeviceInfo; descriptor.setThingId(existingThings.first()->id()); } ParamList params; - params << Param(liveWallboxThingIpAddressParamTypeId, networkDevice.address().toString()); - params << Param(liveWallboxThingMacAddressParamTypeId, networkDevice.macAddress()); + params << Param(liveWallboxThingIpAddressParamTypeId, networkDeviceInfo.address().toString()); + params << Param(liveWallboxThingMacAddressParamTypeId, networkDeviceInfo.macAddress()); descriptor.setParams(params); info->addThingDescriptor(descriptor); }