Merge PR #284: Don't use deprecated Events and Actions namespaces any more

This commit is contained in:
Jenkins nymea 2019-12-12 14:29:44 +01:00
commit dae0f29688
3 changed files with 51 additions and 25 deletions

View File

@ -34,22 +34,6 @@ DeviceManager::DeviceManager(JsonRpcClient* jsonclient, QObject *parent) :
m_jsonClient(jsonclient) m_jsonClient(jsonclient)
{ {
m_jsonClient->registerNotificationHandler(this, "notificationReceived"); 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() void DeviceManager::clear()
@ -62,6 +46,35 @@ void DeviceManager::clear()
void DeviceManager::init() 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; m_fetchingData = true;
emit fetchingDataChanged(); emit fetchingDataChanged();
m_jsonClient->sendCommand("Devices.GetPlugins", this, "getPluginsResponse"); m_jsonClient->sendCommand("Devices.GetPlugins", this, "getPluginsResponse");
@ -166,6 +179,18 @@ void DeviceManager::notificationReceived(const QVariantMap &data)
return; return;
} }
p->setValue(value); 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 { } else {
qWarning() << "DeviceManager unhandled device notification received" << notification; qWarning() << "DeviceManager unhandled device notification received" << notification;
} }
@ -472,8 +497,9 @@ int DeviceManager::executeAction(const QUuid &deviceId, const QUuid &actionTypeI
if (!params.isEmpty()) { if (!params.isEmpty()) {
p.insert("params", params); 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) BrowserItems *DeviceManager::browseDevice(const QUuid &deviceId, const QString &itemId)

View File

@ -33,6 +33,7 @@
class BrowserItem; class BrowserItem;
class BrowserItems; class BrowserItems;
class EventHandler;
class DeviceManager : public JsonHandler class DeviceManager : public JsonHandler
{ {
@ -141,10 +142,12 @@ private:
QHash<int, QPointer<BrowserItem> > m_browserDetailsRequests; QHash<int, QPointer<BrowserItem> > 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 { class EventHandler: public JsonHandler {
Q_OBJECT Q_OBJECT

View File

@ -393,13 +393,10 @@ DevicePageBase {
ColorAnimation { to: "lightgray"; duration: 500 } ColorAnimation { to: "lightgray"; duration: 500 }
} }
} }
LogsModelNg { Connections {
engine: _engine target: root.device
live: true onEventTriggered: {
deviceId: root.device.id flashlightAnimation.start();
typeIds: eventComponentItem.eventType.id
onCountChanged: {
flashlightAnimation.start()
} }
} }
} }