Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
00040 private ROSMonitorService _master;
00041
00042 private String _lastWarning;
00043
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
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
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 }