Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mennekes: Update to networkdevice interface #193

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions mennekes/amtronecudiscovery.cpp
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: contact@nymea.io
*
* This file is part of nymea.
Expand Down Expand Up @@ -50,12 +50,12 @@ void AmtronECUDiscovery::startDiscovery()

NetworkDeviceDiscoveryReply *discoveryReply = m_networkDeviceDiscovery->discover();

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

connect(discoveryReply, &NetworkDeviceDiscoveryReply::hostAddressDiscovered, this, &AmtronECUDiscovery::checkNetworkDevice);
connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, discoveryReply, &NetworkDeviceDiscoveryReply::deleteLater);
connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, this, [=](){
qCDebug(dcMennekes()) << "Discovery: Network discovery finished. Found" << discoveryReply->networkDeviceInfos().count() << "network devices";
m_networkDeviceInfos = discoveryReply->networkDeviceInfos();
m_gracePeriodTimer.start();
discoveryReply->deleteLater();
});
}

Expand All @@ -64,24 +64,24 @@ QList<AmtronECUDiscovery::Result> AmtronECUDiscovery::discoveryResults() const
return m_discoveryResults;
}

void AmtronECUDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo)
void AmtronECUDiscovery::checkNetworkDevice(const QHostAddress &address)
{
int port = 502;
int slaveId = 0xff;
qCDebug(dcMennekes()) << "Discovery: Checking network device:" << networkDeviceInfo << "Port:" << port << "Slave ID:" << slaveId;
qCDebug(dcMennekes()) << "Discovery: Checking network device:" << address.toString() << "Port:" << port << "Slave ID:" << slaveId;

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

connect(connection, &AmtronECU::reachableChanged, this, [=](bool reachable){
connect(connection, &AmtronECU::reachableChanged, this, [this, connection, address](bool reachable){
if (!reachable) {
cleanupConnection(connection);
return;
}

connect(connection, &AmtronECU::initializationFinished, this, [=](bool success){
if (!success) {
qCDebug(dcMennekes()) << "Discovery: Initialization failed on" << networkDeviceInfo.address().toString();
qCDebug(dcMennekes()) << "Discovery: Initialization failed on" << address.toString();
cleanupConnection(connection);
return;
}
Expand All @@ -90,40 +90,40 @@ void AmtronECUDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDevi
result.detectedVersion = connection->detectedVersion();
result.firmwareVersion = QString::fromUtf8(QByteArray::fromHex(QByteArray::number(connection->firmwareVersion(), 16)));
result.model = connection->model();
result.networkDeviceInfo = networkDeviceInfo;
result.address = address;

// Note: the model is only known in firmware >= 5.22
// Some useres have to stay on 5.12 due to calibration law which is not available on 5.22
switch(connection->detectedVersion()) {
case AmtronECU::VersionOld:
qCDebug(dcMennekes()) << "Discovery: Found wallbox with old firmware version:" << result.firmwareVersion << result.networkDeviceInfo;
qCDebug(dcMennekes()) << "Discovery: Found wallbox with old firmware version:" << result.firmwareVersion << address.toString();
m_discoveryResults.append(result);
break;
case AmtronECU::VersionNew:
if (connection->model().isEmpty()) {
qCDebug(dcMennekes()) << "Discovery: Firmware version is >= 5.22 but the model could not be fetched. Skipping" << networkDeviceInfo.address();
qCDebug(dcMennekes()) << "Discovery: Firmware version is >= 5.22 but the model could not be fetched. Skipping" << address.toString();
break;
}

qCDebug(dcMennekes()) << "Discovery: Found wallbox with new firmware version:" << result.model << result.firmwareVersion << result.networkDeviceInfo;
m_discoveryResults.append(result);
break;
case AmtronECU::VersionUnknown:
qCDebug(dcMennekes()) << "Discovery: Firmware version or model invalid. Skipping" << networkDeviceInfo.address();
qCDebug(dcMennekes()) << "Discovery: Firmware version or model invalid. Skipping" << address.toString();
break;
}

cleanupConnection(connection);
});

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

connect(connection, &AmtronECU::checkReachabilityFailed, this, [=](){
qCDebug(dcMennekes()) << "Discovery: Checking reachability failed on" << networkDeviceInfo.address().toString();
qCDebug(dcMennekes()) << "Discovery: Checking reachability failed on" << address.toString();
cleanupConnection(connection);
});

Expand All @@ -141,6 +141,10 @@ void AmtronECUDiscovery::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 (AmtronECU *connection, m_connections)
cleanupConnection(connection);
Expand Down
12 changes: 6 additions & 6 deletions mennekes/amtronecudiscovery.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: contact@nymea.io
*
* This file is part of nymea.
Expand Down Expand Up @@ -42,14 +42,16 @@ class AmtronECUDiscovery : public QObject
{
Q_OBJECT
public:
explicit AmtronECUDiscovery(NetworkDeviceDiscovery *networkDeviceDiscovery, QObject *parent = nullptr);
struct Result {
AmtronECU::Version detectedVersion;
QString firmwareVersion;
QString model;
QHostAddress address;
NetworkDeviceInfo networkDeviceInfo;
};

explicit AmtronECUDiscovery(NetworkDeviceDiscovery *networkDeviceDiscovery, QObject *parent = nullptr);

void startDiscovery();

QList<Result> discoveryResults() const;
Expand All @@ -59,15 +61,13 @@ class AmtronECUDiscovery : public QObject

private:
NetworkDeviceDiscovery *m_networkDeviceDiscovery = nullptr;

QTimer m_gracePeriodTimer;
QDateTime m_startDateTime;

NetworkDeviceInfos m_networkDeviceInfos;
QList<AmtronECU *> m_connections;

QList<Result> m_discoveryResults;

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

void finishDiscovery();
Expand Down
33 changes: 18 additions & 15 deletions mennekes/amtronhcc3discovery.cpp
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: contact@nymea.io
*
* This file is part of nymea.
Expand Down Expand Up @@ -48,12 +48,12 @@ void AmtronHCC3Discovery::startDiscovery()
qCInfo(dcMennekes()) << "Discovery: Searching for AMTRON wallboxes in the network...";
NetworkDeviceDiscoveryReply *discoveryReply = m_networkDeviceDiscovery->discover();

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

connect(discoveryReply, &NetworkDeviceDiscoveryReply::hostAddressDiscovered, this, &AmtronHCC3Discovery::checkNetworkDevice);
connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, discoveryReply, &NetworkDeviceDiscoveryReply::deleteLater);
connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, this, [=](){
qCDebug(dcMennekes()) << "Discovery: Network discovery finished. Found" << discoveryReply->networkDeviceInfos().count() << "network devices";
m_networkDeviceInfos = discoveryReply->networkDeviceInfos();
m_gracePeriodTimer.start();
discoveryReply->deleteLater();
});
}

Expand All @@ -62,13 +62,13 @@ QList<AmtronHCC3Discovery::AmtronDiscoveryResult> AmtronHCC3Discovery::discovery
return m_discoveryResults;
}

void AmtronHCC3Discovery::checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo)
void AmtronHCC3Discovery::checkNetworkDevice(const QHostAddress &address)
{
int port = 502;
int slaveId = 0xff;
qCDebug(dcMennekes()) << "Checking network device:" << networkDeviceInfo << "Port:" << port << "Slave ID:" << slaveId;

AmtronHCC3ModbusTcpConnection *connection = new AmtronHCC3ModbusTcpConnection(networkDeviceInfo.address(), port, slaveId, this);
qCDebug(dcMennekes()) << "Checking network device:" << address << "Port:" << port << "Slave ID:" << slaveId;
AmtronHCC3ModbusTcpConnection *connection = new AmtronHCC3ModbusTcpConnection(address, port, slaveId, this);
m_connections.append(connection);

connect(connection, &AmtronHCC3ModbusTcpConnection::reachableChanged, this, [=](bool reachable){
Expand All @@ -81,40 +81,39 @@ void AmtronHCC3Discovery::checkNetworkDevice(const NetworkDeviceInfo &networkDev
// Modbus TCP connected...ok, let's try to initialize it!
connect(connection, &AmtronHCC3ModbusTcpConnection::initializationFinished, this, [=](bool success){
if (!success) {
qCDebug(dcMennekes()) << "Discovery: Initialization failed on" << networkDeviceInfo.address().toString();
qCDebug(dcMennekes()) << "Discovery: Initialization failed on" << address.toString();
cleanupConnection(connection);
return;
}
if (connection->serialNumber() == 0 || connection->name().isEmpty()) {
qCDebug(dcMennekes()) << "Serial number or name invalid. Skipping" << networkDeviceInfo.address();
qCDebug(dcMennekes()) << "Serial number or name invalid. Skipping" << address.toString();
cleanupConnection(connection);
return;
}

AmtronDiscoveryResult result;
result.wallboxName = connection->name();
result.serialNumber = connection->serialNumber();
result.networkDeviceInfo = networkDeviceInfo;
result.address = address;
m_discoveryResults.append(result);

qCDebug(dcMennekes()) << "Discovery: --> Found" << result.wallboxName
<< "Serial number:" << result.serialNumber
<< result.networkDeviceInfo;

<< result.address.toString();

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

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

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

Expand All @@ -133,12 +132,16 @@ void AmtronHCC3Discovery::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 (AmtronHCC3ModbusTcpConnection *connection, m_connections)
cleanupConnection(connection);

qCInfo(dcMennekes()) << "Discovery: Finished the discovery process. Found" << m_discoveryResults.count()
<< "AMTRON wallboxes in" << QTime::fromMSecsSinceStartOfDay(durationMilliSeconds).toString("mm:ss.zzz");
<< "AMTRON HCC3 wallboxes in" << QTime::fromMSecsSinceStartOfDay(durationMilliSeconds).toString("mm:ss.zzz");
m_gracePeriodTimer.stop();

emit discoveryFinished();
Expand Down
12 changes: 6 additions & 6 deletions mennekes/amtronhcc3discovery.h
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: contact@nymea.io
*
* This file is part of nymea.
Expand Down Expand Up @@ -42,13 +42,15 @@ class AmtronHCC3Discovery : public QObject
{
Q_OBJECT
public:
explicit AmtronHCC3Discovery(NetworkDeviceDiscovery *networkDeviceDiscovery, QObject *parent = nullptr);
typedef struct AmtronDiscoveryResult {
QString wallboxName;
QString serialNumber;
QHostAddress address;
NetworkDeviceInfo networkDeviceInfo;
} AmtronDiscoveryResult;

explicit AmtronHCC3Discovery(NetworkDeviceDiscovery *networkDeviceDiscovery, QObject *parent = nullptr);

void startDiscovery();

QList<AmtronDiscoveryResult> discoveryResults() const;
Expand All @@ -58,15 +60,13 @@ class AmtronHCC3Discovery : public QObject

private:
NetworkDeviceDiscovery *m_networkDeviceDiscovery = nullptr;

QTimer m_gracePeriodTimer;
QDateTime m_startDateTime;

NetworkDeviceInfos m_networkDeviceInfos;
QList<AmtronHCC3ModbusTcpConnection *> m_connections;

QList<AmtronDiscoveryResult> m_discoveryResults;

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

void finishDiscovery();
Expand Down
Loading