2 * This file is part of jSpeed.
4 * jSpeed is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * jSpeed is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with jSpeed. If not, see <http://www.gnu.org/licenses/>.
19 #include <QtCore/QTime>
20 #include <QtCore/QDebug>
21 #include <QtCore/QTimer>
27 QString const TOTAL_FIELD = "odometer_total";
28 QString const TOTALTIME_FIELD = "odometer_totaltime";
29 QString const TRIP_FIELD = "odometer_trip";
30 QString const TRIPTIME_FIELD = "odometer_triptime";
31 QString const MAXSPEED_FIELD = "odometer_maxspeed";
32 QString const KM_UNIT = "km";
33 QString const MILE_UNIT = "mi";
34 QString const KM_SPEEDUNIT = "km/h";
35 QString const MILE_SPEEDUNIT = "mph";
36 double const DEFAULT_SPEED_TRESHOLD = 8.0;
37 double const MIN_SPEED_TRESHOLD = 0.9;
38 double const SPEED_IGNORE_LEVEL = 0.01;
41 Odometer::Odometer(): QObject(0), trip_(0), total_(0),
42 maxSpeed_(0), totalTime_(0), tripTime_(0), fixTimer_(0),
43 mainTimer_(0), emitUpdate_(true), location_(0), signalTimer_(0)
45 total_ = Settings::instance().value(TOTAL_FIELD, 0).toDouble();
46 totalTime_ = Settings::instance().value(TOTALTIME_FIELD, 0).toULongLong();
47 maxSpeed_ = Settings::instance().value(MAXSPEED_FIELD, 0).toDouble();
48 trip_ = Settings::instance().value(TRIP_FIELD, 0).toDouble();
49 tripTime_ = Settings::instance().value(TRIPTIME_FIELD, 0).toULongLong();
50 signalTimer_ = new QTimer(this);
51 signalTimer_->setSingleShot(false);
52 signalTimer_->setInterval(1000);
53 connect(signalTimer_, SIGNAL(timeout()), this, SIGNAL(timeUpdated()));
65 Odometer& Odometer::instance()
67 static Odometer instance;
71 void Odometer::start()
73 location_ = new Location;
74 connect(location_, SIGNAL(locationChanged(Location::Fix const&)),
75 this, SLOT(update(Location::Fix const&)));
85 void Odometer::update(Location::Fix const& fix)
87 if(fix.kmSpeed > maxSpeed_)
89 maxSpeed_ = fix.kmSpeed;
94 fixTimer_ = new QTime();
98 int elapsed = fixTimer_->elapsed();
100 fixTimer_->restart();
102 if(fix.kmSpeed > SPEED_IGNORE_LEVEL)
104 double treshold = DEFAULT_SPEED_TRESHOLD;
108 treshold = fix.eps * 0.23822 + 0.471204;
110 if(treshold < MIN_SPEED_TRESHOLD)
112 treshold = MIN_SPEED_TRESHOLD;
116 if(fix.kmSpeed > treshold && elapsed > 200 && elapsed < 8000)
118 double km = fix.kmSpeed * (static_cast<double>(elapsed) / (1000 * 3600));
137 if(latestFix_.kmSpeed > SPEED_IGNORE_LEVEL)
140 latestFix_.speed = 0.0;
141 latestFix_.kmSpeed = 0.0;
149 double Odometer::getTrip() const
151 return trip_ * Location::getUnitMultiplier();
154 double Odometer::getAverageSpeed() const
156 int elapsed = getTripTime();
163 return (static_cast<double>(trip_) * Location::getUnitMultiplier()) / (static_cast<double>(elapsed) / 60.0);
166 double Odometer::getTotal() const
168 return total_ * Location::getUnitMultiplier();
171 double Odometer::getMaxSpeed() const
173 return maxSpeed_ * Location::getUnitMultiplier();
176 qulonglong Odometer::getTotalTime() const
178 return totalTime_ + timeAddition();
181 qulonglong Odometer::getTripTime() const
183 return tripTime_ + timeAddition();
186 void Odometer::resetTrip()
200 void Odometer::resetTotal()
213 void Odometer::resetAll()
223 void Odometer::store()
225 Settings::instance().setValue(TOTAL_FIELD, total_);
226 Settings::instance().setValue(TOTALTIME_FIELD, getTotalTime());
227 Settings::instance().setValue(TRIP_FIELD, trip_);
228 Settings::instance().setValue(TRIPTIME_FIELD, getTripTime());
229 Settings::instance().setValue(MAXSPEED_FIELD, maxSpeed_);
230 Settings::instance().sync();
233 Location::Fix const& Odometer::getLatestFix() const
238 double Odometer::getSignalStrength() const
245 return location_->getSignalStrength();
248 QString const& Odometer::getUnit()
250 if(Location::getUnit() == Location::KM)
260 QString const& Odometer::getSpeedUnit()
262 if(Location::getUnit() == Location::KM)
268 return MILE_SPEEDUNIT;
272 void Odometer::updateUnit()
274 QString unit = Settings::instance().value("unit", "km").toString();
278 Location::setUnit(Location::KM);
280 else if(unit == "mile")
282 Location::setUnit(Location::MILE);
292 void Odometer::startTiming()
294 signalTimer_->start();
298 mainTimer_->restart();
302 mainTimer_ = new QTime();
304 signalTimer_->start();
307 void Odometer::endTiming()
309 signalTimer_->stop();
316 int elapsed = mainTimer_->elapsed() / 1000;
317 totalTime_ += elapsed;
318 tripTime_ += elapsed;
323 void Odometer::resetTiming()
334 int Odometer::timeAddition() const
338 return mainTimer_->elapsed() / 1000;