GTUTransceiver.java

  1. package org.opentrafficsim.sim0mq.publisher;

  2. import java.rmi.RemoteException;

  3. import org.djunits.unit.DirectionUnit;
  4. import org.djunits.unit.PositionUnit;
  5. import org.djunits.value.vdouble.scalar.Direction;
  6. import org.djutils.metadata.MetaData;
  7. import org.djutils.metadata.ObjectDescriptor;
  8. import org.djutils.serialization.SerializationException;
  9. import org.opentrafficsim.core.geometry.OTSPoint3D;
  10. import org.opentrafficsim.core.gtu.GTU;
  11. import org.opentrafficsim.core.network.OTSNetwork;
  12. import org.sim0mq.Sim0MQException;

  13. import nl.tudelft.simulation.language.d3.DirectedPoint;

  14. /**
  15.  * Transceiver for GTU data.
  16.  * <p>
  17.  * Copyright (c) 2020-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  18.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
  19.  * </p>
  20.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  21.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  22.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  23.  */
  24. public class GTUTransceiver extends AbstractEventTransceiver
  25. {
  26.     /** The network. */
  27.     private final OTSNetwork network;

  28.     /** Transceiver for the GTU ids. */
  29.     private final TransceiverInterface gtuIdSource;

  30.     /**
  31.      * Construct a GTUTransceiver.
  32.      * @param network Network; the Network
  33.      * @param gtuIdSource GTUIdTransceiver; the transceiver that can produce all active GTU ids in the Network
  34.      */
  35.     public GTUTransceiver(final OTSNetwork network, final GTUIdTransceiver gtuIdSource)
  36.     {
  37.         super("GTU transceiver", new MetaData("GTU id", "GTU id",
  38.                 new ObjectDescriptor[] { new ObjectDescriptor("GTU id", "GTU id", String.class) }), GTU.MOVE_EVENT);
  39.         this.network = network;
  40.         this.gtuIdSource = gtuIdSource;
  41.     }

  42.     /** {@inheritDoc} */
  43.     @Override
  44.     public final TransceiverInterface getIdSource(final int addressLevel, final ReturnWrapper returnWrapper)
  45.             throws Sim0MQException, SerializationException
  46.     {
  47.         if (addressLevel != 0)
  48.         {
  49.             returnWrapper.encodeReplyAndTransmit("Only empty address is valid");
  50.             throw new IndexOutOfBoundsException("Only empty address is valid");
  51.         }
  52.         return this.gtuIdSource;
  53.     }

  54.     /** {@inheritDoc} */
  55.     @Override
  56.     public boolean hasIdSource()
  57.     {
  58.         return true;
  59.     }

  60.     /** {@inheritDoc} */
  61.     @Override
  62.     public final Object[] get(final Object[] address, final ReturnWrapper returnWrapper)
  63.             throws RemoteException, Sim0MQException, SerializationException
  64.     {
  65.         String bad = verifyMetaData(getAddressFields(), address);
  66.         if (bad != null)
  67.         {
  68.             returnWrapper.nack(bad);
  69.             return null;
  70.         }

  71.         GTU gtu = this.network.getGTU((String) address[0]);
  72.         if (null == gtu)
  73.         {
  74.             returnWrapper.nack("No GTU found with id \"" + address[0] + "\"");
  75.             return null;
  76.         }
  77.         DirectedPoint gtuPosition = gtu.getLocation();
  78.         return new Object[] { gtu.getId(), gtu.getGTUType().getId(),
  79.                 new OTSPoint3D(gtuPosition).doubleVector(PositionUnit.METER),
  80.                 new Direction(gtuPosition.getRotZ(), DirectionUnit.EAST_DEGREE), gtu.getSpeed(), gtu.getAcceleration() };
  81.     }

  82.     /** {@inheritDoc} */
  83.     @Override
  84.     public String toString()
  85.     {
  86.         return "GTUTransceiver [network=" + this.network.getId() + ", super=" + super.toString() + "]";
  87.     }

  88. }