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.djunits.value.vdouble.vector.PositionVector;
9 import org.djutils.draw.point.OrientedPoint2d;
10 import org.djutils.metadata.MetaData;
11 import org.djutils.metadata.ObjectDescriptor;
12 import org.djutils.serialization.SerializationException;
13 import org.opentrafficsim.core.gtu.Gtu;
14 import org.opentrafficsim.core.network.Network;
15 import org.sim0mq.Sim0MQException;
16
17
18
19
20
21
22
23
24
25
26
27 public class GtuTransceiver extends AbstractEventTransceiver
28 {
29
30 private final Network network;
31
32
33 private final TransceiverInterface gtuIdSource;
34
35
36
37
38
39
40 public GtuTransceiver(final Network network, final GtuIdTransceiver gtuIdSource)
41 {
42 super("GTU transceiver", new MetaData("GTU id", "GTU id",
43 new ObjectDescriptor[] {new ObjectDescriptor("GTU id", "GTU id", String.class)}), Gtu.MOVE_EVENT);
44 this.network = network;
45 this.gtuIdSource = gtuIdSource;
46 }
47
48
49 @Override
50 public final TransceiverInterface getIdSource(final int addressLevel, final ReturnWrapper returnWrapper)
51 throws Sim0MQException, SerializationException
52 {
53 if (addressLevel != 0)
54 {
55 returnWrapper.encodeReplyAndTransmit("Only empty address is valid");
56 throw new IndexOutOfBoundsException("Only empty address is valid");
57 }
58 return this.gtuIdSource;
59 }
60
61
62 @Override
63 public boolean hasIdSource()
64 {
65 return true;
66 }
67
68
69 @Override
70 public final Object[] get(final Object[] address, final ReturnWrapper returnWrapper)
71 throws RemoteException, Sim0MQException, SerializationException
72 {
73 String bad = verifyMetaData(getAddressFields(), address);
74 if (bad != null)
75 {
76 returnWrapper.nack(bad);
77 return null;
78 }
79
80 Gtu gtu = this.network.getGTU((String) address[0]);
81 if (null == gtu)
82 {
83 returnWrapper.nack("No GTU found with id \"" + address[0] + "\"");
84 return null;
85 }
86 OrientedPoint2d gtuPosition = gtu.getLocation();
87 return new Object[] {gtu.getId(), gtu.getType().getId(),
88 new PositionVector(new double[] {gtuPosition.x, gtuPosition.y}, PositionUnit.METER),
89 new Direction(gtuPosition.getDirZ(), DirectionUnit.EAST_DEGREE), gtu.getSpeed(), gtu.getAcceleration()};
90 }
91
92
93 @Override
94 public String toString()
95 {
96 return "GtuTransceiver [network=" + this.network.getId() + ", super=" + super.toString() + "]";
97 }
98
99 }