-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgimporterApp.mc
395 lines (334 loc) · 12 KB
/
gimporterApp.mc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
using Toybox.Application as App;
using Toybox.WatchUi as Ui;
using Toybox.Communications as Comm;
using Toybox.PersistedContent as PC;
using Toybox.Timer as Timer;
using Toybox.System as System;
class gimporterApp extends App.AppBase {
var tracks;
var trackToStart;
var canLoadList;
var status;
var mGPXorFIT;
var bluetoothTimer;
var mIntent;
var exitTimer;
function initialize() {
AppBase.initialize();
tracks = null;
canLoadList = true;
status = "";
mGPXorFIT = Ui.loadResource(Rez.Strings.GPXorFIT);
System.println("GPXorFit = " + mGPXorFIT);
bluetoothTimer = new Timer.Timer();
exitTimer = new Timer.Timer();
mIntent = null;
}
// onStart() is called on application start up
function onStart(state) {
//loadTrackList();
status = Rez.Strings.PressStart;
}
// onStop() is called when your application is exiting
function onStop(state) {
}
// Return the initial view of your application here
function getInitialView() {
return [ new gimporterView(), new gimporterDelegate() ];
}
function getStatus() {
return status;
}
function getTracks() {
return tracks;
}
function loadTrackList() {
tracks = null;
trackToStart = null;
mIntent = null;
var settings = System.getDeviceSettings();
if (! settings.phoneConnected) {
bluetoothTimer.stop();
status = Rez.Strings.WaitingForBluetooth;
bluetoothTimer.start(method(:loadTrackList), 1000, false);
Ui.requestUpdate();
return;
}
if ((settings has :connectionInfo) && (settings.connectionInfo has :wifi) && (settings.connectionInfo[:wifi].state == CONNECTION_STATE_CONNECTED)) {
bluetoothTimer.stop();
status = Rez.Strings.SwitchOffWifi;
bluetoothTimer.start(method(:loadTrackList), 1000, false);
Ui.requestUpdate();
return;
}
/*
if ((settings has :connectionInfo) || !(settings.connectionInfo has :bluetooth) || (settings.connectionInfo[:bluetooth].state != CONNECTION_STATE_CONNECTED)) {
bluetoothTimer.stop();
status = Rez.Strings.WaitingForBluetooth;
bluetoothTimer.start(method(:loadTrackList), 1000, false);
Ui.requestUpdate();
return;
}
*/
status = Rez.Strings.GettingTracklist;
canLoadList = false;
try {
Comm.makeWebRequest("http://127.0.0.1:22222/dir.json", { "type" => mGPXorFIT, "short" => "1", "longname" => "1" },
{
:method => Comm.HTTP_REQUEST_METHOD_GET,
:headers => {
"Content-Type" => Comm.REQUEST_CONTENT_TYPE_JSON
},
:responseType => Comm.HTTP_RESPONSE_CONTENT_TYPE_JSON
}, method(:onReceiveTracks)
);
} catch( ex ) {
canLoadList = true;
status = ex.getErrorMessage();
}
Ui.requestUpdate();
}
function onReceiveTracks(responseCode, data) {
status = "";
canLoadList = true;
if (responseCode == Comm.BLE_CONNECTION_UNAVAILABLE) {
System.println("Bluetooth disconnected");
status = Rez.Strings.BluetoothDisconnected;
Ui.requestUpdate();
return;
}
if (responseCode != 200) {
System.println("data == null\nCode " + responseCode.toString() + "\n");
status = Rez.Strings.ConnectionFailed;
Ui.requestUpdate();
return;
}
if (!(data instanceof Toybox.Lang.Dictionary)) {
System.println("data is not Dict");
status = Rez.Strings.ConnectionFailed;
Ui.requestUpdate();
return;
}
if (! data.hasKey("tracks")) {
System.println("data has no track key");
status = Rez.Strings.ConnectionFailed;
Ui.requestUpdate();
return;
}
tracks = data["tracks"];
if (tracks == null) {
System.println("tracks == null");
status = Rez.Strings.NoTracks;
Ui.requestUpdate();
return;
}
if (!(tracks instanceof Toybox.Lang.Array)) {
System.println("tracks != Array");
status = Rez.Strings.NoTracks;
tracks = null;
Ui.requestUpdate();
return;
}
Ui.pushView(new TrackChooser(0), new TrackChooserDelegate(0), Ui.SLIDE_IMMEDIATE);
}
function loadTrackNum(index) {
System.println("loadTrack: " + tracks[index].toString());
// TODO: check hasKey
var trackurl = tracks[index]["url"];
trackToStart = tracks[index]["title"];
if ((trackurl.length() < 7) || (!trackurl.substring(0, 7).equals("http://"))) {
trackurl = "http://127.0.0.1:22222/" + trackurl;
}
status = Rez.Strings.Downloading;
canLoadList = false;
System.println("GPXorFIT: " + mGPXorFIT);
Ui.pushView(new gimporterView(), new gimporterDelegate(), Ui.SLIDE_IMMEDIATE);
Ui.requestUpdate();
try {
if (mGPXorFIT.equals("FIT")) {
System.println("Downloading FIT");
Comm.makeWebRequest(trackurl, { "type" => "FIT", "longname" => "1" }, {:method => Comm.HTTP_REQUEST_METHOD_GET,:responseType => Comm.HTTP_RESPONSE_CONTENT_TYPE_FIT}, method(:onReceiveTrack) );
} else {
System.println("Downloading GPX");
Comm.makeWebRequest(trackurl, { "type" => "GPX", "longname" => "1" }, {:method => Comm.HTTP_REQUEST_METHOD_GET,:responseType => Comm.HTTP_RESPONSE_CONTENT_TYPE_GPX}, method(:onReceiveTrack) );
}
} catch( ex ) {
status = Rez.Strings.DownloadNotSupported;
}
}
function doExitInto() {
if (mIntent != null) {
System.exitTo(mIntent);
mIntent = null;
}
}
function exitInto(intent) {
if (intent != null) {
mIntent = intent;
exitTimer.start(method(:doExitInto), 200, false);
}
}
function onReceiveTrack(responseCode, downloads) {
System.println("onReceiveTrack");
if (responseCode == Comm.BLE_CONNECTION_UNAVAILABLE) {
System.println("Bluetooth disconnected");
status = Rez.Strings.BluetoothDisconnected;
Ui.requestUpdate();
return;
}
else if (responseCode != 200) {
System.println("Code: " + responseCode);
status = Rez.Strings.DownloadFailed;
Ui.requestUpdate();
return;
}
else if (downloads == null) {
System.println("downloads == null");
status = Rez.Strings.DownloadFailed;
Ui.requestUpdate();
return;
}
else {
var download = downloads.next();
System.println("onReceiveTrack: " + (download == null ? null : download.getName() + "/" + download.getId()));
// FIXME: Garmin
// Without switchToView() the widget is gone
Ui.switchToView(new gimporterView(), new gimporterDelegate(), Ui.SLIDE_IMMEDIATE);
status = Rez.Strings.DownloadComplete;
if (download != null) {
Ui.requestUpdate();
exitInto(download.toIntent());
return;
}
status = Rez.Strings.AlreadyDownloaded;
/*
if (trackToStart.length() > 4) {
var postfix = trackToStart.substring(trackToStart.length()-4, trackToStart.length()).toLower();
if (postfix.equals(".fit") || postfix.equals(".gpx")) {
trackToStart = trackToStart.substring(0, trackToStart.length()-4);
}
}
if (trackToStart.length() > 15) {
trackToStart = trackToStart.substring(0, 15);
}
*/
var ret = false;
if (PC has :getAppCourses) {
System.println("Searching in App courses");
ret = searchCourse(PC.getAppCourses());
}
if (PC has :getCourses) {
System.println("Searching in courses");
ret = searchCourse(PC.getCourses());
}
if ((ret == false) && (PC has :getAppTracks)) {
System.println("Searching in App tracks");
ret = searchCourse(PC.getTracks());
}
if ((ret == false) && (PC has :getTracks)) {
System.println("Searching in tracks");
ret = searchCourse(PC.getTracks());
}
if ((ret == false ) && (PC has :getAppRoutes)) {
System.println("Searching in App routes");
ret = searchCourse(PC.getAppRoutes());
}
if ((ret == false ) && (PC has :getRoutes)) {
System.println("Searching in routes");
ret = searchCourse(PC.getRoutes());
}
Ui.requestUpdate();
return;
}
}
function searchCourse(cit) {
var course;
var startcourse = null;
var startcoursename = null;
var sclen = 0;
var tlen = trackToStart.length();
// Search for the longest coursename matching ours
while (cit != null) {
course = cit.next();
if (course == null) {
break;
}
var coursename = course.getName();
var clen = coursename.length();
if ((clen > 11) && coursename.substring(clen-11, clen).equals("_course.fit")) {
coursename = coursename.substring(0, clen-11);
clen = clen - 11;
}
// Shortened course names end with 60b2 on Fenix 5
System.println("Checking if " + trackToStart + " == " + coursename);
if ((clen > tlen) || (sclen > clen) || (!trackToStart.substring(0, clen).equals(coursename))) {
continue;
}
startcourse = course;
startcoursename = coursename;
sclen = clen;
}
if (startcourse != null) {
System.println("Found course: " + startcourse.getName() + " asking for start");
Ui.popView(Ui.SLIDE_IMMEDIATE);
canLoadList = true;
status = Rez.Strings.PressStart;
// FIXME: Garmin
// I can't do System.exitTo(course.toIntent())
// It causes the Fenix5 to be in a strange state
exitInto(startcourse.toIntent()); // workaround
return true;
} else {
System.println("No course found");
}
return false;
}
}
class gimporterView extends Ui.View {
var st;
var ps;
var app;
function initialize() {
View.initialize();
app = App.getApp();
}
function onLayout(dc) {
setLayout(Rez.Layouts.MainLayout(dc));
st = findDrawableById("status");
ps = Ui.loadResource(Rez.Strings.PressStart);
}
function onUpdate(dc) {
var status = app.getStatus();
if (status.equals("")) {
st.setText(ps);
} else {
st.setText(status);
}
View.onUpdate(dc);
}
}
class gimporterDelegate extends Ui.BehaviorDelegate {
var app;
function initialize() {
BehaviorDelegate.initialize();
app = App.getApp();
}
function onBack() {
app.canLoadList = true;
app.status = Rez.Strings.PressStart;
Ui.requestUpdate();
return false;
}
function onMenu() {
if (app.canLoadList) {
app.loadTrackList();
}
return true;
}
function onSelect() {
if (app.canLoadList) {
app.loadTrackList();
}
return true;
}
}