Example #1
0
void JsonProcessor::handleDeleteEffectCommand(const QJsonObject& message, const QString& command, const int tan)
{
	if(message.size() > 0)
	{
		QString effectName = message["name"].toString();
		std::list<EffectDefinition> effectsDefinition = _hyperion->getEffects();
		std::list<EffectDefinition>::iterator it = std::find_if(effectsDefinition.begin(), effectsDefinition.end(), find_effect(effectName));

		if (it != effectsDefinition.end())
		{
			QFileInfo effectConfigurationFile(it->file);
			if (effectConfigurationFile.absoluteFilePath().mid(0, 1)  != ":" )
			{
				if (effectConfigurationFile.exists())
				{
					bool result = QFile::remove(effectConfigurationFile.absoluteFilePath());
					if (result)
					{
						Info(_log, "Reload effect list");
						_hyperion->reloadEffects();
						sendSuccessReply(command, tan);
					} else
						sendErrorReply("Can't delete effect configuration file: " + effectConfigurationFile.absoluteFilePath() + ". Please check permissions", command, tan);
				} else
					sendErrorReply("Can't find effect configuration file: " + effectConfigurationFile.absoluteFilePath(), command, tan);
			} else
				sendErrorReply("Can't delete internal effect: " + message["name"].toString(), command, tan);
		} else
			sendErrorReply("Effect " + message["name"].toString() + " not found", command, tan);
	} else
		sendErrorReply("Error while parsing json: Message size " + QString(message.size()), command, tan);
}
void GetFaceResourceTest::testFaces(const QString& fileName) {
    QString base = testResources + QDir::separator() + fileName;
    QFile file(base + ".jpg");
    QJsonDocument current;
    int status = faceResource->getJSONFaces(&file, current);
    file.close();
    QCOMPARE(HttpHeaders::STATUS_SUCCESS, status);

    QFile jsonFile(base + ".json");

    QVERIFY(jsonFile.exists());

    jsonFile.open(QIODevice::ReadOnly | QIODevice::Text);
    QJsonDocument expected = QJsonDocument().fromJson(jsonFile.readAll());
    jsonFile.close();

    QJsonArray currentArray = current.array();
    QJsonArray expectedArray = expected.array();

    // Make sure we're not comparing zero to zero
    QVERIFY(expectedArray.size() > 0);
    QCOMPARE(currentArray.size(), expectedArray.size());

    for (int i=0; i<currentArray.size(); ++i) {
        QJsonObject cFaceParts = currentArray[i].toObject();
        QJsonObject eFaceParts = expectedArray[i].toObject();

        QJsonObject cFace = eFaceParts["face"].toObject();
        QJsonObject eFace = eFaceParts["face"].toObject();
        compareDoubles(cFace["x1"].toDouble(), eFace["x1"].toDouble(), 0.0001);
        compareDoubles(cFace["x2"].toDouble(), eFace["x2"].toDouble(), 0.0001);
        compareDoubles(cFace["y1"].toDouble(), eFace["y1"].toDouble(), 0.0001);
        compareDoubles(cFace["y2"].toDouble(), eFace["y2"].toDouble(), 0.0001);

        QCOMPARE(cFaceParts["pose"], eFaceParts["pose"]);
        QCOMPARE(cFaceParts["model"], eFaceParts["model"]);

        QJsonObject cParts = cFaceParts["parts"].toObject();
        QJsonObject eParts = eFaceParts["parts"].toObject();

        QCOMPARE(cParts.size(), eParts.size());

        for (QJsonObject::const_iterator cIter = cParts.constBegin(); cIter != cParts.constEnd(); ++cIter) {
            QJsonArray cSubpart = cIter.value().toArray();
            QJsonArray eSubpart = cParts[cIter.key()].toArray();

            QCOMPARE(cSubpart.size(), eSubpart.size());

            for (int i=0; i<cSubpart.size(); ++i) {
                QJsonObject cPart = cSubpart[i].toObject();
                QJsonObject ePart = eSubpart[i].toObject();
                compareDoubles(cPart["x"].toDouble(), ePart["x"].toDouble(), 0.0001);
                compareDoubles(cPart["y"].toDouble(), ePart["y"].toDouble(), 0.0001);
                QCOMPARE(cPart["num"], ePart["num"]);
            }
        }
    }
}
Example #3
0
BaseJob::Status SyncJob::parseJson(const QJsonDocument& data)
{
    QJsonObject json = data.object();
    d->nextBatch = json.value("next_batch").toString();
    // TODO: presence
    // TODO: account_data
    QJsonObject rooms = json.value("rooms").toObject();

    const struct { QString jsonKey; JoinState enumVal; } roomStates[]
    {
        { "join", JoinState::Join },
        { "invite", JoinState::Invite },
        { "leave", JoinState::Leave }
    };
    for (auto roomState: roomStates)
    {
        const QJsonObject rs = rooms.value(roomState.jsonKey).toObject();
        d->roomData.reserve(rs.size());
        for( auto r = rs.begin(); r != rs.end(); ++r )
        {
            d->roomData.push_back({r.key(), r.value().toObject(), roomState.enumVal});
        }
    }

    return Success;
}
bool RequestRefresh::handle(Client *cl, QJsonObject arg)
{
    if(cl == NULL && arg.size()==1) {
    }
    qDebug() << "Refresh";
    return true;
}
bool RequestLang::handle(BCWebSocketServer *cl, QJsonObject arg)
{
    if(cl == NULL || arg.size()!=2 || !arg.contains("lang")){
        throw InvalidRequestException();
    }

    TranslationsLang* lang = _manager->getLang(arg.value("lang").toString());

    if(lang == NULL){
        return false;
    }

    cl->setLang(lang);
    return true;
}
Example #6
0
void protoObject::fromJSON(QJsonObject &obj) {
    //TODO: internal id check
    //qDebug() << obj;
    QMap<QString, QMap<int, protoObject* >* > bufferMap;
    QMap<int, protoObject* >* tmp_bufferMap;

    int         length = obj.size();
    int         i;
    protoObject*      tmp_obj;
    QStringList keys = obj.keys();
    QJsonValue  value;
    QJsonObject json_obj;
    QJsonObject json_info;

    for (i = 0; i < length; i++) {
        value = obj.value(keys.at(i));
        if (value.isArray()) {            
            QJsonArray tmp_arr = value.toArray();
            QList< QSharedPointer<protoObject> > list;
            int length = tmp_arr.count(), j;
            for (j = 0; j < length; j++) {
                tmp_obj = new protoObject(this);
                value = tmp_arr.at(j);
                if (value.isObject()) {                    
                    json_obj = value.toObject();
                    tmp_obj->fromJSON(json_obj);
                };
                list.append(QSharedPointer<protoObject >(tmp_obj));
            };
            qDebug() << list;
            this->setProperty(keys.at(i).toLatin1(), QVariant::fromValue(list));
        };
        if (value.isObject()) {
            tmp_obj = new protoObject(this);
            json_obj = value.toObject();
            tmp_obj->fromJSON(json_obj);
            QVariant variant = QVariant::fromValue(QSharedPointer<protoObject>(tmp_obj));
            this->setProperty(keys.at(i).toLatin1(), variant);
        };
        if (value.isString()) this->setProperty(keys.at(i).toLatin1(), value.toString());
        if (value.isDouble()) this->setProperty(keys.at(i).toLatin1(), value.toDouble());
        if (value.isBool()) this->setProperty(keys.at(i).toLatin1(), value.toBool());
    };
}
    void PresetKeywordsModelConfig::parsePresetArray(const QJsonArray &array) {
        const int size = array.size();

        for (int i = 0; i < size; ++i) {
            QJsonValue item = array.at(i);

            if (!item.isObject()) { continue; }

            QJsonObject presetKeywordsItem = item.toObject();

            // legacy format
            if (presetKeywordsItem.size() == 1) {
                QString presetName = presetKeywordsItem.keys()[0];
                QJsonValue presetKeywordsValue = presetKeywordsItem.value(presetName);
                QStringList keywords;

                if (tryParsePresetKeywords(presetKeywordsValue, keywords)) {
                    m_PresetData.push_back({keywords, presetName, DEFAULT_GROUP_ID});
                }
            } else {
                QString presetName;
                QJsonValue presetNameValue = presetKeywordsItem.value(PRESET_NAME_KEY);
                if (presetNameValue.isString()) {
                    presetName = presetNameValue.toString().trimmed();
                } else {
                    continue;
                }

                if (presetName.isEmpty()) { continue; }

                QJsonValue keywordsValue = presetKeywordsItem.value(PRESET_KEYWORDS_KEY);
                QStringList keywords;
                if (!tryParsePresetKeywords(keywordsValue, keywords)) { continue; }

                QJsonValue groupIdValue = presetKeywordsItem.value(PRESET_GROUP_ID_KEY);
                int groupID = DEFAULT_GROUP_ID;
                if (groupIdValue.isDouble()) {
                    groupID = groupIdValue.toInt(DEFAULT_GROUP_ID);
                }

                m_PresetData.push_back({keywords, presetName, groupID});
            }
        }
    }
Example #8
0
void JsonProcessor::handleCreateEffectCommand(const QJsonObject& message, const QString &command, const int tan)
{
	if(message.size() > 0)
	{
		if (!message["args"].toObject().isEmpty())
		{
			QString scriptName;
			(message["script"].toString().mid(0, 1)  == ":" )
				? scriptName = ":/effects//" + message["script"].toString().mid(1)
				: scriptName = message["script"].toString();

			std::list<EffectSchema> effectsSchemas = _hyperion->getEffectSchemas();
			std::list<EffectSchema>::iterator it = std::find_if(effectsSchemas.begin(), effectsSchemas.end(), find_schema(scriptName));

			if (it != effectsSchemas.end())
			{
				QString errors;

				if (!checkJson(message["args"].toObject(), it->schemaFile, errors))
				{
					sendErrorReply("Error while validating json: " + errors, command, tan);
					return;
				}

				QJsonObject effectJson;
				QJsonArray effectArray;
				effectArray = _hyperion->getQJsonConfig()["effects"].toObject()["paths"].toArray();

				if (effectArray.size() > 0)
				{
					if (message["name"].toString().trimmed().isEmpty() || message["name"].toString().trimmed().startsWith("."))
					{
						sendErrorReply("Can't save new effect. Effect name is empty or begins with a dot.", command, tan);
						return;
					}

					effectJson["name"] = message["name"].toString();
					effectJson["script"] = message["script"].toString();
					effectJson["args"] = message["args"].toObject();

					std::list<EffectDefinition> availableEffects = _hyperion->getEffects();
					std::list<EffectDefinition>::iterator iter = std::find_if(availableEffects.begin(), availableEffects.end(), find_effect(message["name"].toString()));

					QFileInfo newFileName;
					if (iter != availableEffects.end())
					{
						newFileName.setFile(iter->file);
						if (newFileName.absoluteFilePath().mid(0, 1)  == ":")
						{
							sendErrorReply("The effect name '" + message["name"].toString() + "' is assigned to an internal effect. Please rename your effekt.", command, tan);
							return;
						}
					} else
					{
						newFileName.setFile(effectArray[0].toString() + QDir::separator() + message["name"].toString().replace(QString(" "), QString("")) + QString(".json"));

						while(newFileName.exists())
						{
							newFileName.setFile(effectArray[0].toString() + QDir::separator() + newFileName.baseName() + QString::number(qrand() % ((10) - 0) + 0) + QString(".json"));
						}
					}

					QJsonFactory::writeJson(newFileName.absoluteFilePath(), effectJson);
					Info(_log, "Reload effect list");
					_hyperion->reloadEffects();
					sendSuccessReply(command, tan);
				} else
				{
					sendErrorReply("Can't save new effect. Effect path empty", command, tan);
					return;
				}
			} else
				sendErrorReply("Missing schema file for Python script " + message["script"].toString(), command, tan);
		} else
			sendErrorReply("Missing or empty Object 'args'", command, tan);
	} else
		sendErrorReply("Error while parsing json: Message size " + QString(message.size()), command, tan);
}
Example #9
0
QJsonObject RemDev::newNotification(const QString &method, const QJsonObject &params) const {
	QJsonObject o(rpc_seed);
	o.insert("method", method);
	if (params.size()) o.insert("params", params);
	return o;
}