eca9efb693c1ce822dad9e04581a670ec0df27ae
[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 }
77
78 /**
79   *This slot function is called when GPS update location.
80   */
81 void GPSData::agnss()
82 {
83     //satellitesInUse   = location->getSatellitesInUse());  //Returns number of satellites in use.
84     //satellitesInView  = location->getSatellitesInView();  //Returns number of satellites in view.
85     //signalStrength    = location->getSignalStrength();    //Returns average signal strength of satellites which are in use.
86     //gpsOnline         = location->getGpsOnline();         //Returns gsp online
87     //ept               = location->getEpt();               //Returns time accuracy in seconds.
88     //eph               = location->getEph();               //Returns horizontal position accuracy in cm.
89     //track             = location->getTrack();             //Returns direction of motion in degrees(0-359).
90     //epd               = location->getEpd();               //Returns track accuracy in degrees.
91     //climb             = location->getClimb();             //Returns current rate of climb in m/s.
92     //epc               = location->getEpc();               //Returns climb accuracy in m/s.
93     //time              = location->getTime();              //Returns timestamp of the update in seconds.
94     //epv               = location->getEpv();               //Returns altitude accuracy in meters.
95     //eps               = location->getEps();               //Returns speed accuracy in km/h.
96     //distance          = location->distance_between_two_points(double latitude_s, double longitude_s, double latitude_f, double longitude_f);
97
98     //If route recording true
99     if ( recordingStatus == true )
100     {
101         latitudePrevious = latitude;
102         longitudePrevious = longitude;
103         latitude    = location->getLatitude();  //Returns latitude.
104         longitude   = location->getLongitude(); //Returns longitude.
105         altitude    = location->getAltitude();  //Returns fix altitude in meters.
106         speed       = location->getSpeed();     //Returns current speed in km/h.
107
108         QFile routeTempFile("routetemp.xml");//Temp xml.
109
110         //If first round
111         if (roundCounter == 0)
112         {
113             if (!routeTempFile.open(QIODevice::WriteOnly | QIODevice::Text))
114                 return;
115             writeRouteXml(&routeTempFile, 0);
116             routeTempFile.close();
117             roundCounter ++;
118         }
119
120         //Points writing round.
121         else
122         { 
123             sLatitudeNow.sprintf("%.4f", latitude);  //Latitude now to string
124             sLongitudeNow.sprintf("%.4f", longitude);//Longitude now to string
125             sLatitudePrevious.sprintf("%.4f", latitudePrevious);  //Previous latitude to string
126             sLongitudePrevious.sprintf("%.4f", longitudePrevious); //Previous longitude to string
127
128             //If latitude or longitude change
129             if ( sLatitudeNow != sLatitudePrevious || sLongitudeNow != sLongitudePrevious )
130             {
131                 if (!routeTempFile.open(QIODevice::Append | QIODevice::Text))
132                     return;
133
134                 writeRouteXml(&routeTempFile, 0);
135                 roundCounter ++;
136                 routeTempFile.close();
137             }
138         }
139     }
140 }
141
142 /**
143   *This slot function is called when gprs update location.
144   */
145 void GPSData::awcp()
146 {
147
148 }
149
150 /**
151   *This slot function is called when .
152   */
153 void GPSData::locationUpdated()
154 {
155
156 }
157
158 /**
159   *This slot function is called when .
160   */
161 void GPSData::gpsConnected()
162 {
163
164 }
165
166 /**
167   *This slot function is called when .
168   */
169 void GPSData::gpsDisconnected()
170 {
171
172 }
173
174 /**
175   *This slot function is called when .
176   */
177 void GPSData::gpsError()
178 {
179
180 }
181
182 /**
183   *This slot function is called when .
184   */
185 void GPSData::gpsdRunning()
186 {
187
188 }
189
190 /**
191   *This slot function is called when .
192   */
193 void GPSData::gpsdStopped()
194 {
195
196 }
197
198 /**
199   *This function start route recording.
200   *@param QString time recording start time.
201   */
202 void GPSData::startRouteRecording()
203 {
204     if (recordingStatus == false)
205     {
206         //Get start time and start recording.
207         gpsDateTime->setTime_t(location->getTime());
208         routeStartTime = gpsDateTime->toString("dd.MM.yyyy hh:mm:ss");
209         recordingStatus = true;
210         roundCounter = 0;
211     }
212 }
213
214 /**
215   *This function stop route recording.
216   *@param QString time recording stop time.
217   */
218 void GPSData::stopRouteRecording()
219 {
220     if (recordingStatus == true)
221     {
222         //Get stop time and stop recording.
223         gpsDateTime->setTime_t(location->getTime());
224         routeStopTime = gpsDateTime->toString("dd.MM.yyyy hh:mm:ss");
225         recordingStatus = false;
226
227         //Write final xml.
228         QFile file("route.xml");
229         if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
230             return;
231         writeRouteXml(&file, 1);
232         file.close();
233         roundCounter = 0;
234     }
235 }
236
237 /**
238   *This function write route to .xml file.
239   */
240 void GPSData::writeRouteXml(QIODevice *device, int round)
241 {
242     xmlwriter.setDevice(device);
243
244     //Write temp xml (routetemp.xml).
245     if ( round == 0 )
246     {
247         xmlwriter.writeStartElement("Point");
248         xmlwriter.writeAttribute("Latitude", QString::number(latitude));
249         xmlwriter.writeAttribute("Longitude", QString::number(longitude));
250         xmlwriter.writeAttribute("Altitude", QString::number(altitude));
251         xmlwriter.writeAttribute("Speed", QString::number(speed));
252         xmlwriter.writeEndElement();//Point
253     }
254
255     //Write final xml (route.xml).
256     else if ( round == 1 )
257     {
258         xmlwriter.writeStartDocument();
259         xmlwriter.writeStartElement("Route");
260         xmlwriter.writeAttribute("Start-time", routeStartTime);
261         xmlwriter.writeAttribute("Stop-time", routeStopTime);
262         xmlwriter.writeAttribute("Points", QString::number(roundCounter));
263
264         //Open temp xml and read points
265         QFile tempFile("routetemp.xml");
266         if (!tempFile.open(QIODevice::ReadOnly | QIODevice::Text))
267             return;
268         QTextStream readRoute(&tempFile);
269         QTextStream writeRoute(device);
270         writeRoute << readRoute.readLine();
271         tempFile.close();//Close temp xml
272
273         xmlwriter.writeEndElement();//Route
274         xmlwriter.writeEndDocument();     
275     }
276 }