abd4141e9c03af059d6ea9628125ded1ad4318ea
[speedfreak] / Client / gpsdata.cpp
1 /*
2  * GPS data
3  *
4  * @author     Toni Jussila <toni.jussila@fudeco.com>
5  * @copyright  (c) 2010 Speed Freak team
6  * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
7  */
8
9 #include "gpsdata.h"
10 #include <QDebug>
11
12 /**
13   *Default constructor of this class.
14   */
15 GPSData::GPSData(Maemo5Location *maemo5location)
16 {
17     qDebug() << "__GPSData";
18     location = maemo5location;
19
20     connect(location,SIGNAL(agnss()),this,SLOT(agnss()));
21     connect(location,SIGNAL(awcp()),this,SLOT(awcp()));
22     connect(location,SIGNAL(locationUpdated()),this,SLOT(locationUpdated()));
23     connect(location,SIGNAL(gps_connected()),this,SLOT(gpsConnected()));
24     connect(location,SIGNAL(gps_disconnected()),this,SLOT(gpsDisconnected()));
25     connect(location,SIGNAL(gps_error(int)),this,SLOT(gpsError()));
26     connect(location,SIGNAL(gpsd_running()),this,SLOT(gpsdRunning()));
27     connect(location,SIGNAL(gpsd_stopped()),this,SLOT(gpsdStopped()));
28
29     gpsDateTime = new QDateTime();
30     resetAll();
31 }
32
33 /**
34   *Destructor of this class. Deletes all dynamic objects and sets them to NULL.
35   */
36 GPSData::~GPSData()
37 {
38     qDebug() << "__~GPSData";
39     location = NULL;
40
41     if(gpsDateTime)
42         delete gpsDateTime;
43 }
44
45 /**
46   * This function reset all variables
47   */
48 void GPSData::resetAll()
49 {
50     satellitesInUse = 0;
51     satellitesInView = 0;
52     signalStrength = 0;
53     latitude = 0;
54     longitude = 0;
55     time = 0;
56     ept = 0;
57     eph = 0;
58     altitude = 0;
59     epv = 0;
60     track = 0;
61     epd = 0;
62     speed = 0;
63     eps = 0;
64     climb = 0;
65     epc = 0;
66     latitudePrevious = 0;
67     longitudePrevious = 0;
68     sLatitudeNow = "";
69     sLongitudeNow = "";
70     sLatitudePrevious = "";
71     sLongitudePrevious = "";
72     routeStartTime = "";
73     routeStopTime = "";
74     recordingStatus = false;
75     roundCounter = 0;
76     distance = 0;
77 }
78
79 /**
80   *This slot function is called when GPS update location.
81   */
82 void GPSData::agnss()
83 {
84     //satellitesInUse   = location->getSatellitesInUse());  //Returns number of satellites in use.
85     //satellitesInView  = location->getSatellitesInView();  //Returns number of satellites in view.
86     //signalStrength    = location->getSignalStrength();    //Returns average signal strength of satellites which are in use.
87     //gpsOnline         = location->getGpsOnline();         //Returns gsp online
88     //ept               = location->getEpt();               //Returns time accuracy in seconds.
89     //eph               = location->getEph();               //Returns horizontal position accuracy in cm.
90     //track             = location->getTrack();             //Returns direction of motion in degrees(0-359).
91     //epd               = location->getEpd();               //Returns track accuracy in degrees.
92     //climb             = location->getClimb();             //Returns current rate of climb in m/s.
93     //epc               = location->getEpc();               //Returns climb accuracy in m/s.
94     //time              = location->getTime();              //Returns timestamp of the update in seconds.
95     //epv               = location->getEpv();               //Returns altitude accuracy in meters.
96     //eps               = location->getEps();               //Returns speed accuracy in km/h.
97     //distance          = location->distance_between_two_points(double latitude_s, double longitude_s, double latitude_f, double longitude_f);
98
99     //If route recording true
100     if ( recordingStatus == true )
101     {
102         latitudePrevious = latitude;
103         longitudePrevious = longitude;
104         latitude    = location->getLatitude();  //Returns latitude.
105         longitude   = location->getLongitude(); //Returns longitude.
106         altitude    = location->getAltitude();  //Returns fix altitude in meters.
107         speed       = location->getSpeed();     //Returns current speed in km/h.
108
109         QFile routeTempFile("routetemp.xml");//Temp xml.
110
111         //If GPS find 4 or more satellite and signal stregth is 30 or more.
112         if (location->getSatellitesInUse() >= 4 && location->getSignalStrength() >= 30)
113         {
114             //If first round
115             if (roundCounter == 0)
116             {
117                 if (!routeTempFile.open(QIODevice::WriteOnly | QIODevice::Text))
118                     return;
119                 writeRouteXml(&routeTempFile, 0);
120                 routeTempFile.close();
121                 roundCounter ++;
122             }
123
124             //Points writing round.
125             else
126             {
127                 sLatitudeNow.sprintf("%.4f", latitude);  //Latitude now to string
128                 sLongitudeNow.sprintf("%.4f", longitude);//Longitude now to string
129                 sLatitudePrevious.sprintf("%.4f", latitudePrevious);  //Previous latitude to string
130                 sLongitudePrevious.sprintf("%.4f", longitudePrevious); //Previous longitude to string
131
132                 //If latitude or longitude change
133                 if ( sLatitudeNow != sLatitudePrevious || sLongitudeNow != sLongitudePrevious )
134                 {
135                     if (!routeTempFile.open(QIODevice::Append | QIODevice::Text))
136                         return;
137
138                     distance += location->distance_between_two_points(latitudePrevious, longitudePrevious, latitude, longitude);
139                     writeRouteXml(&routeTempFile, 0);
140                     roundCounter ++;
141                     routeTempFile.close();
142                 }
143             }
144         }
145     }
146 }
147
148 /**
149   *This slot function is called when gprs update location.
150   */
151 void GPSData::awcp()
152 {
153
154 }
155
156 /**
157   *This slot function is called when .
158   */
159 void GPSData::locationUpdated()
160 {
161
162 }
163
164 /**
165   *This slot function is called when .
166   */
167 void GPSData::gpsConnected()
168 {
169
170 }
171
172 /**
173   *This slot function is called when .
174   */
175 void GPSData::gpsDisconnected()
176 {
177
178 }
179
180 /**
181   *This slot function is called when .
182   */
183 void GPSData::gpsError()
184 {
185
186 }
187
188 /**
189   *This slot function is called when .
190   */
191 void GPSData::gpsdRunning()
192 {
193
194 }
195
196 /**
197   *This slot function is called when .
198   */
199 void GPSData::gpsdStopped()
200 {
201
202 }
203
204 /**
205   *This function start route recording.
206   *@param QString time recording start time.
207   */
208 void GPSData::startRouteRecording()
209 {
210     if (recordingStatus == false)
211     {
212         //Get start time and start recording.
213         gpsDateTime->setTime_t(location->getTime());
214         routeStartTime = gpsDateTime->toString("dd.MM.yyyy hh:mm:ss");
215         recordingStatus = true;
216         roundCounter = 0;
217     }
218 }
219
220 /**
221   *This function stop route recording.
222   *@param QString time recording stop time.
223   */
224 void GPSData::stopRouteRecording()
225 {
226     if (recordingStatus == true)
227     {
228         //Get stop time and stop recording.
229         gpsDateTime->setTime_t(location->getTime());
230         routeStopTime = gpsDateTime->toString("dd.MM.yyyy hh:mm:ss");
231         recordingStatus = false;
232
233         //Write final xml.
234         QFile file("route.xml");
235         if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
236             return;
237         writeRouteXml(&file, 1);
238         file.close();
239         roundCounter = 0;
240     }
241 }
242
243 /**
244   *This function write route to .xml file.
245   */
246 void GPSData::writeRouteXml(QIODevice *device, int round)
247 {
248     xmlwriter.setDevice(device);
249
250     //Write temp xml (routetemp.xml).
251     if ( round == 0 )
252     {
253         xmlwriter.writeStartElement("Point");
254         xmlwriter.writeAttribute("Latitude", QString::number(latitude));
255         xmlwriter.writeAttribute("Longitude", QString::number(longitude));
256         xmlwriter.writeAttribute("Altitude", QString::number(altitude));
257         xmlwriter.writeAttribute("Speed", QString::number(speed));
258         xmlwriter.writeEndElement();//Point
259     }
260
261     //Write final xml (route.xml).
262     else if ( round == 1 )
263     {
264         xmlwriter.writeStartDocument();
265         xmlwriter.writeStartElement("Route");
266         xmlwriter.writeAttribute("Start-time", routeStartTime);
267         xmlwriter.writeAttribute("Stop-time", routeStopTime);
268         xmlwriter.writeAttribute("Points", QString::number(roundCounter));
269
270         //Open temp xml and read points
271         QFile tempFile("routetemp.xml");
272         if (!tempFile.open(QIODevice::ReadOnly | QIODevice::Text))
273             return;
274         QTextStream readRoute(&tempFile);
275         QTextStream writeRoute(device);
276         writeRoute << readRoute.readLine();
277         tempFile.close();//Close temp xml
278
279         xmlwriter.writeEndElement();//Route
280         xmlwriter.writeEndDocument();     
281     }
282 }
283
284 /**
285   *This function returns distance traveled since recording started.
286   */
287 double GPSData::getDistanceTraveled()
288 {
289     return distance;
290 }