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 ...@@ -71,7 +71,6 @@ public class TraciSimulationController implements VehicleController, SimulationS
private double _endY; private double _endY;
private Map<String, VehicleInformationContainer> _positons = new HashMap<>(); private Map<String, VehicleInformationContainer> _positons = new HashMap<>();
private Map<String, Location> trafficLightsPosition = new HashMap<>();
private boolean _initalized = false; private boolean _initalized = false;
...@@ -984,11 +983,6 @@ public class TraciSimulationController implements VehicleController, SimulationS ...@@ -984,11 +983,6 @@ public class TraciSimulationController implements VehicleController, SimulationS
return result; return result;
} }
@Override
public Location getTrafficLightPosition(String pTrafficLightID) {
return trafficLightsPosition.get(pTrafficLightID);
}
public List<RoadNetworkLink> getControlledLinks(String pTrafficLightID) { public List<RoadNetworkLink> getControlledLinks(String pTrafficLightID) {
SumoCommand trafficLightCommand = Trafficlight.getControlledLinks(pTrafficLightID); SumoCommand trafficLightCommand = Trafficlight.getControlledLinks(pTrafficLightID);
Object controlledLinksObject = requestObject(trafficLightCommand); Object controlledLinksObject = requestObject(trafficLightCommand);
......
package de.tud.kom.p2psim.impl.topology.placement; package de.tud.kom.p2psim.impl.topology.placement;
import de.tud.kom.p2psim.api.topology.movement.MovementModel; import de.tud.kom.p2psim.api.topology.TopologyComponent;
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator; import de.tud.kom.p2psim.api.topology.placement.PlacementModel;
import de.tud.kom.p2psim.impl.topology.PositionVector; import de.tud.kom.p2psim.impl.topology.PositionVector;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.traci.TraciSimulationController; 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.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.sensor.location.Location;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.api.SimulationSetupExtractor; 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 de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List; 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 sumoExe;
private final String sumoConfigFile; private final String sumoConfigFile;
private final String sumoIntersections;
private final int offsetX; private final int offsetX;
private final int offsetY; private final int offsetY;
private final int width; private final int width;
private final int height; private final int height;
private final long timestepConversion = Time.SECOND;
private double scenarioWidth = 0; private SimulationSetupExtractor controller;
private double scenarioHeight = 0;
private static final Location DEFAULT_LOCATION = new PositionVector(Double.NaN, Double.NaN); private List<Location> intersections;
private boolean initialized = false; private int currentIndex = 0;
private TrafficLightController controller;
private SimulationSetupExtractor extractor;
@XMLConfigurableConstructor({ "sumoExe", "sumoConfigFile", "offsetX", "offsetY", "width", "height" }) @XMLConfigurableConstructor({ "sumoExe", "sumoConfigFile", "offsetX", "offsetY", "width", "height" })
public TrafficLightPlacementModel(String sumoExe, String sumoConfigFile, String offsetX, String offsetY, String width, String 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.sumoExe = sumoExe;
this.sumoConfigFile = sumoConfigFile; this.sumoConfigFile = sumoConfigFile;
this.sumoIntersections = null;
this.offsetX = Integer.parseInt(offsetX); this.offsetX = Integer.parseInt(offsetX);
this.offsetY = Integer.parseInt(offsetY); this.offsetY = Integer.parseInt(offsetY);
this.width = Integer.parseInt(width); this.width = Integer.parseInt(width);
this.height = Integer.parseInt(height); 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 { initializeModel();
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);
}
}
}
if (Time.getCurrentTime() < 5 * Time.SECOND) { protected void initializeModel() {
for (SimLocationActuator simLocationActuator : freeComponents) { if (this.sumoExe != null) {
simLocationActuator.updateCurrentLocation(DEFAULT_LOCATION); 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 @Override
public void addComponent(SimLocationActuator actuator) { public void addComponent(TopologyComponent comp) {
actuator.updateCurrentLocation(DEFAULT_LOCATION);
components.add(actuator);
}
@Override
public void placeComponent(SimLocationActuator actuator) {
actuator.updateCurrentLocation(DEFAULT_LOCATION);
if (!initialized) {
initializeModel();
}
} }
protected void initializeModel() { @Override
if (!initialized) { public PositionVector place(TopologyComponent comp) {
if (sumoExe != null) { if (currentIndex < intersections.size()) {
TraciSimulationController traciSimulationController = TraciSimulationController.createSimulationController(sumoExe, sumoConfigFile); Location intersection = intersections.get(currentIndex);
controller = traciSimulationController; currentIndex++;
controller.setObservedArea(offsetX, offsetY, offsetX + width, offsetY + height); return new PositionVector(intersection.getLongitude(), intersection.getLatitude());
controller.init(); } else {
controller.nextStep(); return new PositionVector(Double.NaN, Double.NaN);
extractor = traciSimulationController;
}
scenarioWidth = extractor.getScenarioWidth();
scenarioHeight = extractor.getScenarioHeight();
System.out.println("Initialization: done.");
} }
} }
@Override @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