Skip to content
Permalink
6eb3472a15
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
125 lines (111 sloc) 4.32 KB
/*
TODO
We want to expose to the user the ability to selectively transmit stored GPS locations when on wifi to cut down on dataplan costs.
The available plugin has a lot of bugs when the app is sitting in the background for awhile which is exactly our use case : https://github.com/apache/cordova-plugin-network-information
So I'll wait for those bugs to be fixed before implementing that feature.
*/
import { Component } from '@angular/core';
import { NavController, Platform } from 'ionic-angular';
import { NativeStorage } from '@ionic-native/native-storage';
import BackgroundGeolocation, {
Geofence
} from "cordova-background-geolocation";
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
Tracking_UI_Text: string = "Begin Tracking";
isTracking: boolean = false;
Upload_Over_Celluar: boolean = false;
Transfer_As_Stream: boolean = false;
DB_URL: string = "determine me";
ID: number;
load_or_generate_unique_id(){
this.nativeStorage.getItem('ID').then(
data => {
this.ID = data;
console.log('UserID was already set to: ', this.ID);
},
error => {
console.log("ID not set. First time ran? Generating ID.");
var max = 9007199254740992; // largest *consequative* integer represented by doubles.
this.ID = Math.floor(Math.random() * max);
this.nativeStorage.setItem('ID', this.ID).then(
() => console.log('ID Set to: ', this.nativeStorage.getItem('ID')), //NOTE(Jesse): If we fail here then something really off has happened.
error => console.error('Could not set ID for an unknown reason. Consult https://github.com/TheCocoaProject/cordova-plugin-nativestorage', error)
);
}
);
}
constructor(public navCtrl: NavController, public platform: Platform, private nativeStorage: NativeStorage) {
platform.ready().then(this.configureBackgroundGeolocation.bind(this)).then(this.load_or_generate_unique_id.bind(this));
}
configureBackgroundGeolocation() {
BackgroundGeolocation.addGeofence({
identifier: "UCONN Campus",
radius: 500,
latitude: 41.807757,//41.99550394,// //LAT / LONG of UCONN Campus : 41.807757, -72.253990 from http://www.latitude-longitude.net
longitude: -72.253990,//-72.27337069,//,
notifyOnEntry: true,
notifyOnExit: true,
//extras: {route_id: 1234}
}).then((success) => {
console.log('[addGeofence] UCONN Campus Geofence added.');
}).catch((error) => {
console.log('[addGeofence] FAILURE: ', error);
});
BackgroundGeolocation.onGeofence((event) => {
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();
}
});
BackgroundGeolocation.onLocation(location => {
console.log('[location] ', location.coords.latitude);
console.log('[location] ', location.coords.longitude);
}, error => {
console.log('[location] ERROR: ', error);
});
/*
BackgroundGeolocation.sync((records) => {
console.log('[sync] success: ', records);
}).catch((error) => {
console.log('[sync] FAILURE: ', error);
});
*/
BackgroundGeolocation.ready({
debug: true,
logLevel: BackgroundGeolocation.LOG_LEVEL_OFF,//LOG_LEVEL_VERBOSE,
distanceFilter: 5,
stopTimeout: 1,
//stopOnTerminate: false,
//startOnBoot: true,
//foregroundService: true,
//url: '192.168.0.212',
//autoSync: true,
//batchSync: true,
//autoSyncThreshold: 2, // HTTP requests drain battery far quicker than GPS tracking according to docs.
//params: {} authentication with DB
}, (state) => {
console.log('- Configure success: ', state);
});
}
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();
}
}
}