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://github.com/peter-knoppers">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 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      @Override
50      public final Object[] get(final Object[] address, final ReturnWrapper returnWrapper)
51              throws Sim0MQException, SerializationException
52      {
53          String bad = verifyMetaData(getAddressFields(), address);
54          if (bad != null)
55          {
56              returnWrapper.nack("Bad address; need id of a link and id of a CrossSectionElement");
57              return null;
58          }
59          Link link = this.network.getLink((String) address[0]);
60          if (null == link || (!(link instanceof CrossSectionLink)))
61          {
62              returnWrapper.nack("Network does not contain a link with id " + address[0]);
63              return null;
64          }
65          CrossSectionLink csl = (CrossSectionLink) link;
66          CrossSectionElement cse = csl.getCrossSectionElement((String) address[1]);
67          if (null == cse)
68          {
69              returnWrapper.nack("Link " + address[0] + " does not contain a cross section element with id " + address[1]);
70              return null;
71          }
72          if (!(cse instanceof Lane))
73          {
74              returnWrapper.nack("CrossSectionElement " + address[1] + " of link with id " + address[0] + ", is not a lane");
75              return null;
76          }
77          Lane lane = (Lane) cse;
78          ImmutableList<LaneBasedGtu> gtus = lane.getGtuList();
79          Object[] result = new Object[gtus.size()];
80          int nextIndex = 0;
81          for (Gtu gtu : gtus)
82          {
83              result[nextIndex++] = gtu.getId();
84          }
85          return result;
86      }
87  
88      @Override
89      public String toString()
90      {
91          return "LaneGtuIdTransceiver [network=" + this.network + ", super=" + super.toString() + "]";
92      }
93  
94  }