View Javadoc
1   package org.opentrafficsim.road.network.lane.object.trafficlight;
2   
3   import java.rmi.RemoteException;
4   
5   import org.djunits.unit.LengthUnit;
6   import org.djunits.value.vdouble.scalar.Length;
7   import org.djutils.event.EventType;
8   import org.djutils.exceptions.Throw;
9   import org.djutils.metadata.MetaData;
10  import org.djutils.metadata.ObjectDescriptor;
11  import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
12  import org.opentrafficsim.core.network.NetworkException;
13  import org.opentrafficsim.road.network.lane.Lane;
14  import org.opentrafficsim.road.network.lane.object.AbstractLaneBasedObject;
15  import org.opentrafficsim.road.network.lane.object.LaneBasedObject;
16  
17  /**
18   * Standard implementation of a traffic light.
19   * <p>
20   * Copyright (c) 2013-2023 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   */
26  public class TrafficLight extends AbstractLaneBasedObject
27  {
28      /** */
29      private static final long serialVersionUID = 20230216L;
30  
31      /** The color of the traffic light. */
32      private TrafficLightColor trafficLightColor;
33  
34      /** The simulator to schedule events on. */
35      private final OtsSimulatorInterface simulator;
36  
37      /** Default elevation of a traffic light (above zero; don't use this for lanes at non-zero elevation). */
38      public static final Length DEFAULT_TRAFFICLIGHT_ELEVATION = new Length(1, LengthUnit.METER);
39  
40      /**
41       * The <b>timed</b> event type for pub/sub indicating the change of color of a traffic light. <br>
42       * Payload: Object[] {String trafficLightId, TrafficLight trafficLight, TrafficLightColor newColor}
43       */
44      public static final EventType TRAFFICLIGHT_CHANGE_EVENT = new EventType("TRAFFICLIGHT.CHANGE",
45              new MetaData("Traffic light changed", "Color of traffic light has changed",
46                      new ObjectDescriptor("Traffic light id", "Id of the traffic light", String.class),
47                      new ObjectDescriptor("Traffic light", "The traffic light itself", TrafficLight.class),
48                      new ObjectDescriptor("Traffic light color", "New traffic light color", TrafficLightColor.class)));
49  
50      /**
51       * Construct an AbstractTrafficLight with specified elevation.
52       * @param id String; traffic light id
53       * @param lane Lane; lane where the traffic light is located
54       * @param longitudinalPosition Length; position of the traffic light on the lane, in the design direction
55       * @param simulator OtsSimulatorInterface; the simulator for animation and timed events
56       * @param height Length; the elevation of the traffic light
57       * @throws NetworkException on failure to place the object
58       */
59      public TrafficLight(final String id, final Lane lane, final Length longitudinalPosition,
60              final OtsSimulatorInterface simulator, final Length height) throws NetworkException
61      {
62          super(id, lane, longitudinalPosition, LaneBasedObject.makeGeometry(lane, longitudinalPosition), height);
63  
64          Throw.whenNull(simulator, "Simulator may not be null");
65          this.simulator = simulator;
66          this.trafficLightColor = TrafficLightColor.RED;
67  
68          init();
69      }
70  
71      /**
72       * Construct an AbstractTrafficLight at default elevation (use only on roads at elevation 0).
73       * @param id String; traffic light id
74       * @param lane Lane; lane where the traffic light is located
75       * @param longitudinalPosition Length; position of the traffic light on the lane, in the design direction
76       * @param simulator OtsSimulatorInterface; the simulator for animation and timed events
77       * @throws NetworkException on failure to place the object
78       */
79      public TrafficLight(final String id, final Lane lane, final Length longitudinalPosition,
80              final OtsSimulatorInterface simulator) throws NetworkException
81      {
82          this(id, lane, longitudinalPosition, simulator, DEFAULT_TRAFFICLIGHT_ELEVATION);
83      }
84  
85      /**
86       * Get the current traffic light color.
87       * @return TrafficLightColor; current traffic light color.
88       */
89      public final TrafficLightColor getTrafficLightColor()
90      {
91          return this.trafficLightColor;
92      }
93  
94      /**
95       * Set the new traffic light color.
96       * @param trafficLightColor TrafficLightColor; set the trafficLightColor
97       */
98      public final void setTrafficLightColor(final TrafficLightColor trafficLightColor)
99      {
100         this.trafficLightColor = trafficLightColor;
101         fireTimedEvent(TRAFFICLIGHT_CHANGE_EVENT, new Object[] {getId(), this, trafficLightColor},
102                 this.simulator.getSimulatorTime());
103     }
104 
105     /** {@inheritDoc} */
106     @Override
107     public double getZ() throws RemoteException
108     {
109         return -0.0001;
110     }
111 
112     /** {@inheritDoc} */
113     @Override
114     @SuppressWarnings("checkstyle:designforextension")
115     public String toString()
116     {
117         return "SimpleTrafficLight [trafficLightColor=" + getTrafficLightColor() + "]";
118     }
119 
120 }