Skip to content

Commit 656f692

Browse files
authored
feat(web-visualizer): display charging stations (#476)
1 parent b823f65 commit 656f692

File tree

2 files changed

+129
-2
lines changed

2 files changed

+129
-2
lines changed

fed/mosaic-output/src/main/resources/web/visualizer-dev.js

+128-1
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,94 @@ const TrafficLight = {
398398
}
399399
}
400400

401+
/**
402+
* Represents a charging station.
403+
*/
404+
const ChargingStation = {
405+
name: 'unnamed_charging_station',
406+
latitude: 0,
407+
longitude: 0,
408+
marker: null,
409+
timeStateChange: 0,
410+
state: {},
411+
412+
init(name, latitude, longitude) {
413+
this.name = name
414+
this.latitude = latitude
415+
this.longitude = longitude
416+
this.marker = new Feature({
417+
type: 'charging-station',
418+
geometry: new Point(fromLonLat([ longitude, latitude ]))
419+
})
420+
this.marker.setProperties(['name'])
421+
this.marker.set('name', name)
422+
this.state = {
423+
sending: false,
424+
receiving: false,
425+
};
426+
},
427+
428+
getMarker() {
429+
return this.marker
430+
},
431+
432+
setIsEquipped(isEquipped) {
433+
this.state.equipped = isEquipped
434+
},
435+
436+
setLocation(latitude, longitude) {
437+
this.latitude = latitude
438+
this.longitude = longitude
439+
},
440+
441+
setState(stateName) {
442+
if (this.state[stateName] !== undefined) {
443+
this.state[stateName] = true
444+
this.timeStateChange = Date.now();
445+
}
446+
},
447+
448+
/**
449+
* Updates the marker of the Charging Station.
450+
*/
451+
updateView() {
452+
// Location
453+
this.marker.setGeometry(new Point(fromLonLat([ this.longitude, this.latitude ])))
454+
455+
// Update style
456+
const style = this.createStyle()
457+
this.marker.setStyle(style)
458+
459+
if ((Date.now() - this.timeStateChange) > 500) {
460+
// Clear sending/receiving states
461+
this.state.sending = false
462+
this.state.receiving = false
463+
}
464+
},
465+
466+
/**
467+
* Creates the style of the RSU marker based on the Charging Station's state.
468+
*/
469+
createStyle() {
470+
let style = 'charging-station'
471+
if (this.state.equipped) {
472+
style = 'charging-station-equipped'
473+
}
474+
if (this.state.sending) {
475+
style = 'charging-station-sending'
476+
}
477+
if (this.state.receiving) {
478+
style = 'charging-station-receiving'
479+
}
480+
return new Style({
481+
image: new Icon({
482+
anchor: [0.5, 1],
483+
src: `markers/${ style }.png`
484+
})
485+
})
486+
}
487+
}
488+
401489

402490
/**
403491
* Controls the map and its unit markers.
@@ -425,10 +513,16 @@ const map = (function() {
425513

426514
/**
427515
* Stores all traffic lights (equipped)
428-
* Map<name: string, traffic
516+
* Map<name: string, traffic-light: TrafficLight>
429517
*/
430518
let trafficLights = {}
431519

520+
/**
521+
* Stores all charging stations (equipped)
522+
* Map<name: string, charging-station: ChargingStation>
523+
*/
524+
let chargingStations = {}
525+
432526
/**
433527
* An additional layer beside the map itself.
434528
* This layer contains all unit markers.
@@ -534,6 +628,22 @@ const map = (function() {
534628
}
535629
}
536630

631+
/**
632+
* Creates a charging station and adds it to the map.
633+
* @param {string} name Name of the charging station
634+
* @param {number} latitude Latitude value of the geo position
635+
* @param {number} longitude Longitude value of the geo position
636+
* @param {boolean} equipped true if charging station is equipped with an application
637+
*/
638+
function addChargingStation(name, latitude, longitude, equipped) {
639+
if (!chargingStations[name]) {
640+
chargingStations[name] = Object.assign({}, ChargingStation)
641+
chargingStations[name].init(name, latitude, longitude)
642+
chargingStations[name].setIsEquipped(equipped)
643+
addMarker(chargingStations[name].getMarker())
644+
}
645+
}
646+
537647
/**
538648
* Updates the state of a unit.
539649
* @param {string} unitName Name of the unit to update
@@ -546,6 +656,8 @@ const map = (function() {
546656
rsus[unitName].setState(state)
547657
} else if (trafficLights[unitName]) {
548658
trafficLights[unitName].setState(state);
659+
} else if (chargingStations[unitName]) {
660+
chargingStations[unitName].setState(state);
549661
}
550662
}
551663

@@ -605,6 +717,8 @@ const map = (function() {
605717
rsus[unitName].updateView()
606718
} else if (trafficLights[unitName]) {
607719
trafficLights[unitName].updateView()
720+
} else if (chargingStations[unitName]) {
721+
chargingStations[unitName].updateView()
608722
}
609723
}
610724
}
@@ -631,6 +745,10 @@ const map = (function() {
631745
marker = trafficLights[unitName].getMarker()
632746
vectorLayer.getSource().removeFeature(marker)
633747
delete trafficLights[unitName]
748+
} else if (chargingStations[unitName]) {
749+
marker = chargingStations[unitName].getMarker()
750+
vectorLayer.getSource().removeFeature(marker)
751+
delete chargingStations[unitName]
634752
}
635753
}
636754

@@ -642,6 +760,7 @@ const map = (function() {
642760
agents = {}
643761
rsus = {}
644762
trafficLights = {}
763+
chargingStations = {}
645764
vectorLayer.getSource().clear()
646765
}
647766

@@ -654,6 +773,7 @@ const map = (function() {
654773
addAgent,
655774
addRsu,
656775
addTrafficLight,
776+
addChargingStation,
657777
updateViews,
658778
removeUnit,
659779
removeAllUnits,
@@ -809,6 +929,13 @@ const WebSocketClient = (function() {
809929
map.addTrafficLight(unitName, position.latitude, position.longitude, equipped)
810930
updatedUnits.push(unitName)
811931
}
932+
} else if (data.ChargingStationRegistration) {
933+
// Add traffic light to map
934+
unitName = data.ChargingStationRegistration.chargingStationMapping.name
935+
let position = data.ChargingStationRegistration.chargingStationMapping.position
936+
let equipped = data.ChargingStationRegistration.chargingStationMapping.applications.length > 0;
937+
map.addChargingStation(unitName, position.latitude, position.longitude, equipped)
938+
updatedUnits.push(unitName)
812939
}
813940

814941
// Update markers of updated units

0 commit comments

Comments
 (0)