| [93ea008] | 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"
|
|---|
| [17ef75d] | 24 | #include "siweatherclientconfig.h"
|
|---|
| [93ea008] | 25 |
|
|---|
| 26 | #include <siweatherstation.h>
|
|---|
| 27 |
|
|---|
| 28 | #include <QCoreApplication>
|
|---|
| 29 |
|
|---|
| 30 | using namespace SiWeather::Library;
|
|---|
| 31 |
|
|---|
| 32 | StationsRegister::StationsRegister(QObject* parent)
|
|---|
| 33 | : QObject(parent)
|
|---|
| 34 | {
|
|---|
| 35 | _ioHandler = new InputOutputHandler(this);
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | void StationsRegister::registerStation()
|
|---|
| 39 | {
|
|---|
| [17ef75d] | 40 | registerStation(SiWeatherStation());
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | void 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;
|
|---|
| [93ea008] | 50 |
|
|---|
| [17ef75d] | 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 |
|
|---|
| 60 | void 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 | }
|
|---|
| [93ea008] | 74 |
|
|---|
| [17ef75d] | 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 |
|
|---|
| 81 | void 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;
|
|---|
| [93ea008] | 89 |
|
|---|
| [17ef75d] | 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());
|
|---|
| [93ea008] | 94 |
|
|---|
| [17ef75d] | 95 | station.setEMail(getValue(tr("Your E-Mail Address"), station.eMail()));
|
|---|
| [93ea008] | 96 | QString defaultType = "siweatherstation 1";
|
|---|
| [17ef75d] | 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()));
|
|---|
| [93ea008] | 101 |
|
|---|
| [17ef75d] | 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 |
|
|---|
| [93ea008] | 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;
|
|---|
| [17ef75d] | 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;
|
|---|
| [93ea008] | 126 | }
|
|---|
| 127 | else{
|
|---|
| [17ef75d] | 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 |
|
|---|
| [93ea008] | 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 | }
|
|---|
| [17ef75d] | 153 | _lastStation = station;
|
|---|
| [93ea008] | 154 | }
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| [17ef75d] | 157 | QString StationsRegister::getValue(const QString& label, const QString& defaultValue)
|
|---|
| [93ea008] | 158 | {
|
|---|
| [17ef75d] | 159 | if(!_ioHandler)
|
|---|
| 160 | return QString();
|
|---|
| [93ea008] | 161 |
|
|---|
| [17ef75d] | 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;
|
|---|
| [93ea008] | 170 | }
|
|---|
| 171 |
|
|---|
| [17ef75d] | 172 | QString StationsRegister::getCoordinate(const QString& type, double defaultValue)
|
|---|
| [93ea008] | 173 | {
|
|---|
| [17ef75d] | 174 | if(!_ioHandler)
|
|---|
| 175 | return QString();
|
|---|
| [93ea008] | 176 |
|
|---|
| [17ef75d] | 177 | QString coordinate = getValue(tr("%1 (at least 4 digits, point as separator)").arg(type), coordinateToString(defaultValue));
|
|---|
| 178 | if(coordinate.length() - coordinate.indexOf(".") < 5){
|
|---|
| [93ea008] | 179 | *_ioHandler << tr("Your %1 does not contain 4 digits. Try it again!").arg(type) << endl;
|
|---|
| [17ef75d] | 180 | return getCoordinate(type, defaultValue);
|
|---|
| [93ea008] | 181 | }
|
|---|
| 182 | return coordinate;
|
|---|
| 183 | }
|
|---|
| [17ef75d] | 184 |
|
|---|
| 185 | QString StationsRegister::coordinateToString(double coordinate) const
|
|---|
| 186 | {
|
|---|
| 187 | return QString::number(coordinate, 'f', 4);
|
|---|
| 188 | }
|
|---|