po: update French translation
[xcsoar.git] / src / Tracking / TrackingGlue.cpp
blob8cf3d3777ee9b5659897bcda08be133e90a6eed2
1 /*
2 Copyright_License {
4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2012 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "TrackingGlue.hpp"
25 #include "NMEA/Info.hpp"
26 #include "NMEA/Derived.hpp"
27 #include "Units/System.hpp"
29 TrackingGlue::TrackingGlue()
31 settings.SetDefaults();
32 LiveTrack24::SetServer(settings.livetrack24.server);
35 void
36 TrackingGlue::StopAsync()
38 ScopeLock protect(mutex);
39 StandbyThread::StopAsync();
42 void
43 TrackingGlue::WaitStopped()
45 ScopeLock protect(mutex);
46 StandbyThread::WaitStopped();
49 void
50 TrackingGlue::SetSettings(const TrackingSettings &_settings)
52 ScopeLock protect(mutex);
54 if (_settings.livetrack24.server != settings.livetrack24.server) {
55 state.ResetSession();
56 LiveTrack24::SetServer(_settings.livetrack24.server);
59 if (_settings.livetrack24.username != settings.livetrack24.username ||
60 _settings.livetrack24.password != settings.livetrack24.password)
61 state.ResetSession();
63 settings = _settings;
66 void
67 TrackingGlue::OnTimer(const NMEAInfo &basic, const DerivedInfo &calculated)
69 if (!settings.livetrack24.enabled)
70 /* disabled by configuration */
71 /* note that we are allowed to read "settings" without locking the
72 mutex, because the background thread never writes to this
73 attribute */
74 return;
76 if (!basic.time_available || !basic.location_available)
77 /* can't track without a valid GPS fix */
78 return;
80 if (!clock.CheckUpdate(settings.interval * 1000))
81 /* later */
82 return;
84 ScopeLock protect(mutex);
85 if (IsBusy())
86 /* still running, skip this submission */
87 return;
89 date_time = basic.date_time_utc;
90 if (!basic.date_available)
91 /* use "today" if the GPS didn't provide a date */
92 (BrokenDate &)date_time = (BrokenDate)BrokenDateTime::NowUTC();
94 location = basic.location;
95 /* XXX use nav_altitude? */
96 altitude = basic.gps_altitude_available && positive(basic.gps_altitude)
97 ? (unsigned)basic.gps_altitude
98 : 0u;
99 ground_speed = basic.ground_speed_available
100 ? (unsigned)Units::ToUserUnit(basic.ground_speed, Unit::KILOMETER_PER_HOUR)
101 : 0u;
102 track = basic.track_available
103 ? basic.track
104 : Angle::Zero();
106 flying = calculated.flight.flying;
108 Trigger();
111 void
112 TrackingGlue::Tick()
114 if (!settings.livetrack24.enabled)
115 /* settings have been cleared meanwhile, bail out */
116 return;
118 unsigned tracking_interval = settings.interval;
119 LiveTrack24Settings copy = this->settings.livetrack24;
121 mutex.Unlock();
123 if (!state.HasSession()) {
124 LiveTrack24::UserID user_id = 0;
125 if (!copy.username.empty() && !copy.password.empty())
126 user_id = LiveTrack24::GetUserID(copy.username, copy.password);
128 if (user_id == 0) {
129 copy.username.clear();
130 copy.password.clear();
131 state.session_id = LiveTrack24::GenerateSessionID();
132 } else {
133 state.session_id = LiveTrack24::GenerateSessionID(user_id);
136 if (!LiveTrack24::StartTracking(state.session_id, copy.username,
137 copy.password, tracking_interval,
138 LiveTrack24::VehicleType::GLIDER)) {
139 state.ResetSession();
140 mutex.Lock();
141 return;
144 state.packet_id = 2;
147 LiveTrack24::SendPosition(state.session_id, state.packet_id++,
148 location, altitude, ground_speed, track,
149 date_time.ToUnixTimeUTC());
151 mutex.Lock();