-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello-world.ts
226 lines (207 loc) · 6.48 KB
/
hello-world.ts
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
import { Component, NgZone } from '@angular/core';
import { IonicPage, NavController, NavParams, Platform } from 'ionic-angular';
////
// NOTE: normally you will simply import from "cordova-background-geolocation-lt" or "cordova-background-geolocation"
// from "../../cordova-background-geolocation" is only fro convenience in the SampleApp for easily switching
// between public / private version of the plugin
//
import BackgroundGeolocation, {
Location,
HttpEvent,
MotionActivityEvent,
ProviderChangeEvent,
MotionChangeEvent,
ConnectivityChangeEvent,
AuthorizationEvent,
TransistorAuthorizationToken
} from "../../cordova-background-geolocation";
import ENV from "../../ENV";
@IonicPage()
@Component({
selector: 'page-hello-world',
templateUrl: 'hello-world.html',
})
export class HelloWorldPage {
// UI State
enabled: boolean;
isMoving: boolean;
// ion-list datasource
events: any;
constructor(public navCtrl: NavController, public navParams: NavParams, private platform: Platform, private zone:NgZone) {
this.isMoving = false;
this.enabled = false;
this.events = [];
// Listen for deviceready to configure BackgroundGeolocation
this.platform.ready().then(this.onDeviceReady.bind(this));
}
ionViewDidLoad() {
console.log('ionViewDidLoad HelloWorldPage');
}
onDeviceReady() {
this.configureBackgroundGeolocation();
}
async configureBackgroundGeolocation() {
// Compose #url: tracker.transistorsoft.com/locations/{username}
let localStorage = (<any>window).localStorage;
let token:TransistorAuthorizationToken = await BackgroundGeolocation.findOrCreateTransistorAuthorizationToken(
localStorage.getItem('orgname'),
localStorage.getItem('username'),
ENV.TRACKER_HOST
);
// Step 1: Listen to events
BackgroundGeolocation.onLocation(this.onLocation.bind(this));
BackgroundGeolocation.onMotionChange(this.onMotionChange.bind(this));
BackgroundGeolocation.onActivityChange(this.onActivityChange.bind(this));
BackgroundGeolocation.onHttp(this.onHttpSuccess.bind(this));
BackgroundGeolocation.onProviderChange(this.onProviderChange.bind(this));
BackgroundGeolocation.onPowerSaveChange(this.onPowerSaveChange.bind(this));
BackgroundGeolocation.onConnectivityChange(this.onConnectivityChange.bind(this));
BackgroundGeolocation.onAuthorization(this.onAuthorization.bind(this));
// Step 2: Configure the plugin
BackgroundGeolocation.ready({
reset: true,
debug: true,
logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
distanceFilter: 10,
stopTimeout: 1,
stopOnTerminate: false,
startOnBoot: true,
url: ENV.TRACKER_HOST + '/api/locations',
authorization: { // <-- JWT authorization for tracker.transistorsoft.com
strategy: 'jwt',
accessToken: token.accessToken,
refreshToken: token.refreshToken,
refreshUrl: ENV.TRACKER_HOST + '/api/refresh_token',
refreshPayload: {
refresh_token: '{refreshToken}'
},
expires: token.expires
},
autoSync: true
}, (state) => {
console.log('- Configure success: ', state);
// Update UI state (toggle switch, changePace button)
this.zone.run(() => {
this.isMoving = state.isMoving;
this.enabled = state.enabled;
});
});
}
// Return to Home screen (app switcher)
onClickHome() {
this.navCtrl.setRoot('HomePage');
}
// #start / #stop tracking
onToggleEnabled() {
if (this.enabled) {
BackgroundGeolocation.start();
} else {
BackgroundGeolocation.stop();
}
}
// Fetch the current position
onClickGetCurrentPosition() {
BackgroundGeolocation.getCurrentPosition({persist: true}, (location) => {
console.log('- getCurrentPosition: ', location);
}, (error) => {
console.warn('- Location error: ', error);
});
}
// Change plugin state between stationary / tracking
onClickChangePace() {
this.isMoving = !this.isMoving;
BackgroundGeolocation.changePace(this.isMoving);
}
// Clear the list of events from ion-list
onClickClear() {
this.events = [];
}
/**
* @event location
*/
onLocation(location:Location) {
console.log('[event] location: ', location);
let event = location.event || 'location';
this.zone.run(() => {
this.addEvent(event, new Date(location.timestamp), location);
})
}
/**
* @event motionchange
*/
onMotionChange(event:MotionChangeEvent) {
console.log('[event] motionchange, isMoving: ', event.isMoving, ', location: ', event.location);
this.zone.run(() => {
this.isMoving = event.isMoving;
});
}
/**
* @event activitychange
*/
onActivityChange(event:MotionActivityEvent) {
console.log('[event] activitychange: ', event);
this.zone.run(() => {
this.addEvent('activitychange', new Date(), event);
});
}
/**
* @event http
*/
onHttpSuccess(response:HttpEvent) {
console.log('[event] http: ', response);
this.zone.run(() => {
this.addEvent('http', new Date(), response);
});
}
onHttpFailure(response:HttpEvent) {
console.warn('[event] http failure: ', response);
this.zone.run(() => {
this.addEvent('http failure', new Date(), response);
});
}
/**
* @event providerchange
*/
onProviderChange(provider:ProviderChangeEvent) {
console.log('[event] providerchange', provider);
this.zone.run(() => {
this.addEvent('providerchange', new Date(), provider);
});
}
/**
* @event powersavechange
*/
onPowerSaveChange(isPowerSaveEnabled:boolean) {
console.log('[event] powersavechange', isPowerSaveEnabled);
this.zone.run(() => {
this.addEvent('powersavechange', new Date(), {isPowerSaveEnabled: isPowerSaveEnabled});
});
}
/**
* @event connectivitychange
*/
onConnectivityChange(event:ConnectivityChangeEvent) {
console.log('[event] connectivitychange connected? ', event.connected);
}
/**
* @event authorization
*/
onAuthorization(event:AuthorizationEvent) {
console.log('[event] authorization: ', event);
}
/**
* Add a record to ion-list
* @param {String} event name
* @param {Date} date
* @param {Object} event object, eg: {location}, {provider}, {activity}
*/
private addEvent(name, date, event) {
let timestamp = date.toLocaleDateString() + ' ' + date.toLocaleTimeString();
this.events.unshift({
name: name,
timestamp: timestamp,
object: event,
content: JSON.stringify(event, null, 2)
});
}
}