improve plugin parsing mechanism

This commit is contained in:
Simon Stürz 2016-05-16 12:29:42 +02:00 committed by Michael Zanetti
parent c0c2f4dbbb
commit a0b974ce29
5 changed files with 38 additions and 12 deletions

View File

@ -289,6 +289,15 @@ QList<StateType> DeviceClass::stateTypes() const
return m_stateTypes; return m_stateTypes;
} }
StateType DeviceClass::getStateType(const StateTypeId &stateTypeId)
{
foreach (const StateType &stateType, m_stateTypes) {
if (stateType.id() == stateTypeId)
return stateType;
}
return StateType(StateTypeId());
}
/*! Set the \a stateTypes of this DeviceClass. \{Device}{Devices} created /*! Set the \a stateTypes of this DeviceClass. \{Device}{Devices} created
from this \l{DeviceClass} must have their states matching to this template. */ from this \l{DeviceClass} must have their states matching to this template. */
void DeviceClass::setStateTypes(const QList<StateType> &stateTypes) void DeviceClass::setStateTypes(const QList<StateType> &stateTypes)

View File

@ -141,6 +141,7 @@ public:
void setBasicTags(const QList<BasicTag> &basicTags); void setBasicTags(const QList<BasicTag> &basicTags);
QList<StateType> stateTypes() const; QList<StateType> stateTypes() const;
StateType getStateType(const StateTypeId &stateTypeId);
void setStateTypes(const QList<StateType> &stateTypes); void setStateTypes(const QList<StateType> &stateTypes);
bool hasStateType(const StateTypeId &stateTypeId); bool hasStateType(const StateTypeId &stateTypeId);

View File

@ -257,13 +257,14 @@ QList<DeviceClass> DevicePlugin::supportedDevices() const
} }
deviceClass.setBasicTags(basicTags); deviceClass.setBasicTags(basicTags);
// State Types
QList<ActionType> actionTypes; QList<ActionType> actionTypes;
QList<StateType> stateTypes; QList<StateType> stateTypes;
foreach (const QJsonValue &stateTypesJson, jo.value("stateTypes").toArray()) { foreach (const QJsonValue &stateTypesJson, jo.value("stateTypes").toArray()) {
QJsonObject st = stateTypesJson.toObject(); QJsonObject st = stateTypesJson.toObject();
QStringList missingFields = verifyFields(QStringList() << "type" << "id" << "name" << "index", st); QStringList missingFields = verifyFields(QStringList() << "type" << "id" << "name" << "index" << "defaultValue", st);
if (!missingFields.isEmpty()) { if (!missingFields.isEmpty()) {
qCWarning(dcDeviceManager) << "Skipping device class" << deviceClass.name() << "because of missing" << missingFields.join(", ") << "in stateTypes"; qCWarning(dcDeviceManager) << "Skipping device class" << deviceClass.name() << "because of missing" << missingFields.join(", ") << "in stateType" << st;
broken = true; broken = true;
break; break;
} }
@ -276,6 +277,7 @@ QList<DeviceClass> DevicePlugin::supportedDevices() const
QPair<bool, Types::Unit> unitVerification = loadAndVerifyUnit(st.value("unit").toString()); QPair<bool, Types::Unit> unitVerification = loadAndVerifyUnit(st.value("unit").toString());
if (!unitVerification.first) { if (!unitVerification.first) {
broken = true; broken = true;
break;
} else { } else {
stateType.setUnit(unitVerification.second); stateType.setUnit(unitVerification.second);
} }
@ -293,16 +295,16 @@ QList<DeviceClass> DevicePlugin::supportedDevices() const
} }
stateType.setPossibleValues(possibleValues); stateType.setPossibleValues(possibleValues);
// inform the plugin developer about the error in the plugin json file if (!stateType.possibleValues().contains(stateType.defaultValue())) {
Q_ASSERT_X(stateType.possibleValues().contains(stateType.defaultValue()), qCWarning(dcDeviceManager()) << QString("\"%1\" plugin:").arg(pluginName()).toLatin1().data() << QString("The given default value \"%1\" is not in the possible values of the stateType \"%2\".")
QString("\"%1\" plugin").arg(pluginName()).toLatin1().data(), .arg(stateType.defaultValue().toString()).arg(stateType.name()).toLatin1().data();
QString("The given default value \"%1\" is not in the possible values of the stateType \"%2\".") broken = true;
.arg(stateType.defaultValue().toString()).arg(stateType.name()).toLatin1().data()); break;
}
} }
stateTypes.append(stateType); stateTypes.append(stateType);
// create ActionType if this StateType is writable // ActionTypes for writeable StateTypes
if (st.contains("writable") && st.value("writable").toBool()) { if (st.contains("writable") && st.value("writable").toBool()) {
// Note: fields already checked in StateType // Note: fields already checked in StateType
ActionType actionType(ActionTypeId(stateType.id().toString())); ActionType actionType(ActionTypeId(stateType.id().toString()));
@ -318,6 +320,7 @@ QList<DeviceClass> DevicePlugin::supportedDevices() const
} }
deviceClass.setStateTypes(stateTypes); deviceClass.setStateTypes(stateTypes);
// ActionTypes
foreach (const QJsonValue &actionTypesJson, jo.value("actionTypes").toArray()) { foreach (const QJsonValue &actionTypesJson, jo.value("actionTypes").toArray()) {
QJsonObject at = actionTypesJson.toObject(); QJsonObject at = actionTypesJson.toObject();
QStringList missingFields = verifyFields(QStringList() << "id" << "name" << "index", at); QStringList missingFields = verifyFields(QStringList() << "id" << "name" << "index", at);
@ -333,6 +336,7 @@ QList<DeviceClass> DevicePlugin::supportedDevices() const
QPair<bool, QList<ParamType> > paramVerification = parseParamTypes(at.value("paramTypes").toArray()); QPair<bool, QList<ParamType> > paramVerification = parseParamTypes(at.value("paramTypes").toArray());
if (!paramVerification.first) { if (!paramVerification.first) {
broken = true; broken = true;
break;
} else { } else {
actionType.setParamTypes(paramVerification.second); actionType.setParamTypes(paramVerification.second);
} }
@ -341,6 +345,7 @@ QList<DeviceClass> DevicePlugin::supportedDevices() const
} }
deviceClass.setActionTypes(actionTypes); deviceClass.setActionTypes(actionTypes);
// EventTypes
QList<EventType> eventTypes; QList<EventType> eventTypes;
foreach (const QJsonValue &eventTypesJson, jo.value("eventTypes").toArray()) { foreach (const QJsonValue &eventTypesJson, jo.value("eventTypes").toArray()) {
QJsonObject et = eventTypesJson.toObject(); QJsonObject et = eventTypesJson.toObject();
@ -358,6 +363,7 @@ QList<DeviceClass> DevicePlugin::supportedDevices() const
QPair<bool, QList<ParamType> > paramVerification = parseParamTypes(et.value("paramTypes").toArray()); QPair<bool, QList<ParamType> > paramVerification = parseParamTypes(et.value("paramTypes").toArray());
if (!paramVerification.first) { if (!paramVerification.first) {
broken = true; broken = true;
break;
} else { } else {
eventType.setParamTypes(paramVerification.second); eventType.setParamTypes(paramVerification.second);
} }
@ -366,12 +372,16 @@ QList<DeviceClass> DevicePlugin::supportedDevices() const
} }
deviceClass.setEventTypes(eventTypes); deviceClass.setEventTypes(eventTypes);
// Note: do this after the actionType / stateType / eventType parsing // Note: keep this after the actionType / stateType / eventType parsing
if (jo.contains("criticalStateTypeId")) { if (jo.contains("criticalStateTypeId")) {
StateTypeId criticalStateTypeId = StateTypeId(jo.value("criticalStateTypeId").toString()); StateTypeId criticalStateTypeId = StateTypeId(jo.value("criticalStateTypeId").toString());
if (!deviceClass.hasStateType(criticalStateTypeId)) { if (!deviceClass.hasStateType(criticalStateTypeId)) {
qCWarning(dcDeviceManager) << "Skipping device class" << deviceClass.name() << ": the definend critical stateTypeId" << criticalStateTypeId.toString() << "does not match any StateType of this DeviceClass."; qCWarning(dcDeviceManager) << "Skipping device class" << deviceClass.name() << ": the definend critical stateTypeId" << criticalStateTypeId.toString() << "does not match any StateType of this DeviceClass.";
broken = true; broken = true;
} else if (deviceClass.getStateType(criticalStateTypeId).type() != QVariant::Bool) {
// Make sure the critical stateType is a bool state
qCWarning(dcDeviceManager) << "Skipping device class" << deviceClass.name() << ": the definend critical stateTypeId" << criticalStateTypeId.toString() << "is not a bool StateType.";
broken = true;
} else { } else {
deviceClass.setCriticalStateTypeId(criticalStateTypeId); deviceClass.setCriticalStateTypeId(criticalStateTypeId);
} }
@ -397,9 +407,11 @@ QList<DeviceClass> DevicePlugin::supportedDevices() const
} }
} }
if (!broken) if (!broken) {
deviceClasses.append(deviceClass); deviceClasses.append(deviceClass);
} else {
qCWarning(dcDeviceManager()) << "Skipping device class" << deviceClass.name();
}
} }
} }
return deviceClasses; return deviceClasses;

View File

@ -34,6 +34,7 @@
"idName": "connected", "idName": "connected",
"name": "connected", "name": "connected",
"index": 0, "index": 0,
"defaultValue": false,
"type": "bool" "type": "bool"
}, },
{ {

View File

@ -45,6 +45,7 @@
"idName": "connected", "idName": "connected",
"name": "connected", "name": "connected",
"index": 0, "index": 0,
"defaultValue": false,
"type": "bool" "type": "bool"
}, },
{ {
@ -53,6 +54,7 @@
"name": "mute", "name": "mute",
"index": 1, "index": 1,
"type": "bool", "type": "bool",
"defaultValue": true,
"writable": true "writable": true
}, },
{ {
@ -64,6 +66,7 @@
"index": 2, "index": 2,
"minValue": 0, "minValue": 0,
"maxValue": 100, "maxValue": 100,
"defaultValue": 50,
"writable": true "writable": true
} }
], ],