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