void NetworkManager::processJson(QString json) { //log("Json received : " + json); // Parse main parts of the message (type and body) QJsonDocument doc; doc = doc.fromJson(json.toUtf8()); QString type = doc.object().take("type").toString(); QJsonValue body = doc.object().take("body"); // Routing messages by type if ( type == "hbAck" ){ emit hearthbeatReceived(body.toString()); } else if ( type == "setVariable" ){ QString variable = body.toObject().take("variable").toString(); QString option = body.toObject().take("option").toString(); QString json = QJsonDocument( body.toObject().take("value").toObject() ).toJson(); emit systemVariableChanged(variable, option, json); } else if ( type == "call" ){ QString module = body.toObject().take("module").toString(); QString function = body.toObject().take("function").toString(); if (function == "") function = body.toObject().take("fct").toString(); QString params = QJsonDocument( body.toObject().take("param").toArray() ).toJson(); emit callRequest(module, function, params); } else if ( type == "ssid" ){ setssid(body.toString()); setLoggedState(true); } else if ( type == "login-error" ){ log("Login error : " + body.toString()); setLoggedState(false); } else if ( type == "error"){ log("Server error : " + body.toString()); } else{ emit jsonReceived(type, body); emit jsonStringReceived(json); } }
bool JsonApiJob::finished() { qCInfo(lcJsonApiJob) << "JsonApiJob of" << reply()->request().url() << "FINISHED WITH STATUS" << reply()->error() << (reply()->error() == QNetworkReply::NoError ? QLatin1String("") : errorString()); int statusCode = 0; if (reply()->error() != QNetworkReply::NoError) { qCWarning(lcJsonApiJob) << "Network error: " << path() << errorString() << reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute); emit jsonReceived(QJsonDocument(), statusCode); return true; } QString jsonStr = QString::fromUtf8(reply()->readAll()); if (jsonStr.contains("<?xml version=\"1.0\"?>")) { QRegExp rex("<statuscode>(\\d+)</statuscode>"); if (jsonStr.contains(rex)) { // this is a error message coming back from ocs. statusCode = rex.cap(1).toInt(); } } else { QRegExp rex("\"statuscode\":(\\d+),"); // example: "{"ocs":{"meta":{"status":"ok","statuscode":100,"message":null},"data":{"version":{"major":8,"minor":"... (504) if (jsonStr.contains(rex)) { statusCode = rex.cap(1).toInt(); } } QJsonParseError error; auto json = QJsonDocument::fromJson(jsonStr.toUtf8(), &error); // empty or invalid response if (error.error != QJsonParseError::NoError || json.isNull()) { qCWarning(lcJsonApiJob) << "invalid JSON!" << jsonStr << error.errorString(); emit jsonReceived(json, statusCode); return true; } emit jsonReceived(json, statusCode); return true; }
NetworkManager::NetworkManager(QString host, int port, QObject *parent) : JsonCommunication(host, port, parent) { m_logged = false; m_heartbeatManager = new HeartbeatManager(3,this); connect(m_heartbeatManager, SIGNAL(networkRequest(QString)), this, SLOT(serverRequest(QString))); connect(m_heartbeatManager, SIGNAL(queueFull()), this, SLOT(forceDisconnect())); connect(this,SIGNAL(connectedChanged(bool)),m_heartbeatManager,SLOT(setRunning(bool))); connect(this,SIGNAL(hearthbeatReceived(QString)),m_heartbeatManager,SLOT(validate(QString))); connect(this,SIGNAL(jsonReceived(QString)),this,SLOT(processJson(QString))); connect(this,SIGNAL(serverConnected()),this,SLOT(tryLogin())); connect(this,SIGNAL(serverDisconnected()),this,SLOT(setNotLogged())); }
Kommunikator::Kommunikator(QObject *parent) : QObject(parent), seq_(0), ks_(Unestablished) { connect(&jsr_, SIGNAL(jsonReceived(QVariant)), this, SLOT(handleIncoming(QVariant))); }