Skip to content

Commit

Permalink
EVerest: Update to networkdevice interface
Browse files Browse the repository at this point in the history
  • Loading branch information
t-mon committed Dec 10, 2024
1 parent 3995238 commit 14b0455
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 50 deletions.
13 changes: 10 additions & 3 deletions everest/everestclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,18 @@ 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 {
break;
case NetworkDeviceInfo::MonitorModeHostName:
debug.nospace() << everestClient->monitor()->networkDeviceInfo().hostName() << ", ";
debug.nospace() << everestClient->monitor()->networkDeviceInfo().address().toString() << ", ";
break;
case NetworkDeviceInfo::MonitorModeIp:
debug.nospace() << everestClient->address().toString() << ", ";
break;
}

debug.nospace() << "MQTT connected: " << everestClient->client()->isConnected() << ")";
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
55 changes: 35 additions & 20 deletions everest/integrationplugineverest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@

#include <network/networkdevicediscovery.h>

IntegrationPluginTruffle::IntegrationPluginTruffle()
IntegrationPluginEverest::IntegrationPluginEverest()
{

}

void IntegrationPluginTruffle::init()
void IntegrationPluginEverest::init()
{

}

void IntegrationPluginTruffle::startMonitoringAutoThings()
void IntegrationPluginEverest::startMonitoringAutoThings()
{
// Check on localhost if there is any EVerest instance running and if we have to set up a thing for this EV charger
// Since this integration plugin is most luikly running on an EV charger running EVerest, the local instance should
Expand Down Expand Up @@ -108,7 +108,7 @@ void IntegrationPluginTruffle::startMonitoringAutoThings()
discovery->startLocalhost();
}

void IntegrationPluginTruffle::discoverThings(ThingDiscoveryInfo *info)
void IntegrationPluginEverest::discoverThings(ThingDiscoveryInfo *info)
{
qCDebug(dcEverest()) << "Start discovering Everest systems in the local network";
if (!hardwareManager()->networkDeviceDiscovery()->available()) {
Expand All @@ -127,22 +127,36 @@ void IntegrationPluginTruffle::discoverThings(ThingDiscoveryInfo *info)
foreach(const QString &connectorName, result.connectors) {

QString title = QString("Everest (%1)").arg(connectorName);
QString description = result.networkDeviceInfo.address().toString() +
" " + result.networkDeviceInfo.macAddress();
ThingDescriptor descriptor(everestThingClassId, title, description);
QString description;
MacAddressInfo macInfo;

switch (result.networkDeviceInfo.monitorMode()) {
case NetworkDeviceInfo::MonitorModeMac:
macInfo = result.networkDeviceInfo.macAddressInfos().constFirst();
description = result.networkDeviceInfo.address().toString();
if (!macInfo.vendorName().isEmpty())
description += " - " + result.networkDeviceInfo.macAddressInfos().constFirst().vendorName();

break;
case NetworkDeviceInfo::MonitorModeHostName:
description = result.networkDeviceInfo.address().toString();
break;
case NetworkDeviceInfo::MonitorModeIp:
description = "Interface: " + result.networkDeviceInfo.networkInterface().name();
break;
}

ThingDescriptor descriptor(everestThingClassId, title, description);
qCInfo(dcEverest()) << "Discovered -->" << title << description;

// Note: the network device info already provides the correct set of parameters in order to be used by the monitor
// depending on the possibilities within this network. It is not recommended to fill in all information available.
// Only the information available depending on the monitor mode are relevant for the monitor.
ParamList params;
params.append(Param(everestThingConnectorParamTypeId, connectorName));

if (!MacAddress(result.networkDeviceInfo.macAddress()).isNull())
params.append(Param(everestThingMacParamTypeId, result.networkDeviceInfo.macAddress()));

if (!result.networkDeviceInfo.address().isNull())
params.append(Param(everestThingAddressParamTypeId,
result.networkDeviceInfo.address().toString()));

params.append(Param(everestThingMacAddressParamTypeId, result.networkDeviceInfo.thingParamValueMacAddress()));
params.append(Param(everestThingHostNameParamTypeId, result.networkDeviceInfo.thingParamValueHostName()));
params.append(Param(everestThingAddressParamTypeId, result.networkDeviceInfo.thingParamValueAddress()));
descriptor.setParams(params);

// Let's check if we aleardy have a thing with those params
Expand Down Expand Up @@ -179,12 +193,13 @@ void IntegrationPluginTruffle::discoverThings(ThingDiscoveryInfo *info)
discovery->start();
}

void IntegrationPluginTruffle::setupThing(ThingSetupInfo *info)
void IntegrationPluginEverest::setupThing(ThingSetupInfo *info)
{
Thing *thing = info->thing();

QHostAddress address(thing->paramValue(everestThingAddressParamTypeId).toString());
MacAddress macAddress(thing->paramValue(everestThingMacParamTypeId).toString());
MacAddress macAddress(thing->paramValue(everestThingMacAddressParamTypeId).toString());
QString hostName(thing->paramValue(everestThingHostNameParamTypeId).toString());
QString connector(thing->paramValue(everestThingConnectorParamTypeId).toString());

if (!macAddress.isNull()) {
Expand All @@ -205,7 +220,7 @@ void IntegrationPluginTruffle::setupThing(ThingSetupInfo *info)
qCDebug(dcEverest()) << "Creating new mac address based everst client";
everstClient = new EverestClient(this);
everstClient->setMacAddress(macAddress);
everstClient->setMonitor(hardwareManager()->networkDeviceDiscovery()->registerMonitor(macAddress));
everstClient->setMonitor(hardwareManager()->networkDeviceDiscovery()->registerMonitor(thing));
m_everstClients.append(everstClient);
everstClient->start();
}
Expand Down Expand Up @@ -249,7 +264,7 @@ void IntegrationPluginTruffle::setupThing(ThingSetupInfo *info)
}
}

void IntegrationPluginTruffle::executeAction(ThingActionInfo *info)
void IntegrationPluginEverest::executeAction(ThingActionInfo *info)
{
qCDebug(dcEverest()) << "Executing action for thing" << info->thing()
<< info->action().actionTypeId().toString() << info->action().params();
Expand Down Expand Up @@ -298,7 +313,7 @@ void IntegrationPluginTruffle::executeAction(ThingActionInfo *info)
info->finish(Thing::ThingErrorNoError);
}

void IntegrationPluginTruffle::thingRemoved(Thing *thing)
void IntegrationPluginEverest::thingRemoved(Thing *thing)
{
qCDebug(dcEverest()) << "Remove thing" << thing;
if (thing->thingClassId() == everestThingClassId) {
Expand Down
4 changes: 2 additions & 2 deletions everest/integrationplugineverest.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@

#include <mqttclient.h>

class IntegrationPluginTruffle: public IntegrationPlugin
class IntegrationPluginEverest: public IntegrationPlugin
{
Q_OBJECT

Q_PLUGIN_METADATA(IID "io.nymea.IntegrationPlugin" FILE "integrationplugineverest.json")
Q_INTERFACES(IntegrationPlugin)

public:
explicit IntegrationPluginTruffle();
explicit IntegrationPluginEverest();

void init() override;
void startMonitoringAutoThings() override;
Expand Down
16 changes: 11 additions & 5 deletions everest/integrationplugineverest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,29 @@
"id": "965cbe0d-088c-42a2-965d-ceafbb8b01e9",
"setupMethod": "JustAdd",
"createMethods": ["discovery", "user"],
"interfaces": [ "evcharger", "smartmeterconsumer", "connectable" ],
"interfaces": [ "evcharger", "smartmeterconsumer", "networkdevice", "connectable" ],
"paramTypes": [
{
"id": "f1f3e9a7-3a35-4089-8869-b9bfd64659e5",
"name": "hostName",
"displayName": "Host name",
"type": "QString"
},
{
"id": "911b6fa3-010c-486e-8251-71a6aa21adb3",
"name": "address",
"displayName": "Host address",
"displayName": "IP address",
"type": "QString",
"inputType": "IPv4Address",
"defaultValue": ""
},
{
"id": "cb9517ef-1ae2-49c9-9036-0c6e15bb3652",
"name": "mac",
"name": "macAddress",
"displayName": "MAC address",
"type": "QString",
"readOnly": true,
"defaultValue": "00:00:00:00:00:00"
"defaultValue": "00:00:00:00:00:00",
"readOnly": true
},
{
"id": "73f27e36-6f68-40a9-8805-22e88911736c",
Expand Down

0 comments on commit 14b0455

Please sign in to comment.