View Javadoc
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   * Transceiver for GTU data.
20   * <p>
21   * Copyright (c) 2020-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
22   * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
23   * </p>
24   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
25   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
26   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
27   */
28  public class GTUTransceiver extends AbstractEventTransceiver
29  {
30      /** The network. */
31      private final OTSNetwork network;
32  
33      /** Transceiver for the GTU ids. */
34      private final TransceiverInterface gtuIdSource;
35  
36      /**
37       * Construct a GTUTransceiver.
38       * @param network Network; the Network
39       * @param gtuIdSource GTUIdTransceiver; the transceiver that can produce all active GTU ids in the Network
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      /** {@inheritDoc} */
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      /** {@inheritDoc} */
63      @Override
64      public boolean hasIdSource()
65      {
66          return true;
67      }
68  
69      /** {@inheritDoc} */
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      /** {@inheritDoc} */
94      @Override
95      public String toString()
96      {
97          return "GTUTransceiver [network=" + this.network.getId() + ", super=" + super.toString() + "]";
98      }
99  
100 }