| 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 |
|
|---|
| 25 | #include <siweatherstation.h>
|
|---|
| 26 |
|
|---|
| 27 | #include <QCoreApplication>
|
|---|
| 28 |
|
|---|
| 29 | using namespace SiWeather::Library;
|
|---|
| 30 |
|
|---|
| 31 | StationsRegister::StationsRegister(QObject* parent)
|
|---|
| 32 | : QObject(parent)
|
|---|
| 33 | {
|
|---|
| 34 | _ioHandler = new InputOutputHandler(this);
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | void StationsRegister::registerStation()
|
|---|
| 38 | {
|
|---|
| 39 | QUrl baseUrl("http://localhost:9999/0.1/");
|
|---|
| 40 |
|
|---|
| 41 | *_ioHandler << tr("Please enter some data to describe your station") << endl;
|
|---|
| 42 | SiWeatherStation station;
|
|---|
| 43 |
|
|---|
| 44 | *_ioHandler << tr("Name") +": ";
|
|---|
| 45 | station.setName(_ioHandler->readLine());
|
|---|
| 46 |
|
|---|
| 47 | *_ioHandler << tr("City") +": ";
|
|---|
| 48 | station.setCity(_ioHandler->readLine());
|
|---|
| 49 |
|
|---|
| 50 | station.setLatitude(getCoordinate(tr("Latitude")).toDouble());
|
|---|
| 51 | station.setLongitude(getCoordinate(tr("Longitude")).toDouble());
|
|---|
| 52 |
|
|---|
| 53 | *_ioHandler << tr("Your E-Mail Address") +": ";
|
|---|
| 54 | station.setEMail(_ioHandler->readLine());
|
|---|
| 55 |
|
|---|
| 56 | QString defaultType = "siweatherstation 1";
|
|---|
| 57 | *_ioHandler << tr("Type, describing your station") +" ["+ defaultType +"]: ";
|
|---|
| 58 | QString type = _ioHandler->readLine();
|
|---|
| 59 | if(type.isEmpty())
|
|---|
| 60 | type = defaultType;
|
|---|
| 61 | station.setType(type);
|
|---|
| 62 |
|
|---|
| 63 | *_ioHandler << tr("Url with information about your type (optional)") + ": ";
|
|---|
| 64 | station.setTypeUrl(_ioHandler->readLine());
|
|---|
| 65 |
|
|---|
| 66 | if(!station.isValidForRegister()){
|
|---|
| 67 | *_ioHandler << tr("Your station is not valid for register at the webservice.") << endl;
|
|---|
| 68 | *_ioHandler << tr("This means, the name, city, e-mail address or type is empty.") << endl;
|
|---|
| 69 | }
|
|---|
| 70 | else{
|
|---|
| 71 | *_ioHandler << tr("Your station is defined as follows:") << endl;
|
|---|
| 72 | *_ioHandler << " "+ tr("Name: ") + station.name() << endl;
|
|---|
| 73 | *_ioHandler << " "+ tr("City: ") + station.city() << endl;
|
|---|
| 74 | *_ioHandler << " "+ tr("Latitude: ") + QString::number(station.latitude()) << endl;
|
|---|
| 75 | *_ioHandler << " "+ tr("Longitude: ") + QString::number(station.longitude()) << endl;
|
|---|
| 76 | *_ioHandler << " "+ tr("Type: ") + station.type() << endl;
|
|---|
| 77 | *_ioHandler << " "+ tr("Type Url: ") + station.typeUrl().toString() << endl;
|
|---|
| 78 | *_ioHandler << " "+ tr("E-Mail: ") + station.eMail() << endl;
|
|---|
| 79 | *_ioHandler << tr("Do you want to register this station at %1? (Y/n) [n]").arg(baseUrl.host()) +": ";
|
|---|
| 80 | if(_ioHandler->readLine() != "Y"){
|
|---|
| 81 | *_ioHandler << tr("Aborting!") << endl;
|
|---|
| 82 | qApp->quit();
|
|---|
| 83 | return;
|
|---|
| 84 | }
|
|---|
| 85 | *_ioHandler << tr("Registering station...");
|
|---|
| 86 |
|
|---|
| 87 | SiWebserviceClient *client = new SiWebserviceClient(baseUrl, this);
|
|---|
| 88 | connect(client, SIGNAL(error(SiWeather::Library::SiWebserviceClient::ReplyType, const QString&)),
|
|---|
| 89 | this, SLOT(webserviceError(SiWeather::Library::SiWebserviceClient::ReplyType,QString)));
|
|---|
| 90 | connect(client, SIGNAL(finishedStationsRegister(bool, const QUuid&, const QString&)),
|
|---|
| 91 | this, SLOT(webserviceFinished(bool,QUuid,QString)));
|
|---|
| 92 | QString errorMessage;
|
|---|
| 93 | if(client->stationsRegister(station, errorMessage) != SiWebserviceClient::StationsRegister){
|
|---|
| 94 | *_ioHandler << " "+ tr("FAILED") << endl;
|
|---|
| 95 | *_ioHandler << " "+ errorMessage << endl;
|
|---|
| 96 | qApp->quit();
|
|---|
| 97 | return;
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | void StationsRegister::webserviceError(SiWeather::Library::SiWebserviceClient::ReplyType replyType, const QString& message)
|
|---|
| 103 | {
|
|---|
| 104 | if(replyType != SiWebserviceClient::StationsRegister)
|
|---|
| 105 | return;
|
|---|
| 106 |
|
|---|
| 107 | *_ioHandler << " "+ tr("FAILED") << endl;
|
|---|
| 108 | *_ioHandler << " "+ message << endl;
|
|---|
| 109 | qApp->quit();
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | void StationsRegister::webserviceFinished(bool result, const QUuid& stationUuid, const QString& message)
|
|---|
| 113 | {
|
|---|
| 114 | if(!result){
|
|---|
| 115 | *_ioHandler << " "+ tr("FAILED") << endl;
|
|---|
| 116 | *_ioHandler << " "+ message << endl;
|
|---|
| 117 | qApp->quit();
|
|---|
| 118 | return;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | *_ioHandler << " "+ tr("SUCCESS") << endl;
|
|---|
| 122 | *_ioHandler << " "+ tr("Registered station with uuid %1").arg(stationUuid.toString()) << endl;
|
|---|
| 123 | *_ioHandler << " "+ message << endl;
|
|---|
| 124 | qApp->quit();
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | QString StationsRegister::getCoordinate(const QString& type)
|
|---|
| 128 | {
|
|---|
| 129 | *_ioHandler << tr("%1 (at least 4 digits, point as separator)").arg(type) +": ";
|
|---|
| 130 | QString coordinate = _ioHandler->readLine();
|
|---|
| 131 | if(coordinate.right(5).left(1) != "."){
|
|---|
| 132 | *_ioHandler << tr("Your %1 does not contain 4 digits. Try it again!").arg(type) << endl;
|
|---|
| 133 | return getCoordinate(type);
|
|---|
| 134 | }
|
|---|
| 135 | return coordinate;
|
|---|
| 136 | }
|
|---|