Skip to content

Commit

Permalink
Merge PR #752: EVerest: Update to networkdevice interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins committed Jan 14, 2025
2 parents a25f8de + 34fabef commit d1ec470
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 196 deletions.
50 changes: 19 additions & 31 deletions everest/everestclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@
EverestClient::EverestClient(QObject *parent)
: QObject{parent}
{
m_client = new MqttClient("nymea-" + QUuid::createUuid().toString().left(8), 300,
QString(), QByteArray(), Mqtt::QoS0, false, this);
m_client = new MqttClient("nymea-" + QUuid::createUuid().toString().left(8), 300, QString(), QByteArray(), Mqtt::QoS0, false, this);

connect(m_client, &MqttClient::disconnected, this, [this](){
qCDebug(dcEverest()) << "The MQTT client is now disconnected" << this;
if (!m_address.isNull()) {
if (m_monitor->reachable()) {
// Start the reconnect timer
qCDebug(dcEverest()) << "Starting reconnect timer for mqtt connection to" << m_address.toString();
qCDebug(dcEverest()) << "Starting reconnect timer for mqtt connection to" << m_monitor->networkDeviceInfo().address().toString();
m_reconnectTimer.start();
}
});
Expand All @@ -68,7 +67,7 @@ EverestClient::EverestClient(QObject *parent)
return;
}

m_client->connectToHost(m_address.toString(), m_port);
m_client->connectToHost(m_monitor->networkDeviceInfo().address().toString(), m_port);
});
}

Expand Down Expand Up @@ -123,26 +122,6 @@ Everest *EverestClient::getEverest(Thing *thing) const
return m_everests.value(thing);
}

QHostAddress EverestClient::address() const
{
return m_address;
}

void EverestClient::setAddress(const QHostAddress &address)
{
m_address = address;
}

MacAddress EverestClient::macAddress() const
{
return m_macAddress;
}

void EverestClient::setMacAddress(const MacAddress &macAddress)
{
m_macAddress = macAddress;
}

NetworkDeviceMonitor *EverestClient::monitor() const
{
return m_monitor;
Expand All @@ -168,8 +147,8 @@ void EverestClient::start()
m_client->connectToHost(m_monitor->networkDeviceInfo().address().toString(), m_port);
}
} else {
qCDebug(dcEverest()) << "Connecting MQTT client to" << m_address.toString();
m_client->connectToHost(m_address.toString(), m_port);
qCDebug(dcEverest()) << "Connecting MQTT client to" << m_monitor->networkDeviceInfo().address().toString();
m_client->connectToHost(m_monitor->networkDeviceInfo().address().toString(), m_port);

// Note: on connected this will be stopped, otherwise we want the timer running
m_reconnectTimer.start();
Expand Down Expand Up @@ -201,18 +180,27 @@ void EverestClient::onMonitorReachableChanged(bool reachable)
m_client->disconnectFromHost();

m_client->connectToHost(m_monitor->networkDeviceInfo().address().toString(), m_port);
} else {
m_reconnectTimer.stop();
}
}

QDebug operator<<(QDebug debug, EverestClient *everestClient)
{
QDebugStateSaver saver(debug);
debug.nospace() << "EverestClient(";
if (everestClient->monitor()) {
debug.nospace() << everestClient->monitor()->networkDeviceInfo().macAddress() << ", ";
switch(everestClient->monitor()->monitorMode()) {
case NetworkDeviceInfo::MonitorModeMac:
debug.nospace() << everestClient->monitor()->networkDeviceInfo().macAddressInfos().constFirst() << ", ";
debug.nospace() << everestClient->monitor()->networkDeviceInfo().address().toString() << ", ";
} else {
debug.nospace() << everestClient->address().toString() << ", ";
break;
case NetworkDeviceInfo::MonitorModeHostName:
debug.nospace() << everestClient->monitor()->networkDeviceInfo().hostName() << ", ";
debug.nospace() << everestClient->monitor()->networkDeviceInfo().address().toString() << ", ";
break;
case NetworkDeviceInfo::MonitorModeIp:
debug.nospace() << everestClient->monitor()->networkDeviceInfo().address().toString() << ", ";
break;
}

debug.nospace() << "MQTT connected: " << everestClient->client()->isConnected() << ")";
Expand Down
12 changes: 0 additions & 12 deletions everest/everestclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ class EverestClient : public QObject

Everest *getEverest(Thing *thing) const;

QHostAddress address() const;
void setAddress(const QHostAddress &address);

MacAddress macAddress() const;
void setMacAddress(const MacAddress &macAddress);

NetworkDeviceMonitor *monitor() const;
void setMonitor(NetworkDeviceMonitor *monitor);

Expand All @@ -83,12 +77,6 @@ private slots:
bool m_running = false;

QHash<Thing *, Everest *> m_everests;

// One of both must be defined, never both
QHostAddress m_address;
MacAddress m_macAddress;

// Available for mac based connections
NetworkDeviceMonitor *m_monitor = nullptr;
};

Expand Down
39 changes: 21 additions & 18 deletions everest/everestdiscovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ void EverestDiscovery::start()

NetworkDeviceDiscoveryReply *discoveryReply = m_networkDeviceDiscovery->discover();

connect(discoveryReply, &NetworkDeviceDiscoveryReply::networkDeviceInfoAdded, this, &EverestDiscovery::checkNetworkDevice);
connect(discoveryReply, &NetworkDeviceDiscoveryReply::hostAddressDiscovered, this, &EverestDiscovery::checkHostAddress);
connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, discoveryReply, &NetworkDeviceDiscoveryReply::deleteLater);
connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, this, [=](){
qCDebug(dcEverest()) << "Discovery: Network discovery finished. Found"
<< discoveryReply->networkDeviceInfos().count() << "network devices";
connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, this, [discoveryReply, this](){
qCDebug(dcEverest()) << "Discovery: Network device discovery finished. Found" << discoveryReply->networkDeviceInfos().count() << "network devices";
m_networkDeviceInfos = discoveryReply->networkDeviceInfos();

// Give the last connections added right before the network discovery finished a chance to check the device...
QTimer::singleShot(3000, this, [this](){
Expand All @@ -65,8 +65,7 @@ void EverestDiscovery::start()

// For development, check local host
NetworkDeviceInfo localHostInfo;
localHostInfo.setAddress(QHostAddress::LocalHost);
checkNetworkDevice(localHostInfo);
checkHostAddress(QHostAddress::LocalHost);
}

void EverestDiscovery::startLocalhost()
Expand All @@ -77,26 +76,25 @@ void EverestDiscovery::startLocalhost()

// For development, check local host
NetworkDeviceInfo localHostInfo;
localHostInfo.setAddress(QHostAddress::LocalHost);
checkNetworkDevice(localHostInfo);
checkHostAddress(QHostAddress::LocalHost);
}

QList<EverestDiscovery::Result> EverestDiscovery::results() const
{
return m_results;
}

void EverestDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo)
void EverestDiscovery::checkHostAddress(const QHostAddress &address)
{
MqttClient *client = new MqttClient("nymea-" + QUuid::createUuid().toString().left(8), 300,
QString(), QByteArray(), Mqtt::QoS0, false, this);
client->setAutoReconnect(false);

m_clients.append(client);

connect(client, &MqttClient::error, this, [this, client, networkDeviceInfo](QAbstractSocket::SocketError socketError){
connect(client, &MqttClient::error, this, [this, client, address](QAbstractSocket::SocketError socketError){
qCDebug(dcEverest()) << "Discovery: MQTT client error occurred on"
<< networkDeviceInfo.address().toString() << socketError
<< address.toString() << socketError
<< "...skip connection";
// We give up on the first error here
cleanupClient(client);
Expand All @@ -110,11 +108,11 @@ void EverestDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDevice
cleanupClient(client);
});

connect(client, &MqttClient::connected, this, [this, client, networkDeviceInfo](){
connect(client, &MqttClient::connected, this, [this, client, address](){
// We found a mqtt server, let's check if we find everest_api module on it...
qCDebug(dcEverest()) << "Discovery: Successfully connected to host" << networkDeviceInfo;
qCDebug(dcEverest()) << "Discovery: Successfully connected to host" << address.toString();

connect(client, &MqttClient::publishReceived, client, [this, client, networkDeviceInfo]
connect(client, &MqttClient::publishReceived, client, [this, client, address]
(const QString &topic, const QByteArray &payload, bool retained) {

qCDebug(dcEverest()) << "Discovery: Received publish on" << topic
Expand All @@ -131,9 +129,9 @@ void EverestDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDevice
}

QStringList connectors = jsonDoc.toVariant().toStringList();
qCInfo(dcEverest()) << "Discovery: Found Everest on" << networkDeviceInfo << connectors;
qCInfo(dcEverest()) << "Discovery: Found Everest on" << address.toString() << connectors;
Result result;
result.networkDeviceInfo = networkDeviceInfo;
result.address = address;
result.connectors = connectors;
m_results.append(result);

Expand All @@ -159,10 +157,11 @@ void EverestDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDevice
// });
});

qCDebug(dcEverest()) << "Discovery: Verifying host" << networkDeviceInfo;
client->connectToHost(networkDeviceInfo.address().toString(), 1883);
qCDebug(dcEverest()) << "Discovery: Verifying host" << address.toString();
client->connectToHost(address.toString(), 1883);
}


void EverestDiscovery::cleanupClient(MqttClient *client)
{
if (!m_clients.contains(client))
Expand All @@ -186,6 +185,10 @@ void EverestDiscovery::finishDiscovery()
foreach (MqttClient *client, m_clients)
cleanupClient(client);

// Update results with final network device infos
for (int i = 0; i < m_results.count(); i++)
m_results[i].networkDeviceInfo = m_networkDeviceInfos.get(m_results.at(i).address);

qCInfo(dcEverest()) << "Discovery: Finished the discovery process. Found"
<< m_results.count() << "Everest instances in"
<< QTime::fromMSecsSinceStartOfDay(durationMilliSeconds).toString("mm:ss.zzz");
Expand Down
5 changes: 3 additions & 2 deletions everest/everestdiscovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ class EverestDiscovery : public QObject
Q_OBJECT
public:
typedef struct Result {
QHostAddress address;
QStringList connectors;
NetworkDeviceInfo networkDeviceInfo;
} Result;

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

void start();

void startLocalhost();

QList<EverestDiscovery::Result> results() const;
Expand All @@ -62,12 +62,13 @@ class EverestDiscovery : public QObject
QDateTime m_startDateTime;
QList<EverestDiscovery::Result> m_results;
QList<MqttClient *> m_clients;
NetworkDeviceInfos m_networkDeviceInfos;

bool m_localhostDiscovery = false;

QString m_everestApiModuleTopicConnectors = "everest_api/connectors";

void checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo);
void checkHostAddress(const QHostAddress &address);
void cleanupClient(MqttClient *client);
void finishDiscovery();
};
Expand Down
Loading

0 comments on commit d1ec470

Please sign in to comment.