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
19
20
21
22
23
24
25
26 public class TrafficLight extends AbstractLaneBasedObject
27 {
28
29 private static final long serialVersionUID = 20230216L;
30
31
32 private TrafficLightColor trafficLightColor;
33
34
35 private final OtsSimulatorInterface simulator;
36
37
38 public static final Length DEFAULT_TRAFFICLIGHT_ELEVATION = new Length(1, LengthUnit.METER);
39
40
41
42
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
52
53
54
55
56
57
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
73
74
75
76
77
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
87
88
89 public final TrafficLightColor getTrafficLightColor()
90 {
91 return this.trafficLightColor;
92 }
93
94
95
96
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
106 @Override
107 public double getZ() throws RemoteException
108 {
109 return -0.0001;
110 }
111
112
113 @Override
114 @SuppressWarnings("checkstyle:designforextension")
115 public String toString()
116 {
117 return "SimpleTrafficLight [trafficLightColor=" + getTrafficLightColor() + "]";
118 }
119
120 }