diff --git a/amperfied/amperfiedconnectdiscovery.cpp b/amperfied/amperfiedconnectdiscovery.cpp index 8a6cb679..552e1906 100644 --- a/amperfied/amperfiedconnectdiscovery.cpp +++ b/amperfied/amperfiedconnectdiscovery.cpp @@ -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(); }); @@ -63,13 +64,14 @@ QList 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){ @@ -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; } @@ -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); }); @@ -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); diff --git a/amperfied/amperfiedconnectdiscovery.h b/amperfied/amperfiedconnectdiscovery.h index 47ea0a2a..6bc6d0fe 100644 --- a/amperfied/amperfiedconnectdiscovery.h +++ b/amperfied/amperfiedconnectdiscovery.h @@ -1,6 +1,6 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -* Copyright 2013 - 2023, nymea GmbH +* Copyright 2013 - 2024, nymea GmbH * Contact: contact@nymea.io * * This file is part of nymea. @@ -47,6 +47,7 @@ class AmperfiedConnectDiscovery : public QObject quint16 firmwareVersion; quint16 slaveId; QString modelName; + QHostAddress address; NetworkDeviceInfo networkDeviceInfo; }; @@ -65,10 +66,11 @@ class AmperfiedConnectDiscovery : public QObject QString m_nameFilter; QList m_connections; + NetworkDeviceInfos m_networkDeviceInfos; QList m_discoveryResults; - void checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo); + void checkNetworkDevice(const QHostAddress &address); void cleanupConnection(AmperfiedModbusTcpConnection *connection); void finishDiscovery(); diff --git a/amperfied/integrationpluginamperfied.cpp b/amperfied/integrationpluginamperfied.cpp index 15450a5b..d24b63e3 100644 --- a/amperfied/integrationpluginamperfied.cpp +++ b/amperfied/integrationpluginamperfied.cpp @@ -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); @@ -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); } @@ -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; diff --git a/amperfied/integrationpluginamperfied.json b/amperfied/integrationpluginamperfied.json index 652dd73f..b1244444 100644 --- a/amperfied/integrationpluginamperfied.json +++ b/amperfied/integrationpluginamperfied.json @@ -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": "" } ], @@ -218,7 +234,7 @@ "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", @@ -226,6 +242,21 @@ "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": [ @@ -328,7 +359,7 @@ "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", @@ -336,6 +367,21 @@ "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": [