Merge PR #749: EVerest: Autosetup connectors on localhost
This commit is contained in:
commit
34544b061a
@ -77,6 +77,11 @@ Everest::~Everest()
|
|||||||
deinitialize();
|
deinitialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Thing *Everest::thing() const
|
||||||
|
{
|
||||||
|
return m_thing;
|
||||||
|
}
|
||||||
|
|
||||||
QString Everest::connector() const
|
QString Everest::connector() const
|
||||||
{
|
{
|
||||||
return m_connector;
|
return m_connector;
|
||||||
@ -192,11 +197,13 @@ void Everest::onPublishReceived(const QString &topic, const QByteArray &payload,
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
QVariantMap dataMap = jsonDoc.toVariant().toMap();
|
QVariantMap dataMap = jsonDoc.toVariant().toMap();
|
||||||
|
uint maxCurrent = dataMap.value("max_current_A_import").toUInt();
|
||||||
|
uint minCurrent = dataMap.value("min_current_A_import").toUInt();
|
||||||
|
|
||||||
m_thing->setStateMaxValue(everestMaxChargingCurrentStateTypeId,
|
m_thing->setStateMaxValue(everestMaxChargingCurrentStateTypeId, maxCurrent);
|
||||||
dataMap.value("max_current_A_import").toUInt());
|
m_thing->setStateMinValue(everestMaxChargingCurrentStateTypeId, minCurrent == 0 ? 6 : minCurrent);
|
||||||
m_thing->setStateMinValue(everestMaxChargingCurrentStateTypeId,
|
|
||||||
dataMap.value("min_current_A_import").toUInt());
|
// FIXME: once we have a method for phase switching, we can re-enable the featre here
|
||||||
|
|
||||||
// bool phaseSwitchingAvailable = dataMap.value("supports_changing_phases_during_charging", false).toBool();
|
// bool phaseSwitchingAvailable = dataMap.value("supports_changing_phases_during_charging", false).toBool();
|
||||||
// if (!phaseSwitchingAvailable) {
|
// if (!phaseSwitchingAvailable) {
|
||||||
|
|||||||
@ -61,6 +61,8 @@ public:
|
|||||||
explicit Everest(MqttClient *client, Thing *thing, QObject *parent = nullptr);
|
explicit Everest(MqttClient *client, Thing *thing, QObject *parent = nullptr);
|
||||||
~Everest();
|
~Everest();
|
||||||
|
|
||||||
|
Thing *thing() const;
|
||||||
|
|
||||||
QString connector() const;
|
QString connector() const;
|
||||||
|
|
||||||
void initialize();
|
void initialize();
|
||||||
|
|||||||
@ -36,6 +36,7 @@ EverestClient::EverestClient(QObject *parent)
|
|||||||
{
|
{
|
||||||
m_client = new MqttClient("nymea-" + QUuid::createUuid().toString().left(8), 300,
|
m_client = new MqttClient("nymea-" + QUuid::createUuid().toString().left(8), 300,
|
||||||
QString(), QByteArray(), Mqtt::QoS0, false, this);
|
QString(), QByteArray(), Mqtt::QoS0, false, this);
|
||||||
|
|
||||||
connect(m_client, &MqttClient::disconnected, this, [this](){
|
connect(m_client, &MqttClient::disconnected, this, [this](){
|
||||||
qCDebug(dcEverest()) << "The MQTT client is now disconnected" << this;
|
qCDebug(dcEverest()) << "The MQTT client is now disconnected" << this;
|
||||||
if (!m_address.isNull()) {
|
if (!m_address.isNull()) {
|
||||||
@ -71,6 +72,13 @@ EverestClient::EverestClient(QObject *parent)
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EverestClient::~EverestClient()
|
||||||
|
{
|
||||||
|
foreach (Everest *everest, m_everests) {
|
||||||
|
removeThing(everest->thing());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
MqttClient *EverestClient::client() const
|
MqttClient *EverestClient::client() const
|
||||||
{
|
{
|
||||||
return m_client;
|
return m_client;
|
||||||
@ -86,7 +94,7 @@ void EverestClient::addThing(Thing *thing)
|
|||||||
if (m_everests.contains(thing)) {
|
if (m_everests.contains(thing)) {
|
||||||
qCWarning(dcEverest()) << "The" << thing << "has already been added to the everest client. "
|
qCWarning(dcEverest()) << "The" << thing << "has already been added to the everest client. "
|
||||||
"Please report a bug if you see this message.";
|
"Please report a bug if you see this message.";
|
||||||
// FIXME: maybe cleanup and recreate the client due to reconfigure
|
// TODO: maybe cleanup and recreate the client due to reconfigure
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -49,6 +49,7 @@ class EverestClient : public QObject
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
explicit EverestClient(QObject *parent = nullptr);
|
explicit EverestClient(QObject *parent = nullptr);
|
||||||
|
~EverestClient();
|
||||||
|
|
||||||
MqttClient *client() const;
|
MqttClient *client() const;
|
||||||
|
|
||||||
|
|||||||
@ -69,6 +69,18 @@ void EverestDiscovery::start()
|
|||||||
checkNetworkDevice(localHostInfo);
|
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
|
QList<EverestDiscovery::Result> EverestDiscovery::results() const
|
||||||
{
|
{
|
||||||
return m_results;
|
return m_results;
|
||||||
@ -88,6 +100,10 @@ void EverestDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDevice
|
|||||||
<< "...skip connection";
|
<< "...skip connection";
|
||||||
// We give up on the first error here
|
// We give up on the first error here
|
||||||
cleanupClient(client);
|
cleanupClient(client);
|
||||||
|
|
||||||
|
if (m_localhostDiscovery) {
|
||||||
|
finishDiscovery();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
connect(client, &MqttClient::disconnected, this, [this, client](){
|
connect(client, &MqttClient::disconnected, this, [this, client](){
|
||||||
@ -156,6 +172,10 @@ void EverestDiscovery::cleanupClient(MqttClient *client)
|
|||||||
|
|
||||||
client->disconnectFromHost();
|
client->disconnectFromHost();
|
||||||
client->deleteLater();
|
client->deleteLater();
|
||||||
|
|
||||||
|
if (m_localhostDiscovery) {
|
||||||
|
finishDiscovery();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void EverestDiscovery::finishDiscovery()
|
void EverestDiscovery::finishDiscovery()
|
||||||
|
|||||||
@ -50,6 +50,8 @@ public:
|
|||||||
|
|
||||||
void start();
|
void start();
|
||||||
|
|
||||||
|
void startLocalhost();
|
||||||
|
|
||||||
QList<EverestDiscovery::Result> results() const;
|
QList<EverestDiscovery::Result> results() const;
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
@ -61,6 +63,8 @@ private:
|
|||||||
QList<EverestDiscovery::Result> m_results;
|
QList<EverestDiscovery::Result> m_results;
|
||||||
QList<MqttClient *> m_clients;
|
QList<MqttClient *> m_clients;
|
||||||
|
|
||||||
|
bool m_localhostDiscovery = false;
|
||||||
|
|
||||||
QString m_everestApiModuleTopicConnectors = "everest_api/connectors";
|
QString m_everestApiModuleTopicConnectors = "everest_api/connectors";
|
||||||
|
|
||||||
void checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo);
|
void checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo);
|
||||||
|
|||||||
@ -46,7 +46,66 @@ void IntegrationPluginTruffle::init()
|
|||||||
|
|
||||||
void IntegrationPluginTruffle::startMonitoringAutoThings()
|
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)
|
void IntegrationPluginTruffle::discoverThings(ThingDiscoveryInfo *info)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user