View Javadoc
1   package org.opentrafficsim.sim0mq.publisher;
2   
3   import org.djutils.exceptions.Throw;
4   import org.djutils.immutablecollections.ImmutableList;
5   import org.djutils.metadata.MetaData;
6   import org.djutils.metadata.ObjectDescriptor;
7   import org.djutils.serialization.SerializationException;
8   import org.opentrafficsim.core.gtu.Gtu;
9   import org.opentrafficsim.core.network.Link;
10  import org.opentrafficsim.core.network.Network;
11  import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
12  import org.opentrafficsim.road.network.lane.CrossSectionElement;
13  import org.opentrafficsim.road.network.lane.CrossSectionLink;
14  import org.opentrafficsim.road.network.lane.Lane;
15  import org.sim0mq.Sim0MQException;
16  
17  /**
18   * Transceiver for the ids of the GTUs on a link.
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 LaneGtuIdTransceiver extends AbstractTransceiver
28  {
29      /** The network. */
30      private final Network network;
31  
32      /**
33       * Construct a GtuIdTransceiver.
34       * @param network Network; the OTS network
35       */
36      public LaneGtuIdTransceiver(final Network network)
37      {
38          super("Lane GTU id transceiver",
39                  new MetaData("Link id, lane id", "Link id, lane id",
40                          new ObjectDescriptor[] {new ObjectDescriptor("Link id", "Link id", String.class),
41                                  new ObjectDescriptor("Lane id", "Lane id", String.class)}),
42                  new MetaData("String array", "String array filled with all currently valid GTU ids on the lane",
43                          new ObjectDescriptor[] {new ObjectDescriptor("String array",
44                                  "String array filled with all currently valid GTU ids on the lane", String[].class)}));
45          Throw.whenNull(network, "Network may not be null");
46          this.network = network;
47      }
48  
49      /** {@inheritDoc} */
50      @Override
51      public final Object[] get(final Object[] address, final ReturnWrapper returnWrapper)
52              throws Sim0MQException, SerializationException
53      {
54          String bad = verifyMetaData(getAddressFields(), address);
55          if (bad != null)
56          {
57              returnWrapper.nack("Bad address; need id of a link and id of a CrossSectionElement");
58              return null;
59          }
60          Link link = this.network.getLink((String) address[0]);
61          if (null == link || (!(link instanceof CrossSectionLink)))
62          {
63              returnWrapper.nack("Network does not contain a link with id " + address[0]);
64              return null;
65          }
66          CrossSectionLink csl = (CrossSectionLink) link;
67          CrossSectionElement cse = csl.getCrossSectionElement((String) address[1]);
68          if (null == cse)
69          {
70              returnWrapper.nack("Link " + address[0] + " does not contain a cross section element with id " + address[1]);
71              return null;
72          }
73          if (!(cse instanceof Lane))
74          {
75              returnWrapper.nack("CrossSectionElement " + address[1] + " of link with id " + address[0] + ", is not a lane");
76              return null;
77          }
78          Lane lane = (Lane) cse;
79          ImmutableList<LaneBasedGtu> gtus = lane.getGtuList();
80          Object[] result = new Object[gtus.size()];
81          int nextIndex = 0;
82          for (Gtu gtu : gtus)
83          {
84              result[nextIndex++] = gtu.getId();
85          }
86          return result;
87      }
88  
89      /** {@inheritDoc} */
90      @Override
91      public String toString()
92      {
93          return "LaneGtuIdTransceiver [network=" + this.network + ", super=" + super.toString() + "]";
94      }
95  
96  }