From 68e9fe1ba19e03d27078d91fb2d68af9b0aff9af Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Sun, 9 Feb 2020 19:05:53 +0100 Subject: [PATCH] Cache device classes and keep devices with missing plugins in the system --- libnymea-core/devices/deviceclasscache.cpp | 52 +++++++++ libnymea-core/devices/deviceclasscache.h | 16 +++ .../devices/devicemanagerimplementation.cpp | 109 ++++++++++++++---- libnymea-core/libnymea-core.pro | 2 + libnymea/devices/device.cpp | 16 +-- libnymea/devices/device.h | 7 +- 6 files changed, 166 insertions(+), 36 deletions(-) create mode 100644 libnymea-core/devices/deviceclasscache.cpp create mode 100644 libnymea-core/devices/deviceclasscache.h diff --git a/libnymea-core/devices/deviceclasscache.cpp b/libnymea-core/devices/deviceclasscache.cpp new file mode 100644 index 00000000..46910371 --- /dev/null +++ b/libnymea-core/devices/deviceclasscache.cpp @@ -0,0 +1,52 @@ +#include "deviceclasscache.h" + +#include +#include +#include +#include +#include + +#include "loggingcategories.h" + +PluginInfoCache::PluginInfoCache() +{ + +} + +void PluginInfoCache::cachePluginInfo(const QJsonObject &metaData) +{ + QString fileName = metaData.value("id").toString().remove(QRegExp("[{}]")) + ".cache"; + QDir path = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/plugininfo/"; + if (!path.exists()) { + if (!path.mkpath(path.absolutePath())) { + qCWarning(dcDeviceManager()) << "Error creating device class cache dir at" << path.absolutePath(); + } + } + QFile file(path.absoluteFilePath(fileName)); + if (!file.open(QFile::WriteOnly | QFile::Truncate)) { + qCWarning(dcDeviceManager()) << "Error opening device class cache for writing at" << path.absoluteFilePath(fileName); + return; + } + + file.write(QJsonDocument::fromVariant(metaData.toVariantMap()).toJson(QJsonDocument::Compact)); + file.close(); +} + +QJsonObject PluginInfoCache::loadPluginInfo(const PluginId &pluginId) +{ + QString fileName = pluginId.toString().remove(QRegExp("[{}]")) + ".cache"; + QDir path = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/plugininfo/"; + QFile file(path.absoluteFilePath(fileName)); + if (!file.open(QFile::ReadOnly)) { + return QJsonObject(); + } + + QByteArray data = file.readAll(); + QJsonParseError error; + QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error); + if (error.error != QJsonParseError::NoError) { + qCWarning(dcDeviceManager()) << "Error parsing plugin info cache entry:" << path.absoluteFilePath(fileName); + return QJsonObject(); + } + return QJsonObject::fromVariantMap(jsonDoc.toVariant().toMap()); +} diff --git a/libnymea-core/devices/deviceclasscache.h b/libnymea-core/devices/deviceclasscache.h new file mode 100644 index 00000000..a74a1155 --- /dev/null +++ b/libnymea-core/devices/deviceclasscache.h @@ -0,0 +1,16 @@ +#ifndef PLUGININFOCACHE_H +#define PLUGININFOCACHE_H + +#include "types/deviceclass.h" +#include "devices/deviceplugin.h" + +class PluginInfoCache +{ +public: + PluginInfoCache(); + + static void cachePluginInfo(const QJsonObject &metaData); + static QJsonObject loadPluginInfo(const PluginId &pluginId); +}; + +#endif // PLUGININFOCACHE_H diff --git a/libnymea-core/devices/devicemanagerimplementation.cpp b/libnymea-core/devices/devicemanagerimplementation.cpp index b2e94752..9cd6e145 100644 --- a/libnymea-core/devices/devicemanagerimplementation.cpp +++ b/libnymea-core/devices/devicemanagerimplementation.cpp @@ -38,6 +38,7 @@ #include "typeutils.h" #include "nymeasettings.h" #include "version.h" +#include "deviceclasscache.h" #include "devices/devicediscoveryinfo.h" #include "devices/devicepairinginfo.h" @@ -258,7 +259,7 @@ DeviceDiscoveryInfo* DeviceManagerImplementation::discoverDevices(const DeviceCl if (!plugin) { qCWarning(dcDeviceManager) << "Device discovery failed. Plugin not found for device class" << deviceClass.name(); DeviceDiscoveryInfo *discoveryInfo = new DeviceDiscoveryInfo(deviceClassId, params, this); - discoveryInfo->finish(Device::DeviceErrorPluginNotFound); + discoveryInfo->finish(Device::DeviceErrorPluginNotFound, tr("The plugin for this device or service is not loaded.")); return discoveryInfo; } @@ -594,9 +595,10 @@ DevicePairingInfo *DeviceManagerImplementation::confirmPairing(const PairingTran PairingContext context = m_pendingPairings.take(pairingTransactionId); DeviceClassId deviceClassId = context.deviceClassId; - DevicePlugin *plugin = m_devicePlugins.value(m_supportedDevices.value(deviceClassId).pluginId()); + DeviceClass deviceClass = m_supportedDevices.value(deviceClassId); + DevicePlugin *plugin = m_devicePlugins.value(deviceClass.pluginId()); if (!plugin) { - qCWarning(dcDeviceManager) << "Can't find a plugin for this device class"; + qCWarning(dcDeviceManager) << "Can't find a plugin for this device class:" << deviceClass; DevicePairingInfo *info = new DevicePairingInfo(pairingTransactionId, deviceClassId, context.deviceId, context.deviceName, context.params, context.parentDeviceId, this); info->finish(Device::DeviceErrorPluginNotFound); return info; @@ -633,7 +635,7 @@ DevicePairingInfo *DeviceManagerImplementation::confirmPairing(const PairingTran Device *device = nullptr; if (addNewDevice) { - device = new Device(plugin, deviceClass, internalInfo->deviceId(), this); + device = new Device(plugin->pluginId(), deviceClass, internalInfo->deviceId(), this); if (internalInfo->deviceName().isEmpty()) { device->setName(deviceClass.displayName()); } else { @@ -735,7 +737,7 @@ DeviceSetupInfo* DeviceManagerImplementation::addConfiguredDeviceInternal(const return info; } - Device *device = new Device(plugin, deviceClass, deviceId, this); + Device *device = new Device(plugin->pluginId(), deviceClass, deviceId, this); device->setParentId(parentDeviceId); if (name.isEmpty()) { device->setName(deviceClass.name()); @@ -809,13 +811,24 @@ BrowseResult *DeviceManagerImplementation::browseDevice(const DeviceId &deviceId return result; } + DevicePlugin *plugin = m_devicePlugins.value(device->pluginId()); + if (!plugin) { + qCWarning(dcDeviceManager()) << "Cannot browse device. Plugin not found for device" << device; + return result; + } + + if (!device->setupComplete()) { + qCWarning(dcDeviceManager()) << "Cannot browse device. Device did not finish setup" << device; + return result; + } + if (!device->deviceClass().browsable()) { qCWarning(dcDeviceManager()) << "Cannot browse device. DeviceClass" << device->deviceClass().name() << "is not browsable."; result->finish(Device::DeviceErrorUnsupportedFeature); return result; } - device->plugin()->browseDevice(result); + plugin->browseDevice(result); connect(result, &BrowseResult::finished, this, [result](){ if (result->status() != Device::DeviceErrorNoError) { qCWarning(dcDeviceManager()) << "Browse device failed:" << result->status(); @@ -836,13 +849,24 @@ BrowserItemResult *DeviceManagerImplementation::browserItemDetails(const DeviceI return result; } + DevicePlugin *plugin = m_devicePlugins.value(device->pluginId()); + if (!plugin) { + qCWarning(dcDeviceManager()) << "Cannot browse device. Plugin not found for device" << device; + return result; + } + + if (device->setupStatus() != Device::DeviceSetupStatusComplete) { + qCWarning(dcDeviceManager()) << "Cannot browse device. Device did not finish setup" << device; + return result; + } + if (!device->deviceClass().browsable()) { qCWarning(dcDeviceManager()) << "Cannot browse device. DeviceClass" << device->deviceClass().name() << "is not browsable."; result->finish(Device::DeviceErrorUnsupportedFeature); return result; } - device->plugin()->browserItem(result); + plugin->browserItem(result); connect(result, &BrowserItemResult::finished, this, [result](){ if (result->status() != Device::DeviceErrorNoError) { qCWarning(dcDeviceManager()) << "Browse device failed:" << result->status(); @@ -862,11 +886,24 @@ BrowserActionInfo* DeviceManagerImplementation::executeBrowserItem(const Browser return info; } + DevicePlugin *plugin = m_devicePlugins.value(device->pluginId()); + if (!plugin) { + qCWarning(dcDeviceManager()) << "Cannot browse device. Plugin not found for device" << device; + info->finish(Device::DeviceErrorPluginNotFound); + return info; + } + + if (device->setupStatus() != Device::DeviceSetupStatusComplete) { + qCWarning(dcDeviceManager()) << "Cannot browse device. Device did not finish setup" << device; + info->finish(Device::DeviceErrorSetupFailed); + return info; + } + if (!device->deviceClass().browsable()) { info->finish(Device::DeviceErrorUnsupportedFeature); return info; } - device->plugin()->executeBrowserItem(info); + plugin->executeBrowserItem(info); return info; } @@ -881,13 +918,26 @@ BrowserItemActionInfo* DeviceManagerImplementation::executeBrowserItemAction(con return info; } + DevicePlugin *plugin = m_devicePlugins.value(device->pluginId()); + if (!plugin) { + qCWarning(dcDeviceManager()) << "Cannot execute browser item action. Plugin not found for device" << device; + info->finish(Device::DeviceErrorPluginNotFound); + return info; + } + + if (device->setupStatus() != Device::DeviceSetupStatusComplete) { + qCWarning(dcDeviceManager()) << "Cannot execute browser item action. Device did not finish setup" << device; + info->finish(Device::DeviceErrorSetupFailed); + return info; + } + if (!device->deviceClass().browsable()) { info->finish(Device::DeviceErrorUnsupportedFeature); return info; } // TODO: check browserItemAction.params with deviceClass - device->plugin()->executeBrowserItemAction(info); + plugin->executeBrowserItemAction(info); return info; } @@ -1099,7 +1149,8 @@ void DeviceManagerImplementation::loadPlugins() continue; } - PluginMetadata metaData(loader.metaData().value("MetaData").toObject()); + QJsonObject pluginInfo = loader.metaData().value("MetaData").toObject(); + PluginMetadata metaData(pluginInfo); if (!metaData.isValid()) { foreach (const QString &error, metaData.validationErrors()) { qCWarning(dcDeviceManager()) << error; @@ -1119,6 +1170,7 @@ void DeviceManagerImplementation::loadPlugins() continue; } loadPlugin(pluginIface, metaData); + PluginInfoCache::cachePluginInfo(pluginInfo); } } @@ -1249,13 +1301,28 @@ void DeviceManagerImplementation::loadConfiguredDevices() foreach (const QString &idString, settings.childGroups()) { settings.beginGroup(idString); QString deviceName = settings.value("devicename").toString(); - DevicePlugin *plugin = m_devicePlugins.value(PluginId(settings.value("pluginid").toString())); + PluginId pluginId = PluginId(settings.value("pluginid").toString()); + DevicePlugin *plugin = m_devicePlugins.value(pluginId); if (!plugin) { - qCWarning(dcDeviceManager()) << "Not loading device" << deviceName << idString << "because the plugin for this device could not be found."; - settings.endGroup(); // DeviceId - continue; + qCWarning(dcDeviceManager()) << "Plugin for device" << deviceName << idString << "not found. This device will not be functional until the plugin can be loaded."; + } + DeviceClassId deviceClassId = DeviceClassId(settings.value("deviceClassId").toString()); + DeviceClass deviceClass = findDeviceClass(deviceClassId); + if (!deviceClass.isValid()) { + // Try to load the device class from the cache + QJsonObject pluginInfo = PluginInfoCache::loadPluginInfo(pluginId); + if (!pluginInfo.empty()) { + PluginMetadata pluginMetadata(pluginInfo); + deviceClass = pluginMetadata.deviceClasses().findById(deviceClassId); + if (deviceClass.isValid()) { + m_supportedDevices.insert(deviceClassId, deviceClass); + if (!m_supportedVendors.contains(deviceClass.vendorId())) { + Vendor vendor = pluginMetadata.vendors().findById(deviceClass.vendorId()); + m_supportedVendors.insert(vendor.id(), vendor); + } + } + } } - DeviceClass deviceClass = findDeviceClass(DeviceClassId(settings.value("deviceClassId").toString())); if (!deviceClass.isValid()) { qCWarning(dcDeviceManager()) << "Not loading device" << deviceName << idString << "because the device class for this device could not be found."; settings.endGroup(); // DeviceId @@ -1263,12 +1330,12 @@ void DeviceManagerImplementation::loadConfiguredDevices() } // Cross-check if this plugin still implements this device class - if (!plugin->supportedDevices().contains(deviceClass)) { + if (plugin && !plugin->supportedDevices().contains(deviceClass)) { qCWarning(dcDeviceManager()) << "Not loading device" << deviceName << idString << "because plugin" << plugin->pluginName() << "has removed support for it."; settings.endGroup(); // DeviceId continue; } - Device *device = new Device(plugin, deviceClass, DeviceId(idString), this); + Device *device = new Device(pluginId, deviceClass, DeviceId(idString), this); device->m_autoCreated = settings.value("autoCreated").toBool(); device->setName(deviceName); device->setParentId(DeviceId(settings.value("parentid", QUuid()).toString())); @@ -1463,7 +1530,7 @@ void DeviceManagerImplementation::onAutoDevicesAppeared(const DeviceDescriptors continue; } - device = new Device(plugin, deviceClass, this); + device = new Device(plugin->pluginId(), deviceClass, this); device->m_autoCreated = true; device->setName(deviceDescriptor.title()); device->setParams(deviceDescriptor.params()); @@ -1655,9 +1722,9 @@ DeviceSetupInfo* DeviceManagerImplementation::setupDevice(Device *device) DevicePlugin *plugin = m_devicePlugins.value(deviceClass.pluginId()); if (!plugin) { - qCWarning(dcDeviceManager) << "Can't find a plugin for this device" << device->id(); - DeviceSetupInfo *info = new DeviceSetupInfo(nullptr, this); - info->finish(Device::DeviceErrorPluginNotFound); + qCWarning(dcDeviceManager) << "Can't find a plugin for this device" << device; + DeviceSetupInfo *info = new DeviceSetupInfo(device, this); + info->finish(Device::DeviceErrorPluginNotFound, tr("The plugin for this device or service is not loaded.")); return info; } diff --git a/libnymea-core/libnymea-core.pro b/libnymea-core/libnymea-core.pro index c7494177..d345d713 100644 --- a/libnymea-core/libnymea-core.pro +++ b/libnymea-core/libnymea-core.pro @@ -16,6 +16,7 @@ RESOURCES += $$top_srcdir/icons.qrc \ HEADERS += nymeacore.h \ + devices/deviceclasscache.h \ devices/devicemanagerimplementation.h \ devices/translator.h \ experiences/experiencemanager.h \ @@ -100,6 +101,7 @@ HEADERS += nymeacore.h \ SOURCES += nymeacore.cpp \ + devices/deviceclasscache.cpp \ devices/devicemanagerimplementation.cpp \ devices/translator.cpp \ experiences/experiencemanager.cpp \ diff --git a/libnymea/devices/device.cpp b/libnymea/devices/device.cpp index c58e7ceb..a7a9976f 100644 --- a/libnymea/devices/device.cpp +++ b/libnymea/devices/device.cpp @@ -119,20 +119,20 @@ #include /*! Construct an Device with the given \a pluginId, \a id, \a deviceClassId and \a parent. */ -Device::Device(DevicePlugin *plugin, const DeviceClass &deviceClass, const DeviceId &id, QObject *parent): +Device::Device(const PluginId &pluginId, const DeviceClass &deviceClass, const DeviceId &id, QObject *parent): QObject(parent), m_deviceClass(deviceClass), - m_plugin(plugin), + m_pluginId(pluginId), m_id(id) { } /*! Construct an Device with the given \a pluginId, \a deviceClassId and \a parent. A new DeviceId will be created for this Device. */ -Device::Device(DevicePlugin *plugin, const DeviceClass &deviceClass, QObject *parent): +Device::Device(const PluginId &pluginId, const DeviceClass &deviceClass, QObject *parent): QObject(parent), m_deviceClass(deviceClass), - m_plugin(plugin), + m_pluginId(pluginId), m_id(DeviceId::createDeviceId()) { @@ -153,7 +153,7 @@ DeviceClassId Device::deviceClassId() const /*! Returns the id of the \l{DevicePlugin} this Device is managed by. */ PluginId Device::pluginId() const { - return m_plugin->pluginId(); + return m_pluginId; } /*! Returns the \l{DeviceClass} of this device. */ @@ -162,12 +162,6 @@ DeviceClass Device::deviceClass() const return m_deviceClass; } -/*! Returns the the \l{DevicePlugin} this Device is managed by. */ -DevicePlugin *Device::plugin() const -{ - return m_plugin; -} - /*! Returns the name of this Device. This is visible to the user. */ QString Device::name() const { diff --git a/libnymea/devices/device.h b/libnymea/devices/device.h index 70768ce6..d00054da 100644 --- a/libnymea/devices/device.h +++ b/libnymea/devices/device.h @@ -105,7 +105,6 @@ public: PluginId pluginId() const; DeviceClass deviceClass() const; - DevicePlugin* plugin() const; QString name() const; void setName(const QString &name); @@ -153,14 +152,14 @@ signals: private: friend class DeviceManager; friend class DeviceManagerImplementation; - Device(DevicePlugin *plugin, const DeviceClass &deviceClass, const DeviceId &id, QObject *parent = nullptr); - Device(DevicePlugin *plugin, const DeviceClass &deviceClass, QObject *parent = nullptr); + Device(const PluginId &pluginId, const DeviceClass &deviceClass, const DeviceId &id, QObject *parent = nullptr); + Device(const PluginId &pluginId, const DeviceClass &deviceClass, QObject *parent = nullptr); void setSetupStatus(Device::DeviceSetupStatus status, Device::DeviceError setupError, const QString &displayMessage = QString()); private: DeviceClass m_deviceClass; - DevicePlugin* m_plugin = nullptr; + PluginId m_pluginId; DeviceId m_id; DeviceId m_parentId;