![]() |
Hubiquitus Android
0.3
Android client for hubiquitus protocol
|
00001 /* 00002 * Copyright (c) Novedia Group 2012. 00003 * 00004 * This file is part of Hubiquitus. 00005 * 00006 * Hubiquitus is free software: you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation, either version 3 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * Hubiquitus is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with Hubiquitus. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 package org.hubiquitus.hapi.phonegap; 00020 00021 import org.apache.cordova.api.Plugin; 00022 import org.apache.cordova.api.PluginResult; 00023 import org.hubiquitus.hapi.client.HClient; 00024 import org.hubiquitus.hapi.client.HCommandDelegate; 00025 import org.hubiquitus.hapi.client.HMessageDelegate; 00026 import org.hubiquitus.hapi.client.HStatusDelegate; 00027 import org.hubiquitus.hapi.hStructures.HCommand; 00028 import org.hubiquitus.hapi.hStructures.HMessage; 00029 import org.hubiquitus.hapi.hStructures.HOptions; 00030 import org.hubiquitus.hapi.hStructures.HResult; 00031 import org.hubiquitus.hapi.hStructures.HStatus; 00032 import org.json.JSONArray; 00033 import org.json.JSONException; 00034 import org.json.JSONObject; 00035 00040 public class HClientPhoneGapPlugin extends Plugin implements HStatusDelegate, HMessageDelegate { 00041 00042 private HClient hclient = null; 00043 00047 @Override 00048 public PluginResult execute(String action, JSONArray data, String callbackid) { 00049 //First of all, create hclient instance 00050 if(hclient == null) { 00051 hclient = new HClient(); 00052 hclient.onStatus(this); 00053 hclient.onMessage(this); 00054 } 00055 00056 //do work depending on action 00057 if (action.equalsIgnoreCase("connect")) { 00058 this.connect(action, data, callbackid); 00059 } else if(action.equalsIgnoreCase("disconnect")) { 00060 this.disconnect(action, data, callbackid); 00061 } else if(action.equalsIgnoreCase("command")) { 00062 this.command(action, data, callbackid); 00063 } else if(action.equalsIgnoreCase("subscribe")) { 00064 this.subscribe(action, data, callbackid); 00065 } else if(action.equalsIgnoreCase("unsubscribe")) { 00066 this.unsubscribe(action, data, callbackid); 00067 } else if(action.equalsIgnoreCase("publish")) { 00068 this.publish(action, data, callbackid); 00069 } else if(action.equalsIgnoreCase("getlastmessages")) { 00070 this.getLastMessages(action, data, callbackid); 00071 } else if(action.equalsIgnoreCase("getsubscriptions")) { 00072 this.getSubscriptions(action, data, callbackid); 00073 } else if(action.equalsIgnoreCase("getthread")) { 00074 this.getThread(action, data, callbackid); 00075 } else if(action.equalsIgnoreCase("getthreads")) { 00076 this.getThreads(action, data, callbackid); 00077 } 00078 00079 return null; 00080 } 00081 00088 public void getSubscriptions(String action, JSONArray data, String callbackid) { 00089 JSONObject jsonObj = null; 00090 String jsonCallback = null; 00091 try { 00092 jsonObj = data.getJSONObject(0); 00093 00094 try { 00095 jsonCallback = jsonObj.getString("callback"); 00096 } catch (Exception e) { 00097 } 00098 00099 final String cmdCallback = jsonCallback; 00100 00101 //set the callback 00102 HCommandDelegate commandDelegate = new CommandsDelegate(cmdCallback); 00103 hclient.getSubscriptions(commandDelegate); 00104 } catch (JSONException e) { 00105 e.printStackTrace(); 00106 } 00107 } 00108 00115 public void getLastMessages(String action, JSONArray data, String callbackid) { 00116 JSONObject jsonObj = null; 00117 String chid = null; 00118 int nbLastMsg = -1; 00119 String jsonCallback = null; 00120 try { 00121 jsonObj = data.getJSONObject(0); 00122 00123 try { 00124 chid = jsonObj.getString("chid"); 00125 } catch (Exception e) { 00126 } 00127 00128 try { 00129 nbLastMsg = jsonObj.getInt("nbLastMsg"); 00130 } catch (Exception e) { 00131 } 00132 00133 try { 00134 jsonCallback = jsonObj.getString("callback"); 00135 } catch (Exception e) { 00136 } 00137 00138 final String cmdCallback = jsonCallback; 00139 00140 //set the callback 00141 HCommandDelegate commandDelegate = new CommandsDelegate(cmdCallback); 00142 00143 if (nbLastMsg < 0) { 00144 hclient.getLastMessages(chid, commandDelegate); 00145 } else { 00146 hclient.getLastMessages(chid, nbLastMsg, commandDelegate); 00147 } 00148 } catch (JSONException e) { 00149 // TODO Auto-generated catch block 00150 e.printStackTrace(); 00151 } 00152 } 00153 00160 public void unsubscribe(String action, JSONArray data, String callbackid) { 00161 JSONObject jsonObj = null; 00162 String chid = null; 00163 String jsonCallback = null; 00164 try { 00165 jsonObj = data.getJSONObject(0); 00166 try { 00167 chid = jsonObj.getString("chid"); 00168 } catch (Exception e) { 00169 } 00170 00171 try { 00172 jsonCallback = jsonObj.getString("callback"); 00173 } catch (Exception e) { 00174 } 00175 00176 final String cmdCallback = jsonCallback; 00177 00178 //set the callback 00179 HCommandDelegate commandDelegate = new CommandsDelegate(cmdCallback); 00180 hclient.unsubscribe(chid, commandDelegate); 00181 } catch (JSONException e) { 00182 // TODO Auto-generated catch block 00183 e.printStackTrace(); 00184 } 00185 } 00186 00194 public void publish(String action, JSONArray data, String callbackid) { 00195 JSONObject jsonObj = null; 00196 JSONObject jsonMsg = null; 00197 String jsonCallback = null; 00198 HMessage msg = null; 00199 try { 00200 jsonObj = data.getJSONObject(0); 00201 try { 00202 jsonMsg = jsonObj.getJSONObject("hmessage"); 00203 } catch (Exception e) { 00204 } 00205 00206 msg = new HMessage(jsonMsg); 00207 00208 try { 00209 jsonCallback = jsonObj.getString("callback"); 00210 } catch (Exception e) { 00211 } 00212 00213 final String cmdCallback = jsonCallback; 00214 //set the callback 00215 HCommandDelegate commandDelegate = new CommandsDelegate(cmdCallback); 00216 00217 hclient.publish(msg, commandDelegate); 00218 } catch (JSONException e) { 00219 e.printStackTrace(); 00220 } 00221 } 00222 00229 public void subscribe(String action, JSONArray data, String callbackid) { 00230 JSONObject jsonObj = null; 00231 String chid = null; 00232 String jsonCallback = null; 00233 try { 00234 jsonObj = data.getJSONObject(0); 00235 00236 try { 00237 chid = jsonObj.getString("chid"); 00238 } catch (Exception e) { 00239 } 00240 00241 try { 00242 jsonCallback = jsonObj.getString("callback"); 00243 } catch (Exception e) { 00244 } 00245 00246 final String cmdCallback = jsonCallback; 00247 00248 //set the callback 00249 HCommandDelegate commandDelegate = new CommandsDelegate(cmdCallback); 00250 00251 hclient.subscribe(chid, commandDelegate); 00252 } catch (JSONException e) { 00253 // TODO Auto-generated catch block 00254 e.printStackTrace(); 00255 } 00256 } 00257 00264 public void getThread(String action, JSONArray data, String callbackid) { 00265 JSONObject jsonObj = null; 00266 String chid = null; 00267 String convid = null; 00268 String jsonCallback = null; 00269 try { 00270 jsonObj = data.getJSONObject(0); 00271 00272 try { 00273 chid = jsonObj.getString("chid"); 00274 } catch (Exception e) { 00275 } 00276 00277 try { 00278 convid = jsonObj.getString("convid"); 00279 } catch (Exception e) { 00280 } 00281 00282 try { 00283 jsonCallback = jsonObj.getString("callback"); 00284 } catch (Exception e) { 00285 } 00286 00287 final String cmdCallback = jsonCallback; 00288 00289 //set the callback 00290 HCommandDelegate commandDelegate = new CommandsDelegate(cmdCallback); 00291 00292 hclient.getThread(chid, convid, commandDelegate); 00293 } catch (JSONException e) { 00294 // TODO Auto-generated catch block 00295 e.printStackTrace(); 00296 } 00297 } 00298 00305 public void getThreads(String action, JSONArray data, String callbackid) { 00306 JSONObject jsonObj = null; 00307 String chid = null; 00308 String convState = null; 00309 String jsonCallback = null; 00310 try { 00311 jsonObj = data.getJSONObject(0); 00312 00313 try { 00314 chid = jsonObj.getString("chid"); 00315 } catch (Exception e) { 00316 } 00317 00318 try { 00319 convState = jsonObj.getString("convState"); 00320 } catch (Exception e) { 00321 } 00322 00323 try { 00324 jsonCallback = jsonObj.getString("callback"); 00325 } catch (Exception e) { 00326 } 00327 00328 final String cmdCallback = jsonCallback; 00329 00330 //set the callback 00331 HCommandDelegate commandDelegate = new CommandsDelegate(cmdCallback); 00332 00333 hclient.getThreads(chid, convState, commandDelegate); 00334 } catch (JSONException e) { 00335 // TODO Auto-generated catch block 00336 e.printStackTrace(); 00337 } 00338 } 00339 00346 public void command(String action, JSONArray data, String callbackid) { 00347 JSONObject jsonObj = null; 00348 JSONObject jsonCmd = null; 00349 HCommand cmd = null; 00350 String jsonCallback = null; 00351 try { 00352 jsonObj = data.getJSONObject(0); 00353 00354 try { 00355 jsonCmd = jsonObj.getJSONObject("hcommand"); 00356 } catch (Exception e) { 00357 } 00358 00359 try { 00360 jsonCallback = jsonObj.getString("callback"); 00361 } catch (Exception e) { 00362 } 00363 00364 final String cmdCallback = jsonCallback; 00365 00366 cmd = new HCommand(jsonCmd); 00367 00368 //set the callback 00369 HCommandDelegate commandDelegate = new CommandsDelegate(cmdCallback); 00370 00371 hclient.command(cmd, commandDelegate); 00372 } catch (JSONException e) { 00373 e.printStackTrace(); 00374 } 00375 00376 } 00377 00384 public void disconnect(String action, JSONArray data, String callbackid) { 00385 hclient.disconnect(); 00386 } 00387 00394 public void connect(String action, JSONArray data, String callbackid) { 00395 String publisher = null; 00396 String password = null; 00397 HOptions options = null; 00398 try { 00399 //get vars 00400 JSONObject jsonObj = data.getJSONObject(0); 00401 publisher = jsonObj.getString("publisher"); 00402 password = jsonObj.getString("password"); 00403 JSONObject jsonOptions = (JSONObject) jsonObj.get("options"); 00404 options = new HOptions(jsonOptions); 00405 } catch (Exception e) { 00406 e.printStackTrace(); 00407 } 00408 00409 //set callback 00410 hclient.connect(publisher, password, options); 00411 //hclient.connect(publisher, password, this, new HOptions()); 00412 } 00413 00419 private void notifyJsCallback(final String jsCallback, final String arg) { 00420 if (jsCallback != null && jsCallback.length() > 0) { 00421 00422 //do callback on main thread 00423 this.webView.post(new Runnable() { 00424 00425 public void run() { 00426 //send callback through javascript 00427 String jsCallbackFct = jsCallback + "(" + arg + ");"; 00428 sendJavascript(jsCallbackFct); 00429 } 00430 }); 00431 } 00432 } 00433 00434 @Override 00435 public void onStatus(HStatus status) { 00436 notifyJsCallback("hClient.onStatus", status.toJSON().toString()); 00437 } 00438 00439 @Override 00440 public void onMessage(HMessage message) { 00441 notifyJsCallback("hClient.onMessage", message.toJSON().toString()); 00442 } 00443 00448 private class CommandsDelegate implements HCommandDelegate { 00449 00450 private String cmdCallback = null; 00451 00456 public CommandsDelegate(String cmdCallback) { 00457 this.cmdCallback = cmdCallback; 00458 } 00459 00460 @Override 00461 public void onResult(HResult result) { 00462 notifyJsCallback("var tmpcallback = " + this.cmdCallback + "; tmpcallback", result.toJSON().toString()); 00463 00464 } 00465 00466 } 00467 } 00468