diff --git a/src/app/hirebike/hirebike.page.html b/src/app/hirebike/hirebike.page.html index 9ebd46b1f7dcd96a359bc971f79e876a463254f2..4d4080763c430e4116e5f6c715aeffb635bde942 100644 --- a/src/app/hirebike/hirebike.page.html +++ b/src/app/hirebike/hirebike.page.html @@ -113,4 +113,9 @@ </div> </div> </div> + <!--div style="max-height: 100px; overflow-y: auto;"> + <div *ngFor="let location of locationList;"> + {{"sent height " + location.altitude}} + </div> + </div--> </ion-footer> \ No newline at end of file diff --git a/src/app/hirebike/hirebike.page.ts b/src/app/hirebike/hirebike.page.ts index 240bf4323245b964d11aea419a228a51f0f648ad..acc77ec63ba1a294d5ca6734b06cbcca5c0fb421 100644 --- a/src/app/hirebike/hirebike.page.ts +++ b/src/app/hirebike/hirebike.page.ts @@ -47,7 +47,7 @@ export class HirebikePage implements OnInit { private defaultLayers: any; private locationsGroup: any; - private currentUserPosition = { lat: 48.783480, lng: 9.180319 }; + private currentUserPosition = { lat: 48.783480, lng: 9.180319, altitude: 250 }; private bikePosition = { lat: 48.783480, lng: 9.180319 }; private destinationPosition = { lat: 48.783480, lng: 9.180319 }; @@ -97,14 +97,20 @@ export class HirebikePage implements OnInit { } this.locationService.liveLocationSubject.subscribe((position) => { - console.log('got location inside home subscription'); + console.log('got location inside my ride subscription'); + console.log(position); this.currentUserPosition.lat = position.lat; this.currentUserPosition.lng = position.lng; + this.currentUserPosition.altitude = position.altitude; if (this.currentLocationMarker) { this.currentLocationMarker.setGeometry({ lat: this.currentUserPosition.lat, lng: this.currentUserPosition.lng }) } else { this.showUserLocationOnMap(this.currentUserPosition.lat, this.currentUserPosition.lng); } + // bike already rented + if(this.isTripStarted) { + this.map.setCenter({ lat: this.currentUserPosition.lat, lng: this.currentUserPosition.lng }); + } }); this.getReservedBike(); @@ -437,30 +443,12 @@ export class HirebikePage implements OnInit { this.map.removeObject(this.noGoAreas); } - startTrip() { - this.isBikeHired = true; - this.startRideSubject.next('some value'); - this.loadingService.showLoader(); - this.storage.get('token').then((token) => { - const bikeId = this.bikeDetails.id; - let url = 'http://193.196.52.237:8081/rent' + '?bikeId=' + this.bikeDetails.id; - const headers = new HttpHeaders().set("Authorization", "Bearer " + token); - let bikeApi = this.httpClient.get(url, { headers }); - bikeApi.subscribe((resp) => { - console.log('my data: ', resp); - - this.loadingService.hideLoader(); - this.toastService.showToast("Trip Started"); - this.isBikeHired = true; - }, (error) => { - console.log(error); - this.loadingService.hideLoader(); - this.toastService.showToast("This is ongoing Trip"); - }); - }); - - } + previousPositionObj:any = {}; + currentPositionObj:any = {}; + batteryLevelSentCount = 0; + intervalRef: any; + locationList = []; startTrip2() { this.isBikeHired = true; this.isTripStarted = true; @@ -473,10 +461,25 @@ export class HirebikePage implements OnInit { const headers = new HttpHeaders().set("Authorization", "Bearer " + token); let bikeApi = this.httpClient.get(url, { headers }); bikeApi.subscribe((resp) => { - console.log('my data: ', resp); + console.log('Trip Started: ', resp); this.loadingService.hideLoader(); this.toastService.showToast("Trip Started"); this.isBikeHired = true; + this.currentPositionObj = { + lattitude: this.currentUserPosition.lat, + longitude: this.currentUserPosition.lng, + altitude: this.currentUserPosition.altitude, + } + if(this.batteryLevelSentCount === 0) { + this.previousPositionObj = this.currentPositionObj; + this.intervalRef = setInterval(() => { + this.sendUsageDataToBackend(); + }, 20000); + } else { + this.intervalRef = setInterval(() => { + this.sendUsageDataToBackend(); + }, 300000); + } }, (error) => { console.log(error); this.loadingService.hideLoader(); @@ -486,11 +489,47 @@ export class HirebikePage implements OnInit { } + // this function will draw route after start trip button is clicked drawFinalRouteonMap() { this.map.removeObjects(this.map.getObjects()); this.addRouteShapeToMap(this.selectedRoute); this.addManueversToMap(this.selectedRoute); this.mapDataService.mapDataSubject.next(this.selectedRoute); + this.currentLocationMarker = null; + //get user location + if (this.currentLocationMarker) { + this.currentLocationMarker.setGeometry({ lat: this.currentUserPosition.lat, lng: this.currentUserPosition.lng }) + } else { + this.showUserLocationOnMapForRouting(this.currentUserPosition.lat, this.currentUserPosition.lng); + } + } + + //send bike usage data to backend every 2 minutes + sendUsageDataToBackend() { + this.storage.get('token').then((token) => { + let url = 'http://193.196.52.237:8081/batteryLevel'; + const headers = new HttpHeaders().set("Authorization", "Bearer " + token); + let batteryLevelApi = this.httpClient.get<any>(url,{headers}); + batteryLevelApi.subscribe((batteryResp) => { + console.log("Battery Level Response", batteryResp); + let url = 'http://193.196.52.237:8081/segment'; + const headers = new HttpHeaders().set("Authorization", "Bearer " + token); + this.currentPositionObj.batteryLevel = batteryResp.data; + this.currentPositionObj.altitude = this.currentUserPosition.altitude; + let requestObject = { + previousPosition: this.previousPositionObj, + currentPosition: this.currentPositionObj + } + let usageDataApi = this.httpClient.post<any>(url, {requestObject}, {headers}); + usageDataApi.subscribe((resp) => { + console.log("Usage api response", resp); + this.previousPositionObj = this.currentPositionObj; + this.batteryLevelSentCount++; + this.locationList.push(this.currentPositionObj); + }, (error) => console.log(error)); + + }, (error) => console.log(error)); + }); } CancelTrip() { @@ -807,6 +846,20 @@ export class HirebikePage implements OnInit { //this.map.setCenter({ lat: lat, lng: lng }); } + showUserLocationOnMapForRouting(lat, lng) { + let svgMarkup = '<svg width="24" height="24" ' + + 'xmlns="http://www.w3.org/2000/svg">' + + '<circle cx="10" cy="10" r="10" ' + + 'fill="#007cff" stroke="white" stroke-width="2" />' + + '</svg>'; + let icon = new H.map.Icon(svgMarkup); + // Create a marker using the previously instantiated icon: + this.currentLocationMarker = new H.map.Marker({ lat: lat, lng: lng }, { icon: icon }); + // Add the marker to the map: + this.map.addObject(this.currentLocationMarker); + this.map.setCenter({ lat: lat, lng: lng }); + } + addMarker(lat, lng, img) { var icon = new H.map.Icon(img); // Create a marker using the previously instantiated icon: @@ -961,9 +1014,13 @@ export class HirebikePage implements OnInit { } ionViewDidLeave() { + console.log("Route: Ion View Left.") if (this.mapElement) { this.mapElement.nativeElement.remove(); } + if (this.intervalRef) { + clearInterval(this.intervalRef); + } // if(this.locationService.liveLocationSubject) { // this.locationService.liveLocationSubject.unsubscribe(); // } diff --git a/src/app/services/location.service.ts b/src/app/services/location.service.ts index befbc9446e4094973744d2da8e3b71ca5c616c3c..f96c0fa68d1d6e771799a2b93cd9998186ee3525 100644 --- a/src/app/services/location.service.ts +++ b/src/app/services/location.service.ts @@ -5,24 +5,30 @@ import { Subject } from 'rxjs'; providedIn: 'root' }) export class LocationService { - public preiousUserPosition = { lat: 48.783480, lng: 9.180319 }; - public currentUserPosition = { lat: 48.783480, lng: 9.180319 }; + public preiousUserPosition = { lat: 48.783480, lng: 9.180319, altitude: 250}; + public currentUserPosition = { lat: 48.783480, lng: 9.180319, altitude: 250}; liveLocationSubject = new Subject<any>(); //Decalring new RxJs Subject constructor(private geolocation: Geolocation) { let watch = this.geolocation.watchPosition({ enableHighAccuracy: true, maximumAge: 10000 }); watch.subscribe((position) => { - console.log('IN WATCHER') - console.log('lat'+ position.coords.latitude); - console.log('lng'+ position.coords.longitude); + console.log(position); + let altitude = position.coords.altitude; + if (!altitude) { + altitude = 250; + } this.currentUserPosition.lat = position.coords.latitude; this.currentUserPosition.lng = position.coords.longitude; + this.currentUserPosition.altitude = altitude; + this.preiousUserPosition.lat = position.coords.latitude; this.preiousUserPosition.lng = position.coords.longitude; + this.preiousUserPosition.altitude = altitude; + this.getUserLiveLocation(this.currentUserPosition); }, (errorObj) => { - console.log('error getting live location, setting to previous location'); + console.log('Error getting live location, setting to previous location'); this.getUserLiveLocation(this.preiousUserPosition); }); } @@ -31,12 +37,21 @@ export class LocationService { return new Promise((resolve, reject) => { this.geolocation.getCurrentPosition().then((resp) => { + console.log(resp); let lat = resp.coords.latitude; let lng = resp.coords.longitude; + let altitude = resp.coords.altitude; + if (!altitude) { + altitude = 250; + } + this.currentUserPosition.lat = resp.coords.latitude; this.currentUserPosition.lng = resp.coords.longitude; + this.currentUserPosition.altitude = altitude; + this.preiousUserPosition.lat = resp.coords.latitude; this.preiousUserPosition.lng = resp.coords.longitude; + this.preiousUserPosition.altitude = altitude; resolve(this.currentUserPosition); }, er => { console.log('error getting location setting to previous location');