View Javadoc

1   package cz.cuni.amis.pogamut.usar2004.agent.module.master;
2   
3   import cz.cuni.amis.pogamut.base.agent.module.SensorModule;
4   import cz.cuni.amis.pogamut.base.communication.worldview.IWorldView;
5   import cz.cuni.amis.pogamut.base.communication.worldview.event.IWorldEventListener;
6   import cz.cuni.amis.pogamut.usar2004.agent.USAR2004Bot;
7   import cz.cuni.amis.pogamut.usar2004.agent.module.datatypes.SensorType;
8   import cz.cuni.amis.pogamut.usar2004.agent.module.sensor.SuperSensor;
9   import cz.cuni.amis.pogamut.usar2004.communication.messages.usarinfomessages.SensorMessage;
10  
11  /**
12   * Module that is used for particular Sensors. We can have multiple type of
13   * sensor mounted on our robot. We can use either
14   * SensorMasterModule/SensorMasterModuleQueued to gather all SEN messages or
15   * SensorSpecificModule to catch just the type we desire.
16   *
17   * @author vejmanm
18   */
19  public class SensorSpecificModule<Module extends SuperSensor> extends SensorModule<USAR2004Bot>
20  {
21      protected SensorMessageListener sensorListener;
22      protected Module sensor;
23      protected boolean flagReaded = false;
24  
25      /**
26       * Captures and provides data of the first sensor of respective type
27       *
28       * @param bot USAR2004Bot variable for creating sensor instance
29       * @param c Class definition of Generic class. Ideally we would want to say
30       * Module.class, but that is not possible
31       */
32      public SensorSpecificModule(USAR2004Bot bot, Class<Module> c)
33      {
34          super(bot);
35          sensorListener = new SensorMessageListener(worldView);
36          sensor = createContents(c);
37      }
38  
39      /**
40       * If we know both type and name of sensor we want to get data from - this
41       * ctor is the one to use.
42       *
43       * @param bot USAR2004Bot variable for creating sensor instance
44       * @param name Name of the module
45       * @param c Class definition of Generic class. Ideally we would want to say
46       * Module.class, but that is not possible
47       */
48      public SensorSpecificModule(USAR2004Bot bot, String name, Class<Module> c)
49      {
50          this(bot, c);
51          if(this.sensor != null)//updateMessage ensures that isAvailible returns true.
52          {
53              this.sensor.updateMessage(new SensorMessage(null, 0, name, 0, 0, 0, 0, 0, 0, 0, null, null, 0, false, 0, 0, 0));
54          }
55      }
56  
57      /**
58       * Creates a new Generic instance.
59       *
60       * @param clazz SuperSensor offspring
61       * @return Returns SuperSensor instance to be used for storing information
62       * about specific sensor.
63       */
64      Module createContents(Class<Module> clazz)
65      {
66          try
67          {
68              return (Module) ModuleInstanceProvider.getSensorInstanceByClass(clazz);
69          }
70          catch(Exception e)
71          {
72              System.out.println(e.getMessage());
73          }
74          return null;
75      }
76  
77      /**
78       * Collection of sensor data check.
79       *
80       * @return Returns false if either sensor collection is empty or null;
81       */
82      public Boolean isReady()
83      {
84          return sensor != null;
85      }
86  
87      /**
88       * Check method for making sure we don't use old value.
89       *
90       * @return Returns true if getModule() method was called sooner than a new
91       * message was listened and our sensor was updated.
92       */
93      public boolean isReaded()
94      {
95          return flagReaded;
96      }
97  
98      /**
99       * Returns module of specified type by generics. That means we can call
100      * methods on this without the need to cast.
101      *
102      * @return Returns info about type of sensor picked by generic.
103      */
104     public Module getModule()
105     {
106         flagReaded = true;
107         return sensor;
108     }
109 
110     /**
111      * Updates sensor data when either the sensor name was not specified(Than it
112      * monitors the first Sensor of this type) or the type and name matches.
113      *
114      * @param message
115      */
116     protected void fillModule(SensorMessage message)
117     {
118         if(sensor == null)
119         {
120             return;
121         }
122         if(this.sensor.getSensorType() == SensorType.getType(message.getType()))
123         {
124             if(!sensor.isReady() || this.sensor.getName().equalsIgnoreCase(message.getName()))
125             {
126                 flagReaded = false;
127                 this.sensor.updateMessage(message);
128             }
129         }
130     }
131 
132     @Override
133     protected void cleanUp()
134     {
135         super.cleanUp();
136         sensorListener = null;
137         sensor = null;
138     }
139 
140     private class SensorMessageListener implements IWorldEventListener<SensorMessage>
141     {
142         @Override
143         public void notify(SensorMessage event)
144         {
145             fillModule(event);
146         }
147 
148         public SensorMessageListener(IWorldView worldView)
149         {
150             worldView.addEventListener(SensorMessage.class, this);
151         }
152     }
153 }