3879b54c7f635b06f614cb21e3c4e3ba3fb65c7e
[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         track = location->getTrack(); //Returns direction of motion in degrees(0-359).
109
110         QFile routeTempFile(".//speedfreak_route/routetemp.xml");//Temp xml.
111
112         //If GPS find 4 or more satellite and signal stregth is 30 or more.
113         if (location->getSatellitesInUse() >= 4 && location->getSignalStrength() >= 30)
114         {
115             //If first round
116             if (roundCounter == 0)
117             {
118                 if (!routeTempFile.open(QIODevice::WriteOnly | QIODevice::Text))
119                     return;
120                 writeRouteXml(&routeTempFile, 0);
121                 routeTempFile.close();
122                 roundCounter ++;
123             }
124
125             //Points writing round.
126             else
127             {
128                 sLatitudeNow.sprintf("%.4f", latitude);  //Latitude now to string
129                 sLongitudeNow.sprintf("%.4f", longitude);//Longitude now to string
130                 sLatitudePrevious.sprintf("%.4f", latitudePrevious);  //Previous latitude to string
131                 sLongitudePrevious.sprintf("%.4f", longitudePrevious); //Previous longitude to string
132
133                 //If latitude or longitude change
134                 if ( sLatitudeNow != sLatitudePrevious || sLongitudeNow != sLongitudePrevious )
135                 {
136                     if (!routeTempFile.open(QIODevice::Append | QIODevice::Text))
137                         return;
138
139                     distance += location->distance_between_two_points(latitudePrevious, longitudePrevious, latitude, longitude);
140                     writeRouteXml(&routeTempFile, 0);
141                     roundCounter ++;
142                     routeTempFile.close();
143                 }
144             }
145         }
146     }
147 }
148
149 /**
150   *This slot function is called when gprs update location.
151   */
152 void GPSData::awcp()
153 {
154
155 }
156
157 /**
158   *This slot function is called when .
159   */
160 void GPSData::locationUpdated()
161 {
162
163 }
164
165 /**
166   *This slot function is called when .
167   */
168 void GPSData::gpsConnected()
169 {
170
171 }
172
173 /**
174   *This slot function is called when .
175   */
176 void GPSData::gpsDisconnected()
177 {
178
179 }
180
181 /**
182   *This slot function is called when .
183   */
184 void GPSData::gpsError()
185 {
186
187 }
188
189 /**
190   *This slot function is called when .
191   */
192 void GPSData::gpsdRunning()
193 {
194
195 }
196
197 /**
198   *This slot function is called when .
199   */
200 void GPSData::gpsdStopped()
201 {
202
203 }
204
205 /**
206   *This function start route recording.
207   *@param QString time recording start time.
208   */
209 void GPSData::startRouteRecording()
210 {
211     if (recordingStatus == false)
212     {
213         //Get start time and start recording.
214         gpsDateTime->setTime_t(location->getTime());
215         routeStartTime = gpsDateTime->toString("dd.MM.yyyy hh:mm:ss");
216         recordingStatus = true;
217         roundCounter = 0;
218     }
219 }
220
221 /**
222   *This function stop route recording.
223   *@param QString time recording stop time.
224   */
225 void GPSData::stopRouteRecording()
226 {
227     if (recordingStatus == true)
228     {
229         //Get stop time and stop recording.
230         gpsDateTime->setTime_t(location->getTime());
231         routeStopTime = gpsDateTime->toString("dd.MM.yyyy hh:mm:ss");
232         recordingStatus = false;
233
234         //Write final xml.
235         QFile file(".//speedfreak_route/route.xml");
236         if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
237             return;
238         writeRouteXml(&file, 1);
239         file.close();
240         roundCounter = 0;
241     }
242 }
243
244 /**
245   *This function write route to .xml file.
246   */
247 void GPSData::writeRouteXml(QIODevice *device, int round)
248 {
249     xmlwriter.setDevice(device);
250
251     //Write temp xml (.//speedfreak_route/routetemp.xml).
252     if ( round == 0 )
253     {
254         xmlwriter.writeStartElement("Point");
255         xmlwriter.writeAttribute("Latitude", QString::number(latitude));
256         xmlwriter.writeAttribute("Longitude", QString::number(longitude));
257         xmlwriter.writeAttribute("Altitude", QString::number(altitude));
258         xmlwriter.writeAttribute("Speed", QString::number(speed));
259         xmlwriter.writeEndElement();//Point
260     }
261
262     //Write final xml (.//speedfreak_route/route.xml).
263     else if ( round == 1 )
264     {
265         xmlwriter.writeStartDocument();
266         xmlwriter.writeStartElement("Route");
267         xmlwriter.writeAttribute("Start-time", routeStartTime);
268         xmlwriter.writeAttribute("Stop-time", routeStopTime);
269         xmlwriter.writeAttribute("Points", QString::number(roundCounter));
270
271         //Open temp xml and read points
272         QFile tempFile(".//speedfreak_route/routetemp.xml");
273         if (!tempFile.open(QIODevice::ReadOnly | QIODevice::Text))
274             return;
275         QTextStream readRoute(&tempFile);
276         QTextStream writeRoute(device);
277         writeRoute << readRoute.readLine();
278         tempFile.close();//Close temp xml
279
280         xmlwriter.writeEndElement();//Route
281         xmlwriter.writeEndDocument();     
282     }
283 }
284
285 /**
286   *This function returns distance traveled since recording started.
287   */
288 double GPSData::getDistanceTraveled()
289 {
290     return distance;
291 }
292
293 /**
294   *This function returns direction of motion in degrees(0-359).
295   *@return double track
296   */
297 double GPSData::getDirection()
298 {
299     return track;
300 }