Youtube video and text (draft).
[speedfreak] / Client / gpsdata.cpp
index 14a199c..db0a6bc 100644 (file)
@@ -24,10 +24,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();
 }
 
@@ -37,7 +34,7 @@ GPSData::GPSData(Maemo5Location *maemo5location)
 GPSData::~GPSData()
 {
     delete location;
-    location = NULL;
+    delete gpsDateTime;
 }
 
 void GPSData::resetAll()
@@ -58,7 +55,14 @@ void GPSData::resetAll()
     eps = 0;
     climb = 0;
     epc = 0;
-
+    latitudePrevious = 0;
+    longitudePrevious = 0;
+    sLatitudeNow = "";
+    sLongitudeNow = "";
+    sLatitudePrevious = "";
+    sLongitudePrevious = "";
+    routeStartTime = "";
+    routeStopTime = "";
     recordingStatus = false;
     roundCounter = 0;
 }
@@ -68,48 +72,60 @@ 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)
         {
-            saveRoute();
+            if (!routeTempFile.open(QIODevice::WriteOnly | QIODevice::Text))
+                return;
+            writeRouteXml(&routeTempFile, 0);
+            routeTempFile.close();
+            roundCounter ++;
         }
 
+        //Points writing round.
         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
+            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 ( latitudeNow != latitudePrevious || longitudeNow != longitudePrevious )
+            if ( sLatitudeNow != sLatitudePrevious || sLongitudeNow != sLongitudePrevious )
             {
-                saveRoute();
+                if (!routeTempFile.open(QIODevice::Append | QIODevice::Text))
+                    return;
+
+                writeRouteXml(&routeTempFile, 0);
+                roundCounter ++;
+                routeTempFile.close();
             }
         }
     }
@@ -175,11 +191,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 +207,62 @@ 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).
-  */
-void GPSData::gpsTimerTimeout()
-{
-    gpsTimeMS++;
-}
-
-/**
-  *This function save route to .txt file.
+  *This function write route to .xml file.
   */
-void GPSData::saveRoute()
+void GPSData::writeRouteXml(QIODevice *device, int round)
 {
-    QFile file("route" + routeStartTime + ".txt");
-    QTextStream route(&file);
+    xmlwriter.setDevice(device);
 
-    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
 
-/**
-  *@return RoundCounter, the number of gpsDataArray[][] rows.
-  */
-int GPSData::getRoundCounter()
-{
-    return roundCounter;
+        xmlwriter.writeEndElement();//Route
+        xmlwriter.writeEndDocument();     
+    }
 }