Renderer, ...: use PixelRect::GetCenter()
[xcsoar.git] / src / Tracking / TrackingGlue.cpp
blob7f8c74a481dc315b42d5eb22f5c82291dff82edf
1 /*
2 Copyright_License {
4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 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/MoreData.hpp"
26 #include "NMEA/Derived.hpp"
27 #include "Units/System.hpp"
28 #include "Util/Macros.hpp"
30 #ifdef HAVE_LIVETRACK24
32 static LiveTrack24::VehicleType
33 MapVehicleTypeToLifetrack24(TrackingSettings::VehicleType vt)
35 static constexpr LiveTrack24::VehicleType vehicleTypeMap[] = {
36 LiveTrack24::VehicleType::GLIDER,
37 LiveTrack24::VehicleType::PARAGLIDER,
38 LiveTrack24::VehicleType::POWERED_AIRCRAFT,
39 LiveTrack24::VehicleType::HOT_AIR_BALLOON
42 unsigned vti = (unsigned) vt;
43 if (vti >= ARRAY_SIZE(vehicleTypeMap))
44 vti = 0;
46 return vehicleTypeMap[vti];
49 #endif
51 TrackingGlue::TrackingGlue()
52 #ifdef HAVE_LIVETRACK24
53 :last_timestamp(0),
54 flying(false)
55 #endif
57 settings.SetDefaults();
58 #ifdef HAVE_LIVETRACK24
59 LiveTrack24::SetServer(settings.livetrack24.server);
60 #endif
62 #ifdef HAVE_SKYLINES_TRACKING_HANDLER
63 skylines.SetHandler(this);
64 #endif
67 #ifdef HAVE_LIVETRACK24
69 void
70 TrackingGlue::StopAsync()
72 ScopeLock protect(mutex);
73 StandbyThread::StopAsync();
76 void
77 TrackingGlue::WaitStopped()
79 ScopeLock protect(mutex);
80 StandbyThread::WaitStopped();
83 #endif
85 void
86 TrackingGlue::SetSettings(const TrackingSettings &_settings)
88 #ifdef HAVE_SKYLINES_TRACKING
89 skylines.SetSettings(_settings.skylines);
90 #endif
92 #ifdef HAVE_LIVETRACK24
93 if (_settings.livetrack24.server != settings.livetrack24.server ||
94 _settings.livetrack24.username != settings.livetrack24.username ||
95 _settings.livetrack24.password != settings.livetrack24.password) {
96 /* wait for the current job to finish */
97 LockWaitDone();
99 /* now it's safe to access these variables without a lock */
100 settings = _settings;
101 state.ResetSession();
102 LiveTrack24::SetServer(_settings.livetrack24.server);
103 } else {
104 /* no fundamental setting changes; the write needs to be protected
105 by the mutex, because another job may be running already */
106 ScopeLock protect(mutex);
107 settings = _settings;
109 #endif
112 void
113 TrackingGlue::OnTimer(const MoreData &basic, const DerivedInfo &calculated)
115 #ifdef HAVE_SKYLINES_TRACKING
116 skylines.Tick(basic);
117 #endif
119 #ifdef HAVE_LIVETRACK24
120 if (!settings.livetrack24.enabled)
121 /* disabled by configuration */
122 /* note that we are allowed to read "settings" without locking the
123 mutex, because the background thread never writes to this
124 attribute */
125 return;
127 if (!basic.time_available || !basic.location_available)
128 /* can't track without a valid GPS fix */
129 return;
131 if (!clock.CheckUpdate(settings.interval * 1000))
132 /* later */
133 return;
135 ScopeLock protect(mutex);
136 if (IsBusy())
137 /* still running, skip this submission */
138 return;
140 date_time = basic.date_time_utc;
141 if (!basic.date_available)
142 /* use "today" if the GPS didn't provide a date */
143 (BrokenDate &)date_time = BrokenDate::TodayUTC();
145 location = basic.location;
146 /* XXX use nav_altitude? */
147 altitude = basic.NavAltitudeAvailable() && positive(basic.nav_altitude)
148 ? (unsigned)basic.nav_altitude
149 : 0u;
150 ground_speed = basic.ground_speed_available
151 ? (unsigned)Units::ToUserUnit(basic.ground_speed, Unit::KILOMETER_PER_HOUR)
152 : 0u;
153 track = basic.track_available
154 ? basic.track
155 : Angle::Zero();
157 last_flying = flying;
158 flying = calculated.flight.flying;
160 Trigger();
161 #endif
164 #ifdef HAVE_LIVETRACK24
166 void
167 TrackingGlue::Tick()
169 if (!settings.livetrack24.enabled)
170 /* settings have been cleared meanwhile, bail out */
171 return;
173 unsigned tracking_interval = settings.interval;
174 LiveTrack24Settings copy = this->settings.livetrack24;
176 mutex.Unlock();
178 if (!flying) {
179 if (last_flying && state.HasSession()) {
180 /* landing: end tracking session */
181 LiveTrack24::EndTracking(state.session_id, state.packet_id);
182 state.ResetSession();
183 last_timestamp = 0;
186 /* don't track if not flying */
187 mutex.Lock();
188 return;
191 const int64_t current_timestamp = date_time.ToUnixTimeUTC();
193 if (state.HasSession() && current_timestamp + 60 < last_timestamp) {
194 /* time warp: create a new session */
195 LiveTrack24::EndTracking(state.session_id, state.packet_id);
196 state.ResetSession();
199 last_timestamp = current_timestamp;
201 if (!state.HasSession()) {
202 LiveTrack24::UserID user_id = 0;
203 if (!copy.username.empty() && !copy.password.empty())
204 user_id = LiveTrack24::GetUserID(copy.username, copy.password);
206 if (user_id == 0) {
207 copy.username.clear();
208 copy.password.clear();
209 state.session_id = LiveTrack24::GenerateSessionID();
210 } else {
211 state.session_id = LiveTrack24::GenerateSessionID(user_id);
214 if (!LiveTrack24::StartTracking(state.session_id, copy.username,
215 copy.password, tracking_interval,
216 MapVehicleTypeToLifetrack24(settings.vehicleType))) {
217 state.ResetSession();
218 mutex.Lock();
219 return;
222 state.packet_id = 2;
225 LiveTrack24::SendPosition(state.session_id, state.packet_id++,
226 location, altitude, ground_speed, track,
227 current_timestamp);
229 mutex.Lock();
232 #endif
234 #ifdef HAVE_SKYLINES_TRACKING_HANDLER
236 void
237 TrackingGlue::OnTraffic(uint32_t pilot_id, unsigned time_of_day_ms,
238 const GeoPoint &location, int altitude)
240 bool user_known;
243 const ScopeLock protect(skylines_data.mutex);
244 const SkyLinesTracking::Data::Traffic traffic(time_of_day_ms,
245 location, altitude);
246 skylines_data.traffic[pilot_id] = traffic;
248 user_known = skylines_data.IsUserKnown(pilot_id);
251 if (!user_known)
252 /* we don't know this user's name yet - try to find it out by
253 asking the server */
254 skylines.RequestUserName(pilot_id);
257 void
258 TrackingGlue::OnUserName(uint32_t user_id, const TCHAR *name)
260 const ScopeLock protect(skylines_data.mutex);
261 skylines_data.user_names[user_id] = name;
264 #endif