Added button for sending route to server.
[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     gpsTimer = new QTimer();
28     gpsTimeMS = 0;
29     connect(gpsTimer, SIGNAL(timeout()),this, SLOT(gpsTimerTimeout()));
30
31     resetAll();
32 }
33
34 /**
35   *Destructor of this class. Deletes all dynamic objects and sets them to NULL.
36   */
37 GPSData::~GPSData()
38 {
39     delete location;
40     location = NULL;
41 }
42
43 void GPSData::resetAll()
44 {
45     satellitesInUse = 0;
46     satellitesInView = 0;
47     signalStrength = 0;
48     latitude = 0;
49     longitude = 0;
50     time = 0;
51     ept = 0;
52     eph = 0;
53     altitude = 0;
54     epv = 0;
55     track = 0;
56     epd = 0;
57     speed = 0;
58     eps = 0;
59     climb = 0;
60     epc = 0;
61
62     recordingStatus = false;
63     roundCounter = 0;
64 }
65
66 /**
67   *This slot function is called when GPS update location.
68   */
69 void GPSData::agnss()
70 {
71     //satellitesInUse = QString::number(location->getSatellitesInUse());  //Returns number of satellites in use.
72     //satellitesInView = QString::number(location->getSatellitesInView());//Returns number of satellites in view.
73     //signalStrength = QString::number(location->getSignalStrength());    //Returns average signal strength of satellites which are in use.
74     //gpsOnline = QString::number(location->getGpsOnline());              //Returns gsp online                     
75     //ept = QString::number(location->getEpt());                          //Returns time accuracy in seconds.
76     //eph = QString::number(location->getEph());                          //Returns horizontal position accuracy in cm.
77     //track = QString::number(location->getTrack());                      //Returns direction of motion in degrees(0-359).
78     //epd = QString::number(location->getEpd());                          //Returns track accuracy in degrees.
79     //climb = QString::number(location->getClimb());                      //Returns current rate of climb in m/s.
80     //epc = QString::number(location->getEpc());                          //Returns climb accuracy in m/s.
81     //location->distance_between_two_points(double latitude_s, double longitude_s, double latitude_f, double longitude_f);
82     //time = location->getTime();//Returns timestamp of the update in seconds.
83
84     //If route recording true
85     if ( recordingStatus == true )
86     {
87         latitude    = location->getLatitude();  //Returns latitude.
88         longitude   = location->getLongitude(); //Returns longitude.
89         altitude    = location->getAltitude();  //Returns fix altitude in meters.
90         epv         = location->getEpv();       //Returns altitude accuracy in meters.
91         speed       = location->getSpeed();     //Returns current speed in km/h.
92         eps         = location->getEps();       //Returns speed accuracy in km/h.
93
94         gpsTimer->start(1);
95
96         //If first round
97         if (roundCounter == 0)
98         {
99             saveRoute();
100         }
101
102         else
103         { 
104             latitudeNow.sprintf("%.4f", latitude);  //Latitude now to string
105             longitudeNow.sprintf("%.4f", longitude);//Longitude now to string
106             latitudePrevious.sprintf("%.4f", gpsDataArray[0]);  //Previous latitude to string
107             longitudePrevious.sprintf("%.4f", gpsDataArray[1]); //Previous longitude to string
108
109             //If latitude or longitude change
110             if ( latitudeNow != latitudePrevious || longitudeNow != longitudePrevious )
111             {
112                 saveRoute();
113             }
114         }
115     }
116 }
117
118 /**
119   *This slot function is called when gprs update location.
120   */
121 void GPSData::awcp()
122 {
123
124 }
125
126 /**
127   *This slot function is called when .
128   */
129 void GPSData::locationUpdated()
130 {
131
132 }
133
134 /**
135   *This slot function is called when .
136   */
137 void GPSData::gpsConnected()
138 {
139
140 }
141
142 /**
143   *This slot function is called when .
144   */
145 void GPSData::gpsDisconnected()
146 {
147
148 }
149
150 /**
151   *This slot function is called when .
152   */
153 void GPSData::gpsError()
154 {
155
156 }
157
158 /**
159   *This slot function is called when .
160   */
161 void GPSData::gpsdRunning()
162 {
163
164 }
165
166 /**
167   *This slot function is called when .
168   */
169 void GPSData::gpsdStopped()
170 {
171
172 }
173
174 /**
175   *This function start route recording.
176   *@param QString time recording start time.
177   */
178 void GPSData::startRouteRecording(QString time)
179 {
180     if (recordingStatus == false)
181     {
182         routeStartTime = time;
183         recordingStatus = true;
184         roundCounter = 0;
185     }
186 }
187
188 /**
189   *This function stop route recording.
190   *@param QString time recording stop time.
191   */
192 void GPSData::stopRouteRecording(QString time)
193 {
194     if (recordingStatus == true)
195     {
196         routeStopTime = time;
197         recordingStatus = false;
198         roundCounter = 0;
199         saveRoute();
200     }
201 }
202
203 /**
204   *This slot function is called when gps timer timeout(10s).
205   */
206 void GPSData::gpsTimerTimeout()
207 {
208     gpsTimeMS++;
209 }
210
211 /**
212   *This function save route to .txt file.
213   */
214 void GPSData::saveRoute()
215 {
216     QFile file("route" + routeStartTime + ".txt");
217     QTextStream route(&file);
218
219     if ( recordingStatus == true )
220     {
221         //First round.
222         if ( roundCounter == 0 )
223         {
224             if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
225                 return;
226
227             route << "Start: " << routeStartTime << "\n";
228         }
229
230         else
231         {
232             if (!file.open(QIODevice::Append | QIODevice::Text))
233                 return;
234         }
235
236         gpsDataArray[0] = latitude;
237         gpsDataArray[1] = longitude;
238         gpsDataArray[2] = altitude;
239         gpsDataArray[3] = speed;
240         roundCounter ++;
241
242         route << " la: " << latitude
243               << " \t lo: " << longitude
244               << " \t al: " << altitude
245               << " \t epv: " << epv
246               << " \t sp: " << speed
247               << " \t eps: " << eps
248               << " \t ms: " << gpsTimeMS
249               << "\n";
250
251         gpsTimeMS = 0;
252         file.close();
253     }
254
255     //Final round.
256     else
257     {
258         if (!file.open(QIODevice::Append | QIODevice::Text))
259             return;
260         route << "Stop: " << routeStopTime << "\n";
261         file.close();
262     }
263 }
264
265
266 /**
267   *@return RoundCounter, the number of gpsDataArray[][] rows.
268   */
269 int GPSData::getRoundCounter()
270 {
271     return roundCounter;
272 }