some more checks

pull/1/head
Michael Zanetti 2014-04-13 17:58:33 +02:00
parent b85aa3072e
commit a5eb0ef759
5 changed files with 40 additions and 9 deletions

View File

@ -171,13 +171,30 @@ DeviceManager::DeviceError DeviceManager::addConfiguredDeviceInternal(const Devi
qWarning() << "cannot find a device class with id" << deviceClassId;
return DeviceErrorDeviceClassNotFound;
}
// Make sure we have all required params
foreach (const QVariant &param, deviceClass.params()) {
if (!params.contains(param.toMap().value("name").toString())) {
qWarning() << "Missing parameter when creating device:" << param.toMap().value("name").toString();
return DeviceErrorMissingParameter;
}
// TODO: Check if parameter type matches
}
// Make sure we don't have unused params
foreach (const QString &paramId, params.keys()) {
qDebug() << "searching" << paramId << "in" << deviceClass.params();
bool found = false;
foreach (const QVariant &param, deviceClass.params()) {
if (param.toMap().value("name").toString() == paramId) {
found = true;
continue;
}
}
if (!found) {
// TODO: Check if parameter type matches
return DeviceErrorDeviceParameterError;
}
}
foreach(Device *device, m_configuredDevices) {
if (device->id() == id) {
return DeviceErrorDuplicateUuid;

View File

@ -172,12 +172,6 @@ QList<DeviceClass> DevicePluginOpenweathermap::supportedDevices() const
DeviceClass deviceClassOpenweathermap(pluginId(), openweathermapVendorId, DeviceClassId("985195aa-17ad-4530-88a4-cdd753d747d7"));
deviceClassOpenweathermap.setName("Weather from openweathermap");
QVariantList weatherParams;
QVariantMap autoDetectParam;
autoDetectParam.insert("name", "autodetect");
autoDetectParam.insert("type", "bool");
weatherParams.append(autoDetectParam);
// Actions
QList<ActionType> weatherActions;
ActionType updateWeather(ActionTypeId("cfbc6504-d86f-4856-8dfa-97b6fbb385e4"));

View File

@ -227,6 +227,10 @@ QVariantMap DeviceHandler::AddConfiguredDevice(const QVariantMap &params)
returns.insert("errorMessage", "Error creating device. This device can't be created this way.");
returns.insert("success", false);
break;
case DeviceManager::DeviceErrorDeviceParameterError:
returns.insert("errorMessage", "Error creating device. Invalid device parameter.");
returns.insert("success", false);
break;
default:
returns.insert("errorMessage", "Unknown error.");
returns.insert("success", false);

View File

@ -69,9 +69,11 @@ JsonRPCServer::JsonRPCServer(QObject *parent):
params.clear(); returns.clear();
setDescription("SetNotificationStatus", "Enable/Disable notifications for this connections.");
params.insert("enabled", "bool");
setParams("SetNotificationStatus", params);
returns.insert("success", "bool");
returns.insert("enabled", "bool");
returns.insert("errorMessage", "string");
setReturns("SetNotificationStatus", returns);
// Now set up the logic

View File

@ -68,7 +68,7 @@ void JsonTypes::init()
s_stateType.insert("id", "uuid");
s_stateType.insert("name", "string");
s_stateType.insert("type", basicTypesRef());
// s_stateType.insert("default", "value");
s_stateType.insert("defaultValue", "variant");
// State
s_state.insert("stateTypeId", "uuid");
@ -278,10 +278,11 @@ QVariantMap JsonTypes::packRule(const Rule &rule)
QPair<bool, QString> JsonTypes::validateMap(const QVariantMap &templateMap, const QVariantMap &map)
{
s_lastError.clear();
// Make sure all values defined in the template are around
foreach (const QString &key, templateMap.keys()) {
QString strippedKey = key;
strippedKey.remove(QRegExp("^o:"));
if (!key.startsWith("o:") && !map.contains(strippedKey)) {
qDebug() << "*** missing key" << key;
qDebug() << "Expected:" << templateMap;
@ -297,6 +298,18 @@ QPair<bool, QString> JsonTypes::validateMap(const QVariantMap &templateMap, cons
}
}
}
// Make sure there aren't any other parameters than the allowed ones
foreach (const QString &key, map.keys()) {
QString optKey = "o:" + key;
if (!templateMap.contains(key) && !templateMap.contains(optKey)) {
qDebug() << "Forbidden param" << key << "in params";
QJsonDocument jsonDoc = QJsonDocument::fromVariant(map);
return report(false, QString("Forbidden key \"%1\" in %2").arg(key).arg(QString(jsonDoc.toJson())));
}
}
return report(true, "");
}
@ -331,6 +344,7 @@ QPair<bool, QString> JsonTypes::validateList(const QVariantList &templateList, c
for (int i = 0; i < list.count(); ++i) {
QVariant listEntry = list.at(i);
qDebug() << "validating" << list << templateList;
QPair<bool, QString> result = validateVariant(entryTemplate, listEntry);
if (!result.first) {
qDebug() << "List entry not matching template";