Merge PR #305: Allow emitting events from things directly

This commit is contained in:
Jenkins nymea 2020-07-14 11:26:14 +02:00
commit c369bd15aa
4 changed files with 22 additions and 9 deletions

View File

@ -647,6 +647,7 @@ ThingPairingInfo *ThingManagerImplementation::confirmPairing(const PairingTransa
qCDebug(dcThingManager()) << "Thing added:" << info->thing(); qCDebug(dcThingManager()) << "Thing added:" << info->thing();
m_configuredThings.insert(info->thing()->id(), info->thing()); m_configuredThings.insert(info->thing()->id(), info->thing());
emit thingAdded(info->thing()); emit thingAdded(info->thing());
connect(info->thing(), &Thing::eventTriggered, this, &ThingManagerImplementation::onEventTriggered);
} else { } else {
emit thingChanged(info->thing()); emit thingChanged(info->thing());
} }
@ -728,9 +729,10 @@ ThingSetupInfo* ThingManagerImplementation::addConfiguredThingInternal(const Thi
qCDebug(dcThingManager) << "Thing setup complete."; qCDebug(dcThingManager) << "Thing setup complete.";
m_configuredThings.insert(info->thing()->id(), info->thing()); m_configuredThings.insert(info->thing()->id(), info->thing());
storeConfiguredThings(); storeConfiguredThings();
postSetupThing(info->thing());
emit thingAdded(info->thing()); emit thingAdded(info->thing());
connect(info->thing(), &Thing::eventTriggered, this, &ThingManagerImplementation::onEventTriggered);
postSetupThing(info->thing());
}); });
return info; return info;
@ -1560,6 +1562,8 @@ void ThingManagerImplementation::loadConfiguredThings()
m_configuredThings.insert(thing->id(), thing); m_configuredThings.insert(thing->id(), thing);
emit thingAdded(thing); emit thingAdded(thing);
connect(thing, &Thing::eventTriggered, this, &ThingManagerImplementation::onEventTriggered);
} }
settings.endGroup(); settings.endGroup();
@ -1710,7 +1714,7 @@ void ThingManagerImplementation::onAutoThingsAppeared(const ThingDescriptors &th
storeConfiguredThings(); storeConfiguredThings();
emit thingAdded(info->thing()); emit thingAdded(info->thing());
connect(info->thing(), &Thing::eventTriggered, this, &ThingManagerImplementation::onEventTriggered);
postSetupThing(info->thing()); postSetupThing(info->thing());
}); });
} }

View File

@ -385,6 +385,12 @@ QString Thing::setupDisplayMessage() const
return m_setupDisplayMessage; return m_setupDisplayMessage;
} }
/*! Emits an event from this thing to the system. */
void Thing::emitEvent(const EventTypeId &eventTypeId, const ParamList &params)
{
emit eventTriggered(Event(eventTypeId, m_id, params));
}
/*! Returns true if this thing has been auto-created (not created by the user) */ /*! Returns true if this thing has been auto-created (not created by the user) */
bool Thing::autoCreated() const bool Thing::autoCreated() const
{ {

View File

@ -37,6 +37,7 @@
#include "types/thingclass.h" #include "types/thingclass.h"
#include "types/state.h" #include "types/state.h"
#include "types/param.h" #include "types/param.h"
#include "types/event.h"
#include "types/browseritem.h" #include "types/browseritem.h"
#include <QObject> #include <QObject>
@ -127,10 +128,10 @@ public:
bool hasState(const StateTypeId &stateTypeId) const; bool hasState(const StateTypeId &stateTypeId) const;
void setStates(const States &states); void setStates(const States &states);
QVariant stateValue(const StateTypeId &stateTypeId) const; Q_INVOKABLE QVariant stateValue(const StateTypeId &stateTypeId) const;
void setStateValue(const StateTypeId &stateTypeId, const QVariant &value); Q_INVOKABLE void setStateValue(const StateTypeId &stateTypeId, const QVariant &value);
State state(const StateTypeId &stateTypeId) const; Q_INVOKABLE State state(const StateTypeId &stateTypeId) const;
ThingId parentId() const; ThingId parentId() const;
void setParentId(const ThingId &parentId); void setParentId(const ThingId &parentId);
@ -143,11 +144,15 @@ public:
ThingError setupError() const; ThingError setupError() const;
QString setupDisplayMessage() const; QString setupDisplayMessage() const;
public slots:
void emitEvent(const EventTypeId &eventTypeId, const ParamList &params = ParamList());
signals: signals:
void stateValueChanged(const StateTypeId &stateTypeId, const QVariant &value); void stateValueChanged(const StateTypeId &stateTypeId, const QVariant &value);
void settingChanged(const ParamTypeId &paramTypeId, const QVariant &value); void settingChanged(const ParamTypeId &paramTypeId, const QVariant &value);
void nameChanged(); void nameChanged();
void setupStatusChanged(); void setupStatusChanged();
void eventTriggered(const Event &event);
private: private:
friend class ThingManager; friend class ThingManager;

View File

@ -853,10 +853,8 @@ void IntegrationPluginMock::triggerEvent(const EventTypeId &id)
Thing *device = m_daemons.key(daemon); Thing *device = m_daemons.key(daemon);
Event event(id, device->id()); qCDebug(dcMock) << "Emitting event " << id;
device->emitEvent(id);
qCDebug(dcMock) << "Emitting event " << event.eventTypeId();
emit emitEvent(event);
} }
void IntegrationPluginMock::onDisappear() void IntegrationPluginMock::onDisappear()