View Javadoc
1   package org.opentrafficsim.imb.observers;
2   
3   import java.rmi.RemoteException;
4   
5   import org.opentrafficsim.core.gtu.GTU;
6   
7   import nl.tudelft.simulation.event.EventInterface;
8   import nl.tudelft.simulation.event.EventListenerInterface;
9   import nl.tudelft.simulation.language.d3.DirectedPoint;
10  
11  /**
12   * <p>
13   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
14   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
15   * </p>
16   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
17   * initial version Aug 28, 2016 <br>
18   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
19   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
20   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
21   */
22  public class IMBTransmitter implements EventListenerInterface
23  {
24      /** Observer for gtu move events. */
25      private Observer observer = null;
26  
27      /** */
28      public IMBTransmitter()
29      {
30          try
31          {
32              this.observer = new IMBObserver("localhost" /* "app-usimb01.westeurope.cloudapp.azure.com" */
33              /* "vps17642.public.cloudvps.com" */ /* "localhost" */, 4000, "GTUObserver", 1, "OTS_RT");
34              System.out.println("Observer is " + this.observer);
35          }
36          catch (Exception exception1)
37          {
38              System.out.println("Observer creation failed; GTU movements will not be sent to observer");
39          }
40      }
41  
42      /** {@inheritDoc} */
43      @Override
44      public void notify(final EventInterface event) throws RemoteException
45      {
46          if (this.observer == null)
47          {
48              return;
49          }
50  
51          if (event.getType().equals(GTU.MOVE_EVENT))
52          {
53              Object[] moveInfo = (Object[]) event.getContent();
54              DirectedPoint location = (DirectedPoint) moveInfo[1];
55              try
56              {
57                  this.observer.postMessage("GTU", Observer.CHANGE,
58                          new Object[] { moveInfo[0].toString(), location.x, location.y, location.z, location.getRotZ() });
59              }
60              catch (Exception exception)
61              {
62                  exception.printStackTrace();
63              }
64          }
65          else if (event.getType().equals(GTU.DESTROY_EVENT))
66          {
67              Object[] destroyInfo = (Object[]) event.getContent();
68              DirectedPoint location = (DirectedPoint) destroyInfo[1];
69              try
70              {
71                  this.observer.postMessage("GTU", Observer.DELETE,
72                          new Object[] { destroyInfo[0].toString(), location.x, location.y, location.z, location.getRotZ() });
73              }
74              catch (Exception exception)
75              {
76                  exception.printStackTrace();
77              }
78          }
79      }
80  }
81  
82