diff --git a/libnymea-app-core/devicemanager.cpp b/libnymea-app-core/devicemanager.cpp index a24230d1..325b099b 100644 --- a/libnymea-app-core/devicemanager.cpp +++ b/libnymea-app-core/devicemanager.cpp @@ -34,22 +34,6 @@ DeviceManager::DeviceManager(JsonRpcClient* jsonclient, QObject *parent) : m_jsonClient(jsonclient) { m_jsonClient->registerNotificationHandler(this, "notificationReceived"); - EventHandler *eventHandler = new EventHandler(this); - m_jsonClient->registerNotificationHandler(eventHandler, "notificationReceived"); - connect(eventHandler, &EventHandler::eventReceived, this, [this](const QVariantMap event) { - QUuid deviceId = event.value("deviceId").toUuid(); - QUuid eventTypeId = event.value("eventTypeId").toUuid(); - - Device *dev = m_devices->getDevice(deviceId); - if (!dev) { - qWarning() << "received an event from a device we don't know..." << deviceId << event; - return; - } -// qDebug() << "Event received" << deviceId.toString() << eventTypeId.toString(); - dev->eventTriggered(eventTypeId.toString(), event.value("params").toMap()); - emit eventTriggered(deviceId.toString(), eventTypeId.toString(), event.value("params").toMap()); - - }); } void DeviceManager::clear() @@ -62,6 +46,35 @@ void DeviceManager::clear() void DeviceManager::init() { + // For old nymea setups we need to register to Events.Notifications. + // Deprecated since JSONRPC 4.0/nymea 0.17 + if (!m_jsonClient->ensureServerVersion("4.0")) { + if (!m_eventHandler) { + m_eventHandler = new EventHandler(this); + m_jsonClient->registerNotificationHandler(m_eventHandler, "notificationReceived"); + connect(m_eventHandler, &EventHandler::eventReceived, this, [this](const QVariantMap event) { + QUuid deviceId = event.value("deviceId").toUuid(); + QUuid eventTypeId = event.value("eventTypeId").toUuid(); + + Device *dev = m_devices->getDevice(deviceId); + if (!dev) { + qWarning() << "received an event from a device we don't know..." << deviceId << event; + return; + } +// qDebug() << "Event received" << deviceId.toString() << eventTypeId.toString(); + dev->eventTriggered(eventTypeId.toString(), event.value("params").toMap()); + emit eventTriggered(deviceId.toString(), eventTypeId.toString(), event.value("params").toMap()); + }); + } + } else { + if (m_eventHandler) { + m_jsonClient->unregisterNotificationHandler(m_eventHandler); + m_eventHandler->deleteLater(); + m_eventHandler = nullptr; + } + } + + m_fetchingData = true; emit fetchingDataChanged(); m_jsonClient->sendCommand("Devices.GetPlugins", this, "getPluginsResponse"); @@ -166,6 +179,18 @@ void DeviceManager::notificationReceived(const QVariantMap &data) return; } p->setValue(value); + } else if (notification == "Devices.EventTriggered") { + QVariantMap event = data.value("params").toMap().value("event").toMap(); + QUuid deviceId = event.value("deviceId").toUuid(); + QUuid eventTypeId = event.value("eventTypeId").toUuid(); + + Device *dev = m_devices->getDevice(deviceId); + if (!dev) { + qWarning() << "received an event from a device we don't know..." << deviceId << qUtf8Printable(QJsonDocument::fromVariant(data).toJson()); + return; + } +// qDebug() << "Event received" << deviceId.toString() << eventTypeId.toString(); + dev->eventTriggered(eventTypeId.toString(), event.value("params").toMap()); } else { qWarning() << "DeviceManager unhandled device notification received" << notification; } @@ -472,8 +497,9 @@ int DeviceManager::executeAction(const QUuid &deviceId, const QUuid &actionTypeI if (!params.isEmpty()) { p.insert("params", params); } + QString method = m_jsonClient->ensureServerVersion("4.0") ? "Devices.ExecuteAction" : "Actions.ExecuteAction"; - return m_jsonClient->sendCommand("Actions.ExecuteAction", p, this, "executeActionResponse"); + return m_jsonClient->sendCommand(method, p, this, "executeActionResponse"); } BrowserItems *DeviceManager::browseDevice(const QUuid &deviceId, const QString &itemId) diff --git a/libnymea-app-core/devicemanager.h b/libnymea-app-core/devicemanager.h index 33c4c9a7..eaaa0495 100644 --- a/libnymea-app-core/devicemanager.h +++ b/libnymea-app-core/devicemanager.h @@ -33,6 +33,7 @@ class BrowserItem; class BrowserItems; +class EventHandler; class DeviceManager : public JsonHandler { @@ -141,10 +142,12 @@ private: QHash > m_browserDetailsRequests; + // Deprecated stuff for nymea < 0.17 (JSONRPC < 4.0) + EventHandler *m_eventHandler = nullptr; }; -// TODO: Kinda shitty that Device Events are not sent from the Devices namespace... +// TODO: This is deprecated in nymea now (JSONRPC 4.0/nymea 0.17). Keeping it for a bit for backwards compatibility class EventHandler: public JsonHandler { Q_OBJECT diff --git a/nymea-app/ui/devicepages/GenericDevicePage.qml b/nymea-app/ui/devicepages/GenericDevicePage.qml index 3be7f3db..b89d5798 100644 --- a/nymea-app/ui/devicepages/GenericDevicePage.qml +++ b/nymea-app/ui/devicepages/GenericDevicePage.qml @@ -393,13 +393,10 @@ DevicePageBase { ColorAnimation { to: "lightgray"; duration: 500 } } } - LogsModelNg { - engine: _engine - live: true - deviceId: root.device.id - typeIds: eventComponentItem.eventType.id - onCountChanged: { - flashlightAnimation.start() + Connections { + target: root.device + onEventTriggered: { + flashlightAnimation.start(); } } }