diff --git a/libnymea-core/devices/devicemanagerimplementation.cpp b/libnymea-core/devices/devicemanagerimplementation.cpp index 1bdc273b..60ccba01 100644 --- a/libnymea-core/devices/devicemanagerimplementation.cpp +++ b/libnymea-core/devices/devicemanagerimplementation.cpp @@ -1047,7 +1047,7 @@ void DeviceManagerImplementation::loadPlugin(DevicePlugin *pluginIface, const Pl m_devicePlugins.insert(pluginIface->pluginId(), pluginIface); - connect(pluginIface, &DevicePlugin::emitEvent, this, &DeviceManagerImplementation::eventTriggered); + connect(pluginIface, &DevicePlugin::emitEvent, this, &DeviceManagerImplementation::onEventTriggered); connect(pluginIface, &DevicePlugin::devicesDiscovered, this, &DeviceManagerImplementation::slotDevicesDiscovered, Qt::QueuedConnection); connect(pluginIface, &DevicePlugin::deviceSetupFinished, this, &DeviceManagerImplementation::slotDeviceSetupFinished); connect(pluginIface, &DevicePlugin::actionExecutionFinished, this, &DeviceManagerImplementation::actionExecutionFinished); @@ -1526,6 +1526,23 @@ void DeviceManagerImplementation::cleanupDeviceStateCache() } } +void DeviceManagerImplementation::onEventTriggered(const Event &event) +{ + // Doing some sanity checks here... + Device *device = m_configuredDevices.value(event.deviceId()); + if (!device) { + qCWarning(dcDeviceManager()) << "Invalid device id in emitted event. Not forwarding event."; + return; + } + EventType eventType = device->deviceClass().eventTypes().findById(event.eventTypeId()); + if (!eventType.isValid()) { + qCWarning(dcDeviceManager()) << "The given device does not have an event type of id " + event.eventTypeId().toString() + ". Not forwarding event."; + return; + } + // All good, forward the event + emit eventTriggered(event); +} + void DeviceManagerImplementation::slotDeviceStateValueChanged(const StateTypeId &stateTypeId, const QVariant &value) { Device *device = qobject_cast(sender()); diff --git a/libnymea-core/devices/devicemanagerimplementation.h b/libnymea-core/devices/devicemanagerimplementation.h index 75141c5d..cac8c173 100644 --- a/libnymea-core/devices/devicemanagerimplementation.h +++ b/libnymea-core/devices/devicemanagerimplementation.h @@ -125,6 +125,7 @@ private slots: void onAutoDeviceDisappeared(const DeviceId &deviceId); void onLoaded(); void cleanupDeviceStateCache(); + void onEventTriggered(const Event &event); // Only connect this to Devices. It will query the sender() void slotDeviceStateValueChanged(const StateTypeId &stateTypeId, const QVariant &value); diff --git a/libnymea-core/ruleengine/ruleengine.cpp b/libnymea-core/ruleengine/ruleengine.cpp index 916b7fc6..72f98733 100644 --- a/libnymea-core/ruleengine/ruleengine.cpp +++ b/libnymea-core/ruleengine/ruleengine.cpp @@ -142,6 +142,10 @@ RuleEngine::~RuleEngine() QList RuleEngine::evaluateEvent(const Event &event) { Device *device = NymeaCore::instance()->deviceManager()->findConfiguredDevice(event.deviceId()); + if (!device) { + qCWarning(dcRuleEngine()) << "Invalid event. DeviceID does not reference a valid device"; + return QList(); + } DeviceClass deviceClass = NymeaCore::instance()->deviceManager()->findDeviceClass(device->deviceClassId()); EventType eventType = deviceClass.eventTypes().findById(event.eventTypeId());