Author: vingtetun
Date: 2009-02-14 14:16:32 +0100 (Sat, 14 Feb 2009)
New Revision: 3633

Modified:
   software_suite_v2/tuxware/tuxhttpserver/trunk/src/data/js/TuxAPI.js
Log:
* Add some methods from the python API

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-13 13:36:10 UTC (rev 3632)
+++ software_suite_v2/tuxware/tuxhttpserver/trunk/src/data/js/TuxAPI.js 
2009-02-14 13:16:32 UTC (rev 3633)
@@ -1,3 +1,4 @@
+/* vi:set sw=2: set ts=2: */
 /**
  *
  * This file implements the TuxDroid API for Javascript,
@@ -5,6 +6,7 @@
  *
  * TODO
  * Rename the CommandRequest en TuxHTTPRequest to be consistent
+ * Remove const keyword for other browser than moz
  *
  */
 
@@ -24,6 +26,7 @@
   var versionName     = "tuxisalive";
   var versionNumber   = "0.2.1";
   var _eventHandlers  = new TuxAPI_eventHandlers(self);
+  var self            = this;
 
 
   /*
@@ -63,7 +66,7 @@
 
   /*
   * Get the event handlers of the API.
-  * @return a TUXAPIEventHandlers
+  * @return a TUXEventHandlers
   */
   this.getEventHandlers = function(){
     return _eventHandlers;
@@ -85,11 +88,11 @@
     }
   };
 
-  var _onConnect = function(self){
+  var _onConnect = function(){
     self.event.start();
   };
 
-  var _onDisconnect = function(self){
+  var _onDisconnect = function(){
     self.event.stop();
   }
 
@@ -97,13 +100,13 @@
   this.event          = new TuxAPI_event(this);
   this.status         = new TuxAPI_status(this);
   this.access         = new TuxAPI_access(this);
-  this.mouth          = new TuxAPI_MouthEyes(this, 'mouth');
-  this.eyes           = new TuxAPI_MouthEyes(this, 'eyes');
+  this.mouth          = new TuxAPI_MouthEyes(this, ST_NAME_MOUTH_POSITION,  
ST_NAME_MOUTH_RM, 'mouth');
+  this.eyes           = new TuxAPI_MouthEyes(this, ST_NAME_EYES_POSITION, 
ST_NAME_EYES_RM, 'eyes');
   this.flippers       = new TuxAPI_flippers(this);
   this.leds           = new TuxAPI_leds(this);
   this.spinning       = new TuxAPI_spinning(this);
-  this.radio          = new TuxAPI_DongleRadio(this, 'radio');
-  this.dongle         = new TuxAPI_DongleRadio(this, 'dongle');
+  this.radio          = new TuxAPI_DongleRadio(this, ST_NAME_RADIO_STATE);
+  this.dongle         = new TuxAPI_DongleRadio(this, ST_NAME_DONGLE_PLUG);
   this.ir             = new TuxAPI_ir(this);
   this.attitune       = new TuxAPI_attitune(this);
   this.tts            = new TuxAPI_TTS(this);
@@ -111,6 +114,7 @@
   this.button         = new TuxAPI_Button(this);
   this.wav            = new TuxAPI_WAV(this);
   this.raw            = new TuxAPI_RAW(this);
+  
   //Constructor
   function TuxAPI(self){
     _fillEventHandlers(self);
@@ -520,17 +524,22 @@
   this.getState = function(){
     var rv = self.status.requestOne( aSwitchName );
     if (rv.get("result") == "Success"){
-      return rv.get("data0").get("value");
+      if( rv.get("data0").get("value") == 'False') return false;
+      else return true;
     }
     return null;
   },
 
-  this.waitPressed = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+  this.waitPressed = function( aTimeout ){
+    if( self.server.getClientLevel() == CLIENT_LEVEL_ANONYME ) return false;
+    if(this.getState()) return true;
+    return _eventHandlers.waitCondition( aSwitchName, true , aTimeout );
   },
 
-  this.WaitReleased = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+  this.WaitReleased = function( aTimeout ){
+    if( self.server.getClientLevel() == CLIENT_LEVEL_ANONYME ) return false;
+    if(!this.getState()) return true;
+    return _eventHandlers.waitCondition( aSwitchName, false , aTimeout );
   },
 
   this.registerEventOnPressed = function( aFunc ){
@@ -570,15 +579,20 @@
     if (rv.get("result") == "Success"){
       return rv.get("data0").get("value");
     }
-    return null;
+    return K_RELEASED;
   },
 
-  this.waitPressed = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+  this.waitPressed = function( aTimeout, aKey ){
+    if( self.server.getClientLevel() == CLIENT_LEVEL_ANONYME ) return false;
+    if( REMOTE_KEY_LIST.indexOf( aKey == -1 ) ) return false;
+    if( self.getState() == aKey ) return true;
+    return _eventtHandlers.waitCondition( ST_NAME_REMOTE_BUTTON, aKey, 
aTimeout);
   },
 
-  this.WaitReleased = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+  this.waitReleased = function( aTimeout ){
+    if( self.server.getClientLevel() == CLIENT_LEVEL_ANONYME ) return false;
+    if( self.getState() == K_RELEASED ) return true;
+    return _eventtHandlers.waitCondition( ST_NAME_REMOTE_BUTTON, K_RELEASED, 
aTimeout);
   },
 
   this.registerEventOnPressed = function( aFunc, aKey ){
@@ -612,32 +626,55 @@
  * Access Namespace
  * **************************************************************************/
 var TuxAPI_access = function( self ){
-  this.acquire = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+  this.acquire = function( aPriorityLevel ){
+    if( aPriorityLevel == undefined ) aPriorityLevel = ACCESS_PRIORITY_NORMAL;
+    if( self.server.getClientLevel() == CLIENT_LEVEL_ANONYME ) return false;
+    var rv = self.server.request( 'access', 'acquire' , { 
'priority_level':aPriorityLevel } );
+    if (rv.get("result") == "Success") return true;
+    return false;
   },
 
-  this.waitAcquire = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+  this.waitAcquire = function( aTimeout, aPriorityLevel ){
+    var start = date.getTime();
+    while (!this.acquire(priorityLevel)){
+      if( start + aTimeout > date.getTime() ) return false;
+    }
+    return True
   },
 
-  this.forcingAcquire = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+  this.forcingAcquire = function( aClientID ){
+    if( self.server.getClientLevel() != CLIENT_LEVEL_ROOT ) return false;
+    var rv = self.server.request( 'access', 'forcing_acquire' , { 
'id_client':aClientID } );
+    if (rv.get("result") == "Success") return true;
+    return false;
   },
 
   this.release = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+    if( self.server.getClientLevel() == CLIENT_LEVEL_ANONYME ) return false;
+    var rv = self.server.request( 'access', 'release' , {} );
+    if (rv.get("result") == "Success") return true;
+    return false;
   },
 
   this.forcingRelease = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+    if( self.server.getClientLevel() != CLIENT_LEVEL_ROOT ) return false;
+    var rv = self.server.request( 'access', 'forcing_release' , {} );
+    if (rv.get("result") == "Success") return true;
+    return false;
   },
 
   this.lock = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+    if( self.server.getClientLevel() != CLIENT_LEVEL_ROOT ) return false;
+    var rv = self.server.request( 'access', 'lock' , {} );
+    if (rv.get("result") == "Success") return true;
+    return false;
   },
 
   this.unLock = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+    if( self.server.getClientLevel() != CLIENT_LEVEL_ROOT ) return false;
+    var rv = self.server.request( 'access', 'unlock' , {} );
+    if (rv.get("result") == "Success") return true;
+    return false;
   }
 };
 
@@ -690,7 +727,7 @@
   this.play = function( aTrack, aVolume ){
     if( !this.playAsync( aTrack, aVolume ) ) return false;
     if( self.server.getClientLevel() == CLIENT_LEVEL_ANONYME ) return true;
-    _eventHandlers.waitCondition();
+    //_eventHandlers.waitCondition();
     return true;
   },
 
@@ -705,7 +742,7 @@
 
 
 /*****************************************************************************
- * WAV Namespace
+ * RAW Namespace
  * **************************************************************************/
 var TuxAPI_RAW = function( self ){
   this.send = function( b0, b1, b2, b3, b4 ){
@@ -739,7 +776,7 @@
   this.play = function( aBegin, aEnd, aPath ){
     if( !this.playAsync( aBegin, aEnd, aPath ) ) return false;
     if( self.server.getClientLevel() == CLIENT_LEVEL_ANONYME ) return true;
-    _eventHandlers.waitCondition();
+    //_eventHandlers.waitCondition();
     return true;
   },
 
@@ -762,6 +799,8 @@
  * TTS Namespace
  * **************************************************************************/
 var TuxAPI_TTS = function( self ){
+  var _eventHandlers = self.getEventHandlers();
+
   this.isConsole = function(){
     return false;
   },
@@ -846,16 +885,19 @@
 /*****************************************************************************
  * Server Namespace
  * TODO
- *  Should check if ST_NAME_API_CONNECT works
+ *  Should use an emit / notify instead of a direct call
  * **************************************************************************/
 var TuxAPI_server = function( self, aHost, aPort ){
+  var _eventHandlers = self.getEventHandlers();
+
   this.autoConnect = function( aLevel, aName, aPasswd ){
-    //TuxAPIServer.py:355
-    return TUXAPI_NOT_IMPLEMENTED;
+    _autoConnectTimer = setInterval( function(){
+      _autoConnection( aLevel, aName, aPasswd );
+    }, 100 );
   },
 
   this.connect = function tuxConnect( aLevel, aName, aPasswd ){
-    if (this._connected) return true;
+    if (_connected) return true;
 
     var rv = this.request( 'client', 'create', {'level':aLevel, 'name':aName, 
'passwd':aPasswd } );
     var id_client = "-1";
@@ -865,12 +907,11 @@
     }
 
     if (id_client != "-1"){
-      this._level     = aLevel;
-      this._passwd    = aPasswd;
-      this._connected = true;
-      this._base_url  += id_client + "/";
-      if( this._onConnect )
-        this._onConnect(self);
+      _level     = aLevel;
+      _passwd    = aPasswd;
+      _connected = true;
+      _base_url  += id_client + "/";
+      _eventHandlers.emit( ST_NAME_API_CONNECT, ['True', 0] );
       return true;
     }
     
@@ -878,54 +919,56 @@
   },
 
   this.waitConnected  = function( aTimeout ){
-    //TuxAPIServer.py:127
-    return TUXAPI_NOT_IMPLEMENTED;
+    if( this.getClientLevel() == CLIENT_LEVEL_ANONYME ) return false;
+    if( this.getConnected() ) return true;
+    return _eventHandlers.waitCondition( ST_NAME_API_CONNECT, true, aTimeout );
   },
 
   this.registerEventOnConnected = function ( aFunction ){
-    this._onConnect = aFunction;
+    _eventHandlers.register( ST_NAME_API_CONNECT, aFunction, 'True' );
   },
 
   this.unregisterEventOnConnected = function (){
-    this._onConnect = null;
+    _eventHandlers.unregister( ST_NAME_API_CONNECT, aFunction, 'True' );
   },
 
   this.disconnect = function(){
-    if (!this._connected) return false;
+    if (!_connected) return false;
 
-    this._connected = false;
+    clearInterval( _autoConnectTimer );
+    _connected = false;
     var rv = this.request( 'client', 'destroy', { 'passwd':this._passwd } );
     if (rv.get("result") == "Success") {
-      if( this._onDisconnect )
-        this._onDisconnect(self);
+      _eventHandlers.emit( ST_NAME_API_CONNECT, ['False',0] );
       return true;
     }
     return false;
   },
 
   this.waitDisconnected = function( aTimeout ){
-    //TuxAPIServer.py:145
-    return TUXAPI_NOT_IMPLEMENTED;
+    if( this.getClientLevel() == CLIENT_LEVEL_ANONYME ) return false;
+    if( !this.getConnected() ) return true;
+    return _eventHandlers.waitCondition( ST_NAME_API_CONNECT, false, aTimeout 
);
   },
 
   this.registerEventOnDisconnected = function( aFunction ){
-    this._onDisconnect = aFunction;
+    _eventHandlers.register( ST_NAME_API_CONNECT, aFunction, 'False' );
   },
 
   this.unregisterEventOnDisconnected = function(){
-    this._onDisconnect = null;
+    _eventHandlers.unregister( ST_NAME_API_CONNECT, aFunction, 'False' );
   },
 
   this.getConnected = function(){
-    return this._connected;
+    return _connected;
   },
   
   this.getClientLevel = function(){
-    return this._level;
+    return _level;
   },
 
   this.getVersion = function(){
-    var rv = send_command( this._base_url + 'version?' );
+    var rv = send_command( _base_url + 'version?' );
 
     if (rv.get("result") == "Success")
       return rv.get("data0").get("version");
@@ -937,7 +980,7 @@
   this.request = function( aModule, aCommand , someParams ){
     //if ( !this._connected ) return false;
 
-    var cmd = this._base_url + aModule + '/' + aCommand + '?';
+    var cmd = _base_url + aModule + '/' + aCommand + '?';
     var params = '';
 
     for (param in someParams){ 
@@ -949,12 +992,16 @@
   }
 
   // Private variables
-  this._onConnect = null;
-  this._onDisconnect = null;
-  this._base_url = 'http://' + aHost + ':' + aPort + '/';
-  this._connected = false;
-  this._passwd = '';
-  this._level = null;
+  var _base_url = 'http://' + aHost + ':' + aPort + '/';
+  var _connected = false;
+  var _passwd = '';
+  var _level = null;
+  var _autoConnectTimer = null;
+
+  var _autoConnection = function( aLevel, aName, aPasswd ){
+    if( ! self.server.getConnected() )
+      self.server.connect( aLevel, aName, aPasswd );
+  };
 };
 
 
@@ -975,19 +1022,19 @@
   this.start = function(){
     if( this._timeout != null ) return;
     this._timeout = setInterval( function(){
-      self.event._retrieveEvents();
+      _retrieveEvents();
     }, this._delay);
   },
 
   this.stop = function(){
     clearInterval( this._delay );
     this._delay = null;
-  },
+  }
 
-  this._delay = 100,
-  this._timeout = null,
+  this._delay = 100;
+  this._timeout = null;
 
-  this._retrieveEvents = function (){
+  var _retrieveEvents = function (){
     if ( !self.server.getConnected() ) return;
 
     var events_struct = self.status.events();
@@ -1007,7 +1054,7 @@
       data_node = 'data' + i;
       event_struct = events_struct.get(data_node);
       event_name = event_struct.get("name");
-      this.handler.emit( event_name, [event_struct.get('value'), 
event_struct.get('delay') ] );
+      self.event.handler.emit( event_name, [event_struct.get('value'), 
event_struct.get('delay') ] );
     }
   };
 };
@@ -1033,6 +1080,11 @@
     return _funcList.length - 1;
   };
   
+  var _sleep = function( aTime ){
+    var start = date.getTime();
+    while( start + aTime > date.getTime() ) true;
+    return;
+  };
 
   this.register = function( aFunc, aCondition, anIndex ){
     if( aCondition == undefined ) aCondition = null;
@@ -1094,7 +1146,9 @@
   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 :'(
+    // I have take a look at stuff name narrativeJS and Co but it is not really
+    // an option in our case (or maybe i've made a mistake)
+    return TUXAPI_NOT_IMPLEMENTED;
   },
 
   this.clearPending = function(){
@@ -1109,66 +1163,67 @@
  * Event Handlers Namespace
  * **************************************************************************/
 var TuxAPI_eventHandlers = function(self){
-  var _callback_dict = {};
+  var _evtDict = {};
 
   this.register = function( anEventName, aFunc, aCondition, anIndex ){
-    if( _callback_dict[anEventName] ){
-      _callback_dict[anEventName].register( aFunc, aCondition, anIndex );
+    if( _evtDict[anEventName] ){
+      _evtDict[anEventName].register( aFunc, aCondition, anIndex );
       return true;
     }
     return false;
   },
 
   this.unregister = function( anEventName, anIndex ){
-    if( _callback_dict[anEventName] ){
-      _callback_dict[anEventName].unregister( anIndex );
+    if( _evtDict[anEventName] ){
+      _evtDict[anEventName].unregister( anIndex );
     }
     return false;
   },
 
   this.insert = function( anEventName ){
-    if( !_callback_dict[anEventName] ) return false,
-    _callback_dict[anEventName] = new TuxAPI_eventHandler(self);
+    if( !_evtDict[anEventName] ) return false,
+    _evtDict[anEventName] = new TuxAPI_eventHandler(self);
     return true;
   },
 
   this.getEventHandler = function( anEventName ){
-    if( !_callback_dict[anEventName] ) return null;
-    return _callback_dict[anEventName];
+    if( !_evtDict[anEventName] ) return null;
+    return _evtDict[anEventName];
   },
 
   this.storeContext = function(){
-    for( evt in _callback_dict )
-      _callback_dict[evt].storeContext();
+    for( evt in _evtDict )
+      _evtDict[evt].storeContext();
   },
 
   this.restoreContext = function(){
-    for( evt in _callback_dict )
-      _callback_dict[evt].restoreContext();
+    for( evt in _evtDict )
+      _evtDict[evt].restoreContext();
   },
 
   this.clearContext = function(){
-    for( evt in _callback_dict )
-      _callback_dict[evt].clearContext();
+    for( evt in _evtDict )
+      _evtDict[evt].clearContext();
   },
 
   this.emit = function( anEventName, someParams ){
-    if( _callback_dict[anEventName] )
-      _callback_dict[anEventName].emit( someParams );
+    if( _evtDict[anEventName] )
+      _evtDict[anEventName].emit( someParams );
   },
 
   this.notify = function( anEventName, someParams ){
-    if( _callback_dict[anEventName] )
-      _callback_dict[anEventName].emit( someParams );
+    if( _evtDict[anEventName] )
+      _evtDict[anEventName].emit( someParams );
   },
 
   this.waitCondition = function( anEventName, aCondition, aTimeout ){
-    if( _callback_dict[anEventName] )
-      _callback_dict[anEventName].waitCondition( aCondition, aTimeout );
+    if( _evtDict[anEventName] )
+      _evtDict[anEventName].waitCondition( aCondition, aTimeout );
   },
 
   this.clearPending = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+    for( evt in _evtDict )
+      _evtDict[evt].clearPending();
   }
 
   //Constructor
@@ -1187,15 +1242,16 @@
   this.leftOn = function( aCount ){
     if (!this.leftOnAsync( aCount ) ) return false;
     if (self.server.getClientLevel() == CLIENT_LEVEL_ANONYME) return true;
-    _eventHandlers.waitCondition();
-    return true;
+
+    var timeout = aCount * 5.0;
+    return this.waitLeftMovingOff( timeout );
   },
 
   this.leftOnDuring = function( aDuration){
     if (!this.leftOnDuringAsync( aCount ) ) return false;
     if (self.__parent.server.getClientLevel() == CLIENT_LEVEL_ANONYME) return 
true;
-    _eventHandlers.waitCondition();
-    return true;
+    var timeout = aDuration * 2.0;
+    return this.waitLeftMovingOff( timeout );
   },
 
   this.leftOnAsync = function( aCount ){
@@ -1213,15 +1269,15 @@
   this.rightOn = function( aCount ){
     if (!this.rightOnAsync( aCount ) ) return false;
     if (self.server.getClientLevel() == CLIENT_LEVEL_ANONYME) return true;
-    _eventHandlers.waitCondition();
-    return true;
+    var timeout = aCount * 5.0;
+    return this.waitRightMovingOff( timeout );
   },
 
   this.rightOnDuring = function( aDuration ){
     if (!this.rightOnDuringAsync( aCount ) ) return false;
     if (self.__parent.server.getClientLevel() == CLIENT_LEVEL_ANONYME) return 
true;
-    _eventHandlers.waitCondition();
-    return true;
+    var timeout = aDuration * 2.0;
+    return this.waitRightMovingOff( timeout );
   },
 
   this.rightOnAsync = function( aCount ){
@@ -1248,14 +1304,14 @@
     return false;
   },
 
-  this.waitLeftMovingOff = function(){
-    //TuxAPIFlippers.py:233
-    return TUXAPI_NOT_IMPLEMENTED;
+  this.waitLeftMovingOff = function( aTimeout ){
+    if (self.__parent.server.getClientLevel() == CLIENT_LEVEL_ANONYME) return 
true;
+    return _eventHandlers.waitCondition( ST_NAME_SPIN_LEFT_MOTOR_ON, false, 
aTimeout );
   },
 
-  this.waitRightMovingOff = function(){
-    //TuxAPIFlippers.py:256
-    return TUXAPI_NOT_IMPLEMENTED;
+  this.waitRightMovingOff = function( aTimeout ){
+    if (self.__parent.server.getClientLevel() == CLIENT_LEVEL_ANONYME) return 
true;
+    return _eventHandlers.waitCondition( ST_NAME_SPIN_RIGHT_MOTOR_ON, false, 
aTimeout );
   },
 
   this.getDirection = function(){
@@ -1298,12 +1354,11 @@
 
 /*****************************************************************************
  * Resource Leds
- * XXX Arghhh. I need more namespace here (as in the python source )
  * **************************************************************************/
 var TuxAPI_leds = function(self){
-  this.left   = new TuxAPI_LedBase( self, 'LED_LEFT' );
-  this.right  = new TuxAPI_LedBase( self, 'LED_RIGHT' );
-  this.both   = new TuxAPI_LedBase( self , 'LED_BOTH' );
+  this.left   = new TuxAPI_LedBase( self, LED_NAME_LEFT );
+  this.right  = new TuxAPI_LedBase( self, LED_NAME_RIGHT );
+  this.both   = new TuxAPI_LedBase( self , LED_NAME_BOTH );
 };
 
 
@@ -1311,24 +1366,49 @@
  * Resource LedBase
  * **************************************************************************/
 var TuxAPI_LedBase = function( self, aName ){
-  this.on = function( anIntensity ){
-    var rv = self.server.request( 'leds', 'on' , { 'intensity':anIntensity, 
'leds':aName } );
-    if (rv.get("result") == "Success") return true;
-    return false;
+  var _eventHandlers = self.getEventHandlers();
+
+  var _changeIntensity = function( aType, anIntensity ){
+
+    if( aType == LFX_NONE ){
+      var rv = self.server.request( 'leds', 'on' , { 'intensity':anIntensity, 
'leds':aName } );
+      if (rv.get("result") == "Success") return true;
+      return false;
+    }
+    else{ //some effect
+      var fxStep = 0;
+      if( aType == LFX_FADE ) fxStep = 10;
+      else if( aType == LFX_STEP ) fxStep = 3;
+      var fcxType = LFXEX_GRADIENT_NBR;
+      var rv = self.server.request( 'leds', 'set' , 
+        { 'fx_speed':'0.5', 'fx_step':fxStep, 'fx_type':aType, 
'intensity':anIntensity, 'leds':aName }
+       );
+      if (rv.get("result") == "Success") return true;
+      return false;
+    }
+  };
+
+
+  this.on = function( aType ){
+    if( aType == undefined ) aType = LFX_NONE;
+    _changeIntensity( aType, '1.0' );
   },
 
-  this.off = function(){
-    var rv = self.server.request( 'leds', 'off' , { 'leds':aName } );
-    if (rv.get("result") == "Success") return true;
-    return false;
+  this.off = function( aType ){
+    if( aType == undefined ) aType = LFX_NONE;
+    _changeIntensity( aType, '0.0' );
   },
 
-  this.setIntensity = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+  this.setIntensity = function( anIntensity ){
+    _changeIntensity( LFX_NONE, anIntensity );
   },
 
-  this.blinkDuring = function(){
-    return TUXAPI_NOT_IMPLEMENTED;
+  this.blinkDuring = function( aSpeed, aDuration, aFXType ){
+    if( aFXType == undefined ) aFXType = LFX_NONE;
+    var rv = this.blinkDuringAsync( aSpeed, aDuration , aFXType );
+    var start = date.getTime();
+    while( start + aDuration > date.getTime() ) true;
+    return rv;
   },
 
   this.blinkDuringAsync = function(){
@@ -1363,8 +1443,6 @@
 
   this.on = function( aCount, aFinalState ){
     if (!this.onAsync( aCount, aFinalState ) ) return false;
-    if (self.__parent.server.getClientLevel() == CLIENT_LEVEL_ANONYME) return 
true;
-    _eventHandlers.waitCondition();
     return true;
   },
 
@@ -1377,8 +1455,8 @@
   this.onDuring = function( aDuration, aFinalState ){
     if (!this.onDuringAsync( aDuration, aFinalState ) ) return false;
     if (self.__parent.server.getClientLevel() == CLIENT_LEVEL_ANONYME) return 
true;
-    _eventHandlers.waitCondition();
-    return true;
+    var timeout = aDuration * 2.0;
+    return this.waitMovingOff( timeout );
   },
 
   this.onDuringAsync = function( aDuration, aFinalState ){
@@ -1411,14 +1489,15 @@
     return false;
   },
 
-  this.waitMovingOff = function(){
-    //TuxAPIFlippers.py:204
-    return TUXAPI_NOT_IMPLEMENTED;
+  this.waitMovingOff = function( aTimeout ){
+    if (self.__parent.server.getClientLevel() == CLIENT_LEVEL_ANONYME) return 
true;
+    return _eventHandlers.waitCondition( ST_NAME_FLIPPERS_MOTOR_ON, false, 
aTimeout );
   },
 
-  this.waitPosition = function(){
-    //TuxAPIFlippers.py:216
-    return TUXAPI_NOT_IMPLEMENTED;
+  this.waitPosition = function( aPosition, aTimeout ){
+    if (self.__parent.server.getClientLevel() == CLIENT_LEVEL_ANONYME) return 
true;
+    if( this.getPosition() == aPosition ) return true;
+    return _eventHandlers.waitCondition( ST_NAME_FLIPPERS_POSITION, aPosition, 
aTimeout );
   },
 
   this.getPosition = function(){
@@ -1453,14 +1532,14 @@
 /*****************************************************************************
  * Resource MouthEyes
  * **************************************************************************/
-var TuxAPI_MouthEyes = function( self, aName ){
+var TuxAPI_MouthEyes = function( self, aPositionName, aMVName, 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;
-    _eventHandlers.waitCondition();
-    return true;
+    var timeout = aCount * 5.0;
+    return this.waitMovingOff( timeout );
   },
 
   this.onAsync = function(){
@@ -1472,8 +1551,9 @@
   this.onDuring = function( aDuration, aFinalState){
     if (!this.onDuringAsync( aCount, aFinalState ) ) return false;
     if (self.__parent.server.getClientLevel() == CLIENT_LEVEL_ANONYME) return 
true;
-    _eventHandlers.waitCondition();
     return true;
+    //_eventHandlers.waitCondition();
+    return true;
   },
 
   this.onDuringAsync = function(){
@@ -1500,20 +1580,21 @@
     return false;
   },
 
-  this.waitMovingOff = function(){
-    //TuxAPIMouthEyes.py:177
-    return TUXAPI_NOT_IMPLEMENTED;
+  this.waitMovingOff = function( aTimeout ){
+    if (self.__parent.server.getClientLevel() == CLIENT_LEVEL_ANONYME) return 
true;
+    return _eventHandlers.waitCondition( aMVName, false, aTimeout );
   },
 
-  this.waitPosition = function(){
-    //TuxAPIMouthEyes.py:189
-    return TUXAPI_NOT_IMPLEMENTED;
+  this.waitPosition = function( aPosition, aTimeout ){
+    if (self.__parent.server.getClientLevel() == CLIENT_LEVEL_ANONYME) return 
true;
+    if( this.getPosition() == aPosition ) return true;
+    return _eventHandlers.waitCondition( aPositionName, aPosition, aTimeout );
   },
 
   this.getPosition = function(){
     if (!self.server.getConnected()) return "NDEF";
 
-    var rv = self.status.requestOne(aName + "_position");
+    var rv = self.status.requestOne( aPositionName );
 
     if (rv.get("result") == "Success"){
       return rv.get("data0").get("value");
@@ -1525,7 +1606,7 @@
   this.getMovingState = function(){
     if (!self.server.getConnected()) return false;
 
-    var rv = self.status.requestOne(aName + "_remaining_movements");
+    var rv = self.status.requestOne( aMVName );
 
     if (rv.get("result") == "Success"){
       if (rv.get("data0").get("value") == "0"){
@@ -1602,8 +1683,11 @@
  * Resource DongleRadio
  * **************************************************************************/
 var TuxAPI_DongleRadio = function( self, aName ){
+  var _eventHandlers = self.getEventHandlers();
+  var _name = aName.split('_')[0];
+
   this.getConnected = function(){
-    var rv = self.status.requestOne( aName + '_state' );
+    var rv = self.status.requestOne( _name + '_state' );
     if (rv.get("result") == "Success"){
       rv = rv.get("data0").get("value");
       return rv;


------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn

Reply via email to