diff --git a/everest/everestdiscovery.cpp b/everest/everestdiscovery.cpp index d258daff..fadc3534 100644 --- a/everest/everestdiscovery.cpp +++ b/everest/everestdiscovery.cpp @@ -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::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() diff --git a/everest/everestdiscovery.h b/everest/everestdiscovery.h index 8181e8aa..7a64d552 100644 --- a/everest/everestdiscovery.h +++ b/everest/everestdiscovery.h @@ -50,6 +50,8 @@ public: void start(); + void startLocalhost(); + QList results() const; signals: @@ -61,6 +63,8 @@ private: QList m_results; QList m_clients; + bool m_localhostDiscovery = false; + QString m_everestApiModuleTopicConnectors = "everest_api/connectors"; void checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo); diff --git a/everest/integrationplugineverest.cpp b/everest/integrationplugineverest.cpp index cea8fa7b..a4f7e394 100644 --- a/everest/integrationplugineverest.cpp +++ b/everest/integrationplugineverest.cpp @@ -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 ¶m, 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)