NetworkDetector: Update to networkdevice interface

master
Simon Stürz 2024-12-03 21:38:46 +01:00
parent a35312b7f2
commit 5bb951f778
3 changed files with 105 additions and 125 deletions

View File

@ -1,6 +1,6 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright 2013 - 2022, nymea GmbH * Copyright 2013 - 2024, nymea GmbH
* Contact: contact@nymea.io * Contact: contact@nymea.io
* *
* This file is part of nymea. * This file is part of nymea.
@ -40,17 +40,11 @@ IntegrationPluginNetworkDetector::IntegrationPluginNetworkDetector()
} }
IntegrationPluginNetworkDetector::~IntegrationPluginNetworkDetector() IntegrationPluginNetworkDetector::~IntegrationPluginNetworkDetector()
{ {
} }
void IntegrationPluginNetworkDetector::init()
{
}
void IntegrationPluginNetworkDetector::discoverThings(ThingDiscoveryInfo *info) void IntegrationPluginNetworkDetector::discoverThings(ThingDiscoveryInfo *info)
{ {
if (!hardwareManager()->networkDeviceDiscovery()->available()) { if (!hardwareManager()->networkDeviceDiscovery()->available()) {
@ -61,48 +55,59 @@ void IntegrationPluginNetworkDetector::discoverThings(ThingDiscoveryInfo *info)
qCDebug(dcNetworkDetector()) << "Starting network discovery..."; qCDebug(dcNetworkDetector()) << "Starting network discovery...";
NetworkDeviceDiscoveryReply *discoveryReply = hardwareManager()->networkDeviceDiscovery()->discover(); NetworkDeviceDiscoveryReply *discoveryReply = hardwareManager()->networkDeviceDiscovery()->discover();
connect(discoveryReply, &NetworkDeviceDiscoveryReply::hostAddressDiscovered, this, [=](const QHostAddress &address){
qCDebug(dcNetworkDetector()) << "Address discovered" << address.toString();
});
connect(discoveryReply, &NetworkDeviceDiscoveryReply::networkDeviceInfoAdded, this, [=](const NetworkDeviceInfo &networkDeviceInfo){
qCDebug(dcNetworkDetector()) << "-->" << networkDeviceInfo;
});
connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, discoveryReply, &NetworkDeviceDiscoveryReply::deleteLater); connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, discoveryReply, &NetworkDeviceDiscoveryReply::deleteLater);
connect(discoveryReply, &NetworkDeviceDiscoveryReply::hostAddressDiscovered, this, [=](const QHostAddress &address){
qCDebug(dcNetworkDetector()) << "Host address discovered" << address.toString();
});
connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, info, [=](){ connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, info, [=](){
ThingDescriptors descriptors;
qCDebug(dcNetworkDetector()) << "Discovery finished. Found" << discoveryReply->networkDeviceInfos().count() << "devices"; qCDebug(dcNetworkDetector()) << "Discovery finished. Found" << discoveryReply->networkDeviceInfos().count() << "devices";
foreach (const NetworkDeviceInfo &networkDeviceInfo, discoveryReply->networkDeviceInfos()) { foreach (const NetworkDeviceInfo &networkDeviceInfo, discoveryReply->networkDeviceInfos()) {
qCDebug(dcNetworkDetector()) << "-->" << networkDeviceInfo; qCDebug(dcNetworkDetector()) << "-->" << networkDeviceInfo;
QString title; QString title;
if (networkDeviceInfo.hostName().isEmpty()) {
title = networkDeviceInfo.macAddress();
} else {
title = networkDeviceInfo.hostName() + " (" + networkDeviceInfo.macAddress() + ")";
}
QString description; QString description;
if (networkDeviceInfo.macAddressManufacturer().isEmpty()) { MacAddressInfo macInfo;
description = networkDeviceInfo.address().toString();
} else { switch (networkDeviceInfo.monitorMode()) {
description = networkDeviceInfo.address().toString() + " - " + networkDeviceInfo.macAddressManufacturer(); case NetworkDeviceInfo::MonitorModeMac:
macInfo = networkDeviceInfo.macAddressInfos().constFirst();
description = networkDeviceInfo.address().toString();
if (!macInfo.vendorName().isEmpty())
description += " - " + networkDeviceInfo.macAddressInfos().constFirst().vendorName();
if (networkDeviceInfo.hostName().isEmpty()) {
title = macInfo.macAddress().toString();
} else {
title = networkDeviceInfo.hostName() + " (" + macInfo.macAddress().toString() + ")";
}
break;
case NetworkDeviceInfo::MonitorModeHostName:
title = networkDeviceInfo.hostName();
description = networkDeviceInfo.address().toString();
break;
case NetworkDeviceInfo::MonitorModeIp:
title = "Network device " + networkDeviceInfo.address().toString();
description = "Interface: " + networkDeviceInfo.networkInterface().name();
break;
} }
ThingDescriptor descriptor(networkDeviceThingClassId, title, description); ThingDescriptor descriptor(networkDeviceThingClassId, 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; ParamList params;
params.append(Param(networkDeviceThingMacAddressParamTypeId, networkDeviceInfo.macAddress())); params.append(Param(networkDeviceThingMacAddressParamTypeId, networkDeviceInfo.thingParamValueMacAddress()));
params.append(Param(networkDeviceThingHostNameParamTypeId, networkDeviceInfo.thingParamValueHostName()));
params.append(Param(networkDeviceThingAddressParamTypeId, networkDeviceInfo.thingParamValueAddress()));
descriptor.setParams(params); descriptor.setParams(params);
// Check if we already have set up this device info->addThingDescriptor(descriptor);
Things existingThings = myThings().filterByParam(networkDeviceThingMacAddressParamTypeId, networkDeviceInfo.macAddress());
if (existingThings.count() == 1) {
qCDebug(dcNetworkDetector()) << "This network device already exists in the system" << networkDeviceInfo;
descriptor.setThingId(existingThings.first()->id());
}
descriptors.append(descriptor);
} }
info->addThingDescriptors(descriptors);
info->finish(Thing::ThingErrorNoError); info->finish(Thing::ThingErrorNoError);
}); });
} }
@ -114,61 +119,60 @@ void IntegrationPluginNetworkDetector::setupThing(ThingSetupInfo *info)
if (thing->thingClassId() == networkDeviceThingClassId) { if (thing->thingClassId() == networkDeviceThingClassId) {
MacAddress macAddress(thing->paramValue(networkDeviceThingMacAddressParamTypeId).toString());
if (macAddress.isNull()) {
qCWarning(dcNetworkDetector()) << "Invalid mac address:" << thing->paramValue(networkDeviceThingMacAddressParamTypeId).toString();
info->finish(Thing::ThingErrorInvalidParameter, QT_TR_NOOP("The configured MAC address is not valid."));
return;
}
// Handle reconfigure // Handle reconfigure
if (m_monitors.contains(thing)) { if (m_monitors.contains(thing)) {
hardwareManager()->networkDeviceDiscovery()->unregisterMonitor(m_monitors.take(thing)); hardwareManager()->networkDeviceDiscovery()->unregisterMonitor(m_monitors.take(thing));
} }
NetworkDeviceMonitor *monitor = hardwareManager()->networkDeviceDiscovery()->registerMonitor(macAddress); NetworkDeviceMonitor *monitor = hardwareManager()->networkDeviceDiscovery()->registerMonitor(thing);
m_monitors.insert(thing, monitor); m_monitors.insert(thing, monitor);
info->finish(Thing::ThingErrorNoError); info->finish(Thing::ThingErrorNoError);
QHostAddress cachedAddress; connect(monitor, &NetworkDeviceMonitor::reachableChanged, thing, [=](bool reachable){
if (!monitor->networkDeviceInfo().address().isNull()) { qCDebug(dcNetworkDetector()) << thing->name() << "monitor reachable changed to" << reachable;
cachedAddress = monitor->networkDeviceInfo().address(); if (reachable) {
} else { thing->setStateValue(networkDeviceIsPresentStateTypeId, reachable);
cachedAddress = QHostAddress(thing->stateValue(networkDeviceAddressStateTypeId).toString()); // Remove any possible running grace priod timer
} if (m_gracePeriodTimers.contains(thing)) {
hardwareManager()->pluginTimerManager()->unregisterTimer(m_gracePeriodTimers.take(thing));
}
} else {
int gracePeriodSeconds = thing->setting(networkDeviceSettingsGracePeriodParamTypeId).toInt() * 60;
if (gracePeriodSeconds == 0) {
thing->setStateValue(networkDeviceIsPresentStateTypeId, reachable);
} else {
// Let's wait the grace period before setting not present
PluginTimer *timer = hardwareManager()->pluginTimerManager()->registerTimer(gracePeriodSeconds);
connect(timer, &PluginTimer::timeout, thing, [=](){
if (thing->stateValue(networkDeviceIsPresentStateTypeId).toBool() && monitor->lastSeen().addSecs(gracePeriodSeconds) < QDateTime::currentDateTime()) {
qCDebug(dcNetworkDetector()) << thing->name() << "still not reachable after a grace period of" << gracePeriodSeconds << "seconds. Mark device as not reachable.";
thing->setStateValue(networkDeviceIsPresentStateTypeId, reachable);
hardwareManager()->pluginTimerManager()->unregisterTimer(m_gracePeriodTimers.take(thing));
}
});
// If the address is not known yet, let the monitor do the work and finish the setup... m_gracePeriodTimers.insert(thing, timer);
if (cachedAddress.isNull()) { qCDebug(dcNetworkDetector()) << "Starting grace period timer" << m_gracePeriodTimers << "seconds";
setupMonitorConnections(thing, monitor); timer->start();
thing->setStateValue(networkDeviceIsPresentStateTypeId, monitor->reachable()); }
return;
}
// Make an initial ping in order to check if the device reachable state has changed compaired to the cached state
qCDebug(dcNetworkDetector()) << "Send initial ping to" << cachedAddress.toString() << "in order to get initial reachable state...";
PingReply *pingReply = hardwareManager()->networkDeviceDiscovery()->ping(cachedAddress);
connect(pingReply, &PingReply::finished, this, [=](){
qCDebug(dcNetworkDetector()) << "Initial ping finished" << pingReply->error();
// However the ping result is, the monitor has catched up internally with the reachable state...
if (!monitor->networkDeviceInfo().address().isNull())
thing->setStateValue(networkDeviceAddressStateTypeId, monitor->networkDeviceInfo().address().toString());
thing->setStateValue(networkDeviceHostNameStateTypeId, monitor->networkDeviceInfo().hostName());
thing->setStateValue(networkDeviceMacManufacturerNameStateTypeId, monitor->networkDeviceInfo().macAddressManufacturer());
thing->setStateValue(networkDeviceNetworkInterfaceStateTypeId, monitor->networkDeviceInfo().networkInterface().name());
thing->setStateValue(networkDeviceIsPresentStateTypeId, monitor->reachable());
setupMonitorConnections(thing, monitor);
if (!monitor->lastSeen().isNull()) {
QDateTime minuteBased = QDateTime::fromMSecsSinceEpoch((monitor->lastSeen().toMSecsSinceEpoch() / 60000) * 60000);
thing->setStateValue(networkDeviceLastSeenTimeStateTypeId, minuteBased.toMSecsSinceEpoch() / 1000);
} }
}); });
connect(monitor, &NetworkDeviceMonitor::lastSeenChanged, thing, [=](const QDateTime &lastSeen){
QDateTime minuteBased = QDateTime::fromMSecsSinceEpoch((monitor->lastSeen().toMSecsSinceEpoch() / 60000) * 60000);
qCDebug(dcNetworkDetector()) << thing << "last seen changed to" << lastSeen.toString() << minuteBased.toString();
thing->setStateValue(networkDeviceLastSeenTimeStateTypeId, minuteBased.toMSecsSinceEpoch() / 1000);
});
connect(monitor, &NetworkDeviceMonitor::networkDeviceInfoChanged, thing, [=](const NetworkDeviceInfo &networkInfo){
Q_UNUSED(networkInfo)
updateStates(thing, monitor);
});
// Set initial states from cache
qCDebug(dcNetworkDetector()) << "Set initial states for:" << thing << monitor->networkDeviceInfo();
updateStates(thing, monitor);
} else { } else {
info->finish(Thing::ThingErrorThingClassNotFound); info->finish(Thing::ThingErrorThingClassNotFound);
} }
@ -226,51 +230,16 @@ void IntegrationPluginNetworkDetector::executeAction(ThingActionInfo *info)
} }
} }
void IntegrationPluginNetworkDetector::setupMonitorConnections(Thing *thing, NetworkDeviceMonitor *monitor) void IntegrationPluginNetworkDetector::updateStates(Thing *thing, NetworkDeviceMonitor *monitor)
{ {
connect(monitor, &NetworkDeviceMonitor::reachableChanged, thing, [=](bool reachable){ thing->setStateValue(networkDeviceLastSeenTimeStateTypeId, monitor->lastSeen().toMSecsSinceEpoch() / 1000);
qCDebug(dcNetworkDetector()) << thing->name() << "monitor reachable changed to" << reachable; thing->setStateValue(networkDeviceIsPresentStateTypeId, monitor->reachable());
if (reachable) { thing->setStateValue(networkDeviceAddressStateTypeId, monitor->networkDeviceInfo().address().toString());
thing->setStateValue(networkDeviceIsPresentStateTypeId, reachable); thing->setStateValue(networkDeviceHostNameStateTypeId, monitor->networkDeviceInfo().hostName());
// Remove any possible running grace priod timer if (monitor->networkDeviceInfo().monitorMode() == NetworkDeviceInfo::MonitorModeMac && monitor->networkDeviceInfo().macAddressInfos().count() == 1)
if (m_gracePeriodTimers.contains(thing)) { thing->setStateValue(networkDeviceMacManufacturerNameStateTypeId, monitor->networkDeviceInfo().macAddressInfos().constFirst().vendorName());
hardwareManager()->pluginTimerManager()->unregisterTimer(m_gracePeriodTimers.take(thing));
}
} else {
int gracePeriodSeconds = thing->setting(networkDeviceSettingsGracePeriodParamTypeId).toInt() * 60;
if (gracePeriodSeconds == 0) {
thing->setStateValue(networkDeviceIsPresentStateTypeId, reachable);
} else {
// Let's wait the grace period before setting not present
PluginTimer *timer = hardwareManager()->pluginTimerManager()->registerTimer(gracePeriodSeconds);
connect(timer, &PluginTimer::timeout, thing, [=](){
if (thing->stateValue(networkDeviceIsPresentStateTypeId).toBool() && monitor->lastSeen().addSecs(gracePeriodSeconds) < QDateTime::currentDateTime()) {
qCDebug(dcNetworkDetector()) << thing->name() << "still not reachable after a grace period of" << gracePeriodSeconds << "seconds. Mark device as not reachable.";
thing->setStateValue(networkDeviceIsPresentStateTypeId, reachable);
hardwareManager()->pluginTimerManager()->unregisterTimer(m_gracePeriodTimers.take(thing));
}
});
m_gracePeriodTimers.insert(thing, timer); thing->setStateValue(networkDeviceNetworkInterfaceStateTypeId, monitor->networkDeviceInfo().networkInterface().name());
qCDebug(dcNetworkDetector()) << "Starting grace period timer" << m_gracePeriodTimers << "seconds";
timer->start();
}
}
});
connect(monitor, &NetworkDeviceMonitor::lastSeenChanged, thing, [=](const QDateTime &lastSeen){
QDateTime minuteBased = QDateTime::fromMSecsSinceEpoch((monitor->lastSeen().toMSecsSinceEpoch() / 60000) * 60000);
qCDebug(dcNetworkDetector()) << thing << "last seen changed to" << lastSeen.toString() << minuteBased.toString();
thing->setStateValue(networkDeviceLastSeenTimeStateTypeId, minuteBased.toMSecsSinceEpoch() / 1000);
});
connect(monitor, &NetworkDeviceMonitor::networkDeviceInfoChanged, thing, [=](const NetworkDeviceInfo &networkInfo){
qCDebug(dcNetworkDetector()) << thing << "changed" << networkInfo;
thing->setStateValue(networkDeviceAddressStateTypeId, networkInfo.address().toString());
thing->setStateValue(networkDeviceHostNameStateTypeId, networkInfo.hostName());
thing->setStateValue(networkDeviceMacManufacturerNameStateTypeId, networkInfo.macAddressManufacturer());
thing->setStateValue(networkDeviceNetworkInterfaceStateTypeId, monitor->networkDeviceInfo().networkInterface().name());
});
} }
void IntegrationPluginNetworkDetector::onHostLookupFinished(const QHostInfo &info) void IntegrationPluginNetworkDetector::onHostLookupFinished(const QHostInfo &info)

View File

@ -1,6 +1,6 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright 2013 - 2022, nymea GmbH * Copyright 2013 - 2024, nymea GmbH
* Contact: contact@nymea.io * Contact: contact@nymea.io
* *
* This file is part of nymea. * This file is part of nymea.
@ -48,7 +48,6 @@ public:
explicit IntegrationPluginNetworkDetector(); explicit IntegrationPluginNetworkDetector();
~IntegrationPluginNetworkDetector(); ~IntegrationPluginNetworkDetector();
void init() override;
void discoverThings(ThingDiscoveryInfo *info) override; void discoverThings(ThingDiscoveryInfo *info) override;
void setupThing(ThingSetupInfo *info) override; void setupThing(ThingSetupInfo *info) override;
void thingRemoved(Thing *thing) override; void thingRemoved(Thing *thing) override;
@ -60,7 +59,7 @@ private:
QHash<Thing *, PluginTimer *> m_gracePeriodTimers; QHash<Thing *, PluginTimer *> m_gracePeriodTimers;
QHash<int, ThingActionInfo *> m_pendingHostLookup; QHash<int, ThingActionInfo *> m_pendingHostLookup;
void setupMonitorConnections(Thing *thing, NetworkDeviceMonitor *monitor); void updateStates(Thing *thing, NetworkDeviceMonitor *monitor);
private slots: private slots:
void onHostLookupFinished(const QHostInfo &info); void onHostLookupFinished(const QHostInfo &info);

View File

@ -12,7 +12,7 @@
"id": "bd216356-f1ec-4324-9785-6982d2174e17", "id": "bd216356-f1ec-4324-9785-6982d2174e17",
"name": "networkDevice", "name": "networkDevice",
"displayName": "Network Device", "displayName": "Network Device",
"interfaces": [ "presencesensor" ], "interfaces": [ "presencesensor", "networkdevice" ],
"createMethods": ["user", "discovery"], "createMethods": ["user", "discovery"],
"paramTypes": [ "paramTypes": [
{ {
@ -21,6 +21,18 @@
"displayName": "MAC address", "displayName": "MAC address",
"type": "QString", "type": "QString",
"inputType": "MacAddress" "inputType": "MacAddress"
},
{
"id": "d0674a05-732b-48f6-97fc-7c99af938df3",
"name": "hostName",
"displayName": "Host name",
"type": "QString"
},
{
"id": "5b37b710-68d5-43f8-b09c-a390dd98c964",
"name": "address",
"displayName": "IP address",
"type": "QString"
} }
], ],
"settingsTypes": [ "settingsTypes": [