From a16a5f4ed3711ad28f85951c0d63e666b1f86c11 Mon Sep 17 00:00:00 2001 From: Tiina Kivilinna-Korhola Date: Wed, 24 Mar 2010 10:27:58 +0200 Subject: [PATCH] Added sending route to server. Sends table contents from memory. --- Client/carmainwindow.h | 2 +- Client/categorylist.cpp | 42 ++++++++++++- Client/categorylist.h | 12 +++- Client/gpsdata.cpp | 10 +++ Client/gpsdata.h | 2 + Client/httpclient.cpp | 54 +++++++++++++++- Client/httpclient.h | 2 + Client/xmlreader.cpp | 12 +++- Client/xmlreader.h | 1 + Client/xmlwriter.cpp | 158 +++++++++-------------------------------------- Client/xmlwriter.h | 14 +---- 11 files changed, 159 insertions(+), 150 deletions(-) diff --git a/Client/carmainwindow.h b/Client/carmainwindow.h index 25992c3..851d2d2 100644 --- a/Client/carmainwindow.h +++ b/Client/carmainwindow.h @@ -57,6 +57,7 @@ public: ~CarMainWindow(); Registration *myRegistration; //Check if this should be public or private LoginWindow *myLogin; + GPSData *gpsData; void setComboBoxStartTabUnits(QStringList units); //Start-tab view void setListViewStartTabAccelerationCategories(QStringList numbers); //Start-tab view @@ -72,7 +73,6 @@ private: CategoryList *myCategorylist; HttpClient *myHttpClient; RouteDialog *myRoute; - GPSData *gpsData; Maemo5Location *location; //void initCategoryCompoBox(); void initComboBoxStartTabUnits(); //Start-tab view diff --git a/Client/categorylist.cpp b/Client/categorylist.cpp index 3d3f451..243c5d3 100644 --- a/Client/categorylist.cpp +++ b/Client/categorylist.cpp @@ -36,6 +36,7 @@ QStringList CategoryList::getCategoryList() /** *Append an item in the end of the categorylist. + *@param Item. */ void CategoryList::appendCategoryList(QString item) { @@ -44,6 +45,8 @@ void CategoryList::appendCategoryList(QString item) /** *Input an item into the categorylist. + *@param Index. + *@param Item to be appended. */ void CategoryList::fillCategoryList(int index, QString item) { @@ -52,6 +55,7 @@ void CategoryList::fillCategoryList(int index, QString item) /** *Show an item of the categorylist. + *@param Index. */ QString CategoryList::itemOfCategoryList(int index) { @@ -64,7 +68,6 @@ QString CategoryList::itemOfCategoryList(int index) void CategoryList::clearCategoryList() { categoryList.clear(); - qDebug() << "_clearCategoryList" ; } /** @@ -76,7 +79,42 @@ int CategoryList::sizeOfCategoryList() } /** - *This function is used to get items to top list of current category. + *Append an item in the end of the categoryelementable. + *@param Index. + *@param Description of category. + *@param Unit. + *@param Category. + */ +void CategoryList::appendCats(int ind, QString des, QString uni, QString cat) +{ + cats[ind].description = des; + cats[ind].unit = uni; + cats[ind].category = cat; +} + +/** + *Clear categs. + */ +QString CategoryList::desOfCats(int ind) +{ + return cats[ind].description; +} + +/** + *Clear cats. + */ +void CategoryList::clearCats() +{ + for(int i = 0; i < 10; i++) + { + cats[i].description.clear(); + cats[i].unit.clear(); + cats[i].category.clear(); + } +} + +/** + *This function is used to get items to top list of the category that is chosen from combobox. *@param QString category *@param int size */ diff --git a/Client/categorylist.h b/Client/categorylist.h index c7cdc9f..a03586e 100644 --- a/Client/categorylist.h +++ b/Client/categorylist.h @@ -2,6 +2,7 @@ * Categorylist * * @author Olavi Pulkkinen + * @author Tiina Kivilinna-Korhola * @copyright (c) 2010 Speed Freak team * @license http://opensource.org/licenses/gpl-license.php GNU Public License */ @@ -25,9 +26,18 @@ public: QString getTopList( QString category, int size); void clearCategoryList(); int sizeOfCategoryList(); + void appendCats(int ind, QString des, QString uni, QString cat); + void clearCats(); + QString desOfCats(int i); private: - QStringList categoryList; + QStringList categoryList; //Stores categories. categoryList is routed to UI. + typedef struct { + QString category; //name of category variable + QString description; //verbal description of category + QString unit; //km/h, miles/h + } categoryElements; + categoryElements cats[10]; }; diff --git a/Client/gpsdata.cpp b/Client/gpsdata.cpp index baf6448..40e2f12 100644 --- a/Client/gpsdata.cpp +++ b/Client/gpsdata.cpp @@ -198,3 +198,13 @@ void GPSData::saveRoute() route << "Stop: " << routeStopTime << "\n"; file.close(); } + +double* GPSData::getGpsDataArray() +{ + return *gpsDataArray; +} + +int GPSData::getRoundCounter() +{ + return roundCounter; +} diff --git a/Client/gpsdata.h b/Client/gpsdata.h index 2c6f4b6..fd1d451 100644 --- a/Client/gpsdata.h +++ b/Client/gpsdata.h @@ -23,6 +23,8 @@ public: void startRouteRecording(QString time); void stopRouteRecording(QString time); int roundCounter; //testing, move private!!! + double *getGpsDataArray(); + int getRoundCounter(); private: Maemo5Location *location; diff --git a/Client/httpclient.cpp b/Client/httpclient.cpp index 53ad791..0a68341 100644 --- a/Client/httpclient.cpp +++ b/Client/httpclient.cpp @@ -57,7 +57,6 @@ void HttpClient::requestRegistration() /** *@brief Sends result(s) to the server in xml format. *Send authentication information in the header. - *@todo Read category elsewhere. */ void HttpClient::sendResultXml(QString category) { @@ -86,6 +85,37 @@ void HttpClient::sendResultXml(QString category) } /** + *@brief Sends route to the server in xml format. + *Send authentication information in the header. + *@todo Check destination URL. + */ +void HttpClient::sendRouteXml() +{ + qDebug() << "_sendResultXml"; + + QBuffer *xmlbuffer = new QBuffer(); + + QUrl qurl("http://api.speedfreak-app.com/api/update/route"); + qDebug() << qurl.toString(); + QNetworkRequest request(qurl); + QNetworkReply *currentDownload; + + xmlbuffer->open(QBuffer::ReadWrite); + myXmlwriter->writeGpsTrack(xmlbuffer, myMainw->gpsData->getGpsDataArray(), myMainw->gpsData->getRoundCounter()); + qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data(); + + QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword(); + credentials = "Basic " + credentials.toAscii().toBase64(); + request.setRawHeader(QByteArray("Authorization"),credentials.toAscii()); + + currentDownload = netManager->post(request, ("xml=" + xmlbuffer->data())); + connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRoute())); + //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError))); + + xmlbuffer->close(); +} + +/** *@brief Request the Top10List of certain category from the server. *Send authentication information in the header. *@param Category of results. @@ -177,9 +207,29 @@ void HttpClient::ackOfResult() qDebug() << "errorcode:" << errorcode << reply->errorString(); QMessageBox::about(myMainw, "Server reply to result sending", "Result received " + reply->readAll()); } - } +/** + *@brief React to servers responce after result has been sent. + *@todo Implement consequencies of reply. + */ +void HttpClient::ackOfRoute() +{ + qDebug() << "_ackOfRoute"; + + QNetworkReply* reply = qobject_cast(sender()); + + QNetworkReply::NetworkError errorcode; + errorcode = reply->error(); + if(errorcode != 0) { + qDebug() << "errorcode:" << errorcode << reply->errorString(); + QMessageBox::about(myMainw, "Server reply to route sending ",reply->errorString()); + } + else { + qDebug() << "errorcode:" << errorcode << reply->errorString(); + QMessageBox::about(myMainw, "Server reply to route sending", "Route received " + reply->readAll()); + } +} /** *@brief React to servers responce after registration has been sent. diff --git a/Client/httpclient.h b/Client/httpclient.h index ff50ffa..bdacf0e 100644 --- a/Client/httpclient.h +++ b/Client/httpclient.h @@ -34,9 +34,11 @@ public slots: void requestRegistration(); void checkLogin(); void sendResultXml(QString category); + void sendRouteXml(); void requestTopList(QString category, QString limit); void requestCategories(); void ackOfResult(); + void ackOfRoute(); void ackOfRegistration(); void ackOfCategories(); void ackOfToplist(); diff --git a/Client/xmlreader.cpp b/Client/xmlreader.cpp index b997989..2bcc168 100644 --- a/Client/xmlreader.cpp +++ b/Client/xmlreader.cpp @@ -112,7 +112,7 @@ void XmlReader::xmlReadCategories(QNetworkReply *device) if(myCategoryList->sizeOfCategoryList() != 0) { myCategoryList->clearCategoryList(); } - //qDebug() << "sizeOfCategoryList(): " << myCategoryList->sizeOfCategoryList(); + myCategoryList->clearCats(); //Go trough the xml document while(!xmlreader.atEnd()) @@ -130,8 +130,14 @@ void XmlReader::xmlReadCategories(QNetworkReply *device) if(xmlreader.name() == "category") { qDebug() << xmlreader.name(); - myCategoryList->appendCategoryList(xmlreader.readElementText()); - qDebug() << "i=" << i << myCategoryList->itemOfCategoryList(i); + attr = xmlreader.attributes(); + description = attr.value("description").toString(); + unit = attr.value("unit").toString(); + category = xmlreader.readElementText(); + myCategoryList->appendCategoryList(category); + myCategoryList->appendCats(i, description, unit, category); + //qDebug() << "i=" << i << myCategoryList->itemOfCategoryList(i); + //qDebug() << "description: " << description << "unit: " << unit << "category: " << category; i++; receivedFlag = 1; } diff --git a/Client/xmlreader.h b/Client/xmlreader.h index 9704ec3..4a751a3 100644 --- a/Client/xmlreader.h +++ b/Client/xmlreader.h @@ -32,6 +32,7 @@ private: QString position; QString user; QString value; + QString description; signals: void receivedCategoryList(); diff --git a/Client/xmlwriter.cpp b/Client/xmlwriter.cpp index 51eeb46..26eed66 100644 --- a/Client/xmlwriter.cpp +++ b/Client/xmlwriter.cpp @@ -17,7 +17,6 @@ XmlWriter::XmlWriter() { tmpvalue = 110; - trackInd = 0; } /** @@ -84,17 +83,21 @@ void XmlWriter::writeResult(QBuffer *netbuf) /** *@brief Write track to server. - *@param Starting index of gps results table. - *@param Ending index of gps results table. - *@todo Connect to real values. - *@todo Decide suitable parameters. + *@param ptrTable points to GPSData::gpsDataArray[100][4]. + *@param counter is GPSData::roundCounter. + *@todo Decide suitable attributes. */ -void XmlWriter::writeGpsTrack(int startInd, int stopInd) +void XmlWriter::writeGpsTrack(QBuffer *netbuf, double *ptrTable, int counter) { qDebug() << "_writeGpsTrack"; - analyzeGpsData(); - int i = 0; + double *ptrValue; + ptrValue = ptrTable; + double tmp = 0; + + xmlwriter.setDevice(netbuf); + + xmlwriter.writeStartDocument(); xmlwriter.writeStartElement("gpx"); xmlwriter.writeAttribute("someattribute", "abc"); @@ -115,27 +118,31 @@ void XmlWriter::writeGpsTrack(int startInd, int stopInd) xmlwriter.writeCharacters("Example Track"); xmlwriter.writeEndElement(); xmlwriter.writeStartElement("trkseg"); - for(i = startInd; i < (stopInd - startInd); i++) { + for(int i = 0; i < counter; i++) + { xmlwriter.writeStartElement("trkpt"); - xmlwriter.writeAttribute("lat", QString::number(trackTable[i].latitude)); - xmlwriter.writeAttribute("lon", QString::number(trackTable[i].longitude)); + tmp = *ptrValue; + ptrValue++; + xmlwriter.writeAttribute("lat", QString::number(tmp)); //gpspoints[i][0] + tmp = *ptrValue; + ptrValue++; + xmlwriter.writeAttribute("lon", QString::number(tmp)); //gpspoints[i][1] xmlwriter.writeStartElement("ele"); - xmlwriter.writeCharacters(QString::number(trackTable[i].altitude)); - xmlwriter.writeEndElement(); - xmlwriter.writeStartElement("time"); - xmlwriter.writeCharacters(QString::number(trackTable[i].time)); + tmp = *ptrValue; + ptrValue++; + xmlwriter.writeCharacters(QString::number(tmp)); //gpspoints[i][2] xmlwriter.writeEndElement(); xmlwriter.writeStartElement("speed"); - xmlwriter.writeCharacters(QString::number(trackTable[i].speed)); - xmlwriter.writeEndElement(); - xmlwriter.writeStartElement("track"); - xmlwriter.writeCharacters(QString::number(trackTable[i].track)); + tmp = *ptrValue; + ptrValue++; + xmlwriter.writeCharacters(QString::number(tmp)); //gpspoints[i][3] xmlwriter.writeEndElement(); xmlwriter.writeEndElement(); //trkpt } - xmlwriter.writeEndElement(); //trkseg - xmlwriter.writeEndElement(); //trk - xmlwriter.writeEndElement(); //gpx + xmlwriter.writeEndElement(); //trkseg + xmlwriter.writeEndElement(); //trk + xmlwriter.writeEndElement(); //gpx + xmlwriter.writeEndDocument(); } @@ -176,8 +183,7 @@ bool XmlWriter::writeXmlFile(QIODevice *device) xmlwriter.setDevice(device); xmlwriter.writeStartDocument(); //writeItems(); - //serverWritesXml(); - writeGpsTrack(0, 16); + serverWritesXml(); xmlwriter.writeEndDocument(); return true; @@ -204,26 +210,6 @@ void XmlWriter::writeItems() */ void XmlWriter::serverWritesXml() { - /* Server sends to client */ - /* - int i = 0; - int n = 5; - - //Write top 10 Results - xmlwriter.writeStartElement("results"); - xmlwriter.writeAttribute("category", "acceleration-0-40"); - xmlwriter.writeAttribute("unit", "seconds"); - xmlwriter.writeAttribute("description", "Acceleration from 0 to 100 km/h"); - - for (i = 0; i < n; i++) { - xmlwriter.writeStartElement("result"); - xmlwriter.writeAttribute("position", QString::number(i)); - xmlwriter.writeAttribute("user", "test123"); - xmlwriter.writeAttribute("date", QDateTime::currentDateTime().toString()); - xmlwriter.writeAttribute("value", QString::number(i+i+1)); - xmlwriter.writeEndElement(); - } - */ //Write categories xmlwriter.writeStartElement("categories"); @@ -241,87 +227,3 @@ void XmlWriter::serverWritesXml() xmlwriter.writeEndElement(); } - - -/** - *@brief A temp function during development, used to create data for drawing route and for server. - */ -void XmlWriter::analyzeGpsData() -{ - qDebug() << "_analyzeGpsData"; - - double startTime; - int tableSize = 0; - - qDebug() << "sizeof(analyzeTable)" << sizeof(analyzeTable); - tableSize = 16; - - for(int i = 1; i < tableSize; i++) - { - //example of one feature whose consequent values are compared and saved if they differentiate too much - if(analyzeTable[i].speed < (analyzeTable[i-1].speed - 1) || - analyzeTable[i].speed > (analyzeTable[i-1].speed + 1) ) - { - trackTable[trackInd] = analyzeTable[i]; - trackInd++; - qDebug() << "trackTable[trackInd].speed" << trackTable[trackInd].speed; - } - } -} - -void XmlWriter::initPointTable(gpsPoint *table, int count, double add1, int add2, int add3) -{ - qDebug() << "_initPointTable"; - - int i = 1; - int j = 0; - - table[0].latitude = 67.00; - table[0].longitude = 27.50; - table[0].altitude = 7.00; - table[0].speed = 0; - table[0].time = 0; - - for(j = 0; j < count; j++) - { - table[i].latitude = table[i-1].latitude + add1; - //table[i].longitude = table[i-1].longitude + add1; - table[i].altitude = table[i-1].altitude + add1; - table[i].speed = table[i-1].speed + add2; - table[i].track = table[i-1].track + 1; - table[i].time = table[i-1].time + add3; - i++; - } - for(j = 0; j < count; j++) - { - //table[i].latitude = table[i-1].latitude + add1; - table[i].longitude = table[i-1].longitude + add1; - table[i].altitude = table[i-1].altitude -add1; - table[i].speed = table[i-1].speed + add2; - table[i].track = table[i-1].track + 1; - table[i].time = table[i-1].time + add3; - i++; - } - for(j = 0; j < count; j++) - { - table[i].latitude = table[i-1].latitude - add1; - //table[i].longitude = table[i-1].longitude + add1; - table[i].altitude = table[i-1].altitude + add1; - table[i].speed = table[i-1].speed - add2; - table[i].track = table[i-1].track - 1; - table[i].time = table[i-1].time + add3; - i++; - } - for(j = 0; j < count; j++) - { - //table[i].latitude = table[i-1].latitude + add1; - table[i].longitude = table[i-1].longitude - add1; - table[i].altitude = table[i-1].altitude - add1; - table[i].speed = table[i-1].speed - add2; - table[i].track = table[i-1].track - 1; - table[i].time = table[i-1].time + add3; - i++; - } -} - - diff --git a/Client/xmlwriter.h b/Client/xmlwriter.h index 34bf284..80ff500 100644 --- a/Client/xmlwriter.h +++ b/Client/xmlwriter.h @@ -30,16 +30,6 @@ public: XmlWriter(); ~XmlWriter(); int tmpvalue; - typedef struct { - double time; - double latitude; - double longitude; - double altitude; - double speed; - double track; - } gpsPoint; - gpsPoint trackTable[100]; - gpsPoint analyzeTable[100]; int trackInd; private: @@ -48,14 +38,12 @@ private: public slots: void writeRegistering(QBuffer *netbuf, QString usr, QString psswd, QString email); void writeResult(QBuffer *netbuf); + void writeGpsTrack(QBuffer *netbuf, double *ptrTable, int counter); bool writeXmlFile(QIODevice *device); //void writeXml(QString usr, QString psswd, QString email); void writeXml(); void writeItems(); void serverWritesXml(); - void writeGpsTrack(int startInd, int stopInd); - void initPointTable(gpsPoint *table, int count, double add1, int add2, int add3); - void analyzeGpsData(); }; #endif // XMLWRITER_H -- 1.7.9.5