Skip to content
Permalink
f9b57e69b2
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
131 lines (111 sloc) 4.25 KB
import { Component } from '@angular/core';
import { Platform } from '@ionic/angular';
import { NativeStorage } from '@ionic-native/native-storage/ngx';
import BackgroundGeolocation, {
Location,
GeofenceEvent,
ConnectivityChangeEvent,
HttpEvent,
} from 'cordova-background-geolocation';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
HuskyTrack_ID: number;
isTracking: boolean = false;
Tracking_UI_Text: string = "Begin Tracking";
DB_URL: string = 'http://sdp40.cse.uconn.edu/api/locationdata';
constructor(public platform: Platform, private nativeStorage: NativeStorage) {
platform.ready().then(() => {
this.load_or_generate_unique_id().then(() => {
this.configureBackgroundGeolocation();
});
});
}
onHttp(event:HttpEvent) {
console.log('[http] -', event.success, event.status, event.responseText);
}
onConnectivityChange(event:ConnectivityChangeEvent) {
console.log('[connectivitychange] - connected?', event.connected);
}
onGeofence(event:GeofenceEvent) {
console.log('[onGeofence] ', event);
if (event.action == "ENTER") {
console.log("User has entered UCONN tracking radius. Begin Location Tracking.");
BackgroundGeolocation.start();
}
if (event.action == "EXIT") {
console.log("User has left UCONN tracking radius. Disable Location Tracking.");
BackgroundGeolocation.startGeofences();
}
}
onLocation(location:Location) {
console.log('[location].latitude ', location.coords.latitude);
console.log('[location].longitude ', location.coords.longitude);
}
configureBackgroundGeolocation() {
BackgroundGeolocation.onLocation(this.onLocation.bind(this));
BackgroundGeolocation.onGeofence(this.onGeofence.bind(this));
BackgroundGeolocation.onHttp(this.onHttp.bind(this));
BackgroundGeolocation.onConnectivityChange(this.onConnectivityChange.bind(this));
BackgroundGeolocation.addGeofence({
identifier: "UCONN Campus",
radius: 500,
latitude: 41.807757,//LAT-LONG of UCONN Campus : 41.807757, -72.253990 from http://www.latitude-longitude.net
longitude: -72.253990,
notifyOnEntry: true,
notifyOnExit: true,
}).then((success) => {
console.log('[addGeofence] UCONN Campus Geofence added.');
}).catch((error) => {
console.log('[addGeofence] FAILURE: ', error);
});
BackgroundGeolocation.reset();
BackgroundGeolocation.ready({
reset: true,
debug: false,
logLevel: BackgroundGeolocation.LOG_LEVEL_DEBUG,
distanceFilter: 1,
stopTimeout: 1,
url: this.DB_URL,
autoSync: true,
batchSync: true,
autoSyncThreshold: 1, // HTTP requests drain battery far quicker than GPS tracking according to docs.
params: {
'user_id' : this.HuskyTrack_ID
},
}, (state) => {
console.log('- Configure success: ', state);
});
}
async load_or_generate_unique_id() {
await this.nativeStorage.getItem('HuskyTrack_ID').then((data) => { //The reason I'm using HuskyTrack_ID instead of just ID is to effectively namespace 'ID' on local storage.
this.HuskyTrack_ID = data;
console.log(`UserID was already set to: ${this.HuskyTrack_ID}`);
},
(error) => {
console.log("ID not set. First time ran? Generating ID.");
var max = 9007199254740992; // largest *consequative* integer represented by doubles.
this.HuskyTrack_ID = Math.floor(Math.random() * max);
this.nativeStorage.setItem('HuskyTrack_ID', this.HuskyTrack_ID).then(
() => console.log(`ID Set to: ${this.nativeStorage.getItem('HuskyTrack_ID')}`), //NOTE(Jesse): If we fail here then something really off has happened.
(error) => console.log(`error: ${error}. Could not set ID. Consult https://github.com/TheCocoaProject/cordova-plugin-nativestorage`)
);
}
);
}
toggle_tracking() {
this.isTracking = !this.isTracking;
this.isTracking ? this.Tracking_UI_Text = "Stop Tracking" : this.Tracking_UI_Text = "Start Tracking";
if (this.isTracking){
console.log("Begun Tracking");
BackgroundGeolocation.startGeofences();
}
else {
console.log("Ended Tracking");
BackgroundGeolocation.stop();
}
}
}