EVerest: Autosetup connectors on localhost

Signed-off-by: Simon Stürz <simon.stuerz@nymea.io>
This commit is contained in:
Simon Stürz 2024-11-21 16:41:07 +01:00
parent a35312b7f2
commit 952c87a0d9
3 changed files with 84 additions and 1 deletions

View File

@ -69,6 +69,18 @@ void EverestDiscovery::start()
checkNetworkDevice(localHostInfo);
}
void EverestDiscovery::startLocalhost()
{
qCInfo(dcEverest()) << "Discovery: Start discovering EVerest on localhost ...";
m_startDateTime = QDateTime::currentDateTime();
m_localhostDiscovery = true;
// For development, check local host
NetworkDeviceInfo localHostInfo;
localHostInfo.setAddress(QHostAddress::LocalHost);
checkNetworkDevice(localHostInfo);
}
QList<EverestDiscovery::Result> EverestDiscovery::results() const
{
return m_results;
@ -88,6 +100,10 @@ void EverestDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDevice
<< "...skip connection";
// We give up on the first error here
cleanupClient(client);
if (m_localhostDiscovery) {
finishDiscovery();
}
});
connect(client, &MqttClient::disconnected, this, [this, client](){
@ -156,6 +172,10 @@ void EverestDiscovery::cleanupClient(MqttClient *client)
client->disconnectFromHost();
client->deleteLater();
if (m_localhostDiscovery) {
finishDiscovery();
}
}
void EverestDiscovery::finishDiscovery()

View File

@ -50,6 +50,8 @@ public:
void start();
void startLocalhost();
QList<EverestDiscovery::Result> results() const;
signals:
@ -61,6 +63,8 @@ private:
QList<EverestDiscovery::Result> m_results;
QList<MqttClient *> m_clients;
bool m_localhostDiscovery = false;
QString m_everestApiModuleTopicConnectors = "everest_api/connectors";
void checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo);

View File

@ -46,7 +46,66 @@ void IntegrationPluginTruffle::init()
void IntegrationPluginTruffle::startMonitoringAutoThings()
{
// TODO: auto setup everest instance running on localhost
// 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
// be set up automatically. Additional instances in the network can still be added by running a normal network discovery
EverestDiscovery *discovery = new EverestDiscovery(nullptr, this);
connect(discovery, &EverestDiscovery::finished, discovery, &EverestDiscovery::deleteLater);
connect(discovery, &EverestDiscovery::finished, this, [this, discovery](){
ThingDescriptors descriptors;
foreach (const EverestDiscovery::Result &result, discovery->results()) {
// Create one EV charger foreach available connector on that host
foreach(const QString &connectorName, result.connectors) {
QString title = QString("EVerest");
QString description = connectorName;
ThingDescriptor descriptor(everestThingClassId, title, description);
qCInfo(dcEverest()) << "Discovered -->" << title << description;
ParamList params;
params.append(Param(everestThingConnectorParamTypeId, connectorName));
params.append(Param(everestThingAddressParamTypeId, result.networkDeviceInfo.address().toString()));
descriptor.setParams(params);
// Let's check if we aleardy have a thing with those params
bool thingExists = true;
Thing *existingThing = nullptr;
foreach (Thing *thing, myThings()) {
foreach(const Param &param, params) {
if (param.value() != thing->paramValue(param.paramTypeId())) {
thingExists = false;
break;
}
}
// The params are equal, we already have set up this thing
if (thingExists) {
existingThing = thing;
}
}
// Add only connectors we don't have set up yet
if (existingThing) {
qCDebug(dcEverest()) << "Discovered EVerest connector on localhost but we already set up this connector" << existingThing->name() << existingThing->params();
} else {
qCDebug(dcEverest()) << "Adding new EVerest connector on localhost" << title << params;
descriptors.append(descriptor);
}
}
}
if (!descriptors.isEmpty()) {
qCDebug(dcEverest()) << "Adding" << descriptors.count() << "new EVerest instances.";
emit autoThingsAppeared(descriptors);
}
});
discovery->startLocalhost();
}
void IntegrationPluginTruffle::discoverThings(ThingDiscoveryInfo *info)