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 @Override
49 public final TransceiverInterface getIdSource(final int addressLevel, final ReturnWrapper returnWrapper)
50 throws Sim0MQException, SerializationException
51 {
52 if (addressLevel != 0)
53 {
54 returnWrapper.encodeReplyAndTransmit("Only empty address is valid");
55 throw new IndexOutOfBoundsException("Only empty address is valid");
56 }
57 return this.gtuIdSource;
58 }
59
60 @Override
61 public boolean hasIdSource()
62 {
63 return true;
64 }
65
66 @Override
67 public final Object[] get(final Object[] address, final ReturnWrapper returnWrapper)
68 throws RemoteException, Sim0MQException, SerializationException
69 {
70 String bad = verifyMetaData(getAddressFields(), address);
71 if (bad != null)
72 {
73 returnWrapper.nack(bad);
74 return null;
75 }
76
77 Gtu gtu = this.network.getGTU((String) address[0]);
78 if (null == gtu)
79 {
80 returnWrapper.nack("No GTU found with id \"" + address[0] + "\"");
81 return null;
82 }
83 OrientedPoint2d gtuPosition = gtu.getLocation();
84 return new Object[] {gtu.getId(), gtu.getType().getId(),
85 new PositionVector(new double[] {gtuPosition.x, gtuPosition.y}, PositionUnit.METER),
86 new Direction(gtuPosition.getDirZ(), DirectionUnit.EAST_DEGREE), gtu.getSpeed(), gtu.getAcceleration()};
87 }
88
89 @Override
90 public String toString()
91 {
92 return "GtuTransceiver [network=" + this.network.getId() + ", super=" + super.toString() + "]";
93 }
94
95 }