Update wallbe to make use of nymea internal network discovery
parent
d9138ff01d
commit
d482fe3418
|
|
@ -1,271 +0,0 @@
|
|||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
*
|
||||
* Copyright 2013 - 2020, nymea GmbH
|
||||
* Contact: contact@nymea.io
|
||||
*
|
||||
* This file is part of nymea.
|
||||
* This project including source code and documentation is protected by
|
||||
* copyright law, and remains the property of nymea GmbH. All rights, including
|
||||
* reproduction, publication, editing and translation, are reserved. The use of
|
||||
* this project is subject to the terms of a license agreement to be concluded
|
||||
* with nymea GmbH in accordance with the terms of use of nymea GmbH, available
|
||||
* under https://nymea.io/license
|
||||
*
|
||||
* GNU Lesser General Public License Usage
|
||||
* Alternatively, this project may be redistributed and/or modified under the
|
||||
* terms of the GNU Lesser General Public License as published by the Free
|
||||
* Software Foundation; version 3. This project is distributed in the hope that
|
||||
* it will be useful, but WITHOUT ANY WARRANTY; without even the implied
|
||||
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this project. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* For any further details and any questions please contact us under
|
||||
* contact@nymea.io or see our FAQ/Licensing Information on
|
||||
* https://nymea.io/license/faq
|
||||
*
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
|
||||
#include "discovery.h"
|
||||
#include "extern-plugininfo.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QXmlStreamReader>
|
||||
#include <QNetworkInterface>
|
||||
#include <QHostInfo>
|
||||
#include <QTimer>
|
||||
|
||||
Discovery::Discovery(QObject *parent) : QObject(parent)
|
||||
{
|
||||
connect(&m_timeoutTimer, &QTimer::timeout, this, &Discovery::onTimeout);
|
||||
}
|
||||
|
||||
void Discovery::discoverHosts(int timeout)
|
||||
{
|
||||
if (isRunning()) {
|
||||
qCWarning(dcWallbe()) << "Discovery already running. Cannot start twice.";
|
||||
return;
|
||||
}
|
||||
m_timeoutTimer.start(timeout * 1000);
|
||||
|
||||
foreach (const QString &target, getDefaultTargets()) {
|
||||
QProcess *discoveryProcess = new QProcess(this);
|
||||
m_discoveryProcesses.append(discoveryProcess);
|
||||
connect(discoveryProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(discoveryFinished(int,QProcess::ExitStatus)));
|
||||
|
||||
QStringList arguments;
|
||||
arguments << "-oX" << "-" << "-n" << "-sn";
|
||||
arguments << target;
|
||||
|
||||
qCDebug(dcWallbe()) << "Scanning network:" << "nmap" << arguments.join(" ");
|
||||
discoveryProcess->start(QStringLiteral("nmap"), arguments);
|
||||
}
|
||||
}
|
||||
|
||||
void Discovery::abort()
|
||||
{
|
||||
foreach (QProcess *discoveryProcess, m_discoveryProcesses) {
|
||||
if (discoveryProcess->state() == QProcess::Running) {
|
||||
qCDebug(dcWallbe()) << "Kill running discovery process";
|
||||
discoveryProcess->terminate();
|
||||
discoveryProcess->waitForFinished(5000);
|
||||
}
|
||||
}
|
||||
foreach (QProcess *p, m_pendingArpLookups.keys()) {
|
||||
p->terminate();
|
||||
delete p;
|
||||
}
|
||||
m_pendingArpLookups.clear();
|
||||
m_pendingNameLookups.clear();
|
||||
qDeleteAll(m_scanResults);
|
||||
m_scanResults.clear();
|
||||
}
|
||||
|
||||
bool Discovery::isRunning() const
|
||||
{
|
||||
return !m_discoveryProcesses.isEmpty() || !m_pendingArpLookups.isEmpty() || !m_pendingNameLookups.isEmpty();
|
||||
}
|
||||
|
||||
void Discovery::discoveryFinished(int exitCode, QProcess::ExitStatus exitStatus)
|
||||
{
|
||||
QProcess *discoveryProcess = static_cast<QProcess*>(sender());
|
||||
|
||||
if (exitCode != 0 || exitStatus != QProcess::NormalExit) {
|
||||
qCWarning(dcWallbe()) << "Nmap error failed. Is nmap installed correctly?";
|
||||
m_discoveryProcesses.removeAll(discoveryProcess);
|
||||
discoveryProcess->deleteLater();
|
||||
discoveryProcess = nullptr;
|
||||
finishDiscovery();
|
||||
return;
|
||||
}
|
||||
|
||||
QByteArray data = discoveryProcess->readAll();
|
||||
m_discoveryProcesses.removeAll(discoveryProcess);
|
||||
discoveryProcess->deleteLater();
|
||||
discoveryProcess = nullptr;
|
||||
|
||||
QXmlStreamReader reader(data);
|
||||
|
||||
int foundHosts = 0;
|
||||
qCDebug(dcWallbe()) << "nmap finished network discovery:";
|
||||
while (!reader.atEnd() && !reader.hasError()) {
|
||||
QXmlStreamReader::TokenType token = reader.readNext();
|
||||
if(token == QXmlStreamReader::StartDocument)
|
||||
continue;
|
||||
|
||||
if(token == QXmlStreamReader::StartElement && reader.name() == "host") {
|
||||
bool isUp = false;
|
||||
QString address;
|
||||
QString macAddress;
|
||||
QString vendor;
|
||||
while (!reader.atEnd() && !reader.hasError() && !(token == QXmlStreamReader::EndElement && reader.name() == "host")) {
|
||||
token = reader.readNext();
|
||||
|
||||
if (reader.name() == "address") {
|
||||
QString addr = reader.attributes().value("addr").toString();
|
||||
QString type = reader.attributes().value("addrtype").toString();
|
||||
if (type == "ipv4" && !addr.isEmpty()) {
|
||||
address = addr;
|
||||
} else if (type == "mac") {
|
||||
macAddress = addr;
|
||||
vendor = reader.attributes().value("vendor").toString();
|
||||
}
|
||||
}
|
||||
|
||||
if (reader.name() == "status") {
|
||||
QString state = reader.attributes().value("state").toString();
|
||||
if (!state.isEmpty())
|
||||
isUp = state == "up";
|
||||
}
|
||||
}
|
||||
|
||||
if (isUp) {
|
||||
foundHosts++;
|
||||
qCDebug(dcWallbe()) << " - host:" << vendor << address << macAddress;
|
||||
|
||||
Host *host = new Host();
|
||||
host->setAddress(address);
|
||||
|
||||
if (!macAddress.isEmpty()) {
|
||||
host->setMacAddress(macAddress);
|
||||
} else {
|
||||
QProcess *arpLookup = new QProcess(this);
|
||||
m_pendingArpLookups.insert(arpLookup, host);
|
||||
connect(arpLookup, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(arpLookupDone(int,QProcess::ExitStatus)));
|
||||
arpLookup->start("arp", {"-vn"});
|
||||
}
|
||||
|
||||
host->setVendor(vendor);
|
||||
QHostInfo::lookupHost(address, this, SLOT(hostLookupDone(QHostInfo)));
|
||||
m_pendingNameLookups.insert(address, host);
|
||||
|
||||
m_scanResults.append(host);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (foundHosts == 0 && m_discoveryProcesses.isEmpty()) {
|
||||
qCDebug(dcWallbe()) << "Network scan successful but no hosts found in this network";
|
||||
finishDiscovery();
|
||||
}
|
||||
}
|
||||
|
||||
void Discovery::hostLookupDone(const QHostInfo &info)
|
||||
{
|
||||
Host *host = m_pendingNameLookups.take(info.addresses().first().toString());
|
||||
if (!host) {
|
||||
// Probably aborted...
|
||||
return;
|
||||
}
|
||||
if (info.error() != QHostInfo::NoError) {
|
||||
qWarning(dcWallbe()) << "Host lookup failed:" << info.errorString();
|
||||
}
|
||||
if (host->hostName().isEmpty() || info.hostName() != host->address()) {
|
||||
host->setHostName(info.hostName());
|
||||
}
|
||||
|
||||
finishDiscovery();
|
||||
}
|
||||
|
||||
void Discovery::arpLookupDone(int exitCode, QProcess::ExitStatus exitStatus)
|
||||
{
|
||||
QProcess *p = static_cast<QProcess*>(sender());
|
||||
p->deleteLater();
|
||||
|
||||
Host *host = m_pendingArpLookups.take(p);
|
||||
|
||||
if (exitCode != 0 || exitStatus != QProcess::NormalExit) {
|
||||
qCWarning(dcWallbe()) << "ARP lookup process failed for host" << host->address();
|
||||
finishDiscovery();
|
||||
return;
|
||||
}
|
||||
|
||||
QString data = QString::fromLatin1(p->readAll());
|
||||
foreach (QString line, data.split('\n')) {
|
||||
line.replace(QRegExp("[ ]{1,}"), " ");
|
||||
QStringList parts = line.split(" ");
|
||||
if (parts.count() >= 3 && parts.first() == host->address() && parts.at(1) == "ether") {
|
||||
host->setMacAddress(parts.at(2));
|
||||
break;
|
||||
}
|
||||
}
|
||||
finishDiscovery();
|
||||
}
|
||||
|
||||
void Discovery::onTimeout()
|
||||
{
|
||||
qWarning(dcWallbe()) << "Timeout hit. Stopping discovery";
|
||||
while (!m_discoveryProcesses.isEmpty()) {
|
||||
QProcess *discoveryProcess = m_discoveryProcesses.takeFirst();
|
||||
disconnect(this, SLOT(discoveryFinished(int,QProcess::ExitStatus)));
|
||||
discoveryProcess->terminate();
|
||||
delete discoveryProcess;
|
||||
}
|
||||
foreach (QProcess *p, m_pendingArpLookups.keys()) {
|
||||
p->terminate();
|
||||
m_scanResults.removeAll(m_pendingArpLookups.value(p));
|
||||
delete p;
|
||||
}
|
||||
m_pendingArpLookups.clear();
|
||||
m_pendingNameLookups.clear();
|
||||
finishDiscovery();
|
||||
}
|
||||
|
||||
QStringList Discovery::getDefaultTargets()
|
||||
{
|
||||
QStringList targets;
|
||||
foreach (const QHostAddress &interface, QNetworkInterface::allAddresses()) {
|
||||
if (!interface.isLoopback() && interface.scopeId().isEmpty() && interface.protocol() == QAbstractSocket::IPv4Protocol) {
|
||||
QPair<QHostAddress, int> pair = QHostAddress::parseSubnet(interface.toString() + "/24");
|
||||
QString newTarget = QString("%1/%2").arg(pair.first.toString()).arg(pair.second);
|
||||
if (!targets.contains(newTarget)) {
|
||||
targets.append(newTarget);
|
||||
}
|
||||
}
|
||||
}
|
||||
return targets;
|
||||
}
|
||||
|
||||
void Discovery::finishDiscovery()
|
||||
{
|
||||
if (m_discoveryProcesses.count() > 0 || m_pendingNameLookups.count() > 0 || m_pendingArpLookups.count() > 0) {
|
||||
// Still busy...
|
||||
return;
|
||||
}
|
||||
|
||||
QList<Host> hosts;
|
||||
foreach (Host *host, m_scanResults) {
|
||||
if (!host->macAddress().isEmpty()) {
|
||||
hosts.append(*host);
|
||||
}
|
||||
}
|
||||
qDeleteAll(m_scanResults);
|
||||
m_scanResults.clear();
|
||||
|
||||
qCDebug(dcWallbe()) << "Found" << hosts.count() << "network devices";
|
||||
m_timeoutTimer.stop();
|
||||
emit finished(hosts);
|
||||
}
|
||||
|
||||
|
|
@ -1,75 +0,0 @@
|
|||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
*
|
||||
* Copyright 2013 - 2020, nymea GmbH
|
||||
* Contact: contact@nymea.io
|
||||
*
|
||||
* This file is part of nymea.
|
||||
* This project including source code and documentation is protected by
|
||||
* copyright law, and remains the property of nymea GmbH. All rights, including
|
||||
* reproduction, publication, editing and translation, are reserved. The use of
|
||||
* this project is subject to the terms of a license agreement to be concluded
|
||||
* with nymea GmbH in accordance with the terms of use of nymea GmbH, available
|
||||
* under https://nymea.io/license
|
||||
*
|
||||
* GNU Lesser General Public License Usage
|
||||
* Alternatively, this project may be redistributed and/or modified under the
|
||||
* terms of the GNU Lesser General Public License as published by the Free
|
||||
* Software Foundation; version 3. This project is distributed in the hope that
|
||||
* it will be useful, but WITHOUT ANY WARRANTY; without even the implied
|
||||
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this project. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* For any further details and any questions please contact us under
|
||||
* contact@nymea.io or see our FAQ/Licensing Information on
|
||||
* https://nymea.io/license/faq
|
||||
*
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
|
||||
#ifndef DISCOVERY_H
|
||||
#define DISCOVERY_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QProcess>
|
||||
#include <QHostInfo>
|
||||
#include <QTimer>
|
||||
|
||||
#include "host.h"
|
||||
|
||||
class Discovery : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit Discovery(QObject *parent = nullptr);
|
||||
|
||||
void discoverHosts(int timeout);
|
||||
void abort();
|
||||
bool isRunning() const;
|
||||
|
||||
signals:
|
||||
void finished(const QList<Host> &hosts);
|
||||
|
||||
private:
|
||||
QStringList getDefaultTargets();
|
||||
|
||||
void finishDiscovery();
|
||||
|
||||
private slots:
|
||||
void discoveryFinished(int exitCode, QProcess::ExitStatus exitStatus);
|
||||
void hostLookupDone(const QHostInfo &info);
|
||||
void arpLookupDone(int exitCode, QProcess::ExitStatus exitStatus);
|
||||
void onTimeout();
|
||||
|
||||
private:
|
||||
QList<QProcess*> m_discoveryProcesses;
|
||||
QTimer m_timeoutTimer;
|
||||
|
||||
QHash<QProcess*, Host*> m_pendingArpLookups;
|
||||
QHash<QString, Host*> m_pendingNameLookups;
|
||||
QList<Host*> m_scanResults;
|
||||
};
|
||||
|
||||
#endif // DISCOVERY_H
|
||||
|
||||
104
wallbe/host.cpp
104
wallbe/host.cpp
|
|
@ -1,104 +0,0 @@
|
|||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
*
|
||||
* Copyright 2013 - 2020, nymea GmbH
|
||||
* Contact: contact@nymea.io
|
||||
*
|
||||
* This file is part of nymea.
|
||||
* This project including source code and documentation is protected by
|
||||
* copyright law, and remains the property of nymea GmbH. All rights, including
|
||||
* reproduction, publication, editing and translation, are reserved. The use of
|
||||
* this project is subject to the terms of a license agreement to be concluded
|
||||
* with nymea GmbH in accordance with the terms of use of nymea GmbH, available
|
||||
* under https://nymea.io/license
|
||||
*
|
||||
* GNU Lesser General Public License Usage
|
||||
* Alternatively, this project may be redistributed and/or modified under the
|
||||
* terms of the GNU Lesser General Public License as published by the Free
|
||||
* Software Foundation; version 3. This project is distributed in the hope that
|
||||
* it will be useful, but WITHOUT ANY WARRANTY; without even the implied
|
||||
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this project. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* For any further details and any questions please contact us under
|
||||
* contact@nymea.io or see our FAQ/Licensing Information on
|
||||
* https://nymea.io/license/faq
|
||||
*
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
|
||||
#include "host.h"
|
||||
|
||||
Host::Host()
|
||||
{
|
||||
qRegisterMetaType<Host>();
|
||||
qRegisterMetaType<QList<Host> >();
|
||||
}
|
||||
|
||||
QString Host::macAddress() const
|
||||
{
|
||||
return m_macAddress;
|
||||
}
|
||||
|
||||
void Host::setMacAddress(const QString &macAddress)
|
||||
{
|
||||
m_macAddress = macAddress;
|
||||
}
|
||||
|
||||
QString Host::hostName() const
|
||||
{
|
||||
return m_hostName;
|
||||
}
|
||||
|
||||
void Host::setHostName(const QString &hostName)
|
||||
{
|
||||
m_hostName = hostName;
|
||||
}
|
||||
|
||||
QString Host::vendor() const
|
||||
{
|
||||
return m_vendor;
|
||||
}
|
||||
|
||||
void Host::setVendor(const QString &vendor)
|
||||
{
|
||||
m_vendor = vendor;
|
||||
}
|
||||
|
||||
QString Host::address() const
|
||||
{
|
||||
return m_address;
|
||||
}
|
||||
|
||||
void Host::setAddress(const QString &address)
|
||||
{
|
||||
m_address = address;
|
||||
}
|
||||
|
||||
void Host::seen()
|
||||
{
|
||||
m_lastSeenTime = QDateTime::currentDateTime();
|
||||
}
|
||||
|
||||
QDateTime Host::lastSeenTime() const
|
||||
{
|
||||
return m_lastSeenTime;
|
||||
}
|
||||
|
||||
bool Host::reachable() const
|
||||
{
|
||||
return m_reachable;
|
||||
}
|
||||
|
||||
void Host::setReachable(bool reachable)
|
||||
{
|
||||
m_reachable = reachable;
|
||||
}
|
||||
|
||||
QDebug operator<<(QDebug dbg, const Host &host)
|
||||
{
|
||||
dbg.nospace() << "Host(" << host.macAddress() << "," << host.hostName() << ", " << host.address() << ", " << (host.reachable() ? "up" : "down") << ")";
|
||||
return dbg.space();
|
||||
}
|
||||
|
||||
|
|
@ -1,74 +0,0 @@
|
|||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
*
|
||||
* Copyright 2013 - 2020, nymea GmbH
|
||||
* Contact: contact@nymea.io
|
||||
*
|
||||
* This file is part of nymea.
|
||||
* This project including source code and documentation is protected by
|
||||
* copyright law, and remains the property of nymea GmbH. All rights, including
|
||||
* reproduction, publication, editing and translation, are reserved. The use of
|
||||
* this project is subject to the terms of a license agreement to be concluded
|
||||
* with nymea GmbH in accordance with the terms of use of nymea GmbH, available
|
||||
* under https://nymea.io/license
|
||||
*
|
||||
* GNU Lesser General Public License Usage
|
||||
* Alternatively, this project may be redistributed and/or modified under the
|
||||
* terms of the GNU Lesser General Public License as published by the Free
|
||||
* Software Foundation; version 3. This project is distributed in the hope that
|
||||
* it will be useful, but WITHOUT ANY WARRANTY; without even the implied
|
||||
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this project. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* For any further details and any questions please contact us under
|
||||
* contact@nymea.io or see our FAQ/Licensing Information on
|
||||
* https://nymea.io/license/faq
|
||||
*
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
|
||||
#ifndef HOST_H
|
||||
#define HOST_H
|
||||
|
||||
#include <QString>
|
||||
#include <QDebug>
|
||||
#include <QDateTime>
|
||||
|
||||
class Host
|
||||
{
|
||||
public:
|
||||
Host();
|
||||
|
||||
QString macAddress() const;
|
||||
void setMacAddress(const QString &macAddress);
|
||||
|
||||
QString hostName() const;
|
||||
void setHostName(const QString &hostName);
|
||||
|
||||
QString vendor() const;
|
||||
void setVendor(const QString &vendor);
|
||||
|
||||
QString address() const;
|
||||
void setAddress(const QString &address);
|
||||
|
||||
void seen();
|
||||
QDateTime lastSeenTime() const;
|
||||
|
||||
bool reachable() const;
|
||||
void setReachable(bool reachable);
|
||||
|
||||
private:
|
||||
QString m_macAddress;
|
||||
QString m_hostName;
|
||||
QString m_address;
|
||||
QString m_vendor;
|
||||
QDateTime m_lastSeenTime;
|
||||
bool m_reachable;
|
||||
};
|
||||
Q_DECLARE_METATYPE(Host)
|
||||
|
||||
QDebug operator<<(QDebug dbg, const Host &host);
|
||||
|
||||
#endif // HOST_H
|
||||
|
||||
|
|
@ -30,7 +30,7 @@
|
|||
|
||||
#include "integrationpluginwallbe.h"
|
||||
#include "plugininfo.h"
|
||||
|
||||
#include "network/networkdevicediscovery.h"
|
||||
#include "types/param.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
|
@ -41,66 +41,83 @@
|
|||
|
||||
IntegrationPluginWallbe::IntegrationPluginWallbe()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void IntegrationPluginWallbe::init() {
|
||||
void IntegrationPluginWallbe::init()
|
||||
{
|
||||
// FIXME: make use of the internal network discovery if the device gets unavailable. For now, commented out since it has not been used
|
||||
// at the moment of changing this.
|
||||
|
||||
m_discovery = new Discovery();
|
||||
connect(m_discovery, &Discovery::finished, this, [this](const QList<Host> &hosts) {
|
||||
foreach (const Host &host, hosts) {
|
||||
if (!host.vendor().contains("Phoenix", Qt::CaseSensitivity::CaseInsensitive))
|
||||
continue;
|
||||
// m_discovery = new Discovery();
|
||||
// connect(m_discovery, &Discovery::finished, this, [this](const QList<Host> &hosts) {
|
||||
// foreach (const Host &host, hosts) {
|
||||
// if (!host.vendor().contains("Phoenix", Qt::CaseSensitivity::CaseInsensitive))
|
||||
// continue;
|
||||
|
||||
Q_FOREACH(Thing *existingThing, myThings()) {
|
||||
if (existingThing->paramValue(wallbeEcoThingMacParamTypeId).toString().isEmpty()) {
|
||||
//This device got probably manually setup, to enable auto rediscovery the MAC address needs to setup
|
||||
if (existingThing->paramValue(wallbeEcoThingIpParamTypeId).toString() == host.address()) {
|
||||
qCDebug(dcWallbe()) << "Wallbe Wallbox MAC Address has been discovered" << existingThing->name() << host.macAddress();
|
||||
existingThing->setParamValue(wallbeEcoThingMacParamTypeId, host.macAddress());
|
||||
// Q_FOREACH(Thing *existingThing, myThings()) {
|
||||
// if (existingThing->paramValue(wallbeEcoThingMacParamTypeId).toString().isEmpty()) {
|
||||
// //This device got probably manually setup, to enable auto rediscovery the MAC address needs to setup
|
||||
// if (existingThing->paramValue(wallbeEcoThingIpParamTypeId).toString() == host.address()) {
|
||||
// qCDebug(dcWallbe()) << "Wallbe Wallbox MAC Address has been discovered" << existingThing->name() << host.macAddress();
|
||||
// existingThing->setParamValue(wallbeEcoThingMacParamTypeId, host.macAddress());
|
||||
|
||||
}
|
||||
} else if (existingThing->paramValue(wallbeEcoThingMacParamTypeId).toString() == host.macAddress()) {
|
||||
if (existingThing->paramValue(wallbeEcoThingIpParamTypeId).toString() != host.address()) {
|
||||
qCDebug(dcWallbe()) << "Wallbe Wallbox IP Address has changed, from" << existingThing->paramValue(wallbeEcoThingIpParamTypeId).toString() << "to" << host.address();
|
||||
existingThing->setParamValue(wallbeEcoThingIpParamTypeId, host.address());
|
||||
// }
|
||||
// } else if (existingThing->paramValue(wallbeEcoThingMacParamTypeId).toString() == host.macAddress()) {
|
||||
// if (existingThing->paramValue(wallbeEcoThingIpParamTypeId).toString() != host.address()) {
|
||||
// qCDebug(dcWallbe()) << "Wallbe Wallbox IP Address has changed, from" << existingThing->paramValue(wallbeEcoThingIpParamTypeId).toString() << "to" << host.address();
|
||||
// existingThing->setParamValue(wallbeEcoThingIpParamTypeId, host.address());
|
||||
|
||||
} else {
|
||||
qCDebug(dcWallbe()) << "Wallbe Wallbox" << existingThing->name() << "IP address has not changed" << host.address();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
// } else {
|
||||
// qCDebug(dcWallbe()) << "Wallbe Wallbox" << existingThing->name() << "IP address has not changed" << host.address();
|
||||
// }
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
}
|
||||
|
||||
|
||||
void IntegrationPluginWallbe::discoverThings(ThingDiscoveryInfo *info)
|
||||
{
|
||||
if (info->thingClassId() == wallbeEcoThingClassId){
|
||||
qCDebug(dcWallbe) << "Start Wallbe eco discovery";
|
||||
|
||||
m_discovery->discoverHosts(50);
|
||||
connect(m_discovery, &Discovery::finished, info, [this, info] (const QList<Host> &hosts) {
|
||||
|
||||
foreach (const Host &host, hosts) {
|
||||
if (!host.vendor().contains("Phoenix", Qt::CaseSensitivity::CaseInsensitive))
|
||||
qCDebug(dcWallbe()) << "Start Wallbe eco discovery";
|
||||
NetworkDeviceDiscoveryReply *discoveryReply = hardwareManager()->networkDeviceDiscovery()->discover();
|
||||
connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, this, [=](){
|
||||
ThingDescriptors descriptors;
|
||||
qCDebug(dcWallbe()) << "Discovery finished. Found" << discoveryReply->networkDevices().count() << "devices";
|
||||
foreach (const NetworkDevice &networkDevice, discoveryReply->networkDevices()) {
|
||||
qCDebug(dcWallbe()) << networkDevice;
|
||||
if (!networkDevice.macAddressManufacturer().contains("Phoenix", Qt::CaseSensitivity::CaseInsensitive))
|
||||
continue;
|
||||
|
||||
ThingDescriptor descriptor(wallbeEcoThingClassId);
|
||||
// Rediscovery
|
||||
foreach (Thing *existingThing, myThings()) {
|
||||
if (existingThing->paramValue(wallbeEcoThingMacParamTypeId).toString() == host.macAddress()) {
|
||||
qCDebug(dcWallbe()) << " - Device is already added";
|
||||
descriptor.setThingId(existingThing->id());
|
||||
break;
|
||||
}
|
||||
QString title;
|
||||
if (networkDevice.hostName().isEmpty()) {
|
||||
title += networkDevice.address().toString();
|
||||
} else {
|
||||
title += networkDevice.address().toString() + " (" + networkDevice.hostName() + ")";
|
||||
}
|
||||
descriptor.setTitle(host.hostName().remove(".localdomain"));
|
||||
descriptor.setDescription(host.address());
|
||||
|
||||
QString description;
|
||||
if (networkDevice.macAddressManufacturer().isEmpty()) {
|
||||
description = networkDevice.macAddress();
|
||||
} else {
|
||||
description = networkDevice.macAddress() + " (" + networkDevice.macAddressManufacturer() + ")";
|
||||
}
|
||||
|
||||
ThingDescriptor descriptor(wallbeEcoThingClassId, title, description);
|
||||
|
||||
// Check if we already have set up this device
|
||||
Things existingThings = myThings().filterByParam(wallbeEcoThingIpParamTypeId, networkDevice.address().toString());
|
||||
if (existingThings.count() == 1) {
|
||||
qCDebug(dcWallbe()) << "This thing already exists in the system." << existingThings.first() << networkDevice;
|
||||
descriptor.setThingId(existingThings.first()->id());
|
||||
}
|
||||
|
||||
ParamList params;
|
||||
params.append(Param(wallbeEcoThingIpParamTypeId, host.address()));
|
||||
params.append(Param(wallbeEcoThingMacParamTypeId, host.macAddress()));
|
||||
params << Param(wallbeEcoThingIpParamTypeId, networkDevice.address().toString());
|
||||
params << Param(wallbeEcoThingMacParamTypeId, networkDevice.macAddress());
|
||||
descriptor.setParams(params);
|
||||
info->addThingDescriptor(descriptor);
|
||||
}
|
||||
|
|
@ -297,8 +314,8 @@ void IntegrationPluginWallbe::onReceivedInputRegister(int slaveAddress, int modb
|
|||
} else if (WallbeRegisterAddress(modbusRegister) == WallbeRegisterAddress::FirmwareVersion) {
|
||||
int firmware = (uint32_t)(value[1]<<16)|(uint32_t)(value[0]);
|
||||
uint major = firmware/10000;
|
||||
uint minor = (firmware%10000)/100;
|
||||
uint patch = firmware%100;
|
||||
uint minor = (firmware%10000)/100;
|
||||
uint patch = firmware%100;
|
||||
QString firmwarestring = QString::number(major)+'.'+QString::number(minor)+'.'+QString::number(patch);
|
||||
thing->setStateValue(wallbeEcoFirmwareVersionStateTypeId, firmwarestring);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,8 +34,6 @@
|
|||
#include "integrations/integrationplugin.h"
|
||||
#include "plugintimer.h"
|
||||
|
||||
#include "host.h"
|
||||
#include "discovery.h"
|
||||
#include "../modbus/modbustcpmaster.h"
|
||||
|
||||
#include <QObject>
|
||||
|
|
@ -72,7 +70,6 @@ public:
|
|||
void thingRemoved(Thing *thing) override;
|
||||
|
||||
private:
|
||||
Discovery *m_discovery = nullptr;
|
||||
QHash<Thing *, ModbusTCPMaster *> m_connections;
|
||||
PluginTimer *m_pluginTimer = nullptr;
|
||||
QHash<QUuid, ThingActionInfo *> m_asyncActions;
|
||||
|
|
@ -82,8 +79,8 @@ private:
|
|||
|
||||
private slots:
|
||||
void onConnectionStateChanged(bool status);
|
||||
void onReceivedInputRegister(int slaveAddress, int modbusRegister, const QVector<quint16> &value);
|
||||
void onReceivedCoil(int slaveAddress, int modbusRegister, const QVector<quint16> &value);
|
||||
void onReceivedInputRegister(int slaveAddress, int modbusRegister, const QVector<quint16> &value);
|
||||
void onReceivedCoil(int slaveAddress, int modbusRegister, const QVector<quint16> &value);
|
||||
void onReceivedHoldingRegister(int slaveAddress, int modbusRegister, const QVector<quint16> &value);
|
||||
|
||||
void onWriteRequestExecuted(const QUuid &requestId, bool success);
|
||||
|
|
|
|||
|
|
@ -6,12 +6,8 @@ QT += \
|
|||
|
||||
SOURCES += \
|
||||
integrationpluginwallbe.cpp \
|
||||
../modbus/modbustcpmaster.cpp \
|
||||
discovery.cpp \
|
||||
host.cpp
|
||||
../modbus/modbustcpmaster.cpp
|
||||
|
||||
HEADERS += \
|
||||
integrationpluginwallbe.h \
|
||||
../modbus/modbustcpmaster.h \
|
||||
discovery.h \
|
||||
host.h
|
||||
../modbus/modbustcpmaster.h
|
||||
|
|
|
|||
Loading…
Reference in New Issue