source: src/lib/siwebserviceclient.cpp@ 93ea008

Last change on this file since 93ea008 was a2330fc, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago

Build in php webserver is now launched with QProcess::start instead of QProcess::execute in a different thread

  • Property mode set to 100644
File size: 4.3 KB
RevLine 
[d83a844]1/*
2 Copyright (C) 2015 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 MA 02110-1301 USA.
18*/
19
20#include "siwebserviceclient.h"
21#include "moc_siwebserviceclient.cpp"
22
23#include "silibconfig.h"
24#include "silibrary.h"
25#include "siweatherstation.h"
26
27#include <QNetworkAccessManager>
28#include <QNetworkRequest>
29#include <QNetworkReply>
30#include <QJsonObject>
31#include <QJsonDocument>
32
33using namespace SiWeather::Library;
34
35SiWebserviceClient::SiWebserviceClient(QUrl baseUrl, QObject* parent)
36 : QObject(parent)
37{
38 _baseUrl = baseUrl;
39 _networkManager = new QNetworkAccessManager(this);
40}
41
42QString SiWebserviceClient::apiVersion()
43{
44 return QString(WEBSERVICE_API_VERSION);
45}
46
[915dc18]47SiWebserviceClient::ReplyType SiWebserviceClient::stationsRegister(const SiWeatherStation &station, QString &error)
[d83a844]48{
49 if(!_networkManager)
50 return Error;
51
[915dc18]52 if(station.name().isEmpty() || station.city().isEmpty() || station.eMail().isEmpty() || station.type().isEmpty()){
[d83a844]53 error = tr("Station has empty name, city or eMail address. All of these are needed!");
54 return Error;
55 }
56
57 QJsonObject jsonStation;
58 jsonStation["name"] = station.name();
59 jsonStation["city"] = station.city();
60 jsonStation["longitude"] = Library::geoCoordinateToString(station.longitude());
61 jsonStation["latitude"] = Library::geoCoordinateToString(station.latitude());
62 jsonStation["email"] = station.eMail();
[915dc18]63 jsonStation["type"] = station.type();
64 jsonStation["typeUrl"] = station.typeUrl().toString();
[d83a844]65 QJsonDocument jsonDocument(jsonStation);
66
67 QNetworkRequest request;
[a2330fc]68 request.setUrl(QString("%1%2").arg(appendSlashToUrl(_baseUrl.toString())).arg("index.php/stations/register"));
[d83a844]69 request.setRawHeader("User-Agent", QString("SiWeather Library %1").arg(Library::version()).toUtf8());
70 request.setHeader(QNetworkRequest::ContentTypeHeader,
71 "application/json");
72 QNetworkReply *reply = _networkManager->post(request, jsonDocument.toJson(QJsonDocument::Indented));
73 connect(reply, SIGNAL(finished()),
74 this, SLOT(replyStationsRegister()));
75 return StationsRegister;
76}
77
78void SiWebserviceClient::replyStationsRegister()
79{
80 QNetworkReply *reply = getNetworkReply(sender(), StationsRegister);
81 if(!reply)
82 return;
83
84 QByteArray data = reply->readAll();
[915dc18]85 reply->deleteLater();
86
87 QJsonDocument jsonDocument = QJsonDocument::fromJson(data);
[d83a844]88 QJsonObject json = jsonDocument.object();
89 if(!json.contains("result")){
90 emit finishedStationsRegister(false, "", tr("No result in response found"));
[915dc18]91 emit finished(StationsRegister, false, data);
[d83a844]92 return;
93 }
94 if(json["result"].toString() != "success"){
95 emit finishedStationsRegister(false, "", tr("Operation failed for reason: %1").arg(json["message"].toString()));
[915dc18]96 emit finished(StationsRegister, false, data);
[d83a844]97 return;
98 }
99 if(!json.contains("uuid")){
100 emit finishedStationsRegister(false, "", tr("No uuid in response found"));
[915dc18]101 emit finished(StationsRegister, false, data);
[d83a844]102 return;
103 }
[915dc18]104 emit finishedStationsRegister(true, QUuid(json["uuid"].toString()), json["message"].toString());
105 emit finished(StationsRegister, false, data);
[d83a844]106}
107
108QNetworkReply* SiWebserviceClient::getNetworkReply(QObject* sender, ReplyType type) const
109{
110 if(!sender){
111 emit error(Error, tr("No sender object found"));
112 return NULL;
113 }
114 QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender);
115 if(!reply){
116 emit error(Error, tr("No reply found"));
117 return NULL;
118 }
119
120 if(reply->error() != QNetworkReply::NoError){
121 emit error(type, reply->errorString());
122 reply->deleteLater();
123 return NULL;
124 }
125
126 return reply;
127}
[a2330fc]128
129QString SiWebserviceClient::appendSlashToUrl(QString url)
130{
131 if(url.right(1) != "/")
132 url += "/";
133 return url;
134}
Note: See TracBrowser for help on using the repository browser.