1 package org.opentrafficsim.sim0mq.publisher;
2
3 import java.rmi.RemoteException;
4
5 import org.djunits.unit.DirectionUnit;
6 import org.djunits.unit.PositionUnit;
7 import org.djunits.value.vdouble.scalar.Direction;
8 import org.djutils.metadata.MetaData;
9 import org.djutils.metadata.ObjectDescriptor;
10 import org.djutils.serialization.SerializationException;
11 import org.opentrafficsim.core.geometry.OTSPoint3D;
12 import org.opentrafficsim.core.gtu.GTU;
13 import org.opentrafficsim.core.network.OTSNetwork;
14 import org.sim0mq.Sim0MQException;
15
16 import nl.tudelft.simulation.language.d3.DirectedPoint;
17
18
19
20
21
22
23
24
25
26
27
28 public class GTUTransceiver extends AbstractEventTransceiver
29 {
30
31 private final OTSNetwork network;
32
33
34 private final TransceiverInterface gtuIdSource;
35
36
37
38
39
40
41 public GTUTransceiver(final OTSNetwork network, final GTUIdTransceiver gtuIdSource)
42 {
43 super("GTU transceiver", new MetaData("GTU id", "GTU id",
44 new ObjectDescriptor[] { new ObjectDescriptor("GTU id", "GTU id", String.class) }), GTU.MOVE_EVENT);
45 this.network = network;
46 this.gtuIdSource = gtuIdSource;
47 }
48
49
50 @Override
51 public final TransceiverInterface getIdSource(final int addressLevel, final ReturnWrapper returnWrapper)
52 throws Sim0MQException, SerializationException
53 {
54 if (addressLevel != 0)
55 {
56 returnWrapper.encodeReplyAndTransmit("Only empty address is valid");
57 throw new IndexOutOfBoundsException("Only empty address is valid");
58 }
59 return this.gtuIdSource;
60 }
61
62
63 @Override
64 public boolean hasIdSource()
65 {
66 return true;
67 }
68
69
70 @Override
71 public final Object[] get(final Object[] address, final ReturnWrapper returnWrapper)
72 throws RemoteException, Sim0MQException, SerializationException
73 {
74 String bad = verifyMetaData(getAddressFields(), address);
75 if (bad != null)
76 {
77 returnWrapper.nack(bad);
78 return null;
79 }
80
81 GTU gtu = this.network.getGTU((String) address[0]);
82 if (null == gtu)
83 {
84 returnWrapper.nack("No GTU found with id \"" + address[0] + "\"");
85 return null;
86 }
87 DirectedPoint gtuPosition = gtu.getLocation();
88 return new Object[] { gtu.getId(), gtu.getGTUType().getId(),
89 new OTSPoint3D(gtuPosition).doubleVector(PositionUnit.METER),
90 new Direction(gtuPosition.getRotZ(), DirectionUnit.EAST_DEGREE), gtu.getSpeed(), gtu.getAcceleration() };
91 }
92
93
94 @Override
95 public String toString()
96 {
97 return "GTUTransceiver [network=" + this.network.getId() + ", super=" + super.toString() + "]";
98 }
99
100 }