Update avahimonitor

This commit is contained in:
Michael Zanetti 2019-09-17 16:39:11 +02:00
parent d5a8afba25
commit f18a7e44db
3 changed files with 52 additions and 32 deletions

View File

@ -32,25 +32,16 @@
#include <QStringList> #include <QStringList>
#include <QNetworkInterface> #include <QNetworkInterface>
#include <QDateTime> #include <QDateTime>
#include <QTimer>
DevicePluginAvahiMonitor::DevicePluginAvahiMonitor() DevicePluginAvahiMonitor::DevicePluginAvahiMonitor()
{ {
} }
Device::DeviceSetupStatus DevicePluginAvahiMonitor::setupDevice(Device *device) void DevicePluginAvahiMonitor::setupDevice(DeviceSetupInfo *info)
{ {
qCDebug(dcAvahiMonitor()) << "Setup" << device->name() << device->params(); qCDebug(dcAvahiMonitor()) << "Setup" << info->device()->name() << info->device()->params();
return Device::DeviceSetupStatusSuccess;
}
Device::DeviceError DevicePluginAvahiMonitor::discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params)
{
Q_UNUSED(params)
if (deviceClassId != avahiDeviceClassId)
return Device::DeviceErrorDeviceClassNotFound;
if (!m_serviceBrowser) { if (!m_serviceBrowser) {
m_serviceBrowser = hardwareManager()->zeroConfController()->createServiceBrowser(); m_serviceBrowser = hardwareManager()->zeroConfController()->createServiceBrowser();
@ -58,32 +49,59 @@ Device::DeviceError DevicePluginAvahiMonitor::discoverDevices(const DeviceClassI
connect(m_serviceBrowser, &ZeroConfServiceBrowser::serviceEntryRemoved, this, &DevicePluginAvahiMonitor::onServiceEntryRemoved); connect(m_serviceBrowser, &ZeroConfServiceBrowser::serviceEntryRemoved, this, &DevicePluginAvahiMonitor::onServiceEntryRemoved);
} }
QList<DeviceDescriptor> deviceDescriptors; foreach (const ZeroConfServiceEntry &entry, m_serviceBrowser->serviceEntries()) {
foreach (const ZeroConfServiceEntry &service, m_serviceBrowser->serviceEntries()) { if (info->device()->paramValue(avahiDeviceServiceParamTypeId).toString() == entry.name() &&
DeviceDescriptor deviceDescriptor(avahiDeviceClassId, service.name(), service.hostAddress().toString()); info->device()->paramValue(avahiDeviceHostNameParamTypeId).toString() == entry.hostName()) {
ParamList params; info->device()->setStateValue(avahiIsPresentStateTypeId, true);
params.append(Param(avahiDeviceServiceParamTypeId, service.name())); info->device()->setStateValue(avahiLastSeenTimeStateTypeId, QDateTime::currentDateTime());
params.append(Param(avahiDeviceHostNameParamTypeId, service.hostName()));
deviceDescriptor.setParams(params);
foreach (Device *existingDevice, myDevices()) {
if (existingDevice->paramValue(avahiDeviceServiceParamTypeId).toString() == service.name() && existingDevice->paramValue(avahiDeviceHostNameParamTypeId).toString() == service.hostName()) {
deviceDescriptor.setDeviceId(existingDevice->id());
break;
}
} }
deviceDescriptors.append(deviceDescriptor);
} }
emit devicesDiscovered(avahiDeviceClassId, deviceDescriptors); info->finish(Device::DeviceErrorNoError);
}
return Device::DeviceErrorAsync; void DevicePluginAvahiMonitor::discoverDevices(DeviceDiscoveryInfo *info)
{
if (info->deviceClassId() != avahiDeviceClassId) {
info->finish(Device::DeviceErrorDeviceClassNotFound);
return;
}
if (!m_serviceBrowser) {
m_serviceBrowser = hardwareManager()->zeroConfController()->createServiceBrowser();
connect(m_serviceBrowser, &ZeroConfServiceBrowser::serviceEntryAdded, this, &DevicePluginAvahiMonitor::onServiceEntryAdded);
connect(m_serviceBrowser, &ZeroConfServiceBrowser::serviceEntryRemoved, this, &DevicePluginAvahiMonitor::onServiceEntryRemoved);
}
// give it a bit of time to find things
QTimer::singleShot(2000, info, [this, info](){
QList<DeviceDescriptor> deviceDescriptors;
foreach (const ZeroConfServiceEntry &service, m_serviceBrowser->serviceEntries()) {
DeviceDescriptor deviceDescriptor(avahiDeviceClassId, service.name(), service.serviceType() + " (" + service.hostAddress().toString() + ")");
ParamList params;
params.append(Param(avahiDeviceServiceParamTypeId, service.name()));
params.append(Param(avahiDeviceHostNameParamTypeId, service.hostName()));
deviceDescriptor.setParams(params);
foreach (Device *existingDevice, myDevices()) {
if (existingDevice->paramValue(avahiDeviceServiceParamTypeId).toString() == service.name() && existingDevice->paramValue(avahiDeviceHostNameParamTypeId).toString() == service.hostName()) {
deviceDescriptor.setDeviceId(existingDevice->id());
break;
}
}
deviceDescriptors.append(deviceDescriptor);
}
info->addDeviceDescriptors(deviceDescriptors);
info->finish(Device::DeviceErrorNoError);
});
} }
void DevicePluginAvahiMonitor::onServiceEntryAdded(const ZeroConfServiceEntry &serviceEntry) void DevicePluginAvahiMonitor::onServiceEntryAdded(const ZeroConfServiceEntry &serviceEntry)
{ {
qCDebug(dcAvahiMonitor()) << "Service entry added:" << serviceEntry; qCDebug(dcAvahiMonitor()) << "Service entry added:" << serviceEntry;
foreach (Device *device, myDevices()) { foreach (Device *device, myDevices()) {
if (device->paramValue(avahiDeviceServiceParamTypeId).toString() == serviceEntry.name()) { if (device->paramValue(avahiDeviceServiceParamTypeId).toString() == serviceEntry.name() &&
device->paramValue(avahiDeviceHostNameParamTypeId).toString() == serviceEntry.hostName()) {
device->setStateValue(avahiIsPresentStateTypeId, true); device->setStateValue(avahiIsPresentStateTypeId, true);
device->setStateValue(avahiLastSeenTimeStateTypeId, QDateTime::currentDateTime()); device->setStateValue(avahiLastSeenTimeStateTypeId, QDateTime::currentDateTime());
} }
@ -93,7 +111,8 @@ void DevicePluginAvahiMonitor::onServiceEntryAdded(const ZeroConfServiceEntry &s
void DevicePluginAvahiMonitor::onServiceEntryRemoved(const ZeroConfServiceEntry &serviceEntry) void DevicePluginAvahiMonitor::onServiceEntryRemoved(const ZeroConfServiceEntry &serviceEntry)
{ {
foreach (Device *device, myDevices()) { foreach (Device *device, myDevices()) {
if (device->paramValue(avahiDeviceServiceParamTypeId).toString() == serviceEntry.name()) { if (device->paramValue(avahiDeviceServiceParamTypeId).toString() == serviceEntry.name() &&
device->paramValue(avahiDeviceHostNameParamTypeId).toString() == serviceEntry.hostName()) {
device->setStateValue(avahiIsPresentStateTypeId, false); device->setStateValue(avahiIsPresentStateTypeId, false);
} }
} }

View File

@ -40,8 +40,8 @@ class DevicePluginAvahiMonitor : public DevicePlugin
public: public:
explicit DevicePluginAvahiMonitor(); explicit DevicePluginAvahiMonitor();
Device::DeviceSetupStatus setupDevice(Device *device) override; void discoverDevices(DeviceDiscoveryInfo *info) override;
Device::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params) override; void setupDevice(DeviceSetupInfo *info) override;
private slots: private slots:
void onServiceEntryAdded(const ZeroConfServiceEntry &serviceEntry); void onServiceEntryAdded(const ZeroConfServiceEntry &serviceEntry);

View File

@ -37,7 +37,8 @@
"displayName": "Online", "displayName": "Online",
"type": "bool", "type": "bool",
"defaultValue": false, "defaultValue": false,
"displayNameEvent": "online changed" "displayNameEvent": "online changed",
"cached": false
}, },
{ {
"id": "4a570e58-d227-4af8-a64e-c018b9d84474", "id": "4a570e58-d227-4af8-a64e-c018b9d84474",