Ignore:
Timestamp:
Sep 23, 2015, 6:35:24 PM (11 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
master
Children:
f8c242e
Parents:
93ea008
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/clientcommandline/stationsregister.cpp

    r93ea008 r17ef75d  
    2222
    2323#include "inputoutputhandler.h"
     24#include "siweatherclientconfig.h"
    2425
    2526#include <siweatherstation.h>
    … …  
    3738void StationsRegister::registerStation()
    3839{
    39         QUrl baseUrl("http://localhost:9999/0.1/");
     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;
    4050       
     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
    4188        *_ioHandler << tr("Please enter some data to describe your station") << endl;
    42         SiWeatherStation station;
    4389       
    44         *_ioHandler << tr("Name") +": ";
    45         station.setName(_ioHandler->readLine());
     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());
    4694       
    47         *_ioHandler << tr("City") +": ";
    48         station.setCity(_ioHandler->readLine());
     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()));
    49101       
    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        
     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
    66115        if(!station.isValidForRegister()){
    67116                *_ioHandler << tr("Your station is not valid for register at the webservice.") << endl;
    68117                *_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;
    69126        }
    70127        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"){
     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
    81135                        *_ioHandler << tr("Aborting!") << endl;
    82136                        qApp->quit();
    … …  
    97151                        return;
    98152                }
     153                _lastStation = station;
    99154        }
    100155}
    101156
    102 void StationsRegister::webserviceError(SiWeather::Library::SiWebserviceClient::ReplyType replyType, const QString& message)
     157QString StationsRegister::getValue(const QString& label, const QString& defaultValue)
    103158{
    104         if(replyType != SiWebserviceClient::StationsRegister)
    105                 return;
     159        if(!_ioHandler)
     160                return QString();
    106161
    107         *_ioHandler << "      "+ tr("FAILED") << endl;
    108         *_ioHandler << "  "+ message << endl;
    109         qApp->quit();
     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;
    110170}
    111171
    112 void StationsRegister::webserviceFinished(bool result, const QUuid& stationUuid, const QString& message)
     172QString StationsRegister::getCoordinate(const QString& type, double defaultValue)
    113173{
    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 }
     174        if(!_ioHandler)
     175                return QString();
    126176
    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) != "."){
     177        QString coordinate = getValue(tr("%1 (at least 4 digits, point as separator)").arg(type), coordinateToString(defaultValue));
     178        if(coordinate.length() - coordinate.indexOf(".") < 5){
    132179                *_ioHandler << tr("Your %1 does not contain 4 digits. Try it again!").arg(type) << endl;
    133                 return getCoordinate(type);
     180                return getCoordinate(type, defaultValue);
    134181        }
    135182        return coordinate;
    136183}
     184
     185QString StationsRegister::coordinateToString(double coordinate) const
     186{
     187        return QString::number(coordinate, 'f', 4);
     188}
Note: See TracChangeset for help on using the changeset viewer.