Add position update on map based on event store
This commit is contained in:
parent
99d7ac6a2d
commit
fade4c3af3
@ -1,8 +1,6 @@
|
||||
<button (click)="toggleMarker()">Drive!</button>
|
||||
|
||||
<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 *ngFor="let m of markers" [iconUrl]="m.iconUrl" [visible]="m.visible" [latitude]="m.lat" [longitude]="m.lng" >
|
||||
</agm-marker>-->
|
||||
<agm-marker *ngFor="let m of traffic_light_markers" [iconUrl]="m.iconUrl" [latitude]="m.lat" [longitude]="m.lng" >
|
||||
<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-marker>
|
||||
<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-map>
|
||||
|
||||
@ -1,7 +1,9 @@
|
||||
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 {NGXLogger} from 'ngx-logger';
|
||||
import {interval, Subscription} from 'rxjs';
|
||||
import {startWith, switchMap} from 'rxjs/operators';
|
||||
|
||||
@Component({
|
||||
selector: 'app-landing',
|
||||
@ -22,72 +24,111 @@ export class LandingComponent implements OnInit {
|
||||
zoom = 14;
|
||||
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() {
|
||||
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(
|
||||
(data: any) => {
|
||||
traffic_lights = data;
|
||||
console.log(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['lng'] = traffic_light['location'][0];
|
||||
console.log(traffic_light);
|
||||
this.traffic_light_markers.push(traffic_light);
|
||||
this.traffic_light_markers.set(traffic_light['id'], traffic_light);
|
||||
}
|
||||
},
|
||||
err => this.logger.error(err),
|
||||
() => {
|
||||
this.logger.debug('loaded traffic lights');
|
||||
for (const value of this.traffic_light_markers.values()) {
|
||||
this.getTrafficLightEvents(value['id']);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
getDelta(source, destination, steps) {
|
||||
return {
|
||||
lat: (destination.lat - source.lat) / steps,
|
||||
lng: (destination.lng - source.lng) / steps
|
||||
};
|
||||
}
|
||||
|
||||
toggleMarker() {
|
||||
const delta = this.getDelta(this.markers[0], this.markers[1], 100);
|
||||
|
||||
this.move(this.markers[0], delta, 0, 10, 100);
|
||||
getCars() {
|
||||
this.restService.getCars().subscribe(
|
||||
(data: any) => {
|
||||
for (const car of data['cursor']) {
|
||||
car['iconUrl'] = 'assets/pictures/car.png';
|
||||
this.car_markers.set(car['vin'], car);
|
||||
}
|
||||
},
|
||||
err => this.logger.error(err),
|
||||
() => {
|
||||
this.logger.debug('loaded cars');
|
||||
console.log(this.car_markers);
|
||||
for (const value of this.car_markers.values()) {
|
||||
this.getCarEvents(value['vin']);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
move(source, delta, counter, delay, steps) {
|
||||
source.lat += delta.lat;
|
||||
source.lng += delta.lng;
|
||||
if (counter !== steps) {
|
||||
counter++;
|
||||
setTimeout(this.move.bind(this), delay, source, delta, counter, delay, steps);
|
||||
}
|
||||
private trafficLightToImage(trafficLight) {
|
||||
return (trafficLight === 'RED') ? this.TL_RED_IMAGE : this.TL_GREEN_IMAGE;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -2,7 +2,6 @@ import {HttpClient} from '@angular/common/http';
|
||||
import {Injectable} from '@angular/core';
|
||||
import {NGXLogger} from 'ngx-logger';
|
||||
import {environment} from '../../environments/environment';
|
||||
import {Observable} from 'rxjs';
|
||||
|
||||
@Injectable()
|
||||
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() {
|
||||
return this.http.get(this.currentLocation + 'traffic_lights');
|
||||
}
|
||||
|
||||
getCars() {
|
||||
return this.http.get(this.currentLocation + 'cars');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -4,20 +4,20 @@
|
||||
"location": [16.20719, 47.89584],
|
||||
"range": 542,
|
||||
"switchingTime": 15,
|
||||
"initialColor": "RED"
|
||||
"color": "RED"
|
||||
},
|
||||
{
|
||||
"id": "2",
|
||||
"location": [16.20814, 47.90937],
|
||||
"range": 725,
|
||||
"switchingTime": 20,
|
||||
"initialColor": "GREEN"
|
||||
"color": "GREEN"
|
||||
},
|
||||
{
|
||||
"id": "3",
|
||||
"location": [16.20917, 47.92703],
|
||||
"range": 910,
|
||||
"switchingTime": 25,
|
||||
"initialColor": "RED"
|
||||
"color": "RED"
|
||||
}
|
||||
]
|
||||
@ -52,7 +52,7 @@ class Orchestrator:
|
||||
self.vins.append(car['vin'])
|
||||
|
||||
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'],
|
||||
'last_switch': datetime.now()}
|
||||
|
||||
|
||||
@ -27,6 +27,21 @@ def get_cars_events():
|
||||
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'])
|
||||
def get_cars():
|
||||
try:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user