-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge PR #192: Kostal: Update to networkdevice interface
- Loading branch information
Showing
5 changed files
with
85 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | ||
* | ||
* Copyright 2013 - 2022, nymea GmbH | ||
* Copyright 2013 - 2024, nymea GmbH | ||
* Contact: [email protected] | ||
* | ||
* This file is part of nymea. | ||
|
@@ -49,24 +49,34 @@ void IntegrationPluginKostal::discoverThings(ThingDiscoveryInfo *info) | |
} | ||
|
||
// Create a discovery with the info as parent for auto deleting the object once the discovery info is done | ||
KostalDiscovery *discovery = new KostalDiscovery(hardwareManager()->networkDeviceDiscovery(), 1502, 71, info); | ||
KostalDiscovery *discovery = new KostalDiscovery(hardwareManager()->networkDeviceDiscovery(), | ||
info->params().paramValue(kostalInverterDiscoveryPortParamTypeId).toUInt(), | ||
info->params().paramValue(kostalInverterDiscoverySlaveIdParamTypeId).toUInt(), info); | ||
|
||
connect(discovery, &KostalDiscovery::discoveryFinished, info, [=](){ | ||
foreach (const KostalDiscovery::KostalDiscoveryResult &result, discovery->discoveryResults()) { | ||
|
||
ThingDescriptor descriptor(kostalInverterThingClassId, result.manufacturerName + " " + result.productName, "Serial: " + result.serialNumber + " - " + result.networkDeviceInfo.address().toString()); | ||
ThingDescriptor descriptor(kostalInverterThingClassId, result.manufacturerName + " " + result.productName, | ||
"Serial: " + result.serialNumber + " - " + result.networkDeviceInfo.address().toString()); | ||
|
||
qCDebug(dcKostal()) << "Discovered:" << descriptor.title() << descriptor.description(); | ||
ParamList params; | ||
params << Param(kostalInverterThingSerialNumberParamTypeId, result.serialNumber); | ||
params << Param(kostalInverterThingMacAddressParamTypeId, result.networkDeviceInfo.thingParamValueMacAddress()); | ||
params << Param(kostalInverterThingHostNameParamTypeId, result.networkDeviceInfo.thingParamValueHostName()); | ||
params << Param(kostalInverterThingAddressParamTypeId, result.networkDeviceInfo.thingParamValueAddress()); | ||
// Note: if we discover also the port and modbusaddress, we must fill them in from the discovery here, for now everywhere the defaults... | ||
descriptor.setParams(params); | ||
|
||
// Check if we already have set up this device | ||
Things existingThings = myThings().filterByParam(kostalInverterThingMacAddressParamTypeId, result.networkDeviceInfo.macAddress()); | ||
|
||
// Note: we introduced the serial number later, if the current thing has no serialnumber, we use the mac | ||
Things existingThings = myThings().filterByParam(kostalInverterThingSerialNumberParamTypeId, result.serialNumber); | ||
if (existingThings.count() == 1) { | ||
qCDebug(dcKostal()) << "This Kostal inverter already exists in the system:" << result.networkDeviceInfo; | ||
qCDebug(dcKostal()) << "This Kostal inverter with this serial number already exists in the system:" << result.networkDeviceInfo; | ||
descriptor.setThingId(existingThings.first()->id()); | ||
} | ||
|
||
ParamList params; | ||
params << Param(kostalInverterThingMacAddressParamTypeId, result.networkDeviceInfo.macAddress()); | ||
// Note: if we discover also the port and modbusaddress, we must fill them in from the discovery here, for now everywhere the defaults... | ||
descriptor.setParams(params); | ||
info->addThingDescriptor(descriptor); | ||
} | ||
|
||
|
@@ -103,7 +113,7 @@ void IntegrationPluginKostal::setupThing(ThingSetupInfo *info) | |
} | ||
|
||
// Create the monitor | ||
NetworkDeviceMonitor *monitor = hardwareManager()->networkDeviceDiscovery()->registerMonitor(macAddress); | ||
NetworkDeviceMonitor *monitor = hardwareManager()->networkDeviceDiscovery()->registerMonitor(thing); | ||
m_monitors.insert(thing, monitor); | ||
|
||
QHostAddress address = monitor->networkDeviceInfo().address(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | ||
* | ||
* Copyright 2013 - 2022, nymea GmbH | ||
* Copyright 2013 - 2024, nymea GmbH | ||
* Contact: [email protected] | ||
* | ||
* This file is part of nymea. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | ||
* | ||
* Copyright 2013 - 2022, nymea GmbH | ||
* Copyright 2013 - 2024, nymea GmbH | ||
* Contact: [email protected] | ||
* | ||
* This file is part of nymea. | ||
|
@@ -44,23 +44,17 @@ void KostalDiscovery::startDiscovery() | |
{ | ||
qCInfo(dcKostal()) << "Discovery: Start searching for Kostal inverters in the network..."; | ||
NetworkDeviceDiscoveryReply *discoveryReply = m_networkDeviceDiscovery->discover(); | ||
m_startDateTime = QDateTime::currentDateTime(); | ||
|
||
// Imedialty check any new device gets discovered | ||
connect(discoveryReply, &NetworkDeviceDiscoveryReply::networkDeviceInfoAdded, this, &KostalDiscovery::checkNetworkDevice); | ||
connect(discoveryReply, &NetworkDeviceDiscoveryReply::hostAddressDiscovered, this, &KostalDiscovery::checkNetworkDevice); | ||
|
||
// Check what might be left on finished | ||
connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, discoveryReply, &NetworkDeviceDiscoveryReply::deleteLater); | ||
connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, discoveryReply, &NetworkDeviceDiscoveryReply::deleteLater); | ||
connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, this, [=](){ | ||
qCDebug(dcKostal()) << "Discovery: Network discovery finished. Found" << discoveryReply->networkDeviceInfos().count() << "network devices"; | ||
m_networkDeviceInfos = discoveryReply->networkDeviceInfos(); | ||
|
||
// Send a report request to nework device info not sent already... | ||
foreach (const NetworkDeviceInfo &networkDeviceInfo, m_networkDeviceInfos) { | ||
if (!m_verifiedNetworkDeviceInfos.contains(networkDeviceInfo)) { | ||
checkNetworkDevice(networkDeviceInfo); | ||
} | ||
} | ||
|
||
// Give the last connections added right before the network discovery finished a chance to check the device... | ||
QTimer::singleShot(3000, this, [this](){ | ||
qCDebug(dcKostal()) << "Discovery: Grace period timer triggered."; | ||
|
@@ -74,19 +68,15 @@ QList<KostalDiscovery::KostalDiscoveryResult> KostalDiscovery::discoveryResults( | |
return m_discoveryResults; | ||
} | ||
|
||
void KostalDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo) | ||
void KostalDiscovery::checkNetworkDevice(const QHostAddress &address) | ||
{ | ||
// Create a kostal connection and try to initialize it. | ||
// Only if initialized successfully and all information have been fetched correctly from | ||
// the device we can assume this is what we are locking for (ip, port, modbus address, correct registers). | ||
// We cloud tough also filter the result only for certain software versions, manufactueres or whatever... | ||
|
||
if (m_verifiedNetworkDeviceInfos.contains(networkDeviceInfo)) | ||
return; | ||
|
||
KostalModbusTcpConnection *connection = new KostalModbusTcpConnection(networkDeviceInfo.address(), m_port, m_modbusAddress, this); | ||
KostalModbusTcpConnection *connection = new KostalModbusTcpConnection(address, m_port, m_modbusAddress, this); | ||
m_connections.append(connection); | ||
m_verifiedNetworkDeviceInfos.append(networkDeviceInfo); | ||
|
||
connect(connection, &KostalModbusTcpConnection::reachableChanged, this, [=](bool reachable){ | ||
if (!reachable) { | ||
|
@@ -98,7 +88,7 @@ void KostalDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDeviceI | |
// Modbus TCP connected...ok, let's try to initialize it! | ||
connect(connection, &KostalModbusTcpConnection::initializationFinished, this, [=](bool success){ | ||
if (!success) { | ||
qCDebug(dcKostal()) << "Discovery: Initialization failed on" << networkDeviceInfo.address().toString() << "Continue...";; | ||
qCDebug(dcKostal()) << "Discovery: Initialization failed on" << address.toString() << "Continue...";; | ||
cleanupConnection(connection); | ||
return; | ||
} | ||
|
@@ -110,15 +100,15 @@ void KostalDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDeviceI | |
result.articleNumber = connection->inverterArticleNumber(); | ||
result.softwareVersionIoController = connection->softwareVersionIoController(); | ||
result.softwareVersionMainController = connection->softwareVersionMainController(); | ||
result.networkDeviceInfo = networkDeviceInfo; | ||
result.address = address; | ||
m_discoveryResults.append(result); | ||
|
||
qCDebug(dcKostal()) << "Discovery: --> Found" << result.manufacturerName << result.productName | ||
<< "Article:" << result.articleNumber | ||
<< "Serial number:" << result.serialNumber | ||
<< "Software version main controller:" << result.softwareVersionMainController | ||
<< "Software version IO controller:" << result.softwareVersionIoController | ||
<< result.networkDeviceInfo; | ||
<< result.address.toString(); | ||
|
||
|
||
// Done with this connection | ||
|
@@ -127,22 +117,22 @@ void KostalDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDeviceI | |
|
||
// Initializing... | ||
if (!connection->initialize()) { | ||
qCDebug(dcKostal()) << "Discovery: Unable to initialize connection on" << networkDeviceInfo.address().toString() << "Continue...";; | ||
qCDebug(dcKostal()) << "Discovery: Unable to initialize connection on" << address.toString() << "Continue...";; | ||
cleanupConnection(connection); | ||
} | ||
}); | ||
|
||
// If we get any error...skip this host... | ||
connect(connection->modbusTcpMaster(), &ModbusTcpMaster::connectionErrorOccurred, this, [=](QModbusDevice::Error error){ | ||
if (error != QModbusDevice::NoError) { | ||
qCDebug(dcKostal()) << "Discovery: Connection error on" << networkDeviceInfo.address().toString() << "Continue...";; | ||
qCDebug(dcKostal()) << "Discovery: Connection error on" << address.toString() << "Continue...";; | ||
cleanupConnection(connection); | ||
} | ||
}); | ||
|
||
// If check reachability failed...skip this host... | ||
connect(connection, &KostalModbusTcpConnection::checkReachabilityFailed, this, [=](){ | ||
qCDebug(dcKostal()) << "Discovery: Check reachability failed on" << networkDeviceInfo.address().toString() << "Continue...";; | ||
qCDebug(dcKostal()) << "Discovery: Check reachability failed on" << address.toString() << "Continue...";; | ||
cleanupConnection(connection); | ||
}); | ||
|
||
|
@@ -161,11 +151,15 @@ void KostalDiscovery::finishDiscovery() | |
{ | ||
qint64 durationMilliSeconds = QDateTime::currentMSecsSinceEpoch() - m_startDateTime.toMSecsSinceEpoch(); | ||
|
||
// Fill in all network device infos we have | ||
for (int i = 0; i < m_discoveryResults.count(); i++) | ||
m_discoveryResults[i].networkDeviceInfo = m_networkDeviceInfos.get(m_discoveryResults.at(i).address); | ||
|
||
// Cleanup any leftovers...we don't care any more | ||
foreach (KostalModbusTcpConnection *connection, m_connections) | ||
cleanupConnection(connection); | ||
|
||
qCInfo(dcKostal()) << "Discovery: Finished the discovery process. Found" << m_discoveryResults.count() << "Kostal Inverters in" << QTime::fromMSecsSinceStartOfDay(durationMilliSeconds).toString("mm:ss.zzz"); | ||
|
||
qCInfo(dcKostal()) << "Discovery: Finished the discovery process. Found" << m_discoveryResults.count() | ||
<< "Kostal Inverters in" << QTime::fromMSecsSinceStartOfDay(durationMilliSeconds).toString("mm:ss.zzz"); | ||
emit discoveryFinished(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | ||
* | ||
* Copyright 2013 - 2022, nymea GmbH | ||
* Copyright 2013 - 2024, nymea GmbH | ||
* Contact: [email protected] | ||
* | ||
* This file is part of nymea. | ||
|
@@ -50,6 +50,7 @@ class KostalDiscovery : public QObject | |
QString articleNumber; | ||
QString softwareVersionMainController; | ||
QString softwareVersionIoController; | ||
QHostAddress address; | ||
NetworkDeviceInfo networkDeviceInfo; | ||
} KostalDiscoveryResult; | ||
|
||
|
@@ -64,17 +65,14 @@ class KostalDiscovery : public QObject | |
NetworkDeviceDiscovery *m_networkDeviceDiscovery = nullptr; | ||
quint16 m_port; | ||
quint16 m_modbusAddress; | ||
|
||
QDateTime m_startDateTime; | ||
|
||
NetworkDeviceInfos m_networkDeviceInfos; | ||
NetworkDeviceInfos m_verifiedNetworkDeviceInfos; | ||
|
||
QList<KostalModbusTcpConnection *> m_connections; | ||
|
||
QList<KostalDiscoveryResult> m_discoveryResults; | ||
|
||
void checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo); | ||
void checkNetworkDevice(const QHostAddress &address); | ||
void cleanupConnection(KostalModbusTcpConnection *connection); | ||
|
||
void finishDiscovery(); | ||
|