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://github.com/peter-knoppers">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 the Network
38       * @param gtuIdSource 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      @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  }