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