Author: remi
Date: 2009-03-27 15:07:59 +0100 (Fri, 27 Mar 2009)
New Revision: 4222

Modified:
   
software_suite_v2/tuxware/tuxdroidserver/trunk/resources/01_robot_system/resourceAttituneManager.py
Log:
* added a start and a stop attitune

Modified: 
software_suite_v2/tuxware/tuxdroidserver/trunk/resources/01_robot_system/resourceAttituneManager.py
===================================================================
--- 
software_suite_v2/tuxware/tuxdroidserver/trunk/resources/01_robot_system/resourceAttituneManager.py
 2009-03-27 14:07:14 UTC (rev 4221)
+++ 
software_suite_v2/tuxware/tuxdroidserver/trunk/resources/01_robot_system/resourceAttituneManager.py
 2009-03-27 14:07:59 UTC (rev 4222)
@@ -39,6 +39,9 @@
         self.name = "attitune_manager"
         self.comment = "Resource to manage the attitunes container."
         self.fileName = RESOURCE_FILENAME
+        self.__attituneMutex = threading.Lock()
+        self.__attituneRunMutex = threading.Lock()
+        self.__attituneRunFlag = False
         # Create a gadgets container
         self.__attitunesContainer = AttitunesContainer()
         
self.__attitunesContainer.setOnDirectoryDeployedCallback(self.__onDirectoryDeployed)
@@ -102,7 +105,7 @@
             observerName, attituneFileName, message))
         self.__publishEvents(False, ST_NAME_AM_CONTAINER_ERROR, messagesList)
 
-    def __onAttituneUndeployed(self, gadget, attituneWorkingPath):
+    def __onAttituneUndeployed(self, attitune, attituneWorkingPath):
         self.logger.logDebug("Attitune undeployed [%s] to [%s]" % (
             attitune.getDescription().getName(), attituneWorkingPath))
         self.__publishEvents(False, ST_NAME_AM_ATTITUNE_UNLOADED,
@@ -131,6 +134,35 @@
         t = threading.Thread(target = async)
         t.start()
 
+    def __isAttituneRun(self):
+        self.__attituneRunMutex.acquire()
+        isRun = self.__attituneRunFlag
+        self.__attituneRunMutex.release()
+        return isRun
+
+    def __setAttituneRun(self, isRun):
+        self.__attituneRunMutex.acquire()
+        self.__attituneRunFlag = isRun
+        self.__attituneRunMutex.release()
+
+    def __attituneStartingLoop(self, attitune, duration):
+        self.__setAttituneRun(True)
+        timeout = int(duration * 10)
+        print timeout, duration
+        self.logger.logInfo("Attitune starting [%s]" % (
+            attitune.getDescription().getName(),))
+        self.__publishEvents(True, ST_NAME_AM_ATTITUNE_STARTING,
+            [attitune.getDescription().getName(),])
+        while self.__isAttituneRun():
+            timeout -= 1
+            time.sleep(0.1)
+            if timeout == 0:
+                break
+        self.logger.logInfo("Attitune stopped [%s]" % (
+            attitune.getDescription().getName(),))
+        self.__publishEvents(True, ST_NAME_AM_ATTITUNE_STOPPED,
+            [attitune.getDescription().getName(),])
+
     # 
--------------------------------------------------------------------------
     # Shared methods
     # 
--------------------------------------------------------------------------
@@ -196,6 +228,58 @@
                 return True
         return False
 
+    def playAttitune(self, name, begin):
+        """Play an attitune.
+        @param name: Attitune name.
+        @param begin: Begining position.
+        @return: True or False.
+        """
+        if not resourceTuxDriver.getDonglePlugged():
+            return False
+        attituneExists = False
+        attitunes = self.getAttitunesContainer().getAttitunes()
+        for attitune in attitunes:
+            if attitune.getDescription().getName() == name:
+                attituneExists = True
+                break
+        if not attituneExists:
+            return False
+        def async():
+            self.__attituneMutex.acquire()
+            if self.__isAttituneRun():
+                self.__setAttituneRun(False)
+                resourceTuxOSL.clearAll()
+                resourceTuxDriver.clearAll()
+                time.sleep(0.2)
+            macro = attitune.getMacro(begin)
+            macro = resourceTuxOSL.reencodeTTSTextInMacro(macro)
+            if len(macro) <= 16384:
+                resourceTuxDriver.executeMacro(macro)
+                resourceTuxOSL.executeMacro(macro)
+                duration = attitune.getDescription().getDuration() - \
+                    begin
+                if duration > 0.0:
+                    t = threading.Thread(target = self.__attituneStartingLoop,
+                        args = (attitune, duration))
+                    t.start()
+            self.__attituneMutex.release()
+        t = threading.Thread(target = async)
+        t.start()
+        return True
+
+    def stopAttitune(self):
+        """Stop the current played attitune.
+        """
+        def async():
+            self.__attituneMutex.acquire()
+            self.__setAttituneRun(False)
+            resourceTuxOSL.clearAll()
+            resourceTuxDriver.clearAll()
+            time.sleep(0.2)
+            self.__attituneMutex.release()
+        t = threading.Thread(target = async)
+        t.start()
+
 # Create an instance of the resource
 resourceAttituneManager = TDSResourceAttituneManager("resourceAttituneManager")
 # Register the resource into the resources manager
@@ -290,3 +374,53 @@
 
 # Register the service into the resource
 resourceAttituneManager.addService(TDSServiceAttituneManagerAttitunesInfos)
+
+# 
------------------------------------------------------------------------------
+# Declaration of the service "start_attitune_by_name".
+# 
------------------------------------------------------------------------------
+class TDSServiceAttituneManagerStartAttituneByName(TDSService):
+
+    def configure(self):
+        self.parametersDict = {
+            'name' : 'string',
+            'begin' : 'float',
+        }
+        self.minimalUserLevel = TDS_CLIENT_LEVEL_ANONYMOUS
+        self.exclusiveExecution = False
+        self.name = "start_attitune_by_name"
+        self.comment = "Start an attitune by it name."
+
+    def execute(self, id, parameters):
+        headersStruct = self.getDefaultHeadersStruct()
+        contentStruct = self.getDefaultContentStruct()
+        contentStruct['root']['result'] = getStrError(E_TDREST_SUCCESS)
+        name = parameters['name']
+        begin = parameters['begin']
+        if not resourceAttituneManager.playAttitune(name, begin):
+            contentStruct['root']['result'] = getStrError(E_TDREST_FAILED)
+        return headersStruct, contentStruct
+
+# Register the service into the resource
+resourceAttituneManager.addService(TDSServiceAttituneManagerStartAttituneByName)
+
+# 
------------------------------------------------------------------------------
+# Declaration of the service "stop_attitune".
+# 
------------------------------------------------------------------------------
+class TDSServiceAttituneManagerStopAttitune(TDSService):
+
+    def configure(self):
+        self.parametersDict = {}
+        self.minimalUserLevel = TDS_CLIENT_LEVEL_ANONYMOUS
+        self.exclusiveExecution = False
+        self.name = "stop_attitune"
+        self.comment = "Stop the current played attitune."
+
+    def execute(self, id, parameters):
+        headersStruct = self.getDefaultHeadersStruct()
+        contentStruct = self.getDefaultContentStruct()
+        contentStruct['root']['result'] = getStrError(E_TDREST_SUCCESS)
+        resourceAttituneManager.stopAttitune()
+        return headersStruct, contentStruct
+
+# Register the service into the resource
+resourceAttituneManager.addService(TDSServiceAttituneManagerStopAttitune)


------------------------------------------------------------------------------
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn

Reply via email to