TopicManager.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.Iterator;
00020 import java.util.Map;
00021 import java.util.regex.Pattern;
00022 
00023 import org.json.JSONException;
00024 import org.json.JSONObject;
00025 
00026 import android.util.Log;
00027 import de.tavendo.autobahn.WebSocketConnection;
00028 import de.tavendo.autobahn.WebSocketHandler;
00029 
00030 public class TopicManager extends WebSocketHandler{
00031 
00032         private static final String tabs = "\t\t\t\t\t";
00033 
00034         private String topic;
00035         private String url;
00036         private WebSocketConnection mConnection;
00037         private String text;
00038         private String originalPayload;
00039         private Map<String, String> fieldMap;
00040 
00041         public TopicManager(String topic, String url, WebSocketConnection mConnection){
00042                 this.topic = topic;
00043                 this.url = url;
00044                 this.mConnection = mConnection;
00045                 this.text = "NO DATA?";
00046                 fieldMap = new HashMap<String, String>();
00047         }
00048 
00049         @Override
00050         public void onOpen() {
00051                 Log.d("Data Status", "Status: Connected to " + url);
00052 
00053                 JSONObject jsonRequest = new JSONObject();
00054 
00055                 try {
00056                         jsonRequest.put("op", "subscribe");
00057                         jsonRequest.put("topic", topic);
00058                 } catch(Exception e) {
00059 
00060 
00061                 }
00062                 mConnection.sendTextMessage(jsonRequest.toString());
00063         }
00064 
00065         @Override
00066         public void onTextMessage(String payload) {
00067                 originalPayload = payload;
00068         }
00069 
00070         @Override
00071         public void onClose(int code, String reason) {
00072                 Log.d("Data Closed", "Connection lost." + reason);
00073         }
00074 
00081         public String getField(String field){
00082                 String [] subfields = field.split(Pattern.quote("."));
00083                 try{
00084 
00085                         JSONObject obj = new JSONObject(getOriginalMessage());
00086                         return getJSONMessageRec(obj.getJSONObject("msg"), subfields, 0);
00087 
00088                 } catch (JSONException e) {
00089                         e.printStackTrace();
00090                         return "JSON ERROR";
00091 
00092                 }       
00093         }
00094 
00099         public String getText(){
00100                 text = parseMessage(originalPayload);
00101                 return text;
00102         }
00103 
00108         public String getOriginalMessage(){
00109                 return originalPayload;
00110         }
00111 
00112         public void start(){
00113                 //Do nothing?
00114         }
00115 
00116         public void stop(){
00117                 //Do nothing?
00118         }
00119 
00125         private String parseMessage(String msg){
00126 
00127                 JSONObject obj;
00128 
00129                 if(msg == null){
00130                         return "No Data";
00131                 }
00132 
00133                 try {
00134                         obj = new JSONObject(msg);
00135                         StringBuilder str = buildRecursiveList(obj.getJSONObject("msg"), 0);
00136                         return str.toString();
00137 
00138 
00139                 } catch (JSONException e) {
00140                         e.printStackTrace();
00141                         return "JSON ERROR";
00142                 }       
00143         }
00144 
00145         
00153         private StringBuilder buildRecursiveList(JSONObject msg, int tab_level) throws JSONException{
00154                 Iterator<String> fields = msg.keys();
00155                 StringBuilder build = new StringBuilder();
00156 
00157                 while(fields.hasNext()){
00158                         String val = fields.next();
00159                         try{
00160                                 StringBuilder a = buildRecursiveList(msg.getJSONObject(val), tab_level + 1);
00161                                 for(int j = 0; j < tab_level; j++){
00162                                         build.append(tabs);
00163                                 }
00164 
00165                                 build.append(val);
00166                                 build.append(":");
00167                                 build.append("\n");
00168 
00169                                 build.append(a);
00170 
00171                         }catch(Exception e){
00172                                 for(int j = 0;j < tab_level; j++){
00173                                         build.append(tabs);
00174                                 }
00175 
00176                                 build.append(val);
00177                                 build.append(":");
00178                                 build.append(msg.get(val));
00179                                 build.append("\n");
00180                         }
00181                 }
00182                 return build;
00183         }
00184 
00185         private String getJSONMessageRec(JSONObject obj, String [] subfields, int index) throws JSONException{
00186                 if(index + 1 >= subfields.length){
00187                         String ret = obj.getString(subfields[index]);
00188                         if(ret == null){
00189                                 return "No Data";
00190                         }else{
00191                                 return ret;
00192                         }
00193                 }else{
00194                         return getJSONMessageRec(obj.getJSONObject(subfields[index]), subfields, index+1);
00195                 }
00196         }
00197 
00198         
00204         public static String performFormatting(String string){
00205 
00206 
00207 
00208                 if(string.contains(":")){
00209                         String [] pieces = string.split(":");
00210                         
00211                         if(pieces.length>1){
00212                                 String num = pieces[1].trim();
00213 
00214                                 try{
00215                                         float f = Float.parseFloat(num);
00216                                         num = String.format("%.4f", f);
00217                                 }catch(java.lang.NumberFormatException e){
00218                                         //do nothing
00219                                 }
00220 
00221                                 try{
00222                                         double d = Double.parseDouble(num);
00223                                         num = String.format("%.4f", d);
00224                                 }catch(Exception e){
00225                                         //do nothing
00226                                 }
00227 
00228                                 return pieces[0]+": "+num;
00229                         }else{
00230                                 return string;
00231                         }
00232 
00233                 }else{
00234                         return string;
00235                 }
00236         }
00237 }


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