Route and Results buttons updated.
[speedfreak] / Client / gpsdata.cpp
index 14a199c..abd4141 100644 (file)
@@ -7,12 +7,14 @@
  */
 
 #include "gpsdata.h"
+#include <QDebug>
 
 /**
   *Default constructor of this class.
   */
 GPSData::GPSData(Maemo5Location *maemo5location)
 {
+    qDebug() << "__GPSData";
     location = maemo5location;
 
     connect(location,SIGNAL(agnss()),this,SLOT(agnss()));
@@ -24,10 +26,7 @@ GPSData::GPSData(Maemo5Location *maemo5location)
     connect(location,SIGNAL(gpsd_running()),this,SLOT(gpsdRunning()));
     connect(location,SIGNAL(gpsd_stopped()),this,SLOT(gpsdStopped()));
 
-    gpsTimer = new QTimer();
-    gpsTimeMS = 0;
-    connect(gpsTimer, SIGNAL(timeout()),this, SLOT(gpsTimerTimeout()));
-
+    gpsDateTime = new QDateTime();
     resetAll();
 }
 
@@ -36,10 +35,16 @@ GPSData::GPSData(Maemo5Location *maemo5location)
   */
 GPSData::~GPSData()
 {
-    delete location;
+    qDebug() << "__~GPSData";
     location = NULL;
+
+    if(gpsDateTime)
+        delete gpsDateTime;
 }
 
+/**
+  * This function reset all variables
+  */
 void GPSData::resetAll()
 {
     satellitesInUse = 0;
@@ -58,9 +63,17 @@ void GPSData::resetAll()
     eps = 0;
     climb = 0;
     epc = 0;
-
+    latitudePrevious = 0;
+    longitudePrevious = 0;
+    sLatitudeNow = "";
+    sLongitudeNow = "";
+    sLatitudePrevious = "";
+    sLongitudePrevious = "";
+    routeStartTime = "";
+    routeStopTime = "";
     recordingStatus = false;
     roundCounter = 0;
+    distance = 0;
 }
 
 /**
@@ -68,48 +81,65 @@ void GPSData::resetAll()
   */
 void GPSData::agnss()
 {
-    //satellitesInUse = QString::number(location->getSatellitesInUse());  //Returns number of satellites in use.
-    //satellitesInView = QString::number(location->getSatellitesInView());//Returns number of satellites in view.
-    //signalStrength = QString::number(location->getSignalStrength());    //Returns average signal strength of satellites which are in use.
-    //gpsOnline = QString::number(location->getGpsOnline());              //Returns gsp online                     
-    //ept = QString::number(location->getEpt());                          //Returns time accuracy in seconds.
-    //eph = QString::number(location->getEph());                          //Returns horizontal position accuracy in cm.
-    //track = QString::number(location->getTrack());                      //Returns direction of motion in degrees(0-359).
-    //epd = QString::number(location->getEpd());                          //Returns track accuracy in degrees.
-    //climb = QString::number(location->getClimb());                      //Returns current rate of climb in m/s.
-    //epc = QString::number(location->getEpc());                          //Returns climb accuracy in m/s.
-    //location->distance_between_two_points(double latitude_s, double longitude_s, double latitude_f, double longitude_f);
-    //time = location->getTime();//Returns timestamp of the update in seconds.
+    //satellitesInUse   = location->getSatellitesInUse());  //Returns number of satellites in use.
+    //satellitesInView  = location->getSatellitesInView();  //Returns number of satellites in view.
+    //signalStrength    = location->getSignalStrength();    //Returns average signal strength of satellites which are in use.
+    //gpsOnline         = location->getGpsOnline();         //Returns gsp online
+    //ept               = location->getEpt();               //Returns time accuracy in seconds.
+    //eph               = location->getEph();               //Returns horizontal position accuracy in cm.
+    //track             = location->getTrack();             //Returns direction of motion in degrees(0-359).
+    //epd               = location->getEpd();               //Returns track accuracy in degrees.
+    //climb             = location->getClimb();             //Returns current rate of climb in m/s.
+    //epc               = location->getEpc();               //Returns climb accuracy in m/s.
+    //time              = location->getTime();              //Returns timestamp of the update in seconds.
+    //epv               = location->getEpv();               //Returns altitude accuracy in meters.
+    //eps               = location->getEps();               //Returns speed accuracy in km/h.
+    //distance          = location->distance_between_two_points(double latitude_s, double longitude_s, double latitude_f, double longitude_f);
 
     //If route recording true
     if ( recordingStatus == true )
     {
+        latitudePrevious = latitude;
+        longitudePrevious = longitude;
         latitude    = location->getLatitude();  //Returns latitude.
         longitude   = location->getLongitude(); //Returns longitude.
         altitude    = location->getAltitude();  //Returns fix altitude in meters.
-        epv         = location->getEpv();       //Returns altitude accuracy in meters.
         speed       = location->getSpeed();     //Returns current speed in km/h.
-        eps         = location->getEps();       //Returns speed accuracy in km/h.
 
-        gpsTimer->start(1);
+        QFile routeTempFile("routetemp.xml");//Temp xml.
 
-        //If first round
-        if (roundCounter == 0)
+        //If GPS find 4 or more satellite and signal stregth is 30 or more.
+        if (location->getSatellitesInUse() >= 4 && location->getSignalStrength() >= 30)
         {
-            saveRoute();
-        }
-
-        else
-        { 
-            latitudeNow.sprintf("%.4f", latitude);  //Latitude now to string
-            longitudeNow.sprintf("%.4f", longitude);//Longitude now to string
-            latitudePrevious.sprintf("%.4f", gpsDataArray[0]);  //Previous latitude to string
-            longitudePrevious.sprintf("%.4f", gpsDataArray[1]); //Previous longitude to string
+            //If first round
+            if (roundCounter == 0)
+            {
+                if (!routeTempFile.open(QIODevice::WriteOnly | QIODevice::Text))
+                    return;
+                writeRouteXml(&routeTempFile, 0);
+                routeTempFile.close();
+                roundCounter ++;
+            }
 
-            //If latitude or longitude change
-            if ( latitudeNow != latitudePrevious || longitudeNow != longitudePrevious )
+            //Points writing round.
+            else
             {
-                saveRoute();
+                sLatitudeNow.sprintf("%.4f", latitude);  //Latitude now to string
+                sLongitudeNow.sprintf("%.4f", longitude);//Longitude now to string
+                sLatitudePrevious.sprintf("%.4f", latitudePrevious);  //Previous latitude to string
+                sLongitudePrevious.sprintf("%.4f", longitudePrevious); //Previous longitude to string
+
+                //If latitude or longitude change
+                if ( sLatitudeNow != sLatitudePrevious || sLongitudeNow != sLongitudePrevious )
+                {
+                    if (!routeTempFile.open(QIODevice::Append | QIODevice::Text))
+                        return;
+
+                    distance += location->distance_between_two_points(latitudePrevious, longitudePrevious, latitude, longitude);
+                    writeRouteXml(&routeTempFile, 0);
+                    roundCounter ++;
+                    routeTempFile.close();
+                }
             }
         }
     }
@@ -175,11 +205,13 @@ void GPSData::gpsdStopped()
   *This function start route recording.
   *@param QString time recording start time.
   */
-void GPSData::startRouteRecording(QString time)
+void GPSData::startRouteRecording()
 {
     if (recordingStatus == false)
     {
-        routeStartTime = time;
+        //Get start time and start recording.
+        gpsDateTime->setTime_t(location->getTime());
+        routeStartTime = gpsDateTime->toString("dd.MM.yyyy hh:mm:ss");
         recordingStatus = true;
         roundCounter = 0;
     }
@@ -189,84 +221,70 @@ void GPSData::startRouteRecording(QString time)
   *This function stop route recording.
   *@param QString time recording stop time.
   */
-void GPSData::stopRouteRecording(QString time)
+void GPSData::stopRouteRecording()
 {
     if (recordingStatus == true)
     {
-        routeStopTime = time;
+        //Get stop time and stop recording.
+        gpsDateTime->setTime_t(location->getTime());
+        routeStopTime = gpsDateTime->toString("dd.MM.yyyy hh:mm:ss");
         recordingStatus = false;
+
+        //Write final xml.
+        QFile file("route.xml");
+        if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
+            return;
+        writeRouteXml(&file, 1);
+        file.close();
         roundCounter = 0;
-        saveRoute();
     }
 }
 
 /**
-  *This slot function is called when gps timer timeout(10s).
+  *This function write route to .xml file.
   */
-void GPSData::gpsTimerTimeout()
+void GPSData::writeRouteXml(QIODevice *device, int round)
 {
-    gpsTimeMS++;
-}
+    xmlwriter.setDevice(device);
 
-/**
-  *This function save route to .txt file.
-  */
-void GPSData::saveRoute()
-{
-    QFile file("route" + routeStartTime + ".txt");
-    QTextStream route(&file);
-
-    if ( recordingStatus == true )
+    //Write temp xml (routetemp.xml).
+    if ( round == 0 )
     {
-        //First round.
-        if ( roundCounter == 0 )
-        {
-            if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
-                return;
-
-            route << "Start: " << routeStartTime << "\n";
-        }
-
-        else
-        {
-            if (!file.open(QIODevice::Append | QIODevice::Text))
-                return;
-        }
-
-        gpsDataArray[0] = latitude;
-        gpsDataArray[1] = longitude;
-        gpsDataArray[2] = altitude;
-        gpsDataArray[3] = speed;
-        roundCounter ++;
-
-        route << " la: " << latitude
-              << " \t lo: " << longitude
-              << " \t al: " << altitude
-              << " \t epv: " << epv
-              << " \t sp: " << speed
-              << " \t eps: " << eps
-              << " \t ms: " << gpsTimeMS
-              << "\n";
-
-        gpsTimeMS = 0;
-        file.close();
+        xmlwriter.writeStartElement("Point");
+        xmlwriter.writeAttribute("Latitude", QString::number(latitude));
+        xmlwriter.writeAttribute("Longitude", QString::number(longitude));
+        xmlwriter.writeAttribute("Altitude", QString::number(altitude));
+        xmlwriter.writeAttribute("Speed", QString::number(speed));
+        xmlwriter.writeEndElement();//Point
     }
 
-    //Final round.
-    else
+    //Write final xml (route.xml).
+    else if ( round == 1 )
     {
-        if (!file.open(QIODevice::Append | QIODevice::Text))
+        xmlwriter.writeStartDocument();
+        xmlwriter.writeStartElement("Route");
+        xmlwriter.writeAttribute("Start-time", routeStartTime);
+        xmlwriter.writeAttribute("Stop-time", routeStopTime);
+        xmlwriter.writeAttribute("Points", QString::number(roundCounter));
+
+        //Open temp xml and read points
+        QFile tempFile("routetemp.xml");
+        if (!tempFile.open(QIODevice::ReadOnly | QIODevice::Text))
             return;
-        route << "Stop: " << routeStopTime << "\n";
-        file.close();
+        QTextStream readRoute(&tempFile);
+        QTextStream writeRoute(device);
+        writeRoute << readRoute.readLine();
+        tempFile.close();//Close temp xml
+
+        xmlwriter.writeEndElement();//Route
+        xmlwriter.writeEndDocument();     
     }
 }
 
-
 /**
-  *@return RoundCounter, the number of gpsDataArray[][] rows.
+  *This function returns distance traveled since recording started.
   */
-int GPSData::getRoundCounter()
+double GPSData::getDistanceTraveled()
 {
-    return roundCounter;
+    return distance;
 }