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.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
6 import org.opentrafficsim.core.network.NetworkException;
7 import org.opentrafficsim.road.network.lane.Lane;
8 import org.opentrafficsim.road.network.lane.object.AbstractLaneBasedObject;
9 import org.opentrafficsim.road.network.lane.object.LaneBasedObject;
10
11 import nl.tudelft.simulation.language.Throw;
12
13 /**
14 * Basic, abstract implementation of a traffic light.
15 * <p>
16 * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
17 * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
18 * </p>
19 * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
20 * initial version Nov 30, 2015 <br>
21 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
22 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
23 */
24 public abstract class AbstractTrafficLight extends AbstractLaneBasedObject implements TrafficLight
25 {
26 /** */
27 private static final long serialVersionUID = 201601001L;
28
29 /** The color of the traffic light. */
30 private TrafficLightColor trafficLightColor;
31
32 /** The simulator to schedule events on. */
33 private final OTSDEVSSimulatorInterface simulator;
34
35 /** Default elevation of a traffic light (above zero; don't use this for lanes at non-zero elevation). */
36 public static final Length DEFAULT_TRAFFICLIGHT_ELEVATION = new Length(1, LengthUnit.METER);
37
38 /**
39 * Construct an AbstractTrafficLight with specified elevation.
40 * @param id traffic light id
41 * @param lane lane where the traffic light is located
42 * @param longitudinalPosition position of the traffic light on the lane, in the design direction
43 * @param simulator the simulator for animation and timed events
44 * @param height Length; the elevation of the traffic light
45 * @throws NetworkException on failure to place the object
46 */
47 public AbstractTrafficLight(final String id, final Lane lane, final Length longitudinalPosition,
48 final OTSDEVSSimulatorInterface simulator, final Length height) throws NetworkException
49 {
50 super(id, lane, longitudinalPosition, LaneBasedObject.makeGeometry(lane, longitudinalPosition), height);
51
52 Throw.whenNull(simulator, "Simulator may not be null");
53 this.simulator = simulator;
54
55 this.trafficLightColor = TrafficLightColor.RED;
56 }
57
58 /**
59 * Construct an AbstractTrafficLight at default elevation (use only on roads at elevation 0).
60 * @param id traffic light id
61 * @param lane lane where the traffic light is located
62 * @param longitudinalPosition position of the traffic light on the lane, in the design direction
63 * @param simulator 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 OTSDEVSSimulatorInterface 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 }, this.simulator
85 .getSimulatorTime().get());
86 }
87
88 }