2021-06-12 12:17:23 +02:00

161 lines
4.5 KiB
TypeScript

import {Component, OnInit, ViewChild} from '@angular/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',
templateUrl: './landing.component.html',
styleUrls: ['./landing.component.css']
})
export class LandingComponent implements OnInit {
constructor(
private restService: RestService,
private logger: NGXLogger
) {
}
@ViewChild('map') map: AgmMap;
zoom = 14;
center = {lat: 47.90620, lng: 16.20785};
traffic_light_markers = new Map();
car_markers = new Map();
infoWindows = 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';
CAR_IMAGE_NCE = 'assets/pictures/car_orange.png';
NCE_SOUND = 'assets/sound/crash.mp3';
ngOnInit() {
this.getTrafficLights();
this.getCars();
}
getCarEvents(vin) {
this.timeInterval = interval(1000)
.pipe(
startWith(0),
switchMap(() => this.restService.getCarEvents(vin))
).subscribe((data: any) => {
const carEvent = data.body['cursor'];
const car = this.car_markers.get(vin);
car['velocity'] = carEvent['velocity'];
car['timestamp'] = carEvent['timestamp'];
car['near_crash_event'] = carEvent['near_crash_event'];
if (car['near_crash_event']) {
car['iconUrl'] = this.CAR_IMAGE_NCE;
this.playCrashSound();
} else {
car['iconUrl'] = this.CAR_IMAGE;
}
car['lat'] = carEvent['gps_location']['latitude'];
car['lng'] = carEvent['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) => {
for (const traffic_light of data['cursor']) {
traffic_light['iconUrl'] = this.trafficLightToImage(traffic_light['color']);
traffic_light['lat'] = traffic_light['location'][1];
traffic_light['lng'] = traffic_light['location'][0];
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']);
}
}
);
}
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']);
}
}
);
}
private trafficLightToImage(trafficLight) {
return (trafficLight === 'RED') ? this.TL_RED_IMAGE : this.TL_GREEN_IMAGE;
}
openInfoWindow(id) {
this.infoWindows.set(id, true);
}
closeInfoWindow(id) {
this.infoWindows.delete(id);
}
isInfoWindowOpen(id) {
return this.infoWindows.has(id);
}
playCrashSound() {
const audio = new Audio();
audio.src = this.NCE_SOUND;
audio.load();
audio.play();
}
}