source: src/lib/siwebserviceclient.cpp@ d83a844

Last change on this file since d83a844 was d83a844, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago
  • Added type and typeUrl to the stations data, to allow the user to get an idea of the used weather station
    • Started to implement c++ client, which runs on the weather station
  • Property mode set to 100644
File size: 3.9 KB
Line 
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
47SiWebserviceClient::ReplyType SiWebserviceClient::stationsRegister(SiWeatherStation station, QString &error)
48{
49 if(!_networkManager)
50 return Error;
51
52 if(station.name().isEmpty() || station.city().isEmpty() || station.eMail().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 QJsonDocument jsonDocument(jsonStation);
64
65 QNetworkRequest request;
66 request.setUrl(QString("%1%2").arg(_baseUrl.toString()).arg("/stations/register"));
67 request.setRawHeader("User-Agent", QString("SiWeather Library %1").arg(Library::version()).toUtf8());
68 request.setHeader(QNetworkRequest::ContentTypeHeader,
69 "application/json");
70 QNetworkReply *reply = _networkManager->post(request, jsonDocument.toJson(QJsonDocument::Indented));
71 connect(reply, SIGNAL(finished()),
72 this, SLOT(replyStationsRegister()));
73 return StationsRegister;
74}
75
76void SiWebserviceClient::replyStationsRegister()
77{
78 QNetworkReply *reply = getNetworkReply(sender(), StationsRegister);
79 if(!reply)
80 return;
81
82 QByteArray data = reply->readAll();
83 QJsonDocument jsonDocument = QJsonDocument::fromBinaryData(data);
84 QJsonObject json = jsonDocument.object();
85 if(!json.contains("result")){
86 emit finishedStationsRegister(false, "", tr("No result in response found"));
87 return;
88 }
89 if(json["result"].toString() != "success"){
90 emit finishedStationsRegister(false, "", tr("Operation failed for reason: %1").arg(json["message"].toString()));
91 return;
92 }
93 if(!json.contains("uuid")){
94 emit finishedStationsRegister(false, "", tr("No uuid in response found"));
95 return;
96 }
97 emit finishedStationsRegister(true, QUuid(json["uuid"].toString()), "");
98 emit finished(StationsRegister, data);
99 reply->deleteLater();
100}
101
102QNetworkReply* SiWebserviceClient::getNetworkReply(QObject* sender, ReplyType type) const
103{
104 if(!sender){
105 emit error(Error, tr("No sender object found"));
106 return NULL;
107 }
108 QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender);
109 if(!reply){
110 emit error(Error, tr("No reply found"));
111 return NULL;
112 }
113
114 if(reply->error() != QNetworkReply::NoError){
115 emit error(type, reply->errorString());
116 reply->deleteLater();
117 return NULL;
118 }
119
120 return reply;
121}
122
Note: See TracBrowser for help on using the repository browser.