Hubiquitus Android  0.3
Android client for hubiquitus protocol
HClient.js
Go to the documentation of this file.
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 
00020 //Make it compatible with node and web browser
00021 if (typeof define !== 'function') { var define = require('amdefine')(module) }
00022 
00023 define(
00024                   ['./codes'],
00025                   function(codes){
00026 
00031                            var HClient = function(){
00032                                     this._connectionStatus = codes.statuses.DISCONNECTED; //update by phonegap plugin
00033                            };
00034 
00035                            HClient.prototype = {
00036                                              connect : function(publisher, password, options){
00037                                                       this.publisher = publisher;
00038                                                       this.options = options;
00039                                                       
00040                                                       return cordova.exec(null, null, 'HClientPhoneGapPlugin', 'connect', [{publisher: publisher, password: password, options:options}]);
00041                                              },
00042                                              disconnect : function(){
00043                                                       this.publisher = null;
00044                                                       return cordova.exec(null, null, 'HClientPhoneGapPlugin', 'disconnect', []);
00045                                              },
00046                                              
00047 
00048                                              command: function(cmd, callback){
00049                                                       cordova.exec(null, null, 'HClientPhoneGapPlugin', 'command', [{hcommand: cmd, callback: String(callback)}]);
00050                                              },
00051 
00052                                              subscribe : function(channel, callback){
00053                                                       cordova.exec(null, null, 'HClientPhoneGapPlugin', 'subscribe', [{chid: channel, callback: String(callback)}]);
00054                                              },
00055 
00056                                              unsubscribe : function(channel, callback){
00057                                                       cordova.exec(null, null, 'HClientPhoneGapPlugin', 'unsubscribe', [{chid: channel, callback: String(callback)}]);
00058                                              },
00059 
00060                                              publish : function(hmessage, callback){
00061                                                       cordova.exec(null, null, 'HClientPhoneGapPlugin', 'publish', [{hmessage: hmessage, callback: String(callback)}]);
00062                                              },
00063 
00064                                              getSubscriptions: function(callback){
00065                                                       cordova.exec(null, null, 'HClientPhoneGapPlugin', 'getSubscriptions', [{callback: String(callback)}]);
00066                                              },
00067 
00068                                              getLastMessages: function(chid, nbLastMsg, callback){
00069                                                       cordova.exec(null, null, 'HClientPhoneGapPlugin', 'getLastMessages', [{chid: chid, nbLastMsg: quantity, callback: String(callback)}]);
00070                                              },
00071                                              
00072                                              getLastMessages: function(chid, callback){
00073                                                       cordova.exec(null, null, 'HClientPhoneGapPlugin', 'getLastMessages', [{chid: chid, nbLastMsg: -1, callback: String(callback)}]);
00074                                              },
00075                                              
00076                                              getThread: function(chid, convid, callback){
00077                                                       cordova.exec(null, null, 'HClientPhoneGapPlugin', 'getThread', [{chid: chid, convid: convid, callback: String(callback)}]);
00078                                              },
00079                                              
00080                                              getThreads: function(chid, convState, callback){
00081                                                       cordova.exec(null, null, 'HClientPhoneGapPlugin', 'getThreads', [{chid: chid, convState: convState, callback: String(callback)}]);
00082                                              },
00083 
00084                                              buildMessage: function(chid, type, payload, options){
00085                                   options = options || {};
00086 
00087                                   if(!chid)
00088                                       throw new Error('missing chid');
00089 
00090                                   return {
00091                                       chid: chid,
00092                                       convid: options.convid,
00093                                       type: type,
00094                                       priority: options.priority,
00095                                       relevance: options.relevance,
00096                                       transient: options.transient,
00097                                       location: options.location,
00098                                       author: options.author,
00099                                       published: options.published,
00100                                       headers: options.headers,
00101                                       payload: payload
00102                                   };
00103                               },
00104 
00105                               buildMeasure: function(chid, value, unit, options){
00106                                   if(!value)
00107                                       throw new Error('missing value');
00108                                   else if (!unit)
00109                                       throw new Error('missing unit');
00110 
00111                                   return this.buildMessage(chid, 'hMeasure', {unit: unit, value: value}, options);
00112                               },
00113 
00114                               buildAlert: function(chid, alert, options){
00115                                   if(!alert)
00116                                       throw new Error('missing alert');
00117 
00118                                   return this.buildMessage(chid, 'hAlert', {alert: alert}, options);
00119                               },
00120 
00121                               buildAck: function(chid, ackid, ack, options){
00122                                   if(!ackid)
00123                                       throw new Error('missing ackid');
00124                                   else if(!ack)
00125                                       throw new Error('missing ack');
00126                                   else if(!/recv|read/i.test(ack))
00127                                       throw new Error('ack does not match "recv" or "read"');
00128 
00129                                   return this.buildMessage(chid, 'hAck', {ackid: ackid, ack: ack}, options);
00130                               },
00131                               
00132                               buildConvState: function(chid, convid, status, options){
00133                                   if(!convid)
00134                                       throw new Error('missing convid');
00135                                   else if(!status)
00136                                       throw new Error('missing status');
00137                                   if(!options)
00138                                       options = {};
00139 
00140                                   options.convid = convid;
00141 
00142                                   return this.buildMessage(chid, 'hConvState', {status: status}, options);
00143                               },
00144                               
00145                               checkJID: function(jid){
00146                                   return new RegExp("^(?:([^@/<>'\"]+)@)([^@/<>'\"]+)(?:/([^/<>'\"]*))?$").test(jid);
00147                               },
00148 
00149                               splitJID: function(jid){
00150                                   return jid.match(new RegExp("^(?:([^@/<>'\"]+)@)([^@/<>'\"]+)(?:/([^/<>'\"]*))?$")).splice(1, 3);
00151                               },
00152 
00153                                              errors: codes.errors,
00154                                              statuses: codes.statuses,
00155                                              hResultStatus: codes.hResultStatus
00156                            };
00157 
00158 
00159                            cordova.addConstructor(function() {
00160                                     hClient = new HClient();
00161                                     cordova.addPlugin("hClient", hClient);
00162                            });
00163                   }
00164 );
 All Classes Namespaces Files Functions Variables