devices doesn't access the Engine singleton any more

This is a not *that* straight forward. For convenience, Devices
offers information from the DeviceClasses and used to get that stuff
from the engine. In order to solve this I've added a pointer to the
DeviceClass in Device. This however, implies that DeviceClasses
must be created before Devices and destroyed after them. As the
general concept of the system is built in such a way this isn't
an issue but it requires some extra throughts when tearing down
things.
This commit is contained in:
Michael Zanetti 2018-08-31 19:41:55 +02:00
parent b1338caf33
commit 56a87e9e97
5 changed files with 62 additions and 29 deletions

View File

@ -107,6 +107,13 @@ void DeviceManager::notificationReceived(const QVariantMap &data)
delete dev; delete dev;
return; return;
} }
DeviceClass *dc = deviceClasses()->getDeviceClass(dev->deviceClassId());
if (!dc) {
qWarning() << "Skipping invalid device. Don't have a device class for it";
delete dev;
return;
}
dev->setDeviceClass(dc);
m_devices->addDevice(dev); m_devices->addDevice(dev);
} else if (notification == "Devices.DeviceRemoved") { } else if (notification == "Devices.DeviceRemoved") {
QUuid deviceId = data.value("params").toMap().value("deviceId").toUuid(); QUuid deviceId = data.value("params").toMap().value("deviceId").toUuid();
@ -221,6 +228,7 @@ void DeviceManager::getConfiguredDevicesResponse(const QVariantMap &params)
qWarning() << "Can't find a deviceClass for this device" << device->name(); qWarning() << "Can't find a deviceClass for this device" << device->name();
continue; continue;
} }
device->setDeviceClass(dc);
// set initial state values // set initial state values
QVariantList stateVariantList = deviceVariant.toMap().value("states").toList(); QVariantList stateVariantList = deviceVariant.toMap().value("states").toList();
@ -255,12 +263,21 @@ void DeviceManager::addDeviceResponse(const QVariantMap &params)
} else if (params.value("params").toMap().keys().contains("device")) { } else if (params.value("params").toMap().keys().contains("device")) {
QVariantMap deviceVariant = params.value("params").toMap().value("device").toMap(); QVariantMap deviceVariant = params.value("params").toMap().value("device").toMap();
Device *device = new Device(); Device *device = new Device();
if (JsonTypes::unpackDevice(deviceVariant, device)) { if (!JsonTypes::unpackDevice(deviceVariant, device)) {
qDebug() << "Device added" << device->id().toString(); qWarning() << "Couldn't parse json in addDeviceResponse";
m_devices->addDevice(device); device->deleteLater();
} else { return;
qWarning() << "Error unpacking device json";
} }
DeviceClass *dc = deviceClasses()->getDeviceClass(device->deviceClassId());
if (!dc) {
qWarning() << "Uh.. We couldn't find a DeviceClass for the Device we just added. Skipping...";
device->deleteLater();
return;
}
device->setDeviceClass(dc);
qDebug() << "Device added" << device->id().toString();
m_devices->addDevice(device);
} }
emit addDeviceReply(params.value("params").toMap()); emit addDeviceReply(params.value("params").toMap());
} }

View File

@ -75,54 +75,47 @@ QVariant Devices::data(const QModelIndex &index, int role) const
case RoleSetupComplete: case RoleSetupComplete:
return device->setupComplete(); return device->setupComplete();
case RoleInterfaces: { case RoleInterfaces: {
DeviceClass *dc = Engine::instance()->deviceManager()->deviceClasses()->getDeviceClass(device->deviceClassId()); return device->deviceClass()->interfaces();
if (!dc) {
return QVariant();
}
return dc->interfaces();
} }
case RoleBaseInterface: { case RoleBaseInterface: {
DeviceClass *dc = Engine::instance()->deviceManager()->deviceClasses()->getDeviceClass(device->deviceClassId()); QStringList interfaces = device->deviceClass()->interfaces();
if (!dc) { if (interfaces.contains("gateway")) {
return QVariant();
}
if (dc->interfaces().contains("gateway")) {
return "gateway"; return "gateway";
} }
if (dc->interfaces().contains("shutter")) { if (interfaces.contains("shutter")) {
return "shutter"; return "shutter";
} }
if (dc->interfaces().contains("blind")) { if (interfaces.contains("blind")) {
return "blind"; return "blind";
} }
if (dc->interfaces().contains("garagegate")) { if (interfaces.contains("garagegate")) {
return "garagegate"; return "garagegate";
} }
if (dc->interfaces().contains("inputtrigger")) { if (interfaces.contains("inputtrigger")) {
return "inputtrigger"; return "inputtrigger";
} }
if (dc->interfaces().contains("awning")) { if (interfaces.contains("awning")) {
return "awning"; return "awning";
} }
if (dc->interfaces().contains("outputtrigger")) { if (interfaces.contains("outputtrigger")) {
return "outputtrigger"; return "outputtrigger";
} }
if (dc->interfaces().contains("light")) { if (interfaces.contains("light")) {
return "light"; return "light";
} }
if (dc->interfaces().contains("sensor")) { if (interfaces.contains("sensor")) {
return "sensor"; return "sensor";
} }
if (dc->interfaces().contains("weather")) { if (interfaces.contains("weather")) {
return "weather"; return "weather";
} }
if (dc->interfaces().contains("media")) { if (interfaces.contains("media")) {
return "media"; return "media";
} }
if (dc->interfaces().contains("button")) { if (interfaces.contains("button")) {
return "button"; return "button";
} }
if (dc->interfaces().contains("notifications")) { if (interfaces.contains("notifications")) {
return "notifications"; return "notifications";
} }
return "uncategorized"; return "uncategorized";

View File

@ -43,7 +43,7 @@ public:
}; };
Q_ENUM(Roles) Q_ENUM(Roles)
explicit Devices(QObject *parent = 0); explicit Devices(QObject *parent = nullptr);
QList<Device *> devices(); QList<Device *> devices();

View File

@ -21,6 +21,7 @@
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "device.h" #include "device.h"
#include "deviceclass.h"
#include <QDebug> #include <QDebug>
@ -93,6 +94,16 @@ void Device::setStates(States *states)
emit statesChanged(); emit statesChanged();
} }
DeviceClass *Device::deviceClass() const
{
return m_deviceClass;
}
void Device::setDeviceClass(DeviceClass *deviceClass)
{
m_deviceClass = deviceClass;
}
bool Device::hasState(const QUuid &stateTypeId) bool Device::hasState(const QUuid &stateTypeId)
{ {
foreach (State *state, states()->states()) { foreach (State *state, states()->states()) {

View File

@ -30,6 +30,8 @@
#include "states.h" #include "states.h"
#include "statesproxy.h" #include "statesproxy.h"
class DeviceClass;
class Device : public QObject class Device : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -39,9 +41,10 @@ class Device : public QObject
Q_PROPERTY(bool setupComplete READ setupComplete NOTIFY setupCompleteChanged) Q_PROPERTY(bool setupComplete READ setupComplete NOTIFY setupCompleteChanged)
Q_PROPERTY(Params *params READ params NOTIFY paramsChanged) Q_PROPERTY(Params *params READ params NOTIFY paramsChanged)
Q_PROPERTY(States *states READ states NOTIFY statesChanged) Q_PROPERTY(States *states READ states NOTIFY statesChanged)
Q_PROPERTY(DeviceClass *deviceClass READ deviceClass CONSTANT)
public: public:
explicit Device(QObject *parent = 0); explicit Device(QObject *parent = nullptr);
QString name() const; QString name() const;
void setName(const QString &name); void setName(const QString &name);
@ -61,11 +64,18 @@ public:
States *states() const; States *states() const;
void setStates(States *states); void setStates(States *states);
DeviceClass *deviceClass() const;
Q_INVOKABLE bool hasState(const QUuid &stateTypeId); Q_INVOKABLE bool hasState(const QUuid &stateTypeId);
Q_INVOKABLE QVariant stateValue(const QUuid &stateTypeId); Q_INVOKABLE QVariant stateValue(const QUuid &stateTypeId);
void setStateValue(const QUuid &stateTypeId, const QVariant &value); void setStateValue(const QUuid &stateTypeId, const QVariant &value);
private:
void setDeviceClass(DeviceClass *deviceClass);
friend class DeviceManager;
private: private:
QString m_name; QString m_name;
QUuid m_id; QUuid m_id;
@ -73,6 +83,8 @@ private:
bool m_setupComplete; bool m_setupComplete;
Params *m_params = nullptr; Params *m_params = nullptr;
States *m_states = nullptr; States *m_states = nullptr;
DeviceClass *m_deviceClass = nullptr;
signals: signals:
void nameChanged(); void nameChanged();