Author: vingtetun
Date: 2009-02-04 01:53:11 +0100 (Wed, 04 Feb 2009)
New Revision: 3555

Modified:
   software_suite_v2/tuxware/tuxhttpserver/trunk/src/data/js/TuxAPI.js
Log:
Rewriting of the event handler part

Modified: software_suite_v2/tuxware/tuxhttpserver/trunk/src/data/js/TuxAPI.js
===================================================================
--- software_suite_v2/tuxware/tuxhttpserver/trunk/src/data/js/TuxAPI.js 
2009-02-03 20:34:05 UTC (rev 3554)
+++ software_suite_v2/tuxware/tuxhttpserver/trunk/src/data/js/TuxAPI.js 
2009-02-04 00:53:11 UTC (rev 3555)
@@ -70,13 +70,6 @@
   };
 
 
-  // We don't have any wait/pause/Thread.sleep function in javascript
-  // so we she do.... ugly...things...
-  // XXX Move this to event ? (have to look in the python source code )
-  this.wait = function(){
-    //Nothing actually but an awful do...while soon :'(
-  };
-
   var _fillEventHandlers = function(self){
     for( var i = 0 ; i < SW_NAME_DRIVER.length ; i++ ){
       _eventHandlers.insert( SW_NAME_DRIVER[i] );
@@ -511,9 +504,9 @@
  * Button Namespace
  * **************************************************************************/
 var TuxAPI_Button = function( self ){
-  this.left   = new TuxAPI_Switch( self );
-  this.right  = new TuxAPI_Switch( self );
-  this.head   = new TuxAPI_Switch( self );
+  this.left   = new TuxAPI_Switch( self, ST_NAME_LEFT_BUTTON);
+  this.right  = new TuxAPI_Switch( self, ST_NAME_RIGHT_BUTTON);
+  this.head   = new TuxAPI_Switch( self, ST_NAME_HEAD_BUTTON );
   this.remote = new TuxAPI_Remote( self );
 };
 
@@ -521,9 +514,15 @@
 /*****************************************************************************
  * Button Switch Namespace
  * **************************************************************************/
-var TuxAPI_Switch = function( self ){
+var TuxAPI_Switch = function( self, aSwitchName ){
+  var _eventHandlers = self.getEventHandlers();
+
   this.getState = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+    var rv = self.status.requestOne( aSwitchName );
+    if (rv.get("result") == "Success"){
+      return rv.get("data0").get("value");
+    }
+    return null;
   },
 
   this.waitPressed = function(){
@@ -534,20 +533,28 @@
     return TUXAPI_NOT_IMPLEMENTED;
   },
 
-  this.registerEventOnPressed = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+  this.registerEventOnPressed = function( aFunc ){
+    if( self.server.getClientLevel() == CLIENT_LEVEL_ANONYME ) return false;
+    _eventHandlers.register( aSwitchName, aFunc, true );
+    return true;
   },
 
   this.unregisterEventOnPressed = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+    if( self.server.getClientLevel() == CLIENT_LEVEL_ANONYME ) return false;
+    _eventHandlers.unregister( aSwitchName, aFunc , true);
+    return true;
   },
 
-  this.registerEventOnReleased = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+  this.registerEventOnReleased = function( aFunc ){
+    if( self.server.getClientLevel() == CLIENT_LEVEL_ANONYME ) return false;
+    _eventHandlers.register( aSwitchName, aFunc, false);
+    return true;
   },
 
   this.unregisterEventOnReleased = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+    if( self.server.getClientLevel() == CLIENT_LEVEL_ANONYME ) return false;
+    _eventHandlers.unregister( aSwitchName, aFunc, false );
+    return true;
   }
 };
 
@@ -556,8 +563,14 @@
  * Button Remote Namespace
  * **************************************************************************/
 var TuxAPI_Remote = function( self ){
+  var _eventHandlers = self.getEventHandlers();
+
   this.getState = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+    var rv = self.status.requestOne( ST_NAME_REMOTE_BUTTON );
+    if (rv.get("result") == "Success"){
+      return rv.get("data0").get("value");
+    }
+    return null;
   },
 
   this.waitPressed = function(){
@@ -568,20 +581,28 @@
     return TUXAPI_NOT_IMPLEMENTED;
   },
 
-  this.registerEventOnPressed = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+  this.registerEventOnPressed = function( aFunc, aKey ){
+    if( self.server.getClientLevel() == CLIENT_LEVEL_ANONYME ) return false;
+    _eventHandlers.register( ST_NAME_REMOTE_BUTTON, aFunc, aKey );
+    return true;
   },
 
-  this.unregisterEventOnPressed = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+  this.unregisterEventOnPressed = function( aKey ){
+    if( self.server.getClientLevel() == CLIENT_LEVEL_ANONYME ) return false;
+    _eventHandlers.unregister( ST_NAME_REMOTE_BUTTON, aFunc, aKey );
+    return true;
   },
 
-  this.registerEventOnReleased = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+  this.registerEventOnReleased = function( aFunc, aKey ){
+    if( self.server.getClientLevel() == CLIENT_LEVEL_ANONYME ) return false;
+    _eventHandlers.register( ST_NAME_REMOTE_BUTTON, aFunc, aKey );
+    return true;
   },
 
-  this.unregisterEventOnReleased = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+  this.unregisterEventOnReleased = function( aKey ){
+    if( self.server.getClientLevel() == CLIENT_LEVEL_ANONYME ) return false;
+    _eventHandlers.unregister( ST_NAME_REMOTE_BUTTON, aFunc, aKey );
+    return true;
   }
 };
 
@@ -658,6 +679,8 @@
  * SoundFlash Namespace
  * **************************************************************************/
 var TuxAPI_soundFlash = function( self ){
+  var _eventHandlers = self.getEventHandlers();
+
   this.playAsync = function(){
     var rv = self.server.request( 'sound_flash', 'play' , { 'track': aTrack, 
'volume':aVolume } );
     if (rv.get("result") == "Success") return true;
@@ -667,7 +690,7 @@
   this.play = function( aTrack, aVolume ){
     if( !this.playAsync( aTrack, aVolume ) ) return false;
     if( self.server.getClientLevel() == CLIENT_LEVEL_ANONYME ) return true;
-    self.wait();
+    _eventHandlers.waitCondition();
     return true;
   },
 
@@ -705,6 +728,8 @@
  * WAV Namespace
  * **************************************************************************/
 var TuxAPI_WAV = function( self ){
+  var _eventHandlers = self.getEventHandlers();
+
   this.playAsync = function( aBegin, aEnd, aPath ){
     var rv = self.server.request( 'wav', 'play' , { 'begin':aBegin, 
'end':aEnd, 'path':aPath } );
     if (rv.get("result") == "Success") return true;
@@ -714,7 +739,7 @@
   this.play = function( aBegin, aEnd, aPath ){
     if( !this.playAsync( aBegin, aEnd, aPath ) ) return false;
     if( self.server.getClientLevel() == CLIENT_LEVEL_ANONYME ) return true;
-    self.wait();
+    _eventHandlers.waitCondition();
     return true;
   },
 
@@ -978,14 +1003,11 @@
     var event_count = events_struct.get("data_count");
     var cbk, event_name, data_node, event_struct;
 
-    for (var i = 0; i < event_count; i++){
+    for ( var i = 0 ; i < event_count ; i++ ){
       data_node = 'data' + i;
       event_struct = events_struct.get(data_node);
       event_name = event_struct.get("name");
-      cbk = this.handler.getEventHandler(event_name);
-      if (cbk){
-        cbk(event_struct.get("value"), event_struct.get("delay"));
-      }
+      this.handler.emit( event_name, [event_struct.get('value'), 
event_struct.get('delay') ] );
     }
   };
 };
@@ -995,47 +1017,88 @@
  * Event Handler Namespace
  * **************************************************************************/
 var TuxAPI_eventHandler = function(self){
-  this.register = function( anEventName, aFunc ){
-    if (this._callback_dict.containsKey( anEventName )){
-      this._callback_dict.put(anEventName, aFunc);
-      return true;
+  
+  var _funcList = [];
+  var _context  = [];
+
+  var _updateRegister = function( aFunc, aCondition, anIndex ){
+    if( _funcList[anIndex] == undefined )
+      return false;
+    _funcList[anIndex] = { 'func':aFunc, 'cond':aCondition };
+    return true;
+  };
+
+  var _register = function( aFunc, aCondition ){
+    _funcList.push( {'func':aFunc, 'cond':aCondition } );
+    return _funcList.length - 1;
+  };
+  
+
+  this.register = function( aFunc, aCondition, anIndex ){
+    if( aCondition == undefined ) aCondition = null;
+    if( anIndex == undefined ) anIndex = null;
+
+    if ( anIndex == null ){
+      return _register( aFunc, aCondition );
     }
-    return false;
+    
+    var result = _updateRegister( aFunc, aCondition, anIndex );
+    if( !result ){
+      return _register( aFunc, aCondition );
+    }
+    return result;
   },
 
-  this.unregister = function( anEventName ){
-    if (this._callback_dict.containsKey( anEventName )){
-      if ( this._callback_dict.get( anEventName ) != null ){
-        this._callback_dict.remove(anEventName);
-      }
+  this.unregister = function( anIndex ){
+    if( _funcList[anIndex] != undefined ){
+      _funcList[anIndex] = {};
+      return true;
     }
     return false;
   },
+
   this.storeContext = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+    _context.push(_funcStruct.splice());
+    this.clearContext();
   },
 
   this.restoreContext = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+    if( _context.length ){
+      _funcList = _context.pop();
+    }
   },
 
-  this.restoreContext = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+  this.clearContext = function(){
+    _funcList = [];
   },
 
-  this.emit = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+  this.emit = function( someParams ){
+    this.notify( someParams );
   },
 
-  this.notify = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+  this.notify = function( someParams ){
+    for( var i = 0 ; i < _funcList.length ; i++ ){
+      var evt = _funcList[i];
+      if( !evt['func'] ) continue;
+      if( !evt['cond'] ){
+        evt['func']( someParams[0], someParams[1] );
+      }
+      for( var j = 0 ; j < someParams.length ; j++ ){
+        if( evt['cond'] == someParams[j] ){
+          evt['func']( someParams[0], someParams[1] );
+        }
+      }
+    }
   },
 
-  this.waitCondition = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+  this.waitCondition = function( aCondition, aTimeout ){
+    // We don't have any wait/pause/Thread.sleep function in javascript
+    // so we she do.... ugly...things...
+    //Nothing actually but an awful do...while soon :'(
   },
 
   this.clearPending = function(){
+    //Clear all pending wait
     return TUXAPI_NOT_IMPLEMENTED;
   }
 };
@@ -1046,58 +1109,62 @@
  * Event Handlers Namespace
  * **************************************************************************/
 var TuxAPI_eventHandlers = function(self){
-  var _callback_dict = new Hashtable();
+  var _callback_dict = {};
 
-  this.register = function( anEventName, aFunc ){
-    if (_callback_dict.containsKey( anEventName )){
-      _callback_dict.put(anEventName, aFunc);
+  this.register = function( anEventName, aFunc, aCondition, anIndex ){
+    if( _callback_dict[anEventName] ){
+      _callback_dict[anEventName].register( aFunc, aCondition, anIndex );
       return true;
     }
     return false;
   },
 
-  this.unregister = function( anEventName ){
-    if (_callback_dict.containsKey( anEventName )){
-      if ( _callback_dict.get( anEventName ) != null ){
-        _callback_dict.remove(anEventName);
-      }
+  this.unregister = function( anEventName, anIndex ){
+    if( _callback_dict[anEventName] ){
+      _callback_dict[anEventName].unregister( anIndex );
     }
     return false;
   },
 
   this.insert = function( anEventName ){
-    if (!_callback_dict.containsKey( anEventName )) return false,
-    _callback_dict.put( anEventName, false);
+    if( !_callback_dict[anEventName] ) return false,
+    _callback_dict[anEventName] = new TuxAPI_eventHandler(self);
     return true;
   },
 
   this.getEventHandler = function( anEventName ){
-    if (!_callback_dict.containsKey( anEventName )) return null;
-    return _callback_dict.get( anEventName );
+    if( !_callback_dict[anEventName] ) return null;
+    return _callback_dict[anEventName];
   },
 
   this.storeContext = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+    for( evt in _callback_dict )
+      _callback_dict[evt].storeContext();
   },
 
   this.restoreContext = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+    for( evt in _callback_dict )
+      _callback_dict[evt].restoreContext();
   },
 
   this.clearContext = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+    for( evt in _callback_dict )
+      _callback_dict[evt].clearContext();
   },
 
-  this.emit = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+  this.emit = function( anEventName, someParams ){
+    if( _callback_dict[anEventName] )
+      _callback_dict[anEventName].emit( someParams );
   },
 
-  this.notify = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+  this.notify = function( anEventName, someParams ){
+    if( _callback_dict[anEventName] )
+      _callback_dict[anEventName].emit( someParams );
   },
 
-  this.waitCondition = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+  this.waitCondition = function( anEventName, aCondition, aTimeout ){
+    if( _callback_dict[anEventName] )
+      _callback_dict[anEventName].waitCondition( aCondition, aTimeout );
   },
 
   this.clearPending = function(){
@@ -1115,17 +1182,19 @@
  * Resource Spinning
  * **************************************************************************/
 var TuxAPI_spinning = function(self){
+  var _eventHandlers = self.getEventHandlers();
+
   this.leftOn = function( aCount ){
     if (!this.leftOnAsync( aCount ) ) return false;
     if (self.server.getClientLevel() == CLIENT_LEVEL_ANONYME) return true;
-    self.wait();
+    _eventHandlers.waitCondition();
     return true;
   },
 
   this.leftOnDuring = function( aDuration){
     if (!this.leftOnDuringAsync( aCount ) ) return false;
     if (self.__parent.server.getClientLevel() == CLIENT_LEVEL_ANONYME) return 
true;
-    self.wait();
+    _eventHandlers.waitCondition();
     return true;
   },
 
@@ -1144,14 +1213,14 @@
   this.rightOn = function( aCount ){
     if (!this.rightOnAsync( aCount ) ) return false;
     if (self.server.getClientLevel() == CLIENT_LEVEL_ANONYME) return true;
-    self.wait();
+    _eventHandlers.waitCondition();
     return true;
   },
 
   this.rightOnDuring = function( aDuration ){
     if (!this.rightOnDuringAsync( aCount ) ) return false;
     if (self.__parent.server.getClientLevel() == CLIENT_LEVEL_ANONYME) return 
true;
-    self.wait();
+    _eventHandlers.waitCondition();
     return true;
   },
 
@@ -1290,10 +1359,12 @@
  * Resource Flippers
  * **************************************************************************/
 var TuxAPI_flippers = function(self){
+  var _eventHandlers = self.getEventHandlers();
+
   this.on = function( aCount, aFinalState ){
     if (!this.onAsync( aCount, aFinalState ) ) return false;
     if (self.__parent.server.getClientLevel() == CLIENT_LEVEL_ANONYME) return 
true;
-    self.wait();
+    _eventHandlers.waitCondition();
     return true;
   },
 
@@ -1306,7 +1377,7 @@
   this.onDuring = function( aDuration, aFinalState ){
     if (!this.onDuringAsync( aDuration, aFinalState ) ) return false;
     if (self.__parent.server.getClientLevel() == CLIENT_LEVEL_ANONYME) return 
true;
-    self.wait();
+    _eventHandlers.waitCondition();
     return true;
   },
 
@@ -1383,10 +1454,12 @@
  * Resource MouthEyes
  * **************************************************************************/
 var TuxAPI_MouthEyes = function( self, aName ){
+  var _eventHandlers = self.getEventHandlers();
+
   this.on = function( aCount, aFinalState){
     if (!this.onAsync( aCount, aFinalState ) ) return false;
     if (self.__parent.server.getClientLevel() == CLIENT_LEVEL_ANONYME) return 
true;
-    self.wait();
+    _eventHandlers.waitCondition();
     return true;
   },
 
@@ -1399,7 +1472,7 @@
   this.onDuring = function( aDuration, aFinalState){
     if (!this.onDuringAsync( aCount, aFinalState ) ) return false;
     if (self.__parent.server.getClientLevel() == CLIENT_LEVEL_ANONYME) return 
true;
-    self.wait();
+    _eventHandlers.waitCondition();
     return true;
   },
 
@@ -1499,22 +1572,26 @@
  * Resource Status
  * **************************************************************************/
 var TuxAPI_status = function(self){
+  var _eventHandlers = self.getEventHandlers();
+
   this.events = function(){
     return self.server.request( 'status', 'events', {} );
   },
+
   this.requestOne = function( aStatusName ){
     return self.server.request( 'status', 'request_one', { 
'status_name':aStatusName } );
   },
+
   this.requestAll = function(){
     return self.server.request( 'status', 'request_all', {} );
   },
+
   this.send = function( aName, aValue ){
     return self.server.request( 'status', 'send', { 'name':aName, 
'value':aValue } );
   },
-  this.wait = function(){
-    //wait(self, statusName, condition = None, timeout = 999999999.0):
-    //TuxAPIStatus.py:96
-    return TUXAPI_NOT_IMPLEMENTED;
+
+  this.wait = function( aStatusName, aCondition, aTimeout ){
+    return _eventHandlers.waitCondition( aStatusName, aCondition, aTimeout );
   }
 };
 


------------------------------------------------------------------------------
Create and Deploy Rich Internet Apps outside the browser with Adobe(R)AIR(TM)
software. With Adobe AIR, Ajax developers can use existing skills and code to
build responsive, highly engaging applications that combine the power of local
resources and data with the reach of the web. Download the Adobe AIR SDK and
Ajax docs to start building applications today-http://p.sf.net/sfu/adobe-com
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn

Reply via email to