1 package org.opentrafficsim.imb.transceiver.urbanstrategy;
2
3 import java.rmi.RemoteException;
4
5 import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
6 import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
7 import org.opentrafficsim.core.gtu.RelativePosition;
8 import org.opentrafficsim.core.network.Link;
9 import org.opentrafficsim.core.network.Network;
10 import org.opentrafficsim.core.network.OTSNetwork;
11 import org.opentrafficsim.imb.IMBException;
12 import org.opentrafficsim.imb.connector.Connector;
13 import org.opentrafficsim.imb.connector.Connector.IMBEventType;
14 import org.opentrafficsim.imb.transceiver.AbstractTransceiver;
15 import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
16 import org.opentrafficsim.road.network.lane.CrossSectionLink;
17 import org.opentrafficsim.road.network.lane.Lane;
18 import org.opentrafficsim.road.network.lane.object.sensor.Sensor;
19
20 import nl.tudelft.simulation.event.Event;
21 import nl.tudelft.simulation.event.EventInterface;
22 import nl.tudelft.simulation.event.EventType;
23 import nl.tudelft.simulation.event.TimedEvent;
24 import nl.tudelft.simulation.language.d3.DirectedPoint;
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
61
62
63
64
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202 public class SensorGTUTransceiver extends AbstractTransceiver
203 {
204
205 private static final long serialVersionUID = 20160918L;
206
207
208 private final OTSNetwork network;
209
210
211
212
213
214
215
216
217
218 public SensorGTUTransceiver(final Connector connector, final OTSDEVSSimulatorInterface simulator, final OTSNetwork network)
219 throws IMBException
220 {
221 super("Sensor_GTU", connector, simulator);
222 this.network = network;
223
224
225 addListeners();
226 }
227
228
229
230
231
232 private void addListeners() throws IMBException
233 {
234
235 this.network.addListener(this, Network.LINK_ADD_EVENT);
236 this.network.addListener(this, Network.LINK_REMOVE_EVENT);
237
238
239 for (Link link : this.network.getLinkMap().values())
240 {
241 try
242 {
243 this.notify(new TimedEvent<OTSSimTimeDouble>(Network.LINK_ADD_EVENT, this.network, link.getId(),
244 getSimulator().getSimulatorTime()));
245 }
246 catch (RemoteException exception)
247 {
248 throw new IMBException(exception);
249 }
250 }
251 }
252
253
254 @Override
255 public void notify(final EventInterface event) throws RemoteException
256 {
257 EventType type = event.getType();
258 if (type.equals(Network.LINK_ADD_EVENT))
259 {
260 Link link = this.network.getLink((String) event.getContent());
261 if (!(link instanceof CrossSectionLink))
262 {
263 System.err.println("SensorGTUTransceiver.notify(LINK_ADD) - Don't know how to handle a non-CrossSectionLink");
264 return;
265 }
266 CrossSectionLink csl = (CrossSectionLink) link;
267
268 csl.addListener(this, CrossSectionLink.LANE_ADD_EVENT);
269 csl.addListener(this, CrossSectionLink.LANE_REMOVE_EVENT);
270
271
272 for (Lane lane : csl.getLanes())
273 {
274 try
275 {
276 this.notify(new Event(CrossSectionLink.LANE_ADD_EVENT, csl, new Object[] { link.getNetwork().getId(),
277 link.getId(), lane.getId(), lane, csl.getLanes().indexOf(lane) }));
278 }
279 catch (RemoteException exception)
280 {
281 System.err.println("SensorGTUTransceiver.notify(LINK_ADD) - RemoteException: " + exception.getMessage());
282 return;
283 }
284 }
285 }
286
287 else if (type.equals(CrossSectionLink.LANE_ADD_EVENT))
288 {
289 Object[] content = (Object[]) event.getContent();
290 Lane lane = (Lane) content[3];
291
292 lane.addListener(this, Lane.SENSOR_ADD_EVENT);
293 lane.addListener(this, Lane.SENSOR_REMOVE_EVENT);
294
295
296 for (Sensor sensor : lane.getSensors())
297 {
298 try
299 {
300 this.notify(new TimedEvent<OTSSimTimeDouble>(Lane.SENSOR_ADD_EVENT, lane,
301 new Object[] { sensor.getId(), sensor }, getSimulator().getSimulatorTime()));
302 }
303 catch (RemoteException exception)
304 {
305 System.err.println("SensorGTUTransceiver.notify(LANE_ADD) - RemoteException: " + exception.getMessage());
306 return;
307 }
308 }
309 }
310
311 else if (type.equals(Lane.SENSOR_ADD_EVENT))
312 {
313 Object[] content = (Object[]) event.getContent();
314 Sensor sensor = (Sensor) content[1];
315 sensor.addListener(this, Sensor.SENSOR_TRIGGER_EVENT);
316
317 try
318 {
319 getConnector().postIMBMessage("Sensor_GTU", IMBEventType.NEW, transformNew(event));
320 }
321 catch (IMBException exception)
322 {
323 System.err.println("SensorGTUTransceiver.notify(SENSOR_ADD) - IMBException: " + exception.getMessage());
324 return;
325 }
326 }
327
328 else if (type.equals(Sensor.SENSOR_TRIGGER_EVENT))
329 {
330 try
331 {
332 getConnector().postIMBMessage("Sensor_GTU", IMBEventType.CHANGE, transformChange(event));
333 }
334 catch (IMBException exception)
335 {
336 System.err.println("SensorGTUTransceiver.notify(SENSOR_TRIGGER) - IMBException: " + exception.getMessage());
337 return;
338 }
339 }
340
341 else if (type.equals(Network.LINK_REMOVE_EVENT))
342 {
343 Link link = this.network.getLink((String) event.getContent());
344 if (!(link instanceof CrossSectionLink))
345 {
346 System.err
347 .println("SensorGTUTransceiver.notify(LINK_REMOVE) - Don't know how to handle a non-CrossSectionLink");
348 return;
349 }
350 CrossSectionLink csl = (CrossSectionLink) link;
351
352 csl.removeListener(this, CrossSectionLink.LANE_ADD_EVENT);
353 csl.removeListener(this, CrossSectionLink.LANE_REMOVE_EVENT);
354
355
356 for (Lane lane : csl.getLanes())
357 {
358 try
359 {
360 this.notify(new Event(CrossSectionLink.LANE_REMOVE_EVENT, csl,
361 new Object[] { link.getNetwork().getId(), link.getId(), lane.getId() }));
362 }
363 catch (RemoteException exception)
364 {
365 System.err.println("SensorGTUTransceiver.notify(LINK_REMOVE) - RemoteException: " + exception.getMessage());
366 return;
367 }
368 }
369 }
370
371 else if (type.equals(CrossSectionLink.LANE_REMOVE_EVENT))
372 {
373 Object[] content = (Object[]) event.getContent();
374 String laneId = (String) content[2];
375 CrossSectionLink csl = (CrossSectionLink) event.getSource();
376 Lane lane = (Lane) csl.getCrossSectionElement(laneId);
377
378 lane.removeListener(this, Lane.SENSOR_ADD_EVENT);
379 lane.removeListener(this, Lane.SENSOR_REMOVE_EVENT);
380
381
382 for (Sensor sensor : lane.getSensors())
383 {
384 try
385 {
386 this.notify(new TimedEvent<OTSSimTimeDouble>(Lane.SENSOR_REMOVE_EVENT, lane,
387 new Object[] { sensor.getId(), sensor }, getSimulator().getSimulatorTime()));
388 }
389 catch (RemoteException exception)
390 {
391 System.err.println("SensorGTUTransceiver.notify(LANE_REMOVE) - RemoteException: " + exception.getMessage());
392 return;
393 }
394 }
395
396
397 try
398 {
399 getConnector().postIMBMessage("Sensor_GTU", IMBEventType.DELETE, transformDelete(event));
400 }
401 catch (IMBException exception)
402 {
403 System.err.println("SensorGTUTransceiver.notify(LANE_REMOVE) - IMBException: " + exception.getMessage());
404 return;
405 }
406 }
407
408 else if (type.equals(Lane.SENSOR_REMOVE_EVENT))
409 {
410 Object[] content = (Object[]) event.getContent();
411 Sensor sensor = (Sensor) content[1];
412 sensor.removeListener(this, Sensor.SENSOR_TRIGGER_EVENT);
413
414 try
415 {
416 getConnector().postIMBMessage("Sensor_GTU", IMBEventType.DELETE, transformDelete(event));
417 }
418 catch (IMBException exception)
419 {
420 System.err.println("SensorGTUTransceiver.notify(SENSOR_REMOVE) - IMBException: " + exception.getMessage());
421 return;
422 }
423 }
424
425 else
426 {
427 System.err.println("SensorGTUTransceiver.notify - Unhandled event: " + event);
428 }
429 }
430
431
432
433
434
435
436 private Object[] transformNew(final EventInterface event)
437 {
438 if (Lane.SENSOR_ADD_EVENT.equals(event.getType()))
439 {
440
441 Object[] content = (Object[]) event.getContent();
442 String sensorId = (String) content[0];
443 Sensor sensor = (Sensor) content[1];
444 Lane lane = sensor.getLane();
445 double longitudinalPosition = sensor.getLongitudinalPosition().si;
446 double length = 0.0;
447 DirectedPoint pos = sensor.getLocation();
448 String triggerPosition = sensor.getPositionType().toString();
449 double timestamp = getSimulator().getSimulatorTime().getTime().si;
450 return new Object[] { timestamp, this.network.getId(), lane.getParentLink().getId(), lane.getId(), sensorId,
451 longitudinalPosition, length, pos.x, pos.y, pos.z, triggerPosition };
452 }
453 System.err.println("SensorGTUTransceiver.transformNew: Don't know how to transform event " + event);
454 return new Object[] {};
455 }
456
457
458
459
460
461
462 private Object[] transformChange(final EventInterface event)
463 {
464 if (Sensor.SENSOR_TRIGGER_EVENT.equals(event.getType()))
465 {
466
467 Object[] content = (Object[]) event.getContent();
468 String sensorId = (String) content[0];
469 Sensor sensor = (Sensor) content[1];
470 Lane lane = sensor.getLane();
471 LaneBasedGTU gtu = (LaneBasedGTU) content[2];
472 String gtuId = gtu.getId();
473 double gtuSpeed = gtu.getSpeed().si;
474 String triggerPosition = ((RelativePosition.TYPE) content[3]).toString();
475 double timestamp = getSimulator().getSimulatorTime().getTime().si;
476 return new Object[] { timestamp, this.network.getId(), lane.getParentLink().getId(), lane.getId(), sensorId, gtuId,
477 gtuSpeed, triggerPosition };
478 }
479 System.err.println("SensorGTUTransceiver.transformChange: Don't know how to transform event " + event);
480 return new Object[] {};
481 }
482
483
484
485
486
487
488 private Object[] transformDelete(final EventInterface event)
489 {
490 if (Lane.SENSOR_REMOVE_EVENT.equals(event.getType()))
491 {
492
493 Object[] content = (Object[]) event.getContent();
494 String sensorId = (String) content[0];
495 Sensor sensor = (Sensor) content[1];
496 Lane lane = sensor.getLane();
497 double timestamp = getSimulator().getSimulatorTime().getTime().si;
498 return new Object[] { timestamp, this.network.getId(), lane.getParentLink().getId(), lane.getId(), sensorId };
499 }
500 System.err.println("SensorGTUTransceiver.transformDelete: Don't know how to transform event " + event);
501 return new Object[] {};
502 }
503
504 }