Skip to content

Commit

Permalink
Merge PR #189: Amperfied: Update to networkdevice interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins committed Dec 18, 2024
2 parents 6efef7d + ba71cac commit d0b7b9a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 20 deletions.
25 changes: 15 additions & 10 deletions amperfied/amperfiedconnectdiscovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ void AmperfiedConnectDiscovery::startDiscovery(const QString &nameFilter)
m_nameFilter = nameFilter;
NetworkDeviceDiscoveryReply *discoveryReply = m_networkDeviceDiscovery->discover();

connect(discoveryReply, &NetworkDeviceDiscoveryReply::networkDeviceInfoAdded, this, &AmperfiedConnectDiscovery::checkNetworkDevice);
connect(discoveryReply, &NetworkDeviceDiscoveryReply::hostAddressDiscovered, this, &AmperfiedConnectDiscovery::checkNetworkDevice);

connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, this, [=](){
qCDebug(dcAmperfied()) << "Discovery: Network discovery finished. Found" << discoveryReply->networkDeviceInfos().count() << "network devices";
m_networkDeviceInfos = discoveryReply->networkDeviceInfos();
m_gracePeriodTimer.start();
discoveryReply->deleteLater();
});
Expand All @@ -63,13 +64,14 @@ QList<AmperfiedConnectDiscovery::Result> AmperfiedConnectDiscovery::discoveryRes
return m_discoveryResults;
}

void AmperfiedConnectDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo)
void AmperfiedConnectDiscovery::checkNetworkDevice(const QHostAddress &address)
{
int port = 502;
int slaveId = 1;
qCDebug(dcAmperfied()) << "Checking network device:" << networkDeviceInfo << "Port:" << port << "Slave ID:" << slaveId;

AmperfiedModbusTcpConnection *connection = new AmperfiedModbusTcpConnection(networkDeviceInfo.address(), port, slaveId, this);
qCDebug(dcAmperfied()) << "Checking network device:" << address.toString() << "Port:" << port << "Slave ID:" << slaveId;

AmperfiedModbusTcpConnection *connection = new AmperfiedModbusTcpConnection(address, port, slaveId, this);
m_connections.append(connection);

connect(connection, &AmperfiedModbusTcpConnection::reachableChanged, this, [=](bool reachable){
Expand All @@ -82,7 +84,7 @@ void AmperfiedConnectDiscovery::checkNetworkDevice(const NetworkDeviceInfo &netw
// Modbus TCP connected...ok, let's try to initialize it!
connect(connection, &AmperfiedModbusTcpConnection::initializationFinished, this, [=](bool success){
if (!success) {
qCDebug(dcAmperfied()) << "Discovery: Initialization failed on" << networkDeviceInfo.address().toString();
qCDebug(dcAmperfied()) << "Discovery: Initialization failed on" << address.toString();
cleanupConnection(connection);
return;
}
Expand All @@ -102,28 +104,27 @@ void AmperfiedConnectDiscovery::checkNetworkDevice(const NetworkDeviceInfo &netw
Result result;
result.firmwareVersion = connection->version();
result.modelName = connection->logisticString();
result.networkDeviceInfo = networkDeviceInfo;
result.address = address;
m_discoveryResults.append(result);

qCDebug(dcAmperfied()) << "Discovery: --> Found"
<< result.modelName
<< "Version:" << result.firmwareVersion
<< result.networkDeviceInfo;

<< result.address.toString();

// Done with this connection
cleanupConnection(connection);
});

if (!connection->initialize()) {
qCDebug(dcAmperfied()) << "Discovery: Unable to initialize connection on" << networkDeviceInfo.address().toString();
qCDebug(dcAmperfied()) << "Discovery: Unable to initialize connection on" << address.toString();
cleanupConnection(connection);
}
});

// If check reachability failed...skip this host...
connect(connection, &AmperfiedModbusTcpConnection::checkReachabilityFailed, this, [=](){
qCDebug(dcAmperfied()) << "Discovery: Checking reachability failed on" << networkDeviceInfo.address().toString();
qCDebug(dcAmperfied()) << "Discovery: Checking reachability failed on" << address.toString();
cleanupConnection(connection);
});

Expand All @@ -142,6 +143,10 @@ void AmperfiedConnectDiscovery::finishDiscovery()
{
qint64 durationMilliSeconds = QDateTime::currentMSecsSinceEpoch() - m_startDateTime.toMSecsSinceEpoch();

// Fill in finished network device information
for (int i = 0; i < m_discoveryResults.count(); i++)
m_discoveryResults[i].networkDeviceInfo = m_networkDeviceInfos.get(m_discoveryResults.value(i).address);

// Cleanup any leftovers...we don't care any more
foreach (AmperfiedModbusTcpConnection *connection, m_connections)
cleanupConnection(connection);
Expand Down
6 changes: 4 additions & 2 deletions amperfied/amperfiedconnectdiscovery.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright 2013 - 2023, nymea GmbH
* Copyright 2013 - 2024, nymea GmbH
* Contact: [email protected]
*
* This file is part of nymea.
Expand Down Expand Up @@ -47,6 +47,7 @@ class AmperfiedConnectDiscovery : public QObject
quint16 firmwareVersion;
quint16 slaveId;
QString modelName;
QHostAddress address;
NetworkDeviceInfo networkDeviceInfo;
};

Expand All @@ -65,10 +66,11 @@ class AmperfiedConnectDiscovery : public QObject
QString m_nameFilter;

QList<AmperfiedModbusTcpConnection *> m_connections;
NetworkDeviceInfos m_networkDeviceInfos;

QList<Result> m_discoveryResults;

void checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo);
void checkNetworkDevice(const QHostAddress &address);
void cleanupConnection(AmperfiedModbusTcpConnection *connection);

void finishDiscovery();
Expand Down
30 changes: 25 additions & 5 deletions amperfied/integrationpluginamperfied.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,29 @@ void IntegrationPluginAmperfied::discoverThings(ThingDiscoveryInfo *info)
qCInfo(dcAmperfied()) << "Discovery results:" << discovery->discoveryResults().count();

foreach (const AmperfiedConnectDiscovery::Result &result, discovery->discoveryResults()) {
ThingDescriptor descriptor(info->thingClassId(), "Amperfied " + result.modelName, QString("MAC: %1").arg(result.networkDeviceInfo.macAddress()));
QString description;
switch (result.networkDeviceInfo.monitorMode()) {
case NetworkDeviceInfo::MonitorModeMac:
description = "MAC " + result.networkDeviceInfo.macAddressInfos().constFirst().macAddress().toString();
break;
case NetworkDeviceInfo::MonitorModeHostName:
description = "Host name " + result.networkDeviceInfo.hostName();
break;
case NetworkDeviceInfo::MonitorModeIp:
description = "IP " + result.networkDeviceInfo.address().toString();
break;
}

ThingDescriptor descriptor(info->thingClassId(), "Amperfied " + result.modelName, description);

ParamTypeId macAddressParamTypeId = thingClass(info->thingClassId()).paramTypes().findByName("macAddress").id();
ParamList params{
{macAddressParamTypeId, result.networkDeviceInfo.macAddress()}
};
ParamTypeId hostNameParamTypeId = thingClass(info->thingClassId()).paramTypes().findByName("hostName").id();
ParamTypeId addressParamTypeId = thingClass(info->thingClassId()).paramTypes().findByName("address").id();

ParamList params;
params.append(Param(macAddressParamTypeId, result.networkDeviceInfo.thingParamValueMacAddress()));
params.append(Param(hostNameParamTypeId, result.networkDeviceInfo.thingParamValueHostName()));
params.append(Param(addressParamTypeId, result.networkDeviceInfo.thingParamValueAddress()));
descriptor.setParams(params);

Thing *existingThing = myThings().findByParams(params);
Expand Down Expand Up @@ -138,7 +155,7 @@ void IntegrationPluginAmperfied::setupThing(ThingSetupInfo *info)

NetworkDeviceMonitor *monitor = m_monitors.value(info->thing());
if (!monitor) {
monitor = hardwareManager()->networkDeviceDiscovery()->registerMonitor(MacAddress(thing->paramValue("macAddress").toString()));
monitor = hardwareManager()->networkDeviceDiscovery()->registerMonitor(thing);
m_monitors.insert(thing, monitor);
}

Expand Down Expand Up @@ -286,6 +303,9 @@ void IntegrationPluginAmperfied::thingRemoved(Thing *thing)
delete m_tcpConnections.take(thing);
}

if (m_monitors.contains(thing))
hardwareManager()->networkDeviceDiscovery()->unregisterMonitor(m_monitors.take(thing));

if (myThings().isEmpty() && m_pluginTimer) {
hardwareManager()->pluginTimerManager()->unregisterTimer(m_pluginTimer);
m_pluginTimer = nullptr;
Expand Down
52 changes: 49 additions & 3 deletions amperfied/integrationpluginamperfied.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,29 @@
"displayName": "connect.home",
"id": "f8805308-1ddd-496c-bea3-ef9163357bfa",
"createMethods": ["discovery", "user"],
"interfaces": ["evcharger", "smartmeterconsumer", "connectable"],
"interfaces": ["evcharger", "smartmeterconsumer", "connectable", "networkdevice"],
"paramTypes": [
{
"id": "b4b0556e-0d5d-4951-b5cd-e0c7986b8dcf",
"name":"macAddress",
"displayName": "MAC address",
"type": "QString",
"inputType": "MacAddress",
"defaultValue": ""
},
{
"id": "134d8e0f-2c57-4318-8dc8-8a9f0779a589",
"name": "address",
"displayName": "Host address",
"type": "QString",
"inputType": "IPv4Address",
"defaultValue": ""
},
{
"id": "c6fdceef-259a-4a7f-8efd-78396171123c",
"name": "hostName",
"displayName": "Host name",
"type": "QString",
"defaultValue": ""
}
],
Expand Down Expand Up @@ -218,14 +234,29 @@
"displayName": "connect.business",
"id": "18e6d077-7cb5-4409-8c4b-b14ce7ddac9f",
"createMethods": ["discovery", "user"],
"interfaces": ["evcharger", "smartmeterconsumer", "connectable"],
"interfaces": ["evcharger", "smartmeterconsumer", "connectable", "networkdevice"],
"paramTypes": [
{
"id": "d8545dfe-65c7-44bc-9ab7-642bfdccf540",
"name":"macAddress",
"displayName": "MAC address",
"type": "QString",
"defaultValue": ""
},
{
"id": "7a3199da-22d7-4629-a58a-0f5ef5089780",
"name": "address",
"displayName": "Host address",
"type": "QString",
"inputType": "IPv4Address",
"defaultValue": ""
},
{
"id": "112d99f9-31a9-4645-99f4-8e4db52ce7b5",
"name": "hostName",
"displayName": "Host name",
"type": "QString",
"defaultValue": ""
}
],
"stateTypes": [
Expand Down Expand Up @@ -328,14 +359,29 @@
"displayName": "connect.solar",
"id": "0bfdf18d-5a5e-4c5d-aab1-139e96a5fec0",
"createMethods": ["discovery", "user"],
"interfaces": ["evcharger", "smartmeterconsumer", "connectable"],
"interfaces": ["evcharger", "smartmeterconsumer", "connectable", "networkdevice"],
"paramTypes": [
{
"id": "72f82b3a-0bb0-476f-a2f9-9cde4de0ef6f",
"name":"macAddress",
"displayName": "MAC address",
"type": "QString",
"defaultValue": ""
},
{
"id": "a9a217fa-fe12-4fdc-ab89-710b4a884d7a",
"name": "address",
"displayName": "Host address",
"type": "QString",
"inputType": "IPv4Address",
"defaultValue": ""
},
{
"id": "464a777b-9e15-4e23-aec1-d6df6ce54991",
"name": "hostName",
"displayName": "Host name",
"type": "QString",
"defaultValue": ""
}
],
"stateTypes": [
Expand Down

0 comments on commit d0b7b9a

Please sign in to comment.