Commit b7c5c6a6 authored by Jose Ignacio Monreal Bailey's avatar Jose Ignacio Monreal Bailey
Browse files

Created TrafficLightModel

parent 2859bb00
......@@ -71,7 +71,6 @@ public class TraciSimulationController implements VehicleController, SimulationS
private double _endY;
private Map<String, VehicleInformationContainer> _positons = new HashMap<>();
private Map<String, Location> trafficLightsPosition = new HashMap<>();
private boolean _initalized = false;
......@@ -984,11 +983,6 @@ public class TraciSimulationController implements VehicleController, SimulationS
return result;
}
@Override
public Location getTrafficLightPosition(String pTrafficLightID) {
return trafficLightsPosition.get(pTrafficLightID);
}
public List<RoadNetworkLink> getControlledLinks(String pTrafficLightID) {
SumoCommand trafficLightCommand = Trafficlight.getControlledLinks(pTrafficLightID);
Object controlledLinksObject = requestObject(trafficLightCommand);
......
package de.tud.kom.p2psim.impl.topology.placement;
import de.tud.kom.p2psim.api.topology.movement.MovementModel;
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
import de.tud.kom.p2psim.api.topology.TopologyComponent;
import de.tud.kom.p2psim.api.topology.placement.PlacementModel;
import de.tud.kom.p2psim.impl.topology.PositionVector;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.traci.TraciSimulationController;
import de.tudarmstadt.maki.simonstrator.api.EventHandler;
import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.api.SimulationSetupExtractor;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.api.TrafficLightController;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.trafficlights.TrafficLight;
import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class TrafficLightPlacementModel implements MovementModel, EventHandler {
private static TrafficLightPlacementModel PLACEMENT;
private final List<SimLocationActuator> components;
private final Map<String, SimLocationActuator> idComponentMatcher;
public class TrafficLightPlacementModel implements PlacementModel, EventHandler {
private final String sumoExe;
private final String sumoConfigFile;
private final String sumoIntersections;
private final int offsetX;
private final int offsetY;
private final int width;
private final int height;
private final long timestepConversion = Time.SECOND;
private double scenarioWidth = 0;
private double scenarioHeight = 0;
private SimulationSetupExtractor controller;
private static final Location DEFAULT_LOCATION = new PositionVector(Double.NaN, Double.NaN);
private boolean initialized = false;
private TrafficLightController controller;
private SimulationSetupExtractor extractor;
private List<Location> intersections;
private int currentIndex = 0;
@XMLConfigurableConstructor({ "sumoExe", "sumoConfigFile", "offsetX", "offsetY", "width", "height" })
public TrafficLightPlacementModel(String sumoExe, String sumoConfigFile, String offsetX, String offsetY, String width, String height) {
PLACEMENT = this;
this.components = new LinkedList<>();
this.idComponentMatcher = new HashMap<>();
this.sumoExe = sumoExe;
this.sumoConfigFile = sumoConfigFile;
this.sumoIntersections = null;
this.offsetX = Integer.parseInt(offsetX);
this.offsetY = Integer.parseInt(offsetY);
this.width = Integer.parseInt(width);
this.height = Integer.parseInt(height);
}
@Override
public void eventOccurred(Object content, int type) {
long currentTime = Time.getCurrentTime() / timestepConversion;
while (controller.getStep() - controller.getStart() < currentTime) {
if (!controller.nextStep()) {
return;
}
}
List<TrafficLight> allTrafficLights = controller.getAllTrafficLights();
for (int i = 0 ; i < allTrafficLights.size() ; ++i) {
TrafficLight trafficlight = allTrafficLights.get(i);
}
/*
List<String> allVehicles = _controller.getAllVehicles();
for (int i = 0; i < allVehicles.size(); i++) {
String vehicle = allVehicles.get(i);
Location position = _controller.getVehiclePosition(vehicle);
if (position == null) {
allVehicles.remove(i--);
continue;
}
SimLocationActuator component = requestSimActuator(vehicle);
try {
RoutedNetLayer routedNetLayer = component.getHost().getComponent(RoutedNetLayer.class);
for (SimNetInterface netInterface : routedNetLayer.getSimNetworkInterfaces()) {
if (netInterface.isOffline()) {
netInterface.goOnline();
}
}
} catch (ComponentNotAvailableException e) {
e.printStackTrace();
}
component.updateCurrentLocation(new PositionVector(position.getLongitude(), position.getLatitude()));
component.setMovementSpeed(_controller.getVehicleSpeed(vehicle));
}
if (allVehicles.size() != idComponentMatcher.size()) {
ArrayList<String> registeredVehicles = new ArrayList<>(idComponentMatcher.keySet());
for (int i = 0; i < registeredVehicles.size(); i++) {
String vehicle = registeredVehicles.get(i);
if (!allVehicles.contains(vehicle)) {
addFreeHost(vehicle);
}
}
}
initializeModel();
}
if (Time.getCurrentTime() < 5 * Time.SECOND) {
for (SimLocationActuator simLocationActuator : freeComponents) {
simLocationActuator.updateCurrentLocation(DEFAULT_LOCATION);
}
protected void initializeModel() {
if (this.sumoExe != null) {
controller = TraciSimulationController.createSimulationController(sumoExe, sumoConfigFile);
controller.init();
controller.setObservedArea(offsetX, offsetY, offsetX + width, offsetY + height);
intersections = controller.getAllIntersections(true);
}
// Reschedule next step
Event.scheduleWithDelay(timeBetweenMoveOperations, this, null, 0);
*/
}
@Override
public void addComponent(SimLocationActuator actuator) {
actuator.updateCurrentLocation(DEFAULT_LOCATION);
components.add(actuator);
}
public void addComponent(TopologyComponent comp) {
@Override
public void placeComponent(SimLocationActuator actuator) {
actuator.updateCurrentLocation(DEFAULT_LOCATION);
if (!initialized) {
initializeModel();
}
}
protected void initializeModel() {
if (!initialized) {
if (sumoExe != null) {
TraciSimulationController traciSimulationController = TraciSimulationController.createSimulationController(sumoExe, sumoConfigFile);
controller = traciSimulationController;
controller.setObservedArea(offsetX, offsetY, offsetX + width, offsetY + height);
controller.init();
controller.nextStep();
extractor = traciSimulationController;
}
scenarioWidth = extractor.getScenarioWidth();
scenarioHeight = extractor.getScenarioHeight();
System.out.println("Initialization: done.");
@Override
public PositionVector place(TopologyComponent comp) {
if (currentIndex < intersections.size()) {
Location intersection = intersections.get(currentIndex);
currentIndex++;
return new PositionVector(intersection.getLongitude(), intersection.getLatitude());
} else {
return new PositionVector(Double.NaN, Double.NaN);
}
}
@Override
public void setTimeBetweenMoveOperations(long time) {
public void eventOccurred(Object content, int type) {
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment