VehicleDataInformationHandler.java 3.65 KB
Newer Older
1
2
3
4
5
6
7
8
package de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.xml;

import java.util.HashMap;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

Tobias Meuser's avatar
Tobias Meuser committed
9
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.VehicleInformationContainer;
10
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
11
12

public class VehicleDataInformationHandler extends DefaultHandler {
Tobias Meuser's avatar
Tobias Meuser committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
	private boolean _next = true;
	private boolean _run = true;
	private HashMap<String, VehicleInformationContainer> _vehiclePositions = new HashMap<>();
	private double _currentStep = -1;

	private boolean _observedAreaSet = false;
	private double _startX = -1;
	private double _startY = -1;
	private double _endX = -1;
	private double _endY = -1;

	private HashMap<String, VehicleInformationContainer> _nextVehiclePositions = new HashMap<>();
	private double _nextStep = -1;

	private static final String doublePattern = "-?[0-9]*\\.?[0-9]+";
	private boolean _terminated = false;

	@Override
	public void startElement(String pUri, String pLocalName, String pQName, Attributes pAttributes)
			throws SAXException {
		if (pQName.equals("timestep")) {
			while (!_next) {
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

			_next = false;
			String value = pAttributes.getValue("time");
			if (value.matches(doublePattern)) {
				_nextStep = Double.valueOf(value);
			}
		} else if (pQName.equals("vehicle")) {
			String id = pAttributes.getValue("id");
			String lonString = pAttributes.getValue("x");
			String latString = pAttributes.getValue("y");
			String speedString = pAttributes.getValue("speed");
			String headingString = pAttributes.getValue("angle");
			if (lonString.matches(doublePattern) && latString.matches(doublePattern) && headingString.matches(doublePattern) && speedString.matches(doublePattern) ) {
				double lon = Double.valueOf(lonString);
				double lat = Double.valueOf(latString);
				double heading = Double.valueOf(headingString);
				double speed = Double.valueOf(speedString);

				if (_observedAreaSet) {
					if (_startX <= lon && lon <= _endX && _startY <= lat && lat <= _endY) {
61
						_nextVehiclePositions.put(id, new VehicleInformationContainer(new PositionVector(lon - _startX, lat - _startY, 0), heading, speed, null));
Tobias Meuser's avatar
Tobias Meuser committed
62
63
					}
				} else {
64
					_nextVehiclePositions.put(id, new VehicleInformationContainer(new PositionVector(lon, lat, 0), heading, speed, null));
Tobias Meuser's avatar
Tobias Meuser committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
				}
			}
		}
	}

	@Override
	public void endElement(String pUri, String pLocalName, String pQName) throws SAXException {
		if (pQName.equals("timestep")) {
			_run = false;
		} else if (pQName.equals("fcd-export")) {
			while (!_next) {
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			_next = false;
			_run = false;
			_terminated = true;
		}
	}

	public void setObservedArea(double pStartX, double pStartY, double pEndX, double pEndY) {
		_startX = pStartX;
		_startY = pStartY;
		_endX = pEndX;
		_endY = pEndY;

		_observedAreaSet = true;
	}

	public boolean isTerminated() {
		return _terminated;
	}

	public HashMap<String, VehicleInformationContainer> getVehiclePositions() {
		return _vehiclePositions;
	}

	public void readNext() {
		while (_run) {
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		_vehiclePositions = _nextVehiclePositions;
		_currentStep = _nextStep;
		_nextStep = -1;
		_nextVehiclePositions = new HashMap<>();

		_run = true;
		_next = true;
	}

	public boolean isRunning() {
		return _run;
	}

	public double getStep() {
		return _currentStep;
	}
129
}