source: src/clientcommandline/stationsregister.cpp@ f8c242e

Last change on this file since f8c242e was 17ef75d, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago

command line client is now able to register stations. All entered data could be reviewed by the user and edited again, before sending it

  • Property mode set to 100644
File size: 6.3 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 "stationsregister.h"
21#include "moc_stationsregister.cpp"
22
23#include "inputoutputhandler.h"
24#include "siweatherclientconfig.h"
25
26#include <siweatherstation.h>
27
28#include <QCoreApplication>
29
30using namespace SiWeather::Library;
31
32StationsRegister::StationsRegister(QObject* parent)
33 : QObject(parent)
34{
35 _ioHandler = new InputOutputHandler(this);
36}
37
38void StationsRegister::registerStation()
39{
40 registerStation(SiWeatherStation());
41}
42
43void StationsRegister::webserviceError(SiWeather::Library::SiWebserviceClient::ReplyType replyType, const QString& message)
44{
45 if(replyType != SiWebserviceClient::StationsRegister)
46 return;
47
48 *_ioHandler << " "+ tr("FAILED") << endl;
49 *_ioHandler << " "+ message << endl;
50
51 QString result = getValue(tr("Do you want to change the station and try it again? (Y/n)"), "Y");
52 if(result != "Y"){
53 *_ioHandler << tr("Aborting!") << endl;
54 qApp->quit();
55 return;
56 }
57 registerStation(_lastStation);
58}
59
60void StationsRegister::webserviceFinished(bool result, const QUuid& stationUuid, const QString& message)
61{
62 if(!result){
63 *_ioHandler << " "+ tr("FAILED") << endl;
64 *_ioHandler << " "+ message << endl;
65 QString result = getValue(tr("Do you want to change the station and try it again? (Y/n)"), "Y");
66 if(result != "Y"){
67 *_ioHandler << tr("Aborting!") << endl;
68 qApp->quit();
69 return;
70 }
71 registerStation(_lastStation);
72 return;
73 }
74
75 *_ioHandler << " "+ tr("SUCCESS") << endl;
76 *_ioHandler << " "+ tr("Registered station with uuid %1").arg(stationUuid.toString()) << endl;
77 *_ioHandler << " "+ message << endl;
78 qApp->quit();
79}
80
81void StationsRegister::registerStation(SiWeatherStation station)
82{
83 if(!_ioHandler)
84 return;
85
86 QUrl baseUrl(QString(WEBSERVICE_BASE_URL));
87
88 *_ioHandler << tr("Please enter some data to describe your station") << endl;
89
90 station.setName(getValue(tr("Name"), station.name()));
91 station.setCity(getValue(tr("City"), station.city()));
92 station.setLatitude(getCoordinate(tr("Latitude"), station.latitude()).toDouble());
93 station.setLongitude(getCoordinate(tr("Longitude"), station.longitude()).toDouble());
94
95 station.setEMail(getValue(tr("Your E-Mail Address"), station.eMail()));
96 QString defaultType = "siweatherstation 1";
97 if(!station.type().isEmpty())
98 defaultType = station.type();
99 station.setType(getValue(tr("Type, describing your station"), defaultType));
100 station.setTypeUrl(getValue(tr("Url with information about your type (optional)"), station.typeUrl().toString()));
101
102 *_ioHandler << tr("Your station is defined as follows:") << endl;
103 _ioHandler->setFieldWidth(20);
104 *_ioHandler << endl;
105 *_ioHandler << tr("Name:") << station.name() << endl;
106 *_ioHandler << tr("City:") << station.city() << endl;
107 *_ioHandler << tr("Latitude:") << coordinateToString(station.latitude()) << endl;
108 *_ioHandler << tr("Longitude:") << coordinateToString(station.longitude()) << endl;
109 *_ioHandler << tr("Type:") << station.type() << endl;
110 *_ioHandler << tr("Type Url:") << station.typeUrl().toString() << endl;
111 *_ioHandler << tr("E-Mail:") << station.eMail() << endl;
112 _ioHandler->setFieldWidth(0);
113 *_ioHandler << endl;
114
115 if(!station.isValidForRegister()){
116 *_ioHandler << tr("Your station is not valid for register at the webservice.") << endl;
117 *_ioHandler << tr("This means, the name, city, e-mail address or type is empty.") << endl;
118 QString result = getValue(tr("Do you want to change the station? (Y/n)"), "Y");
119 if(result != "Y"){
120 *_ioHandler << tr("Aborting!") << endl;
121 qApp->quit();
122 return;
123 }
124 registerStation(station);
125 return;
126 }
127 else{
128 QString result = getValue(tr("Do you want to register this station at %1? (Y/e/n)").arg(baseUrl.host()), "n");
129 if(result != "Y"){
130 if(result == "e"){
131 registerStation(station);
132 return;
133 }
134
135 *_ioHandler << tr("Aborting!") << endl;
136 qApp->quit();
137 return;
138 }
139 *_ioHandler << tr("Registering station...");
140
141 SiWebserviceClient *client = new SiWebserviceClient(baseUrl, this);
142 connect(client, SIGNAL(error(SiWeather::Library::SiWebserviceClient::ReplyType, const QString&)),
143 this, SLOT(webserviceError(SiWeather::Library::SiWebserviceClient::ReplyType,QString)));
144 connect(client, SIGNAL(finishedStationsRegister(bool, const QUuid&, const QString&)),
145 this, SLOT(webserviceFinished(bool,QUuid,QString)));
146 QString errorMessage;
147 if(client->stationsRegister(station, errorMessage) != SiWebserviceClient::StationsRegister){
148 *_ioHandler << " "+ tr("FAILED") << endl;
149 *_ioHandler << " "+ errorMessage << endl;
150 qApp->quit();
151 return;
152 }
153 _lastStation = station;
154 }
155}
156
157QString StationsRegister::getValue(const QString& label, const QString& defaultValue)
158{
159 if(!_ioHandler)
160 return QString();
161
162 QString defaultValueLabel;
163 if(!defaultValue.isEmpty())
164 defaultValueLabel = QString(" [%1]").arg(defaultValue);
165 *_ioHandler << label + defaultValueLabel +": ";
166 QString value = _ioHandler->readLine();
167 if(value.isEmpty())
168 value = defaultValue;
169 return value;
170}
171
172QString StationsRegister::getCoordinate(const QString& type, double defaultValue)
173{
174 if(!_ioHandler)
175 return QString();
176
177 QString coordinate = getValue(tr("%1 (at least 4 digits, point as separator)").arg(type), coordinateToString(defaultValue));
178 if(coordinate.length() - coordinate.indexOf(".") < 5){
179 *_ioHandler << tr("Your %1 does not contain 4 digits. Try it again!").arg(type) << endl;
180 return getCoordinate(type, defaultValue);
181 }
182 return coordinate;
183}
184
185QString StationsRegister::coordinateToString(double coordinate) const
186{
187 return QString::number(coordinate, 'f', 4);
188}
Note: See TracBrowser for help on using the repository browser.