add interface for notifications, finish param checking implementation

This commit is contained in:
Michael Zanetti 2017-10-06 10:37:46 +02:00
parent 8c0ae4eff6
commit a22d220cb7
9 changed files with 106 additions and 14 deletions

View File

@ -6,5 +6,6 @@
<file>colorlight.json</file>
<file>garagegate.json</file>
<file>gateway.json</file>
<file>notifications.json</file>
</qresource>
</RCC>

View File

@ -0,0 +1,14 @@
{
"actions": [
{
"name": "notify",
"params": [
{
"name": "title",
"type": "QString"
}
]
}
]
}

View File

@ -541,20 +541,46 @@ QList<DeviceClass> 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 &paramVariant, 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 &paramVariant, 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) {

View File

@ -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<ParamType> 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<ParamType> &paramTypes)
void ActionType::setParamTypes(const ParamTypes &paramTypes)
{
m_paramTypes = paramTypes;
}

View File

@ -43,14 +43,14 @@ public:
int index() const;
void setIndex(const int &index);
QList<ParamType> paramTypes() const;
void setParamTypes(const QList<ParamType> &paramTypes);
ParamTypes paramTypes() const;
void setParamTypes(const ParamTypes &paramTypes);
private:
ActionTypeId m_id;
QString m_name;
int m_index;
QList<ParamType> m_paramTypes;
ParamTypes m_paramTypes;
};
class ActionTypes: public QList<ActionType>

View File

@ -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<ParamType> EventType::paramTypes() const
ParamTypes EventType::paramTypes() const
{
return m_paramTypes;
}
/*! Set the parameter description for this EventType to \a paramTypes,
* e.g. QList<ParamType>() << ParamType("temperature", QVariant::Real)). */
void EventType::setParamTypes(const QList<ParamType> &paramTypes)
void EventType::setParamTypes(const ParamTypes &paramTypes)
{
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<EventType> &other)
{
foreach (const EventType &at, other) {

View File

@ -43,8 +43,8 @@ public:
int index() const;
void setIndex(const int &index);
QList<ParamType> paramTypes() const;
void setParamTypes(const QList<ParamType> &paramTypes);
ParamTypes paramTypes() const;
void setParamTypes(const ParamTypes &paramTypes);
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;

View File

@ -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 &paramType)
{
@ -213,3 +218,30 @@ QDebug operator<<(QDebug dbg, const QList<ParamType> &paramTypes)
return dbg.space();
}
ParamTypes::ParamTypes(const QList<ParamType> &other)
{
foreach (const ParamType &pt, other) {
append(pt);
}
}
ParamType ParamTypes::findByName(const QString &name)
{
foreach (const ParamType &paramType, *this) {
if (paramType.name() == name) {
return paramType;
}
}
return ParamType();
}
ParamType ParamTypes::findById(const ParamTypeId &id)
{
foreach (const ParamType &paramType, *this) {
if (paramType.id() == id) {
return paramType;
}
}
return ParamType();
}

View File

@ -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<ParamType>
{
public:
ParamTypes() = default;
ParamTypes(const QList<ParamType> &other);
ParamType findByName(const QString &name);
ParamType findById(const ParamTypeId &id);
};
QDebug operator<<(QDebug dbg, const ParamType &paramType);
QDebug operator<<(QDebug dbg, const QList<ParamType> &paramTypes);