View Javadoc
1   package org.opentrafficsim.road.gtu.lane.object;
2   
3   import java.rmi.RemoteException;
4   
5   import javax.naming.NamingException;
6   
7   import nl.tudelft.simulation.dsol.SimRuntimeException;
8   
9   import org.djunits.unit.TimeUnit;
10  import org.djunits.value.vdouble.scalar.Length;
11  import org.djunits.value.vdouble.scalar.Time;
12  import org.opentrafficsim.core.dsol.OTSAnimatorInterface;
13  import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
14  import org.opentrafficsim.core.geometry.OTSGeometryException;
15  import org.opentrafficsim.core.gtu.GTUException;
16  import org.opentrafficsim.core.network.NetworkException;
17  import org.opentrafficsim.core.network.OTSNetwork;
18  import org.opentrafficsim.road.gtu.lane.object.animation.DefaultBlockOnOffAnimation;
19  import org.opentrafficsim.road.network.lane.Lane;
20  
21  /**
22   * <p>
23   * Copyright (c) 2013-2015 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
24   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
25   * </p>
26   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
27   * initial version Jan 6, 2016 <br>
28   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
29   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
30   */
31  public class LaneBlockOnOff extends AbstractTrafficLight
32  {
33      /** */
34      private static final long serialVersionUID = 1L;
35  
36      /**
37       * @param name the name or id of the traffic light
38       * @param lane The lane where the block has to be put
39       * @param position the position on the lane as a length
40       * @param simulator the simulator to avoid NullPointerExceptions
41       * @param network the network that the GTU is initially registered in
42       * @throws GTUException when GTU cannot be created.
43       * @throws NamingException if an error occurs when adding the animation handler
44       * @throws NetworkException when the GTU cannot be placed on the given lane
45       * @throws OTSGeometryException x
46       * @throws SimRuntimeException x
47       */
48      public LaneBlockOnOff(final String name, final Lane lane, final Length.Rel position,
49          final OTSDEVSSimulatorInterface simulator, final OTSNetwork network) throws GTUException, NetworkException,
50          NamingException, SimRuntimeException, OTSGeometryException
51      {
52          super(name, lane, position, simulator, network);
53  
54          try
55          {
56              new DefaultBlockOnOffAnimation(this, getSimulator());
57              // animation
58              if (simulator instanceof OTSAnimatorInterface)
59              {
60                  // TODO
61              }
62              getSimulator().scheduleEventRel(new Time.Rel(30.0, TimeUnit.SECOND), this, this, "changeColorTime", null);
63          }
64          catch (RemoteException exception)
65          {
66              throw new NetworkException(exception);
67          }
68      }
69  
70      /**
71       * Update the block and re-schedule.
72       */
73      protected void changeColorTime()
74      {
75          setBlocked(!isBlocked());
76  
77          try
78          {
79              getSimulator().scheduleEventRel(new Time.Rel(30.0, TimeUnit.SECOND), this, this, "changeColorTime", null);
80          }
81          catch (SimRuntimeException exception)
82          {
83              exception.printStackTrace();
84          }
85      }
86  
87      /** {@inheritDoc} */
88      @Override
89      public String toString()
90      {
91          return "LaneBlockOnOff [lane=" + this.laneTL + ", position=" + this.positionTL + "]";
92      }
93  
94  }