def launch( self, strPathAndFilenameXar, bWaitEnd = True, strHostName = 'localhost', bForceLoading = False, rTimeOutInSec = 60*60*24*7 ): "launch a behavior and return the framemanager id" "return [id, return_value]" "the id has no use if bWaitEnd is true" "the return_value is not set if not bWaitEnd" "rTimeOutInSec: on pourrait peut etre mettre un temps plus court par défaut, ca serait mieux pour les tests" nSimulatedThreadID = random.randint( 1,10000 ); self.log( "INF: launch: Launching %s:%s, rTimeOutInSec=%5.3f (thread:%d)" % ( strHostName, strPathAndFilenameXar, rTimeOutInSec, nSimulatedThreadID ) ); mem = altools.myGetProxy( "ALMemory", strHostName = strHostName ); # mem.raiseMicroEvent( self.getVarName_SoftCancel( strPathAndFilenameXar ), False ); # on ne le fait pas ici, car des fois on a un stoppe, avant de rentrer dans cette méthode, donc autant le lire ici... par contre on le reinitialisera apres le comportement ! if( not bWaitEnd ): mem.raiseMicroEvent( self.getVarName_SoftCancel( strPathAndFilenameXar ), False ); # cependant dans ce cas, vu qu'on ne sera pas mis au courant de la fin de ce behavior, on ne pourra pas le remettre a False a la fin, donc il faut le mettre la try: if( mem.getData( self.getVarName_SoftCancel( strPathAndFilenameXar ) ) == True ): self.log( "INF: launch: Launching %s:%s - end (soft canceled before loading)(thread:%d)" % ( strHostName, strPathAndFilenameXar, nSimulatedThreadID ) ); mem.raiseMicroEvent( self.getVarName_SoftCancel( strPathAndFilenameXar ), False ); # on le réinit pour le prochain lancement return [None, None]; except: pass # oui cette variable peut ne pas encore exister! nXarID = self.load( strPathAndFilenameXar, strHostName, bForceLoading ); if( nXarID == -1 ): self.log( "ERR: launch: '%s:%s' load failure" % ( strHostName, strPathAndFilenameXar, nSimulatedThreadID ) ); return -1; nNumLoadedXar = self.find( strPathAndFilenameXar, strHostName ); if( self.aLoadedXar[nNumLoadedXar][3] == True ): self.log( "WNG: launch: already launched: '%s:%s' => waiting a little (thread:%d)" % ( strHostName, strPathAndFilenameXar, nSimulatedThreadID ) ); time.sleep( 1.0 ); # attend une seconde et reteste nNumLoadedXar = self.find( strPathAndFilenameXar, strHostName ); # refresh nNumLoadedXar if( self.aLoadedXar[nNumLoadedXar][3] == True ): self.log( "ERR: launch: already launched: '%s:%s' => skipped (thread:%d)" % ( strHostName, strPathAndFilenameXar, nSimulatedThreadID ) ); return -1; self.aLoadedXar[nNumLoadedXar][3] = True; mem.insertData( self.getVarName_Results( strPathAndFilenameXar ), self.unknown_value ); mem.insertData( self.getVarName_Cancel( strPathAndFilenameXar ), False ); try: if( mem.getData( self.getVarName_SoftCancel( strPathAndFilenameXar ) ) == True ): # on a été interrompu pendant le chargement ou la connection d'un machin! self.log( "INF: launch: Launching %s:%s - end (soft canceled before launching) (thread:%d)" % ( strHostName, strPathAndFilenameXar, nSimulatedThreadID ) ); mem.raiseMicroEvent( self.getVarName_SoftCancel( strPathAndFilenameXar ), False ); # on le réinit pour le prochain lancement self.aLoadedXar[nNumLoadedXar][3] = False; return [None, None]; except: pass # oui cette variable peut ne pas encore exister! fm = altools.myGetProxy( "ALFrameManager", strHostName = strHostName ); try: self.log( "DBG: launch: before fm.playBehavior(%s) (thread:%d)" % ( str( nXarID ), nSimulatedThreadID ) ); fm.playBehavior( nXarID ); self.log( "DBG: launch: after fm.playBehavior(%s) (thread:%d)" % ( str( nXarID ), nSimulatedThreadID ) ); except BaseException, err: self.log( "ERR: launch: while launching playBehavior on '%s': %s (thread:%d)" % ( strPathAndFilenameXar, err, nSimulatedThreadID ) ); # self.aLoadedXar[nNumLoadedXar][3] = False; # pas la peine: on va carrément l'invalider! # erasing it for next one! self.invalidate( strPathAndFilenameXar, strHostName ); return [None, None];
def runBehavior_BM( strBehavior, nTimeOut = 60 ): "launch a behavior on Nao, return "" if loading failed, or the 'naoqi' name of the behavior" "nTimeOut: temps maximum a attendre en secondes, -1 => pas d'attente (don't wait end)" abm = altools.myGetProxy( "ALBehaviorManager" ); strBeName = getLibraryName( strBehavior ); # comme on ne peut le charger qu'une fois, on doit toujours le stopper avant (ca stoppera un précédent) try: print( "naolibrary.runBehavior: stopping previously running (%s)" % strBehavior ); abm.stopBehavior( strBeName ); except: pass # not previously running ... not a big deal try: print( "naolibrary.runBehavior: unloading previously loaded (%s)" % strBehavior ); abm.unloadBehavior( self.strBehaviorName ); except: pass # not previously running ... not a big deal try: print( "naolibrary.runBehavior: loading (%s)" % strBehavior ); abm.loadBehavior( strBeName ); except: print( "naolibrary.runBehavior: can't preload behavior, version >= 1.4.32 (%s) ?" % strBehavior ); try: print( "naolibrary.runBehavior: running (%s)" % strBehavior ); abm.runBehavior( strBeName ); rStep = 0.2; rTotal = 0; while( abm.isBehaviorRunning( strBeName ) or rTotal > nTimeOut ): print( "naolibrary.runBehavior: waiting '%s'... %f..." % ( strBehavior, rStep ) ); time.sleep( rStep ); rTotal += rStep; except BaseException, err: altools.logToChoregraphe( "naolibrary.runBehavior: error occurs: " + str( err ) ); strBeName = "";
def isFinished( self, strPathAndFilenameXar, strHostName = 'localhost' ): mem = altools.myGetProxy( "ALMemory", strHostName = strHostName ); try: valueResults = mem.getData( self.getVarName_Results( strPathAndFilenameXar ) ); valueCancel = mem.getData( self.getVarName_SoftCancel( strPathAndFilenameXar ) ); # des fois le comportement appellé ne recoit pas que le softcancel a été raisé, donc on n'ajoute ici un test. except BaseException, err: altools.debug( "WNG: XarLoader.isFinished: return value not found for '%s:%s' (%s) => true" % ( strHostName, strPathAndFilenameXar, err ) ); return True;
def stop( self, strPathAndFilenameXar, strHostName = 'localhost' ): "stop a running behavior" "return true if the behavior was really running" self.log( "INF: stop: Stopping %s:%s" % ( strHostName, strPathAndFilenameXar ) ); nNumLoadedXar = self.find( strPathAndFilenameXar, strHostName ); if( nNumLoadedXar == -1 ): self.log( "WNG: stop: xar '%s:%s' is not running (perhaps currently loading - in this case, it will be stopped at the end of the loading)" % ( strHostName, strPathAndFilenameXar) ); # don't return there ! try: mem = altools.myGetProxy( "ALMemory", strHostName = strHostName ); mem.raiseMicroEvent( self.getVarName_SoftCancel( strPathAndFilenameXar ), True ); # pour etre sur on stoppe aussi le comportement (ca rajoute un peu plus de brutalité dans un monde de bug) fm = altools.myGetProxy( "ALFrameManager", strHostName = strHostName ); fm.exitBehavior( self.aLoadedXar[nNumLoadedXar][2] ); # will send a stop # mettre ici deleteBehavior si on veut liberer de la memoire (l'exit est fait dedans) except BaseException, err: self.log( "ERR: stop: deleting '%s' failure (already stopped?): %s" % ( strHostName, strPathAndFilenameXar, str(err) ) ); return False;
def unloadAll( self ): "unload all previously loaded behavior (gain ram, or update of behavior or ...)" for behav in self.aLoadedXar: self.log( "INF: unloadAll: erasing %s:%s" % ( behav[0], behav[1] ) ); try: fm = altools.myGetProxy( "ALFrameManager", strHostName = behav[0] ); fm.exitBehavior( behav[2] ); except BaseException, err: self.log( "ERR: unloadAll: %s!" % ( err ) );
def waitBehaviorEnd_BM( strBehavior, nTimeOut = 60 ): "Wait until the end of a previously launched behavior" "return False if not finished, True if finished" "nTimeOut: temps maximum a attendre en secondes, -1 => pas d'attente (don't wait end)" abm = altools.myGetProxy( "ALBehaviorManager" ); rStep = 0.2; rTotal = 0; while( abm.isBehaviorRunning( strBeName ) or rTotal > nTimeOut ): time.sleep( rStep ); rTotal += rStep; return nTotal <= nTimeOut;
def load( self, strPathAndFilenameXar, strHostName = 'localhost', bForceLoading = False ): "load a behavior and return the framemanager id" "return -1 if error" "ATTENTION: strHostName: PAS TROP ENCORE GERE / TESTE" "ATTENTION: bForceLoading: PAS TROP ENCORE GERE / TESTE" if( not bForceLoading ): nNumLoadedXar = self.find( strPathAndFilenameXar, strHostName ); if( nNumLoadedXar != -1 ): self.log( "INF: load: reusing previously loaded behavior %s:%s" % ( strHostName, strPathAndFilenameXar ) ); return self.aLoadedXar[nNumLoadedXar][2]; # already loaded - end try: fm = altools.myGetProxy( "ALFrameManager", strHostName = strHostName ); except BaseException, err: self.log( "ERR: load: host '%s' unreachable (%s)!" % ( strHostName, err ) ); return -1;
def stopBehavior_BM( strBehavior ): "stop a Behavior" abm = altools.myGetProxy( "ALBehaviorManager" ); abm.stopBehavior( strBehavior );
def prepare( self, strPathAndFilenameXar, strHostName = 'localhost' ): "reset soft cancel and all about this behaviors" "usefull to do before loading or launching, because choregraphe box could be stopped while starting so flags needs to be positionned at the first moment" self.log( "INF: prepare: ***** %s:%s" % ( strHostName, strPathAndFilenameXar ) ); mem = altools.myGetProxy( "ALMemory", strHostName = strHostName ); mem.raiseMicroEvent( self.getVarName_SoftCancel( strPathAndFilenameXar ), False );