GT2/Ejectable/GT2.tsx

287 lines
7.7 KiB
TypeScript
Raw Normal View History

2021-08-24 00:02:21 +00:00
import React, { useState, useEffect } from 'react';
import * as Location from 'expo-location';
2021-08-20 11:24:38 +00:00
import { Coordinate } from "./Coordinate";
import { Text, View } from './components/Themed';
2021-08-24 15:43:25 +00:00
import { StyleSheet } from 'react-native';
2021-08-24 00:02:21 +00:00
var debug:boolean = false;
export var endPending:boolean = false;
var expoGeoState:any = null;
export var locEnabled:boolean = false;
const heartbeat:number = 500;
const displayBeat:number = 3;
2021-08-24 15:43:25 +00:00
export function getEndPending() { return(endPending); }
export function setEndPending(value:boolean) { endPending = value; }
2021-08-20 11:24:38 +00:00
const styles = StyleSheet.create({
tripText: {
marginHorizontal: -10,
marginVertical: 60,
2021-08-23 06:34:16 +00:00
textAlign: 'left',
2021-08-20 11:24:38 +00:00
width: "80%",
fontSize: 12,
fontWeight: 'bold',
}});
2021-08-22 04:30:04 +00:00
2021-08-24 00:02:21 +00:00
function Monitor() {
const [location, setLocation] = useState(Object);
const [errorMsg, setErrorMsg] = useState("");
useEffect(() => {
(async () => {
let { status } = await Location.requestBackgroundPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Permission to monitor location was denied');
return;
} else Trips.setLocEnabled(true);
let location = await Location.getCurrentPositionAsync({});
setLocation(location);
Trips.deltaLoc(location);
})();
}, []);
expoGeoState = 'Waiting..';
if (errorMsg) {
expoGeoState = errorMsg;
} else if (location) {
expoGeoState = JSON.stringify(location);
}
}
class Trip {
2021-08-20 11:24:38 +00:00
2021-08-24 00:02:21 +00:00
public elapsed:string = "00.00.00";
ticks:number = 0;
interval:any;
segments:number = 1;
distance:number = 0;
ds:number = 0;
CO2:number = 0.0;
2021-08-20 11:24:38 +00:00
2021-08-24 00:02:21 +00:00
loc:Coordinate = new Coordinate(0.0,0.0);
startPoint:Coordinate = new Coordinate(0.0,0.0)
2021-08-20 11:24:38 +00:00
tick() {let hours:number = 0, minutes:number = 0, seconds:number = 0;
2021-08-24 00:02:21 +00:00
this.ticks++;
hours = this.ticks < 3600 ? 0 : (this.ticks / 3600);
minutes = this.ticks < 60 ? 0 : (this.ticks - (hours * 3600)) / 60;
seconds = this.ticks % 60;
this.elapsed = hours.toFixed(0) + ":" + minutes.toFixed(0) + ":" + seconds.toFixed(0);
2021-08-20 11:24:38 +00:00
}
2021-08-24 00:02:21 +00:00
public start() { this.interval = setInterval(() => this.tick(), heartbeat); }
public resume() { this.startPoint = new Coordinate(0.0,0.0)
this.loc = new Coordinate(0.0, 0.0);
this.segments++;
this.interval = setInterval(() => this.tick(), heartbeat); }
public stop() { this.ds = this.loc.distanceTo(this.startPoint);
this.distance += this.ds;
clearInterval(this.interval);
}
}
2021-08-20 11:24:38 +00:00
2021-08-24 00:02:21 +00:00
interface expoGeoObj {
coords: [
altitude:number,
altitudeAccuracy:number,
latitude:number,
accuracy:number,
longitude:number,
heading:number,
speed:number,
timestamp:number
]
2021-08-20 11:24:38 +00:00
}
export class GT2 {
2021-08-24 00:02:21 +00:00
trip:Trip = new Trip();
endPoint:Coordinate = new Coordinate(0.0,0.0);
startPoint:Coordinate = new Coordinate(0.0,0.0);
2021-08-21 07:45:27 +00:00
2021-08-23 06:34:16 +00:00
displayInterval:number = displayBeat * heartbeat;
public distance:number = 0.0;
2021-08-24 00:02:21 +00:00
co2Rate:number = 250.0;
2021-08-23 06:34:16 +00:00
z:number = 0.0;
2021-08-24 00:02:21 +00:00
public v:number = 0.0;
2021-08-23 06:34:16 +00:00
public inProgress:boolean = false;
public paused:boolean = false;
public elapsed:number = 0.0;
elevation:number = 0.0;
2021-08-24 00:02:21 +00:00
public units:string = "km";
public CO2Effect:string = "carbon burden";
2021-08-20 11:24:38 +00:00
2021-08-23 06:34:16 +00:00
nTrips:number = 0;
2021-08-24 00:02:21 +00:00
n:number = 0.0;
m:number = 0.0;
2021-08-23 06:34:16 +00:00
public deltaLoc(lastFix:any) {
2021-08-24 00:02:21 +00:00
lastFix = JSON.stringify(lastFix);
let expoFix:expoGeoObj = JSON.parse(lastFix);
2021-08-23 06:34:16 +00:00
2021-08-24 00:02:21 +00:00
this.trip.loc.setLongitude(expoFix.coords['longitude']);
this.trip.loc.setLatitude(expoFix.coords['latitude']);
2021-08-22 04:30:04 +00:00
2021-08-24 00:02:21 +00:00
if (this.trip.startPoint.mLatitude == 0.0) {
this.trip.startPoint.mLatitude = this.trip.loc.mLatitude;
this.trip.startPoint.mLongitude = this.trip.loc.mLongitude;
}
2021-08-20 11:24:38 +00:00
2021-08-24 00:02:21 +00:00
this.trip.ds = this.trip.loc.distanceTo(this.trip.startPoint);
2021-08-20 11:24:38 +00:00
}
public end() {
2021-08-24 00:02:21 +00:00
this.trip.stop();
2021-08-20 11:24:38 +00:00
this.inProgress = false;
2021-08-23 06:34:16 +00:00
this.nTrips++;
2021-08-24 15:43:25 +00:00
setEndPending(true);
2021-08-24 00:02:21 +00:00
2021-08-20 11:24:38 +00:00
}
2021-08-24 00:02:21 +00:00
public setLocEnabled(value:boolean) { locEnabled = value}
2021-08-20 11:24:38 +00:00
public pause() {
2021-08-23 06:34:16 +00:00
if (!this.paused) {
2021-08-24 00:02:21 +00:00
this.trip.stop();
2021-08-23 06:34:16 +00:00
this.paused = true;
} else {
2021-08-24 00:02:21 +00:00
this.trip.resume();
2021-08-23 06:34:16 +00:00
this.paused = false;
}
2021-08-20 11:24:38 +00:00
}
2021-08-24 00:02:21 +00:00
public reset() {
this.distance = 0.0;
this.endPoint = new Coordinate(0,0);
this.startPoint = new Coordinate(0,0);
this.v = 0;
this.inProgress = false;
this.paused = false;
this.elapsed = 0.0;
}
2021-08-20 11:24:38 +00:00
public start() {
2021-08-23 06:34:16 +00:00
this.reset();
2021-08-24 00:02:21 +00:00
this.trip = new Trip();
this.trip.start();
2021-08-20 11:24:38 +00:00
this.inProgress = true;
2021-08-24 00:02:21 +00:00
2021-08-20 11:24:38 +00:00
}
2021-08-24 00:02:21 +00:00
lt:number = 0.0;
public getTripSummary() : string {
return (
'Elapsed - ' + this.trip.elapsed + '\n' +
'Origin: ' + 'lat: ' + this.trip.loc.mLatitude.toFixed(8) +
' long: ' + this.trip.loc.mLongitude.toFixed(8) + '\n' +
'Destination: ' + 'lat: ' + this.trip.loc.mLatitude.toFixed(8) +
' long: ' + this.trip.loc.mLongitude.toFixed(8) + '\n' +
'CO2: ' + this.trip.CO2 + ' grams ' + this.CO2Effect );
}
2021-08-23 06:34:16 +00:00
public getTripPanel() : string {
2021-08-24 00:02:21 +00:00
if (this.inProgress) {
return (
'Elapsed - ' + this.trip.elapsed + '\n' +
'Geo: ' + 'lat: ' + this.trip.loc.mLatitude.toFixed(8) +
' long: ' + this.trip.loc.mLongitude.toFixed(8) + '\n' +
'Vector: ' + 'distance: ' + this.trip.ds + ' velocity: ' + this.v + '\n' +
2021-08-23 06:34:16 +00:00
'Altitude: ' + this.elevation + '\n');
2021-08-24 00:02:21 +00:00
}
else return("No trip in progress. " + this.nTrips + " trip(s) completed.");
2021-08-23 06:34:16 +00:00
}
}
export class TripDisplay extends React.Component {
constructor(props:any) {
super(props);
this.state = { seconds: 0 };
}
interval:any;
tick() {
this.setState(state => ({
seconds: 1
}));
}
componentDidMount() {
this.interval = setInterval(() => this.tick(), Trips.displayInterval);
}
componentWillUnmount() {
clearInterval(this.interval);
}
render() {
2021-08-20 11:24:38 +00:00
2021-08-24 00:02:21 +00:00
if (!debug)
return(
<View>
<Text style={styles.tripText}>
{Trips.getTripPanel() }
</Text>
</View> );
else;
return(
<View>
2021-08-22 04:30:04 +00:00
<Text style={styles.tripText}>
2021-08-23 06:34:16 +00:00
{Trips.getTripPanel() }
2021-08-22 04:30:04 +00:00
</Text>
2021-08-24 00:02:21 +00:00
<Text>{expoGeoState}</Text>
</View> );
2021-08-20 11:24:38 +00:00
}
}
2021-08-24 00:02:21 +00:00
export class TripSummary extends React.Component {
2021-08-24 15:43:25 +00:00
handleRefresh = () => {
// by calling this method react re-renders the component
this.setState({});
};
2021-08-24 00:02:21 +00:00
render() {
return(
<View>
<Text style={styles.tripText}>
{Trips.getTripSummary() }
</Text>
</View> );
}
}
2021-08-23 06:34:16 +00:00
export var Trips:GT2 = new GT2();