/* Copyright (C) 2015 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "stationsregister.h" #include "moc_stationsregister.cpp" #include "inputoutputhandler.h" #include #include using namespace SiWeather::Library; StationsRegister::StationsRegister(QObject* parent) : QObject(parent) { _ioHandler = new InputOutputHandler(this); } void StationsRegister::registerStation() { QUrl baseUrl("http://localhost:9999/0.1/"); *_ioHandler << tr("Please enter some data to describe your station") << endl; SiWeatherStation station; *_ioHandler << tr("Name") +": "; station.setName(_ioHandler->readLine()); *_ioHandler << tr("City") +": "; station.setCity(_ioHandler->readLine()); station.setLatitude(getCoordinate(tr("Latitude")).toDouble()); station.setLongitude(getCoordinate(tr("Longitude")).toDouble()); *_ioHandler << tr("Your E-Mail Address") +": "; station.setEMail(_ioHandler->readLine()); QString defaultType = "siweatherstation 1"; *_ioHandler << tr("Type, describing your station") +" ["+ defaultType +"]: "; QString type = _ioHandler->readLine(); if(type.isEmpty()) type = defaultType; station.setType(type); *_ioHandler << tr("Url with information about your type (optional)") + ": "; station.setTypeUrl(_ioHandler->readLine()); if(!station.isValidForRegister()){ *_ioHandler << tr("Your station is not valid for register at the webservice.") << endl; *_ioHandler << tr("This means, the name, city, e-mail address or type is empty.") << endl; } else{ *_ioHandler << tr("Your station is defined as follows:") << endl; *_ioHandler << " "+ tr("Name: ") + station.name() << endl; *_ioHandler << " "+ tr("City: ") + station.city() << endl; *_ioHandler << " "+ tr("Latitude: ") + QString::number(station.latitude()) << endl; *_ioHandler << " "+ tr("Longitude: ") + QString::number(station.longitude()) << endl; *_ioHandler << " "+ tr("Type: ") + station.type() << endl; *_ioHandler << " "+ tr("Type Url: ") + station.typeUrl().toString() << endl; *_ioHandler << " "+ tr("E-Mail: ") + station.eMail() << endl; *_ioHandler << tr("Do you want to register this station at %1? (Y/n) [n]").arg(baseUrl.host()) +": "; if(_ioHandler->readLine() != "Y"){ *_ioHandler << tr("Aborting!") << endl; qApp->quit(); return; } *_ioHandler << tr("Registering station..."); SiWebserviceClient *client = new SiWebserviceClient(baseUrl, this); connect(client, SIGNAL(error(SiWeather::Library::SiWebserviceClient::ReplyType, const QString&)), this, SLOT(webserviceError(SiWeather::Library::SiWebserviceClient::ReplyType,QString))); connect(client, SIGNAL(finishedStationsRegister(bool, const QUuid&, const QString&)), this, SLOT(webserviceFinished(bool,QUuid,QString))); QString errorMessage; if(client->stationsRegister(station, errorMessage) != SiWebserviceClient::StationsRegister){ *_ioHandler << " "+ tr("FAILED") << endl; *_ioHandler << " "+ errorMessage << endl; qApp->quit(); return; } } } void StationsRegister::webserviceError(SiWeather::Library::SiWebserviceClient::ReplyType replyType, const QString& message) { if(replyType != SiWebserviceClient::StationsRegister) return; *_ioHandler << " "+ tr("FAILED") << endl; *_ioHandler << " "+ message << endl; qApp->quit(); } void StationsRegister::webserviceFinished(bool result, const QUuid& stationUuid, const QString& message) { if(!result){ *_ioHandler << " "+ tr("FAILED") << endl; *_ioHandler << " "+ message << endl; qApp->quit(); return; } *_ioHandler << " "+ tr("SUCCESS") << endl; *_ioHandler << " "+ tr("Registered station with uuid %1").arg(stationUuid.toString()) << endl; *_ioHandler << " "+ message << endl; qApp->quit(); } QString StationsRegister::getCoordinate(const QString& type) { *_ioHandler << tr("%1 (at least 4 digits, point as separator)").arg(type) +": "; QString coordinate = _ioHandler->readLine(); if(coordinate.right(5).left(1) != "."){ *_ioHandler << tr("Your %1 does not contain 4 digits. Try it again!").arg(type) << endl; return getCoordinate(type); } return coordinate; }