diff --git a/libguh/interfaces/interfaces.qrc b/libguh/interfaces/interfaces.qrc index c8f1d169..8ad817b9 100644 --- a/libguh/interfaces/interfaces.qrc +++ b/libguh/interfaces/interfaces.qrc @@ -6,5 +6,6 @@ colorlight.json garagegate.json gateway.json + notifications.json diff --git a/libguh/interfaces/notifications.json b/libguh/interfaces/notifications.json new file mode 100644 index 00000000..b6005829 --- /dev/null +++ b/libguh/interfaces/notifications.json @@ -0,0 +1,14 @@ +{ + "actions": [ + { + "name": "notify", + "params": [ + { + "name": "title", + "type": "QString" + } + ] + } + + ] +} diff --git a/libguh/plugin/deviceplugin.cpp b/libguh/plugin/deviceplugin.cpp index a9ebea9e..111ebdcf 100644 --- a/libguh/plugin/deviceplugin.cpp +++ b/libguh/plugin/deviceplugin.cpp @@ -541,20 +541,46 @@ QList DevicePlugin::supportedDevices() const QVariantList actions = interfaceMap.value("actions").toList(); foreach (const QVariant &actionVariant, actions) { QVariantMap actionMap = actionVariant.toMap(); - if (actionTypes.findByName(actionMap.value("name").toString()).id().isNull()) { + ActionType actionType = actionTypes.findByName(actionMap.value("name").toString()); + if (actionType.id().isNull()) { qCWarning(dcDeviceManager) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but doesn't implement action" << actionMap.value("name").toString(); valid = false; } - // TODO: check params + QVariantList params = actionMap.value("params").toList(); + foreach (const QVariant ¶mVariant, params) { + ParamType paramType = actionType.paramTypes().findByName(paramVariant.toMap().value("name").toString()); + if (!paramType.isValid()) { + qCWarning(dcDeviceManager) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but doesn't implement action param" << actionMap.value("name").toString() << ":" << paramVariant.toMap().value("name").toString(); + valid = false; + } else { + if (paramType.type() != QVariant::nameToType(paramVariant.toMap().value("type").toString().toLatin1())) { + qCWarning(dcDeviceManager()) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but param" << paramType.name() << "is of wrong type:" << QVariant::typeToName(paramType.type()) << "expected:" << paramVariant.toMap().value("type").toString(); + valid = false; + } + } + } } QVariantList events = interfaceMap.value("events").toList(); foreach (const QVariant &eventVariant, events) { QVariantMap eventMap = eventVariant.toMap(); - if (eventTypes.findByName(eventMap.value("name").toString()).id().isNull()) { + EventType eventType = eventTypes.findByName(eventMap.value("name").toString()); + if (eventType.isValid()) { qCWarning(dcDeviceManager) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but doesn't implement event" << eventMap.value("name").toString(); valid = false; } - // TODO: check params + QVariantList params = eventMap.value("params").toList(); + foreach (const QVariant ¶mVariant, params) { + ParamType paramType = eventType.paramTypes().findByName(paramVariant.toMap().value("name").toString()); + if (!paramType.isValid()) { + qCWarning(dcDeviceManager) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but doesn't implement action param" << eventMap.value("name").toString() << ":" << paramVariant.toMap().value("name").toString(); + valid = false; + } else { + if (paramType.type() != QVariant::nameToType(paramVariant.toMap().value("type").toString().toLatin1())) { + qCWarning(dcDeviceManager()) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but param" << paramType.name() << "is of wrong type:" << QVariant::typeToName(paramType.type()) << "expected:" << paramVariant.toMap().value("type").toString(); + valid = false; + } + } + } } if (valid) { diff --git a/libguh/types/actiontype.cpp b/libguh/types/actiontype.cpp index bbe34188..0dbab6ac 100644 --- a/libguh/types/actiontype.cpp +++ b/libguh/types/actiontype.cpp @@ -80,14 +80,14 @@ void ActionType::setIndex(const int &index) /*! Returns the parameter description of this \l{ActionType}. \l{Action}{Actions} created * from this \l{ActionType} must have their parameters matching to this template. */ -QList ActionType::paramTypes() const +ParamTypes ActionType::paramTypes() const { return m_paramTypes; } /*! Set the parameter description of this \l{ActionType}. \l{Action}{Actions} created * from this \l{ActionType} must have their \a paramTypes matching to this template. */ -void ActionType::setParamTypes(const QList ¶mTypes) +void ActionType::setParamTypes(const ParamTypes ¶mTypes) { m_paramTypes = paramTypes; } diff --git a/libguh/types/actiontype.h b/libguh/types/actiontype.h index 482438d4..c51c6e0a 100644 --- a/libguh/types/actiontype.h +++ b/libguh/types/actiontype.h @@ -43,14 +43,14 @@ public: int index() const; void setIndex(const int &index); - QList paramTypes() const; - void setParamTypes(const QList ¶mTypes); + ParamTypes paramTypes() const; + void setParamTypes(const ParamTypes ¶mTypes); private: ActionTypeId m_id; QString m_name; int m_index; - QList m_paramTypes; + ParamTypes m_paramTypes; }; class ActionTypes: public QList diff --git a/libguh/types/eventtype.cpp b/libguh/types/eventtype.cpp index 2bd50a82..e62ebbc9 100644 --- a/libguh/types/eventtype.cpp +++ b/libguh/types/eventtype.cpp @@ -76,14 +76,14 @@ void EventType::setIndex(const int &index) /*! Holds a List describing possible parameters for a \l{Event} of this EventType. * e.g. QList(ParamType("temperature", QVariant::Real)). */ -QList EventType::paramTypes() const +ParamTypes EventType::paramTypes() const { return m_paramTypes; } /*! Set the parameter description for this EventType to \a paramTypes, * e.g. QList() << ParamType("temperature", QVariant::Real)). */ -void EventType::setParamTypes(const QList ¶mTypes) +void EventType::setParamTypes(const ParamTypes ¶mTypes) { m_paramTypes = paramTypes; } @@ -112,6 +112,12 @@ void EventType::setGraphRelevant(const bool &graphRelevant) m_graphRelevant = graphRelevant; } +/*! Returns true if this EventType has a valid id and name */ +bool EventType::isValid() const +{ + return !m_id.isNull() && !m_name.isEmpty(); +} + EventTypes::EventTypes(const QList &other) { foreach (const EventType &at, other) { diff --git a/libguh/types/eventtype.h b/libguh/types/eventtype.h index 87323c46..32269220 100644 --- a/libguh/types/eventtype.h +++ b/libguh/types/eventtype.h @@ -43,8 +43,8 @@ public: int index() const; void setIndex(const int &index); - QList paramTypes() const; - void setParamTypes(const QList ¶mTypes); + ParamTypes paramTypes() const; + void setParamTypes(const ParamTypes ¶mTypes); bool ruleRelevant() const; void setRuleRelevant(const bool &ruleRelevant); @@ -52,6 +52,7 @@ public: bool graphRelevant() const; void setGraphRelevant(const bool &graphRelevant); + bool isValid() const; private: EventTypeId m_id; QString m_name; diff --git a/libguh/types/paramtype.cpp b/libguh/types/paramtype.cpp index d94487b7..9fe92b46 100644 --- a/libguh/types/paramtype.cpp +++ b/libguh/types/paramtype.cpp @@ -187,6 +187,11 @@ void ParamType::setReadOnly(const bool &readOnly) m_readOnly = readOnly; } +bool ParamType::isValid() const +{ + return !m_id.isNull() && !m_name.isEmpty() && m_type != QVariant::Invalid; +} + /*! Writes the name, type defaultValue, min value, max value and readOnly of the given \a paramType to \a dbg. */ QDebug operator<<(QDebug dbg, const ParamType ¶mType) { @@ -213,3 +218,30 @@ QDebug operator<<(QDebug dbg, const QList ¶mTypes) return dbg.space(); } + +ParamTypes::ParamTypes(const QList &other) +{ + foreach (const ParamType &pt, other) { + append(pt); + } +} + +ParamType ParamTypes::findByName(const QString &name) +{ + foreach (const ParamType ¶mType, *this) { + if (paramType.name() == name) { + return paramType; + } + } + return ParamType(); +} + +ParamType ParamTypes::findById(const ParamTypeId &id) +{ + foreach (const ParamType ¶mType, *this) { + if (paramType.id() == id) { + return paramType; + } + } + return ParamType(); +} diff --git a/libguh/types/paramtype.h b/libguh/types/paramtype.h index b7426770..11d77603 100644 --- a/libguh/types/paramtype.h +++ b/libguh/types/paramtype.h @@ -32,7 +32,8 @@ class LIBGUH_EXPORT ParamType { -public: +public: + ParamType() = default; ParamType(const ParamTypeId &id, const QString &name, const QVariant::Type type, const QVariant &defaultValue = QVariant()); ParamTypeId id() const; @@ -70,6 +71,8 @@ public: bool readOnly() const; void setReadOnly(const bool &readOnly); + bool isValid() const; + private: ParamTypeId m_id; QString m_name; @@ -84,6 +87,15 @@ private: bool m_readOnly; }; +class ParamTypes: public QList +{ +public: + ParamTypes() = default; + ParamTypes(const QList &other); + ParamType findByName(const QString &name); + ParamType findById(const ParamTypeId &id); +}; + QDebug operator<<(QDebug dbg, const ParamType ¶mType); QDebug operator<<(QDebug dbg, const QList ¶mTypes);