1 package org.opentrafficsim.road.network.lane.object.trafficlight;
2
3 import org.djunits.unit.LengthUnit;
4 import org.djunits.value.vdouble.scalar.Length;
5 import org.djutils.exceptions.Throw;
6 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
7 import org.opentrafficsim.core.network.NetworkException;
8 import org.opentrafficsim.road.network.lane.Lane;
9 import org.opentrafficsim.road.network.lane.object.AbstractLaneBasedObject;
10 import org.opentrafficsim.road.network.lane.object.LaneBasedObject;
11
12 /**
13 * Basic, abstract implementation of a traffic light.
14 * <p>
15 * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
16 * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
17 * </p>
18 * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
19 * initial version Nov 30, 2015 <br>
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 */
23 public abstract class AbstractTrafficLight extends AbstractLaneBasedObject implements TrafficLight
24 {
25 /** */
26 private static final long serialVersionUID = 201601001L;
27
28 /** The color of the traffic light. */
29 private TrafficLightColor trafficLightColor;
30
31 /** The simulator to schedule events on. */
32 private final OTSSimulatorInterface simulator;
33
34 /** Default elevation of a traffic light (above zero; don't use this for lanes at non-zero elevation). */
35 public static final Length DEFAULT_TRAFFICLIGHT_ELEVATION = new Length(1, LengthUnit.METER);
36
37 /**
38 * Construct an AbstractTrafficLight with specified elevation.
39 * @param id String; traffic light id
40 * @param lane Lane; lane where the traffic light is located
41 * @param longitudinalPosition Length; position of the traffic light on the lane, in the design direction
42 * @param simulator OTSSimulatorInterface; the simulator for animation and timed events
43 * @param height Length; the elevation of the traffic light
44 * @throws NetworkException on failure to place the object
45 */
46 public AbstractTrafficLight(final String id, final Lane lane, final Length longitudinalPosition,
47 final OTSSimulatorInterface simulator, final Length height) throws NetworkException
48 {
49 super(id, lane, longitudinalPosition, LaneBasedObject.makeGeometry(lane, longitudinalPosition), height);
50
51 Throw.whenNull(simulator, "Simulator may not be null");
52 this.simulator = simulator;
53 this.trafficLightColor = TrafficLightColor.RED;
54
55 init();
56 }
57
58 /**
59 * Construct an AbstractTrafficLight at default elevation (use only on roads at elevation 0).
60 * @param id String; traffic light id
61 * @param lane Lane; lane where the traffic light is located
62 * @param longitudinalPosition Length; position of the traffic light on the lane, in the design direction
63 * @param simulator OTSSimulatorInterface; the simulator for animation and timed events
64 * @throws NetworkException on failure to place the object
65 */
66 public AbstractTrafficLight(final String id, final Lane lane, final Length longitudinalPosition,
67 final OTSSimulatorInterface simulator) throws NetworkException
68 {
69 this(id, lane, longitudinalPosition, simulator, DEFAULT_TRAFFICLIGHT_ELEVATION);
70 }
71
72 /** {@inheritDoc} */
73 @Override
74 public final TrafficLightColor getTrafficLightColor()
75 {
76 return this.trafficLightColor;
77 }
78
79 /** {@inheritDoc} */
80 @Override
81 public final void setTrafficLightColor(final TrafficLightColor trafficLightColor)
82 {
83 this.trafficLightColor = trafficLightColor;
84 fireTimedEvent(TRAFFICLIGHT_CHANGE_EVENT, new Object[] {getId(), this, trafficLightColor},
85 this.simulator.getSimulatorTime());
86 }
87
88 }