Update networkdetector plugin

This commit is contained in:
Michael Zanetti 2019-09-19 00:06:45 +02:00
parent 3d2fc78b27
commit b4ba578015
2 changed files with 29 additions and 58 deletions

View File

@ -31,26 +31,21 @@
DevicePluginNetworkDetector::DevicePluginNetworkDetector() DevicePluginNetworkDetector::DevicePluginNetworkDetector()
{ {
m_discovery = new Discovery(this);
connect(m_discovery, &Discovery::finished, this, &DevicePluginNetworkDetector::discoveryFinished);
m_broadcastPing = new BroadcastPing(this); m_broadcastPing = new BroadcastPing(this);
connect(m_broadcastPing, &BroadcastPing::finished, this, &DevicePluginNetworkDetector::broadcastPingFinished); connect(m_broadcastPing, &BroadcastPing::finished, this, &DevicePluginNetworkDetector::broadcastPingFinished);
} }
DevicePluginNetworkDetector::~DevicePluginNetworkDetector() DevicePluginNetworkDetector::~DevicePluginNetworkDetector()
{ {
if (m_discovery->isRunning()) {
m_discovery->abort();
}
} }
void DevicePluginNetworkDetector::init() void DevicePluginNetworkDetector::init()
{ {
} }
Device::DeviceSetupStatus DevicePluginNetworkDetector::setupDevice(Device *device) void DevicePluginNetworkDetector::setupDevice(DeviceSetupInfo *info)
{ {
Device *device = info->device();
qCDebug(dcNetworkDetector()) << "Setup" << device->name() << device->params(); qCDebug(dcNetworkDetector()) << "Setup" << device->name() << device->params();
DeviceMonitor *monitor = new DeviceMonitor(device->name(), DeviceMonitor *monitor = new DeviceMonitor(device->name(),
device->paramValue(networkDeviceDeviceMacAddressParamTypeId).toString(), device->paramValue(networkDeviceDeviceMacAddressParamTypeId).toString(),
@ -80,25 +75,39 @@ Device::DeviceSetupStatus DevicePluginNetworkDetector::setupDevice(Device *devic
m_broadcastPing->run(); m_broadcastPing->run();
} }
return Device::DeviceSetupStatusSuccess; info->finish(Device::DeviceErrorNoError);
} }
Device::DeviceError DevicePluginNetworkDetector::discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params) void DevicePluginNetworkDetector::discoverDevices(DeviceDiscoveryInfo *info)
{ {
Q_UNUSED(params) Discovery *discovery = new Discovery(this);
discovery->discoverHosts(25);
// clean up discovery object when this discovery info is deleted
connect(info, &DeviceDiscoveryInfo::destroyed, discovery, &Discovery::deleteLater);
if (deviceClassId != networkDeviceDeviceClassId) connect(discovery, &Discovery::finished, info, [this, info](const QList<Host> &hosts) {
return Device::DeviceErrorDeviceClassNotFound; qCDebug(dcNetworkDetector()) << "Discovery finished. Found" << hosts.count() << "devices";
foreach (const Host &host, hosts) {
DeviceDescriptor descriptor(networkDeviceDeviceClassId, host.hostName().isEmpty() ? host.address() : host.hostName(), host.address() + " (" + host.macAddress() + ")");
if (m_discovery->isRunning()) { foreach (Device *existingDevice, myDevices()) {
qCWarning(dcNetworkDetector()) << "Network discovery already running"; if (existingDevice->paramValue(networkDeviceDeviceMacAddressParamTypeId).toString() == host.macAddress()) {
return Device::DeviceErrorDeviceInUse; descriptor.setDeviceId(existingDevice->id());
} break;
}
}
m_discovery->discoverHosts(25); ParamList params;
params << Param(networkDeviceDeviceMacAddressParamTypeId, host.macAddress());
params << Param(networkDeviceDeviceAddressParamTypeId, host.address());
descriptor.setParams(params);
return Device::DeviceErrorAsync; info->addDeviceDescriptor(descriptor);
}
info->finish(Device::DeviceErrorNoError);
});
} }
void DevicePluginNetworkDetector::deviceRemoved(Device *device) void DevicePluginNetworkDetector::deviceRemoved(Device *device)
@ -121,41 +130,6 @@ void DevicePluginNetworkDetector::broadcastPingFinished()
} }
} }
void DevicePluginNetworkDetector::discoveryFinished(const QList<Host> &hosts)
{
qCDebug(dcNetworkDetector()) << "Discovery finished. Found" << hosts.count() << "devices";
QList<DeviceDescriptor> discoveredDevices;
foreach (const Host &host, hosts) {
DeviceDescriptor descriptor(networkDeviceDeviceClassId, host.hostName().isEmpty() ? host.address() : host.hostName(), host.address() + " (" + host.macAddress() + ")");
foreach (Device *existingDevice, myDevices()) {
if (existingDevice->paramValue(networkDeviceDeviceMacAddressParamTypeId).toString() == host.macAddress()) {
descriptor.setDeviceId(existingDevice->id());
break;
}
}
ParamList paramList;
Param macAddress(networkDeviceDeviceMacAddressParamTypeId, host.macAddress());
Param address(networkDeviceDeviceAddressParamTypeId, host.address());
paramList.append(macAddress);
paramList.append(address);
descriptor.setParams(paramList);
foreach (Device *existingDevice, myDevices()) {
if (existingDevice->paramValue(networkDeviceDeviceMacAddressParamTypeId).toString() == host.macAddress()) {
descriptor.setDeviceId(existingDevice->id());
break;
}
}
discoveredDevices.append(descriptor);
}
emit devicesDiscovered(networkDeviceDeviceClassId, discoveredDevices);
}
void DevicePluginNetworkDetector::deviceReachableChanged(bool reachable) void DevicePluginNetworkDetector::deviceReachableChanged(bool reachable)
{ {
DeviceMonitor *monitor = static_cast<DeviceMonitor*>(sender()); DeviceMonitor *monitor = static_cast<DeviceMonitor*>(sender());

View File

@ -48,14 +48,12 @@ public:
void init() override; void init() override;
Device::DeviceSetupStatus setupDevice(Device *device) override; void setupDevice(DeviceSetupInfo *info) override;
Device::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params) override; void discoverDevices(DeviceDiscoveryInfo *info) override;
void deviceRemoved(Device *device) override; void deviceRemoved(Device *device) override;
private slots: private slots:
void discoveryFinished(const QList<Host> &hosts);
void deviceReachableChanged(bool reachable); void deviceReachableChanged(bool reachable);
void deviceAddressChanged(const QString &address); void deviceAddressChanged(const QString &address);
void deviceSeen(); void deviceSeen();
@ -64,7 +62,6 @@ private slots:
private: private:
PluginTimer *m_pluginTimer = nullptr; PluginTimer *m_pluginTimer = nullptr;
Discovery *m_discovery = nullptr;
BroadcastPing *m_broadcastPing = nullptr; BroadcastPing *m_broadcastPing = nullptr;
QHash<DeviceMonitor*, Device*> m_monitors; QHash<DeviceMonitor*, Device*> m_monitors;
}; };