Add position update on map based on event store

This commit is contained in:
David Eder 2021-06-12 00:16:05 +02:00
parent 99d7ac6a2d
commit fade4c3af3
6 changed files with 123 additions and 57 deletions

View File

@ -1,8 +1,6 @@
<button (click)="toggleMarker()">Drive!</button> <agm-map id="map" #map [zoom]="zoom" [latitude]="center.lat" [longitude]="center.lng" style="height: 900px; width: 1200px">
<agm-marker *ngFor="let m of car_markers | keyvalue" [iconUrl]="m.value.iconUrl" [latitude]="m.value.lat" [longitude]="m.value.lng" >
<agm-map *ngIf="traffic_light_markers.length > 0" id="map" #map [zoom]="zoom" [latitude]="center.lat" [longitude]="center.lng" style="height: 900px; width: 1200px"> </agm-marker>
<!--<agm-marker *ngFor="let m of markers" [iconUrl]="m.iconUrl" [visible]="m.visible" [latitude]="m.lat" [longitude]="m.lng" > <agm-marker *ngFor="let m of traffic_light_markers | keyvalue" [iconUrl]="m.value.iconUrl" [latitude]="m.value.lat" [longitude]="m.value.lng" >
</agm-marker>-->
<agm-marker *ngFor="let m of traffic_light_markers" [iconUrl]="m.iconUrl" [latitude]="m.lat" [longitude]="m.lng" >
</agm-marker> </agm-marker>
</agm-map> </agm-map>

View File

@ -1,7 +1,9 @@
import {Component, OnInit, ViewChild} from '@angular/core'; import {Component, OnInit, ViewChild} from '@angular/core';
import {AgmMap, AgmMarker} from '@agm/core'; import {AgmMap} from '@agm/core';
import {RestService} from '../../services/rest.service'; import {RestService} from '../../services/rest.service';
import {NGXLogger} from 'ngx-logger'; import {NGXLogger} from 'ngx-logger';
import {interval, Subscription} from 'rxjs';
import {startWith, switchMap} from 'rxjs/operators';
@Component({ @Component({
selector: 'app-landing', selector: 'app-landing',
@ -22,72 +24,111 @@ export class LandingComponent implements OnInit {
zoom = 14; zoom = 14;
center = {lat: 47.90620, lng: 16.20785}; center = {lat: 47.90620, lng: 16.20785};
// Test Data
markers = [
{
Id: 1,
name: 'Car-1',
lat: 47.89053,
lng: 16.20703,
visible: true,
iconUrl: 'assets/pictures/car.png'
},
{
Id: 2,
name: 'Car-2',
lat: 47.89853,
lng: 16.20703,
visible: true,
iconUrl: 'assets/pictures/car.png'
},
];
traffic_light_markers = []; traffic_light_markers = new Map();
car_markers = new Map();
timeInterval: Subscription;
TL_RED_IMAGE = 'assets/pictures/traffic_light_red.png';
TL_GREEN_IMAGE = 'assets/pictures/traffic_light_green.png';
CAR_IMAGE = 'assets/pictures/car.png';
ngOnInit() { ngOnInit() {
let traffic_lights = []; this.getTrafficLights();
this.getCars();
}
getCarEvents(vin) {
this.timeInterval = interval(1000)
.pipe(
startWith(0),
switchMap(() => this.restService.getCarEvents(vin))
).subscribe((data: any) => {
const car = data.body['cursor'];
car['vin'] = car['vehicle_identification_number'];
//TODO: if (car['near_crash_event']) {
car['iconUrl'] = this.CAR_IMAGE;
car['lat'] = car['gps_location']['latitude'];
car['lng'] = car['gps_location']['longitude'];
this.car_markers.set(car['vin'], car);
},
err => this.logger.error(err),
() => {
this.logger.debug('loaded traffic light events');
}
);
}
getTrafficLightEvents(tlid) {
this.timeInterval = interval(1000)
.pipe(
startWith(0),
switchMap(() => this.restService.getTrafficLightEvents(tlid))
).subscribe(
(data: any) => {
const traffic_light_event = data.body['cursor'];
const traffic_light = this.traffic_light_markers.get(traffic_light_event['tlid']);
traffic_light['color'] = traffic_light_event['color'];
traffic_light['last_switch'] = traffic_light_event['last_switch'];
traffic_light['iconUrl'] = this.trafficLightToImage(traffic_light['color']);
this.traffic_light_markers.set(traffic_light_event['tlid'], traffic_light);
},
err => this.logger.error(err),
() => {
this.logger.debug('loaded traffic light events');
}
);
}
getTrafficLights() {
this.restService.getTrafficLights().subscribe( this.restService.getTrafficLights().subscribe(
(data: any) => { (data: any) => {
traffic_lights = data;
console.log(data['cursor']);
for (const traffic_light of data['cursor']) { for (const traffic_light of data['cursor']) {
traffic_light['iconUrl'] = 'assets/pictures/traffic_light_red.png'; traffic_light['iconUrl'] = this.trafficLightToImage(traffic_light['color']);
traffic_light['lat'] = traffic_light['location'][1]; traffic_light['lat'] = traffic_light['location'][1];
traffic_light['lng'] = traffic_light['location'][0]; traffic_light['lng'] = traffic_light['location'][0];
console.log(traffic_light); this.traffic_light_markers.set(traffic_light['id'], traffic_light);
this.traffic_light_markers.push(traffic_light);
} }
}, },
err => this.logger.error(err), err => this.logger.error(err),
() => { () => {
this.logger.debug('loaded traffic lights'); this.logger.debug('loaded traffic lights');
for (const value of this.traffic_light_markers.values()) {
this.getTrafficLightEvents(value['id']);
}
} }
); );
} }
getCars() {
getDelta(source, destination, steps) { this.restService.getCars().subscribe(
return { (data: any) => {
lat: (destination.lat - source.lat) / steps, for (const car of data['cursor']) {
lng: (destination.lng - source.lng) / steps car['iconUrl'] = 'assets/pictures/car.png';
}; this.car_markers.set(car['vin'], car);
} }
},
toggleMarker() { err => this.logger.error(err),
const delta = this.getDelta(this.markers[0], this.markers[1], 100); () => {
this.logger.debug('loaded cars');
this.move(this.markers[0], delta, 0, 10, 100); console.log(this.car_markers);
for (const value of this.car_markers.values()) {
this.getCarEvents(value['vin']);
}
}
);
} }
move(source, delta, counter, delay, steps) { private trafficLightToImage(trafficLight) {
source.lat += delta.lat; return (trafficLight === 'RED') ? this.TL_RED_IMAGE : this.TL_GREEN_IMAGE;
source.lng += delta.lng;
if (counter !== steps) {
counter++;
setTimeout(this.move.bind(this), delay, source, delta, counter, delay, steps);
}
} }
} }

View File

@ -2,7 +2,6 @@ import {HttpClient} from '@angular/common/http';
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import {NGXLogger} from 'ngx-logger'; import {NGXLogger} from 'ngx-logger';
import {environment} from '../../environments/environment'; import {environment} from '../../environments/environment';
import {Observable} from 'rxjs';
@Injectable() @Injectable()
export class RestService { export class RestService {
@ -14,7 +13,20 @@ export class RestService {
) { ) {
} }
getTrafficLightEvents(tlid) {
return this.http.get(this.currentLocation + 'traffic_light_events?id=' + tlid, {observe: 'response'});
}
getCarEvents(vin) {
return this.http.get(this.currentLocation + 'car_events?vin=' + vin, {observe: 'response'});
}
getTrafficLights() { getTrafficLights() {
return this.http.get(this.currentLocation + 'traffic_lights'); return this.http.get(this.currentLocation + 'traffic_lights');
} }
getCars() {
return this.http.get(this.currentLocation + 'cars');
}
} }

View File

@ -4,20 +4,20 @@
"location": [16.20719, 47.89584], "location": [16.20719, 47.89584],
"range": 542, "range": 542,
"switchingTime": 15, "switchingTime": 15,
"initialColor": "RED" "color": "RED"
}, },
{ {
"id": "2", "id": "2",
"location": [16.20814, 47.90937], "location": [16.20814, 47.90937],
"range": 725, "range": 725,
"switchingTime": 20, "switchingTime": 20,
"initialColor": "GREEN" "color": "GREEN"
}, },
{ {
"id": "3", "id": "3",
"location": [16.20917, 47.92703], "location": [16.20917, 47.92703],
"range": 910, "range": 910,
"switchingTime": 25, "switchingTime": 25,
"initialColor": "RED" "color": "RED"
} }
] ]

View File

@ -52,7 +52,7 @@ class Orchestrator:
self.vins.append(car['vin']) self.vins.append(car['vin'])
for traffic_light in traffic_lights['cursor']: for traffic_light in traffic_lights['cursor']:
self.tls[traffic_light['id']] = {'color': traffic_light['initialColor'], self.tls[traffic_light['id']] = {'color': traffic_light['color'],
'switching_time': traffic_light['switchingTime'], 'switching_time': traffic_light['switchingTime'],
'last_switch': datetime.now()} 'last_switch': datetime.now()}

View File

@ -27,6 +27,21 @@ def get_cars_events():
return json_util.dumps({'cursor': cars}) return json_util.dumps({'cursor': cars})
@app.route('/api/v1/resources/traffic_light_events', methods=['GET'])
def get_traffic_light_events():
id = request.args.get('id')
try:
response = requests.get(EVENT_STORE_URL + 'TL:' + id + '/0/')
traffic_lights = json.loads(response.text)
except requests.exceptions.ConnectionError as e:
print("Is the EVENT_STORE_URL running and reachable?")
raise e
return json_util.dumps({'cursor': traffic_lights})
@app.route('/api/v1/resources/cars', methods=['GET']) @app.route('/api/v1/resources/cars', methods=['GET'])
def get_cars(): def get_cars():
try: try: