| 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 |
|
|---|
| 33 | using namespace SiWeather::Library;
|
|---|
| 34 |
|
|---|
| 35 | SiWebserviceClient::SiWebserviceClient(QUrl baseUrl, QObject* parent)
|
|---|
| 36 | : QObject(parent)
|
|---|
| 37 | {
|
|---|
| 38 | _baseUrl = baseUrl;
|
|---|
| 39 | _networkManager = new QNetworkAccessManager(this);
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | QString SiWebserviceClient::apiVersion()
|
|---|
| 43 | {
|
|---|
| 44 | return QString(WEBSERVICE_API_VERSION);
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | SiWebserviceClient::ReplyType SiWebserviceClient::stationsRegister(const SiWeatherStation &station, QString &error)
|
|---|
| 48 | {
|
|---|
| 49 | if(!_networkManager)
|
|---|
| 50 | return Error;
|
|---|
| 51 |
|
|---|
| 52 | if(station.name().isEmpty() || station.city().isEmpty() || station.eMail().isEmpty() || station.type().isEmpty()){
|
|---|
| 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();
|
|---|
| 63 | jsonStation["type"] = station.type();
|
|---|
| 64 | jsonStation["typeUrl"] = station.typeUrl().toString();
|
|---|
| 65 | QJsonDocument jsonDocument(jsonStation);
|
|---|
| 66 |
|
|---|
| 67 | QNetworkRequest request;
|
|---|
| 68 | request.setUrl(QString("%1%2").arg(_baseUrl.toString()).arg("index.php/stations/register"));
|
|---|
| 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 |
|
|---|
| 78 | void SiWebserviceClient::replyStationsRegister()
|
|---|
| 79 | {
|
|---|
| 80 | QNetworkReply *reply = getNetworkReply(sender(), StationsRegister);
|
|---|
| 81 | if(!reply)
|
|---|
| 82 | return;
|
|---|
| 83 |
|
|---|
| 84 | QByteArray data = reply->readAll();
|
|---|
| 85 | reply->deleteLater();
|
|---|
| 86 |
|
|---|
| 87 | QJsonDocument jsonDocument = QJsonDocument::fromJson(data);
|
|---|
| 88 | QJsonObject json = jsonDocument.object();
|
|---|
| 89 | if(!json.contains("result")){
|
|---|
| 90 | emit finishedStationsRegister(false, "", tr("No result in response found"));
|
|---|
| 91 | emit finished(StationsRegister, false, data);
|
|---|
| 92 | return;
|
|---|
| 93 | }
|
|---|
| 94 | if(json["result"].toString() != "success"){
|
|---|
| 95 | emit finishedStationsRegister(false, "", tr("Operation failed for reason: %1").arg(json["message"].toString()));
|
|---|
| 96 | emit finished(StationsRegister, false, data);
|
|---|
| 97 | return;
|
|---|
| 98 | }
|
|---|
| 99 | if(!json.contains("uuid")){
|
|---|
| 100 | emit finishedStationsRegister(false, "", tr("No uuid in response found"));
|
|---|
| 101 | emit finished(StationsRegister, false, data);
|
|---|
| 102 | return;
|
|---|
| 103 | }
|
|---|
| 104 | emit finishedStationsRegister(true, QUuid(json["uuid"].toString()), json["message"].toString());
|
|---|
| 105 | emit finished(StationsRegister, false, data);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | QNetworkReply* 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 | }
|
|---|