ROSMonitorManager.java
Go to the documentation of this file.
00001 /*
00002 copyright 2014 UNL Nimbus Lab 
00003 
00004   Licensed under the Apache License, Version 2.0 (the "License");
00005      you may not use this file except in compliance with the License.
00006     You may obtain a copy of the License at
00007   
00008         http://www.apache.org/licenses/LICENSE-2.0
00009   
00010     Unless required by applicable law or agreed to in writing, software
00011   distributed under the License is distributed on an "AS IS" BASIS,
00012    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013    See the License for the specific language governing permissions and
00014   limitations under the License.
00015 */
00016 package edu.nimbus.glass;
00017 
00018 import java.util.HashMap;
00019 import java.util.Map;
00020 
00021 import org.json.JSONException;
00022 import org.json.JSONObject;
00023 
00024 import android.util.Log;
00025 import de.tavendo.autobahn.WebSocketConnection;
00026 import de.tavendo.autobahn.WebSocketHandler;
00027 
00028 
00029 
00030 
00031 
00037 public class ROSMonitorManager extends WebSocketHandler{
00038 
00039         //service to monitor ros in the background
00040         private ROSMonitorService _master;
00041         //The last warning received
00042         private String _lastWarning;
00043         //websocket
00044         WebSocketConnection mConnection;
00045         
00046         private Map<String, Long> message_times;
00047 
00049         public final static String WARN_TOPIC_NAME = "glass_warn";
00051         public final static String WARN_TOPIC_TYPE = "std_msgs/String";
00052 
00058         public ROSMonitorManager(ROSMonitorService master, WebSocketConnection conn){
00059                 _lastWarning = "";
00060                 message_times = new HashMap<String, Long>();
00061                 _master = master;
00062                 mConnection = conn;
00063         }
00064 
00065         @Override
00066         public void onOpen(){
00067                 //Subscribe to the correct topic on open
00068                 JSONObject req = new JSONObject();
00069                 try {
00070                         req.put("op", "subscribe");
00071                         req.put("topic", WARN_TOPIC_NAME);
00072                         req.put("type", WARN_TOPIC_TYPE);
00073 
00074                 } catch (JSONException e) {
00075                         Log.d("JSON ERROR", e.getMessage());
00076                 }
00077                 mConnection.sendTextMessage(req.toString());
00078 
00079         }       
00080 
00081         @Override
00082         public void onTextMessage(String payload){
00083                 //try and parse the message and check to see if we have a new warning that warrants creating a  new live card.
00084                 try {
00085                         JSONObject res = new JSONObject(payload);
00086                         String error_message = res.getJSONObject("msg").getString("data");
00087                         boolean val = check_display(error_message);
00088                         Log.d("DISPLAY WARN", val+"");
00089                         if(val){
00090                                 _lastWarning = error_message;
00091                                 _master.createWarning(error_message);
00092                         }
00093 
00094                 } catch (JSONException e) {
00095                         Log.d("JSON ERROR", e.getMessage());
00096                 }
00097 
00098         }
00099 
00104         public boolean check_display(String new_error){
00105                 if(_lastWarning == new_error ){
00106                         return false;
00107                 }else{
00108                         long now = System.currentTimeMillis();
00109                         if(message_times.containsKey(new_error)){
00110                                 long last_time = message_times.get(new_error);
00111                                 if((now - last_time > 5000)){
00112                                         message_times.put(new_error, now);
00113                                         return true;
00114                                 }else{
00115                                         return false;
00116                                 }
00117                                 
00118                         }else{
00119                                 message_times.put(new_error, now);
00120                                 return true;
00121                         }
00122                         
00123                 }
00124                 
00125                 
00126         }
00127 
00128         @Override
00129         public void onClose(int code, String reason) {
00130                 Log.d("Data Closed", "Connection lost." + reason);
00131 
00132         }
00133         
00137         public void clearWarning(){
00138                 _lastWarning = "";
00139         }
00140 
00145         public void setWarning(String text){
00146                 _lastWarning = text;
00147         }
00148 
00149 }


ros_glass_tools
Author(s):
autogenerated on Thu Aug 27 2015 14:47:21