View Javadoc

1   package cz.cuni.amis.pogamut.usar2004.communication.messages.usarinfomessages;
2   
3   import java.util.*;
4   import cz.cuni.amis.pogamut.base.communication.worldview.event.*;
5   import cz.cuni.amis.pogamut.base.communication.worldview.object.*;
6   import cz.cuni.amis.pogamut.base.communication.translator.event.*;
7   import cz.cuni.amis.pogamut.usar2004.communication.messages.*;
8   import cz.cuni.amis.pogamut.usar2004.communication.messages.datatypes.CustomTypes.*;
9   
10  /**
11   *
12   * Response message reports the message that user's been asking for. Please note
13   * that the robot Response message parameters depend on the type of robot that
14   * you are driving. For example, a robot of type “GroundVehicle” will not have
15   * the same Response message as a robot of type “AerialVehicle.” * Corresponding
16   * GameBots message is RES.
17   *
18   *
19   *
20   * A response message is delivered to describe the status of a command that has
21   * been sent to USARSim. Response messages are used so that users can tell
22   * whether or not particular commands were successfully executed. There are
23   * three different response messages. The first response message is issued after
24   * a SET {Type Viewports} command. The second response messaged is issued after
25   * a SET {Type Camera} command. The third response message is a generic message
26   * used for sensors and effecters. After a SET {Type Viewports} command, the
27   * response message will be issued.
28   *
29   */
30  public class ResponseMessage extends GBEvent implements IWorldEvent, IWorldChangeEvent
31  {
32      public ResponseMessage(double Time, String Type, String Config, String Status)
33      {
34          this.Type = Type;
35          this.Time = Time;
36          this.Config = Config;
37          if(Status != null)
38          {
39              this.Statuses.add(Status);
40          }
41      }
42      /**
43       * Example how the message looks like - used during parser tests.
44       */
45      public static final String PROTOTYPE = "RES {Time 0} {Type text} {Config text}";
46      /////// Properties BEGIN
47      //After a SET {Type Viewports} command - Time, Type, Config, Status, Viewport1, Status, Viewport2, Status, Viewport3, Status, Viewport4, Status
48      protected double Time = 0;
49  
50      /**
51       * Timestamp form the UT since server start in seconds.
52       *
53       * @return Returns seconds elapsed from the start of the server.
54       */
55      public double getTime()
56      {
57          return Time;
58      }
59      protected String Type = null;
60  
61      /**
62       * Type of the response will be one of the following: "ViewPorts”, “Camera”,
63       * or some type of sensor or effecter.
64       *
65       * @return Returns Type of the response.
66       */
67      public String getType()
68      {
69          return Type;
70      }
71      // describes the current viewport configuration. This parameter will be either “SingleView” or “QuadView”
72      protected String Config = null;
73  
74      /**
75       * Describes the current viewport configuration. This parameter will be
76       * either “SingleView” or “QuadView”
77       *
78       * @return Returns configuration of current viewport.
79       */
80      public String getConfig()
81      {
82          return Config;
83      }
84      //Note: First status is paird with config variable in this case, the others are linked with viewports!!
85      protected List<String> Statuses = new ArrayList<String>();
86  
87      /**
88       * Status of the viewport configuration after the SET command has been
89       * issued. The status will be “OK” when the viewport configuration was
90       * successfully changed. Otherwise, the status will be “Failed”.
91       *
92       * List of statuses is also used when setting cameras. Each value is then
93       * the status for the camera’s field of view after the SET command has been
94       * issued. The status will be “OK” when the camera’s field of view was
95       * successfully changed. Otherwise, the status will be “Failed”.
96       *
97       * When responsing to SET command for sensor or effecter, single value
98       * describing the status of the transaction will be present in the list.
99       *
100      * @return Returns List of status results.
101      */
102     public List<String> getStatuses()
103     {
104         return Statuses;
105     }
106     protected List<String> Viewports = new ArrayList<String>();
107 
108     /**
109      * Each value is the name of the camera currently attached to viewportN,
110      * where N is 1..4. If viewportN has been disabled, this parameter will be
111      * “Disabled”. If viewportN has been attached to a non- existent camera,
112      * this parameter will be “None”.
113      *
114      * @return Returns List of viewport values. Size of four.
115      */
116     public List<String> getViewports()
117     {
118         return Viewports;
119     }
120     //After a SET {Type Camera} command - Time, Type, Name, FOV, Status
121     protected List<String> Names = new ArrayList<String>();
122 
123     /**
124      * Name of the camera that will be described by the next two parameters: FOV
125      * and Status.
126      *
127      * When responsing to SET command for sensor or effecter, single value will
128      * be present in the list.
129      *
130      * @return Returns List of names of cameras.
131      */
132     public List<String> getNames()
133     {
134         return Names;
135     }
136     protected List<String> FOVs = new ArrayList<String>();
137 
138     /**
139      * Each value is the current field of view of the camera being described, in
140      * radians. The current field of view is the field of view after a SET {Type
141      * Camera} has been issued.
142      *
143      * @return Returns List of fields of views of cameras.
144      */
145     public List<String> getFOVs()
146     {
147         return FOVs;
148     }
149     //The generic response message, issued for other sensors and effecters - Time, Type, Name, Status
150 
151     /**
152      * Cloning constructor.
153      */
154     public ResponseMessage(ResponseMessage original)
155     {
156         this.Type = original.Type;
157         this.Time = original.Time;
158         this.Config = original.Config;
159         this.FOVs.addAll(original.FOVs);
160         this.Names.addAll(original.Names);
161         this.Statuses.addAll(original.Statuses);
162         this.Viewports.addAll(original.Viewports);
163     }
164 
165     /**
166      * Used by Yylex to create empty message then to fill it's protected fields
167      * (Yylex is in the same package).
168      */
169     public ResponseMessage()
170     {
171     }
172     /**
173      * Here we save the original object for which this object is an update.
174      */
175     private IWorldObject orig = null;
176 
177     @Override
178     public String toString()
179     {
180         StringBuilder buf = new StringBuilder();
181 
182         buf.append(super.toString() + " | "
183                 + "Type = "
184                 + String.valueOf(Type) + " | "
185                 + "Time = "
186                 + String.valueOf(Time) + " | "
187                 + "Config = "
188                 + String.valueOf(Config) + " | ");
189 
190         if(!Statuses.isEmpty())
191         {
192             for(String i : Statuses)
193             {
194                 buf.append("Status = ").append(i).append(" ");
195             }
196             buf.append(" | ");
197         }
198 
199         if(!Viewports.isEmpty())
200         {
201             for(int i = 0; i < Viewports.size(); i++)
202             {
203                 buf.append("Viewport").append(i).append(" = ").append(Viewports.get(i)).append(" ");
204             }
205             buf.append(" | ");
206         }
207 
208         if(!Names.isEmpty())
209         {
210             for(String i : Names)
211             {
212                 buf.append("Names = ").append(i).append(" ");
213             }
214             buf.append(" | ");
215         }
216 
217         if(!FOVs.isEmpty())
218         {
219             for(String i : FOVs)
220             {
221                 buf.append("FOV = ").append(i).append(" ");
222             }
223             buf.append(" | ");
224         }
225 
226 
227         return buf.toString();
228 
229 
230         /*
231          * Statuses Viewports Names FOVs
232          */
233 
234     }
235 
236     /**
237      * Gets all properties and values to create a HTML formated string;
238      *
239      * @return Returns all properties in HTML format
240      */
241     public String toHtmlString()
242     {
243         StringBuilder buf = new StringBuilder();
244 
245         buf.append(super.toString()
246                 + "<b>Type</b> : "
247                 + String.valueOf(Type)
248                 + " <br/> "
249                 + "<b>Time</b> : "
250                 + String.valueOf(Time)
251                 + " <br/> "
252                 + "<b>Config</b> : "
253                 + String.valueOf(Config)
254                 + " <br/> ");
255 
256         if(!Statuses.isEmpty())
257         {
258             for(String i : Statuses)
259             {
260                 buf.append("<b>Status</b> : ").append(i).append(" <br/> ");
261             }
262         }
263         if(!Viewports.isEmpty())
264         {
265             for(int i = 0; i < Viewports.size(); i++)
266             {
267                 buf.append("<b>Viewport").append(i).append("</b> : ").append(Viewports.get(i)).append(" <br/> ");
268             }
269         }
270         if(!Names.isEmpty())
271         {
272             for(String i : Names)
273             {
274                 buf.append("<b>Names</b> : ").append(i).append(" <br/> ");
275             }
276         }
277         if(!FOVs.isEmpty())
278         {
279             for(String i : FOVs)
280             {
281                 buf.append("<b>FOV</b> : ").append(i).append(" <br/> ");
282             }
283         }
284 
285         return buf.toString();
286     }
287 }