-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
577 lines (501 loc) · 22 KB
/
index.js
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
import React, {Component} from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Image,
Keyboard
} from 'react-native';
import {Navigation, NativeEventsReceiver} from 'react-native-navigation';
import BackgroundGeolocation from "react-native-background-geolocation";
import firebase from 'react-native-firebase';
import moment from 'moment';
import agent from './app/Components/helpers/agent';
import {Provider} from 'react-redux';
import store from './app/store.js';
import {bootstrap} from './app/Components/config/bootstrap';
import {registerScreens} from './screens';
import {NativeModules} from 'react-native';
import realm from './app/Components/helpers/realm';
import helperFunctions from './app/Components/helpers/helperFunctions';
const TRACKER_HOST_TWO = 'https://us-central1-journeyapp91.cloudfunctions.net/graphql/locationUpdate';
bootstrap();
registerScreens(store, Provider);
Navigation.isAppLaunched()
.then(appLaunched => {
if (appLaunched) {
console.log('App is launched -> show UI');
startApp(); // App is launched -> show UI
}
console.log('App hasn\'t been launched yet -> show the UI only when needed.');
new NativeEventsReceiver().appLaunched(startApp); // App hasn't been launched yet -> show the UI only when needed.
});
firebase.auth().onAuthStateChanged((user) => {
if (user) {
console.log('User Logged In - Check for FCM Token');
console.log(user._auth._user.uid);
firebase.messaging().getToken()
.then(fcmToken => {
if (fcmToken) {
// user has a device token
console.log('This is the token: ' + fcmToken);
firebase.firestore().collection('users').doc(user._auth._user.uid).update({fcm_token: fcmToken});
} else {
// user doesn't have a device token yet
console.log('user does not have a token yet')
}
});
} else {
console.log('No user logged in')
}
});
firebase.messaging().hasPermission()
.then(enabled => {
if (enabled) {
// user has permissions
console.log('The user has Permissions');
} else {
// user doesn't have permission
console.log('The user does not have Permissions');
firebase.messaging().requestPermission()
.then(() => {
// User has authorised
console.log('The User has authorized google cloud messaging');
})
.catch(error => {
// User has rejected permissions
console.log('The User has not authorized google cloud messaging');
});
}
});
function startApp() {
Navigation.startTabBasedApp({
tabs: [
// {
// label: 'Login',
// screen: 'login', // this is a registered name for a screen
// icon: require('./app/Assets/icons/visaIcon.png'),
// /*
// selectedIcon: require('../img/one_selected.png'), // iOS only
// */
// title: 'Screen One',
// tabBarHidden: true
// },
// {
// label: 'One',
// screen: 'quotePage', // this is a registered name for a screen
// icon: require('./app/Assets/icons/visaIcon.png'),
// /*
// selectedIcon: require('../img/one_selected.png'), // iOS only
// */
// title: 'Screen One',
// tabBarHidden: true
// },
//
// {
// label: 'two',
// screen: 'neighbors', // this is a registered name for a screen
// icon: require('./app/Assets/icons/visaIcon.png'),
// /*
// selectedIcon: require('../img/one_selected.png'), // iOS only
// */
// title: 'neighborhood Detected',
// tabBarHidden: true,
// overrideBackPress: true
// },
// {
// label: 'three',
// screen: 'locationCommunity', // this is a registered name for a screen
// icon: require('./app/Assets/icons/visaIcon.png'),
// /*
// selectedIcon: require('../img/one_selected.png'), // iOS only
// */
// title: 'location',
// tabBarHidden: true,
// overrideBackPress: true
// },
// {
// label: 'two',
// screen: 'locations', // this is a registered name for a screen
// icon: require('./app/Assets/icons/visaIcon.png'),
// /*
// selectedIcon: require('../img/one_selected.png'), // iOS only
// */
// title: 'neighborhood Detected',
// tabBarHidden: false,
// },
// {
// label: 'two',
// screen: 'liveStreams', // this is a registered name for a screen
// icon: require('./app/Assets/icons/visaIcon.png'),
// /*
// selectedIcon: require('../img/one_selected.png'), // iOS only
// */
// title: 'neighborhood Detected',
// tabBarHidden: false,
// passProps: {sessionId: 'balls'}
// },
// {
// label: 'two',
// screen: 'streamer', // this is a registered name for a screen
// icon: require('./app/Assets/icons/visaIcon.png'),
// /*
// selectedIcon: require('../img/one_selected.png'), // iOS only
// */
// title: 'neighborhood Detected',
// tabBarHidden: false,
// }
// {
// label: 'one?',
// screen: 'mainActions', // this is a registered name for a screen
// icon: require('./app/Assets/icons/americanExpressIcon.png'),
// /*
// selectedIcon: require('../img/one_selected.png'), // iOS only
// */
// title: 'mainActions Detected',
// tabBarHidden: false,
// },
// {
// label: 'BumpConfirmation',
// screen: 'bumpConfirmation', // this is a registered name for a screen
// icon: require('./app/Assets/icons/americanExpressIcon.png'),
// /*
// selectedIcon: require('../img/one_selected.png'), // iOS only
// */
// title: 'mainActions Detected',
// tabBarHidden: false,
// },
//
// {
// label: 'bump',
// screen: 'bump', // this is a registered name for a screen
// icon: require('./app/Assets/icons/visaIcon.png'),
// /*
// selectedIcon: require('../img/one_selected.png'), // iOS only
// */
// title: 'Bump',
// tabBarHidden: false,
// },
// {
// label: 'UrlShare',
// screen: 'UrlShare', // this is a registered name for a screen
// icon: require('./app/Assets/icons/visaIcon.png'),
// /*
// selectedIcon: require('../img/one_selected.png'), // iOS only
// */
// title: 'Bump',
// tabBarHidden: false,
// },
// {
// label: 'JourneyView',
// screen: 'JourneyView', // this is a registered name for a screen
// icon: require('./app/Assets/icons/visaIcon.png'),
// /*
// selectedIcon: require('../img/one_selected.png'), // iOS only
// */
// title: 'JourneyView',
// tabBarHidden: false,
// },
// {
// label: 'Timeline',
// screen: 'Timeline', // this is a registered name for a screen
// icon: require('./app/Assets/icons/visaIcon.png'),
// /*
// selectedIcon: require('../img/one_selected.png'), // iOS only
// */
// title: 'Timeline',
// tabBarHidden: false,
// },
// {
// label: 'LiveStoryView',
// screen: 'LiveStoryView', // this is a registered name for a screen
// icon: require('./app/Assets/icons/visaIcon.png'),
// /*
// selectedIcon: require('../img/one_selected.png'), // iOS only
// */
// title: 'LiveStoryView',
// tabBarHidden: false,
// },
// {
// label: 'LiveStoryMapView',
// screen: 'LiveStoryMapView', // this is a registered name for a screen
// icon: require('./app/Assets/icons/visaIcon.png'),
// /*
// selectedIcon: require('../img/one_selected.png'), // iOS only
// */
// title: 'LiveStoryView',
// tabBarHidden: false,
// },
// {
// label: 'JourneyPicker',
// screen: 'JourneyPicker', // this is a registered name for a screen
// icon: require('./app/Assets/icons/visaIcon.png'),
// /*
// selectedIcon: require('../img/one_selected.png'), // iOS only
// */
// title: 'JourneyPicker',
// tabBarHidden: false,
// },
// {
// label: 'individualTimeline',
// screen: 'IndividualTimeline', // this is a registered name for a screen
// icon: require('./app/Assets/icons/visaIcon.png'),
// /*
// selectedIcon: require('../img/one_selected.png'), // iOS only
// */
// title: 'individualtimeline',
// tabBarHidden: false,
// },
//
// {
// label: 'two',
// screen: 'personalJourneyStart', // this is a registered name for a screen
// icon: require('./app/Assets/icons/visaIcon.png'),
// /*
// selectedIcon: require('../img/one_selected.png'), // iOS only
// */
// title: 'neighborhood Detected',
// tabBarHidden: false,
// },
//
// {
// label: 'two',
// screen: 'neighborhoodDetected', // this is a registered name for a screen
// icon: require('./app/Assets/icons/visaIcon.png'),
// /*
// selectedIcon: require('../img/one_selected.png'), // iOS only
// */
// title: 'neighborhood Detected',
// tabBarHidden: true,
// overrideBackPress: true
// },
//
// {
// label: 'four',
// screen: 'locationConfirmation', // this is a registered name for a screen
// icon: require('./app/Assets/icons/visaIcon.png'),
// /*
// selectedIcon: require('../img/one_selected.png'), // iOS only
// */
// title: 'locationConfirmation',
// tabBarHidden: true,
// overrideBackPress: true
// },
// {
// label: 'Profile',
// screen: 'Profile', // this is a registered name for a screen
// icon: require('./app/Assets/icons/visaIcon.png'),
// /*
// selectedIcon: require('../img/one_selected.png'), // iOS only
// */
// tabBarHidden: false,
//
// },
{
label: 'JourneyStart',
screen: 'JourneyStart', // this is a registered name for a screen
icon: require('./app/Assets/icons/visaIcon.png'),
/*
selectedIcon: require('../img/one_selected.png'), // iOS only
*/
title: 'JourneyStart',
tabBarHidden: false,
},
]
,
drawer: {
left: {
screen: 'SideDrawer',
},
},
tabsStyle: {
topBarShadowColor: 'red',
statusBarColor: '#221B36',
},
appStyle: {
topBarShadowColor: 'red',
statusBarColor: '#221B36',
}
});
}
////
// Define your Headless task -- simply a javascript async function to receive
// events from BackgroundGeolocation:
//
let HeadlessTask = async (event) => {
let params = event.params;
console.log('[BackgroundGeolocation HeadlessTask] -', event.name, params);
switch (event.name) {
case 'heartbeat':
// Use await for async tasks
let location = await getCurrentPosition();
let date = event.timestamp;
console.log('[BackgroundGeolocation HeadlessTask] - getCurrentPosition:', location);
//Take a picture with the current location data.
// NativeModules.picturePackage.takePicture(timestamp);
break;
case 'http':
// Use await for async tasks
// let dog = await getCurrentPosition();
//console.log('[BackgroundGeolocation HeadlessTask] - http:', dog);
// agent.FirebaseQuery.uploadImage();
console.log('===================HEADLESS HTTP =======================>');
let response = params;
let responseText = response.responseText;
let res = JSON.parse(responseText);
console.log(res);
console.log(res.uid);
console.log(res.timestamp);
// agent.FirebaseQuery.uploadImage();
//Retrieve ALL the failed persists and determine if this SUCCESSFUL HTTP is a previously failed attempt
let failedPersists = realm.objects('FailedPersists');
// console.log(failedPersists);
let failedPersist = failedPersists.filtered('uuid = \'' + res.uid.replace(/"/g, "") + '\'');
// console.log('this is the failed persist: ' + failedPersist);
// console.log(failedPersist);
if (failedPersists.length > 0) {
console.log(failedPersists);
failedPersists.forEach(function (element, index, array) {
console.log(element.uuid);
})
}
// console.log('Failed Persist: ' + failedPersist);
//=================== CHECK LAST DATA COLLECT ===================>
let LastDataCollect = realm.objects('LastDataCollect');
console.log('this is the last Data Collect');
console.log(LastDataCollect);
//=================== PICTURE OR VIDEO? BASED ON USER PARAMETERS ===================>
//=================== 1: Picture, 0: Video ===================>
// console.log(helperFunctions.weightedRand());
// let rand012 = helperFunctions.weightedRand({0: 0.5, 1: 0.5});
// let number = rand012();
// console.log('This is the random distribution: ' + rand012());
// console.log('This is the random distribution1: ' + number);
//=================== DEFAULT TO VIDEO ALWAYS ===================>
var number = 0;
console.log(response.status);
if (failedPersist.length === 0) {
if (response.status !== 200) {
console.log('Not a successful HTTP Request. Take a picture and persist as one to upload later with a 200 okay HTTP');
let lastLocationUuid = realm.objects('LastLocation');
console.log('this is the lastLocationUuid: ' + lastLocationUuid[0].uuid);
// NativeModules.picturePackage.takePicture(lastLocationUuid,
// () => {
// console.log('takePicture Callback invoked');
// })
realm.write(() => {
realm.create('FailedPersists', {uuid: lastLocationUuid[0].uuid, timestamp: 'placeholder'})
});
//Log the current failed persists
let FailedPersists = realm.objects('FailedPersists');
console.log('Failed Persists');
if (FailedPersists.length > 0) {
console.log(FailedPersists);
FailedPersists.forEach(function (element, index, array) {
console.log(element.uuid);
})
}
} else if (response.status === 200) {
//Take picture and upload
console.log('response 200');
console.log(LastDataCollect.length);
console.log(LastDataCollect[0]);
console.log(res.timestamp)
if (LastDataCollect.length > 0 && (res.timestamp - LastDataCollect[0].timestamp) > 60000) {
console.log('This was the time Difference: ' + (res.timestamp - LastDataCollect[0].timestamp) / 1000 + 's');
realm.write(() => {
realm.create('LastDataCollect', {
id: 0,
timestamp: moment(res.timestamp).format('x')
}, true);
});
console.log('======================> Take a Video')
NativeModules.videoPackage.takeVideo('test_journey', res.uid.replace(/"/g, ""),
() => {
console.log('takeVideo Callback invoked');
agent.FirebaseQuery.uploadVideo('test_journey', res.uid.replace(/"/g, ""));
})
let rand012 = 0;
let number = 0;
} else if (LastDataCollect.length > 0 && (res.timestamp - LastDataCollect[0].timestamp) <= 60000) {
console.log('response 200 okay, too little time between events. Do not call.');
console.log('PREVENTING COLLISION CITY');
console.log('This was the time Difference: ' + (res.timestamp - LastDataCollect[0].timestamp) / 1000 + 's');
} else {
console.log('in the else');
realm.write(() => {
realm.create('LastDataCollect', {
id: 0,
timestamp: moment(res.timestamp).format('x')
}, true);
});
console.log('======================> Take a Video')
NativeModules.videoPackage.takeVideo('test_journey', res.uid.replace(/"/g, ""),
() => {
console.log('takeVideo Callback invoked');
agent.FirebaseQuery.uploadVideo('test_journey', res.uid.replace(/"/g, ""));
})
}
}
} else {
if (response.status === 200 && failedPersist[0].uuid === res.replace(/"/g, "")) {
console.log('http success, but this was a previously failed event, so only Upload Photo');
// agent.FirebaseQuery.uploadImage(res.uid);
console.log('delete the successfully uploaded photo from realm');
realm.write(() => {
realm.delete(failedPersist);
})
console.log('this is the FAILED PERSISTS with unit removed');
if (failedPersists.length > 0) {
console.log(failedPersists);
failedPersists.forEach(function (element, index, array) {
console.log(element.uuid);
})
}
}
}
break;
case 'location':
// Use await for async tasks
// let cat = await getCurrentPosition();
console.log('[BackgroundGeolocation HeadlessTask] - location:', location);
break;
}
}
////
// Register your HeadlessTask with BackgroundGeolocation plugin.
//
BackgroundGeolocation.registerHeadlessTask(HeadlessTask);
AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => handleBackgroundMessage()); // <-- Add this line
const handleBackgroundMessage = async (message) => {
console.log('message received in the background');
console.log(message);
//Step 4. Reconfigure BackgroundGeolocation to
BackgroundGeolocation.configure({
disableElasticity: true,
distanceFilter: 10,
stopOnTerminate: false,
stopAfterElapsedMinutes: 1,
enableHeadless: true,
heartbeatInterval: 60,
startOnBoot: true,
foregroundService: true,
url: TRACKER_HOST_TWO,
extras: {
journey_id: 'test_journey'
},
autoSync: true,
debug:
true,
logLevel:
BackgroundGeolocation.LOG_LEVEL_VERBOSE
},
(state) => {
console.log('Background Geolocation configured');
console.log('Starting Background Geolocation');
BackgroundGeolocation.start();
})
return Promise.resolve();
};
AppRegistry.registerComponent('Journey', () => Journey);