Route and Results buttons updated.
[speedfreak] / Client / gpsdata.cpp
index baf6448..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()));
@@ -23,6 +25,9 @@ GPSData::GPSData(Maemo5Location *maemo5location)
     connect(location,SIGNAL(gps_error(int)),this,SLOT(gpsError()));
     connect(location,SIGNAL(gpsd_running()),this,SLOT(gpsdRunning()));
     connect(location,SIGNAL(gpsd_stopped()),this,SLOT(gpsdStopped()));
+
+    gpsDateTime = new QDateTime();
+    resetAll();
 }
 
 /**
@@ -30,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;
@@ -52,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;
 }
 
 /**
@@ -62,40 +81,66 @@ 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.
-    //epv = QString::number(location->getEpv());                          //Returns altitude accuracy in meters.
-    //track = QString::number(location->getTrack());                      //Returns direction of motion in degrees(0-359).
-    //epd = QString::number(location->getEpd());                          //Returns track accuracy in degrees.
-    //eps = QString::number(location->getEps());                          //Returns speed accuracy in km/h.
-    //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.
-
-    latitude    = location->getLatitude();  //Returns latitude.
-    longitude   = location->getLongitude(); //Returns longitude.
-    altitude    = location->getAltitude();  //Returns fix altitude in meters.
-    speed       = location->getSpeed();     //Returns current speed in km/h.
-
-    latitudeNow.sprintf("%.4f", latitude);  //Latitude now to string
-    longitudeNow.sprintf("%.4f", longitude);//Longitude now to string
-    latitudePrevious.sprintf("%.4f", gpsDataArray[roundCounter-1][0]);  //Previous latitude to string
-    longitudePrevious.sprintf("%.4f", gpsDataArray[roundCounter-1][1]); //Previous longitude to string
+    //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 )
     {
-        if ( roundCounter == 0 || latitudeNow != latitudePrevious || longitudeNow != longitudePrevious )
+        latitudePrevious = latitude;
+        longitudePrevious = longitude;
+        latitude    = location->getLatitude();  //Returns latitude.
+        longitude   = location->getLongitude(); //Returns longitude.
+        altitude    = location->getAltitude();  //Returns fix altitude in meters.
+        speed       = location->getSpeed();     //Returns current speed in km/h.
+
+        QFile routeTempFile("routetemp.xml");//Temp xml.
+
+        //If GPS find 4 or more satellite and signal stregth is 30 or more.
+        if (location->getSatellitesInUse() >= 4 && location->getSignalStrength() >= 30)
         {
-            gpsDataArray[roundCounter][0] = latitude;
-            gpsDataArray[roundCounter][1] = longitude;
-            gpsDataArray[roundCounter][2] = altitude;
-            gpsDataArray[roundCounter][3] = speed;
-            roundCounter ++;
+            //If first round
+            if (roundCounter == 0)
+            {
+                if (!routeTempFile.open(QIODevice::WriteOnly | QIODevice::Text))
+                    return;
+                writeRouteXml(&routeTempFile, 0);
+                routeTempFile.close();
+                roundCounter ++;
+            }
+
+            //Points writing round.
+            else
+            {
+                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();
+                }
+            }
         }
     }
 }
@@ -156,45 +201,90 @@ void GPSData::gpsdStopped()
 
 }
 
-void GPSData::startRouteRecording(QString time)
+/**
+  *This function start route recording.
+  *@param QString time recording start 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;
     }
 }
 
-void GPSData::stopRouteRecording(QString time)
+/**
+  *This function stop route recording.
+  *@param QString time recording stop time.
+  */
+void GPSData::stopRouteRecording()
 {
-    routeStopTime = time;
-    saveRoute();
     if (recordingStatus == true)
     {
+        //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;
     }
 }
 
-void GPSData::saveRoute()
+/**
+  *This function write route to .xml file.
+  */
+void GPSData::writeRouteXml(QIODevice *device, int round)
 {
-    QFile file("testroute.txt");
-    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
+    xmlwriter.setDevice(device);
+
+    //Write temp xml (routetemp.xml).
+    if ( round == 0 )
     {
-        return;
+        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
     }
-    QTextStream route(&file);
 
-    route << "Start: " << routeStartTime << "\n";
-    for (int i = 0 ; i <= roundCounter ; i++)
+    //Write final xml (route.xml).
+    else if ( round == 1 )
     {
-        route << " lat: " << gpsDataArray[i][0]
-              << " lon: " << gpsDataArray[i][1]
-              << " alt: " << gpsDataArray[i][2]
-              << " spe: " << gpsDataArray[i][3]
-              << "\n";
+        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;
+        QTextStream readRoute(&tempFile);
+        QTextStream writeRoute(device);
+        writeRoute << readRoute.readLine();
+        tempFile.close();//Close temp xml
+
+        xmlwriter.writeEndElement();//Route
+        xmlwriter.writeEndDocument();     
     }
-    route << "Stop: " << routeStopTime << "\n";
-    file.close();
+}
+
+/**
+  *This function returns distance traveled since recording started.
+  */
+double GPSData::getDistanceTraveled()
+{
+    return distance;
 }