Author: jerome
Date: 2009-12-22 11:48:42 +0100 (Tue, 22 Dec 2009)
New Revision: 6008

Modified:
   
software_suite_v3/software/plugin/plugin-skype/branches/in_out_plugin/executables/plugin-skype.py
Log:
* Changed and improved the way to send commands ( more 'readable' ).

Modified: 
software_suite_v3/software/plugin/plugin-skype/branches/in_out_plugin/executables/plugin-skype.py
===================================================================
--- 
software_suite_v3/software/plugin/plugin-skype/branches/in_out_plugin/executables/plugin-skype.py
   2009-12-22 10:46:53 UTC (rev 6007)
+++ 
software_suite_v3/software/plugin/plugin-skype/branches/in_out_plugin/executables/plugin-skype.py
   2009-12-22 10:48:42 UTC (rev 6008)
@@ -39,6 +39,7 @@
 
 from IPN.IPNServer import IPNServer
 from IPN.IPNClient import IPNClient
+from IPNCommands import *
 
 from util.SimplePlugin.SimplePluginConfiguration import 
SimplePluginConfiguration
 from util.SimplePlugin.SimplePlugin import SimplePlugin
@@ -59,8 +60,7 @@
         '''
         SimplePluginConfiguration.__init__(self)
         self.__throwEmoticons = True
-        self.__throwTTS = True
-        self.__isOutgoing = False    
+        self.__throwTTS = True  
     
     # 
--------------------------------------------------------------------------
     # Return true if emoticons will be thrown.
@@ -93,22 +93,6 @@
         '''Set the throwTTS parameter value.
         '''
         self.__throwTTS = throwTTS
-    
-    # 
--------------------------------------------------------------------------
-    #  Return the outgoing parameter value.
-    # 
--------------------------------------------------------------------------
-    def getIsOutgoing(self):
-        '''Return the outgoing parameter value.
-        '''
-        return self.__isOutgoing
-    
-    # 
--------------------------------------------------------------------------
-    #  Set the outgoing parameter value.
-    # 
--------------------------------------------------------------------------
-    def setIsOutgoing(self, isOutgoing):
-        '''Set the outgoing parameter value.
-        '''
-        self.__isOutgoing = isOutgoing
  
  
 class SkypePlugin(SimplePlugin):
@@ -133,28 +117,22 @@
     #Used for outgoing parameter only.
     onlineList = []
     currentContact = -1
-    partner = ""
+    partner = None
+    initialized = False
+    isIncoming = False
+    isOutgoingUser = True 
+    clientStartedByPlugin = False
     
     
     ##----------------------------------------------------------------
     ## * Skype plugin main entry point.
     ##----------------------------------------------------------------
     def start(self):
+        '''Plugin entry point.
         '''
-        Plugin entry point.
-        This method should be used to dispatch commands.
-        '''
-        #Check if skype is installed.
-        if not SkypeClient.isSkypeClientInstalled():
-            if self.getCommand() != "check":
-                self.throwNotification("start")
-                self.throwMessage("Sorry, it looks like skype is not 
installed. Please go to the skype website to download the software.")
-                self.throwNotification("stop")
-            self.stop()
-            
+        self.checkSkype()
         #Init port.
         self.port = TuxDroidServerUtils.getServerPort()
-        
         #Check for the command.
         if self.getCommand() == "check":
             self.runDaemon()
@@ -163,143 +141,148 @@
         else:
             self.runDaemon()
     
+    
+    def checkSkype(self):
+        '''Check for skype installed.
+        '''
+        #Check if skype is installed.
+        if not SkypeClient.isSkypeClientInstalled():
+            if self.getCommand() != "check":
+                self.throwNotification("start")
+                self.throwMessage("Sorry, it looks like skype is not 
installed. Please go to the skype website to download the software.")
+                self.throwNotification("stop")
+            self.stop()
+    
+    
     ##----------------------------------------------------------------
-    ## * PLUGIN COMMAND : Set Tux Droid for an incoming call call.
+    ##  * Tux Droid plugin events. 
     ##----------------------------------------------------------------
+    def onPluginStop(self):
+        '''OnPluginStop event.
+        '''
+        if self.getCommand() == 'check':
+            self.serv.stop()
+        if self.getCommand() == 'run': 
+            self.client.stop()
+    
+    
+    ##----------------------------------------------------------------
+    ## * PLUGIN RUN COMMAND 
+    ##----------------------------------------------------------------
     def run(self):
+        '''Initialize Tux Droid switches for a call ( incoming or outgoing )
         '''
-        Initialize Tux Droid switches for a call ( incoming or outgoing )
-        '''
         self.client = IPNClient('127.0.0.1', 48536)
         self.client.registerOnNotificationCallBack(self.ClientOnNotification)
-        self.client.registerOnConnectedCallBack(self.ClientOnConnected)
         self.client.start()
-        
-        if self.configuration().getIsOutgoing():
-            self.client.notify('get_outgoing_command')
     
     
-    def incomingPluginEvent(self, eventName, eventValues):
+    def stopTTS(self):
+        '''Stop the tts.
         '''
-        Tux Droid plugin event callback.
-        '''
         request = '/tts/stop?'
-        if self.command == 'incoming':
-            #Accept call 
-            if eventName   == 'head':
-                #Stop current tts message.
-                TuxDroidServerUtils.sendRequest("127.0.0.1", self.port, 
request)
-                self.client.notify('accept_call')
-            #Reject Call          
-            elif eventName in ['left', 'right']:
-                TuxDroidServerUtils.sendRequest("127.0.0.1", self.port, 
request)
-                self.client.notify('reject_call')
-                
-        #endcall command, waiting for the call ends.
-        if self.command == 'wait_end_call':
-            if eventName == 'head':
-                #Then send finish call command to IPN server.
-                self.client.notify('end_call')
+        TuxDroidServerUtils.sendRequest("127.0.0.1", self.port, request)
     
-    
-    def outgoingPluginEvent(self, eventName, eventValues):
+  
+    def onPluginEvent(self, eventName, eventValues):
+        '''Callback on plugin event.
         '''
-        Tux Droid event callback for outgoing calls.
-        '''
-        if self.command == 'outgoing':
-            #Proceed call
-            if eventName   == 'head':
-                self.client.notify('call_contact')
-                
-            #Next contact          
-            elif eventName == 'left':
-                self.client.notify('next_contact')
-            elif eventName == 'right':
-                self.client.notify('previous_contact')
+        #Start the adequate function for each plugin command.
+        if not self.getCommand() == 'run':
+            return
         
-        #endcall command, waiting for the call ends.
-        if self.command == 'wait_end_call':
-            if eventName == 'head':
-                #Then send finish call command to IPN server.
-                self.client.notify('end_call')
+        if eventName == 'head':
+            #Incoming call head button
+            if self.command == COMMAND_INCOMING:
+                self.client.notify(ACCEPT_INCOMING)
+                self.command = WAIT_END_CALL
+                self.stopTTS()
+            elif self.command == WAIT_END_CALL:
+                self.client.notify(CLOSE_CALL)
             
+            #Outgoing call head button
+            elif self.command == COMMAND_OUTGOING_USER:
+                self.client.notify(CALL_CONTACT)
+                self.command = WAIT_END_CALL
+                self.stopTTS()
+                self.throwNotification("start")
+                self.throwActuation("openMouth")
+                self.throwActuation("upFlippers")
+                self.throwNotification("stop")
+                
+        elif eventName == 'left':
+            #Incoming call left flipper
+            if self.command == COMMAND_INCOMING:
+                self.client.notify(REJECT_INCOMING)
             
+            #Outgoing call left flipper
+            elif self.command == COMMAND_OUTGOING_USER:
+                self.client.notify(NEXT_CONTACT)
+                
+        elif eventName == 'right':
+            #Incoming call right fliper
+            if self.command == COMMAND_INCOMING:
+                self.client.notify(REJECT_INCOMING)
+            
+            #Outgoing call right fliper.
+            elif self.command == COMMAND_OUTGOING_USER:
+                self.client.notify(PREVIOUS_CONTACT)
+                
          
     def ClientOnNotification(self, Message):
+        '''Recieved a message from server.
         '''
-        Recieved a message from server.
-        '''
-        thread = threading.Thread(target=self.OnNotification, args = (Message, 
))
-        thread.start()
+        #Commands recaived at plugin startup.
+        if Message == COMMAND_INCOMING:
+            self.command = COMMAND_INCOMING
+            #Asking call partner.
+            self.client.notify(GET_INCOMING_CALL_PARTNER)
+            
+        elif Message == COMMAND_OUTGOING_USER:
+            self.command = COMMAND_OUTGOING_USER
+            self.client.notify(GET_CONTACTS_LIST)
         
-    
-    def OnNotification(self, Message):
-        '''
-        Threaded client notification.
-        '''
-        if Message == 'command>incoming':
+        elif Message == COMMAND_OUTGOING_INTERFACE:
+            self.command = COMMAND_OUTGOING_INTERFACE
+            #Simply open mouth and up flippers
             self.throwNotification("start")
+            self.throwActuation("openMouth")
             self.throwActuation("upFlippers")
             self.throwNotification("stop")
-            self.command = 'incoming'
+            #the only available comand is to wait for call ends.
+            self.command = WAIT_END_CALL
         
-        elif Message == 'command>wait_end_call':
-            self.command = 'wait_end_call'
-            
-            #Flippers up and mouth open if outgoing call.
-            if self.configuration().getIsOutgoing():
-                #Stop tts.
-                request = '/tts/stop?'
-                TuxDroidServerUtils.sendRequest("127.0.0.1", self.port, 
request)
-                self.throwNotification("start")
-                self.throwActuation("upFlippers")
-                self.throwActuation("openMouth")
-                self.throwNotification("stop")
-        
-        elif Message == 'command>endcall':
+        elif Message[:1].find(GET_INCOMING_CALL_PARTNER) != -1:
+            #Stop current tts message.
+            self.stopTTS()
+            time.sleep(0.2)
             self.throwNotification("start")
-            self.throwActuation("downFlippers")
-            self.throwActuation("closeMouth")
+            #Once we have call partnet, setting up Tux Droid.
+            self.throwActuation("upFlippers")
+            self.throwMessage('{0} is calling you', Message[1:])
+            self.throwActuation("openMouth")
             self.throwNotification("stop")
-            self.stop()
         
-        elif Message[:4].find('tts>') != -1:
+        elif Message[:1].find(COMMAND_TTS) != -1:
             #Stop current tts message.
-            request = '/tts/stop?'
-            TuxDroidServerUtils.sendRequest("127.0.0.1", self.port, request)
+            self.stopTTS()
+            time.sleep(0.2)
             self.throwNotification("start")
-            self.throwMessage('%s' % Message[4:])
+            self.throwMessage(Message[1:])
             self.throwNotification("stop")
-        
-        elif Message[:18].find("tts_incoming_call>") != -1:
-            if self.command != "incoming":
-                return
+            
+        elif Message == END:
             self.throwNotification("start")
-            self.throwMessage('{0} is calling you', Message[18:])
-            #Re - open mouth
-            self.throwActuation("openMouth")
+            self.throwActuation("downFlippers")
+            self.throwActuation("closeMouth")
             self.throwNotification("stop")
+            self.stopTTS()
+            self.stop()
+            
         
-        elif Message == 'command>interface_outgoing_call':
-            self.throwNotification("start")
-            self.throwActuation("upFlippers")
-            self.throwActuation("openMouth")
-            self.throwNotification("stop")
-            self.command = 'wait_end_call'
-          
-        elif Message == 'command>user_outgoing_call':
-            self.command = 'outgoing'
-            self.client.notify('get_contacts')
-
     
-    def ClientOnConnected(self, id):
-        '''
-        '''
-        pass
-            
-    
     ##----------------------------------------------------------------
-    ##  * DAEMON command and functions.
+    ##  * DAEMON commands and functions.
     ##----------------------------------------------------------------
     
     def runDaemon(self):
@@ -310,11 +293,9 @@
         
         self.serv = IPNServer('127.0.0.1', 48536)
         self.serv.registerOnClientAddedCallBack(self.ServerOnClientAdded)
-        self.serv.registerOnClientRemovedCallBack(self.ServerOnClientRemoved)
         
self.serv.registerOnClientNotificationCallBack(self.ServerOnClientNotification)
         self.serv.start()
         
-        
         #Initialize skype client and api objects.
         self.connectorObj = connector.Connector()
         self.connectorObj.OnAPIReady = self.initializeDaemon
@@ -352,7 +333,9 @@
         self.asynchronious.OnOutgoingCall         = self.OnOutgoingCall
         self.asynchronious.OnOutgoingFinished     = self.OnOutgoingFinished
         
-        self.asynchronious.OnlineContactStatusChanged = 
self.ContactStatusChanged
+        self.mutex.acquire()
+        self.initialized = True
+        self.mutex.release()
       
         
     def OnIncomingRefused(self, contactHandle):
@@ -380,6 +363,7 @@
         '''
         Incoming call event.
         '''
+        self.isIncoming = True
         #Set up Tux Droid motors.
         self.partner = self.synchronious.getTTSName(contactHandle)
         thread = threading.Thread(target=self.startRunMode)
@@ -393,8 +377,6 @@
         '''
         request = '/plugins_server/start_plugin?command=run&uuid='+ self.uuid 
+'&parameters='
         TuxDroidServerUtils.sendRequest("127.0.0.1", self.port, request)
-        self.command = 'incoming'
-        self.serv.notify("tts>%s" % tts)
     
     
     def OnIncomingFinished(self, contactHandle):
@@ -402,30 +384,32 @@
         Incoming call finished event.
         '''
         #Send reset switches signal.
-        self.serv.notify('command>endcall')
-        self.command = ""
-       
+        self.serv.notify(END)
         
     
     def OnOutgoingCall(self, contactHandle):
         '''
-        Outgoing call in progress event.
+        Outgoing call in progress callback.
         '''
         #If call was done from skype client interface, then start the plugin
-        if not ( self.command == 'user_outgoing_call' ):
-            parameters = 'isOutgoing=true'
-            request = '/plugins_server/start_plugin?command=run&uuid='+ 
self.uuid +'&parameters=' + parameters
+        if ( not self.serv.hasClient() ): 
+            self.isOutgoingUser = False
+            request = '/plugins_server/start_plugin?command=run&uuid='+ 
self.uuid +'&parameters='
             TuxDroidServerUtils.sendRequest("127.0.0.1", self.port, request)
-            self.command = 'interface_outgoing_call'
-            
+
+     
         
     
     def OnOutgoingFinished(self, contactHandle):
         '''
+        Outgoing call finished callback.
         '''
+        #Stop Skype client if it was started by the plugin.
+        if self.clientStartedByPlugin:
+            self.connectorObj.stopClient()
+            self.clientStartedByPlugin = False
         #Send reset switches signal.
-        self.serv.notify('command>endcall')
-        self.command = ""
+        self.serv.notify(END)
     
     
     def OnChatMessage(self, contactHandle):
@@ -466,8 +450,15 @@
         '''
         Call the current selected contact.
         '''
+        if not self.connectorObj.isClientRunning():
+            self.serv.notify(END)
+            return
+            
         if self.onlineList[self.currentContact] == 'Quit gadget':
-            self.serv.notify("command>endcall")
+            if self.clientStartedByPlugin:
+                self.connectorObj.stopClient()
+                self.clientStartedByPlugin = False
+            self.serv.notify(END)
         else:
             self.synchronious.call(self.onlineList[self.currentContact])
     
@@ -483,7 +474,7 @@
             if self.currentContact >= len(self.onlineList):
                 self.currentContact = 0
                 
-            self.serv.notify('tts>%s' % self.onlineList[self.currentContact])
+            self.serv.notify(COMMAND_TTS + 
self.onlineList[self.currentContact])
         finally:
             time.sleep(0.4)
             self.mutex.release()            
@@ -501,7 +492,7 @@
                 #Then going to the last online list entry.
                 self.currentContact = len(self.onlineList) - 1
                 
-            self.serv.notify('tts>%s' % self.onlineList[self.currentContact])
+            self.serv.notify(COMMAND_TTS + 
self.onlineList[self.currentContact])
         finally:
             time.sleep(0.4)
             self.mutex.release()  
@@ -521,118 +512,91 @@
                     self.synchronious.setAudioCards()
             except:
                 pass
-                   
-    
-    
-    ##----------------------------------------------------------------
-    ##  * Tux Droid plugin events. 
-    ##----------------------------------------------------------------
-    def onPluginStop(self):
-        '''
-        OnPluginStop event.
-        '''
-        if self.getCommand() == 'run':
-            self.client.stop()
-        else:    
-            if self.connectorObj != None:
-                if self.getCommand() == 'outgoingCall':
-                    #TODO update this function.
-                    self.connectorObj.stop(KillClient=True)
-                else:
-                    #In other cases, close connection but not skype client.
-                    self.connectorObj.stop()
-    
-    
-   
-    def onPluginEvent(self, eventName, eventValues):
-        '''
-        Callback on plugin event.
-        @param eventName: Event name.
-        @param eventValues: Event values.
-        '''
-        #Start the adequate function for each plugin command.
-        if self.getCommand() == 'run':
-            if not self.configuration().getIsOutgoing():
-                self.incomingPluginEvent(eventName, eventValues)
-            else:
-                self.outgoingPluginEvent(eventName, eventValues)
-        
-        
+                
      
     ##----------------------------------------------------------------
     ##  * DAEMON Client / Server IPN Communication.
     ##----------------------------------------------------------------
     def ServerOnClientAdded(self, idClient):
         '''
+        Server IPN callback when IPN client is added.
         '''
-        if not self.configuration().getIsOutgoing():
-            self.serv.notify( "command>" + self.command )
-            time.sleep(0.2)
-            #Sending call partner tts name.
-            self.serv.notify("tts_incoming_call>%s" % self.partner)
-            self.partner = ""
         
-    
-    def ServerOnClientRemoved(self, idClient):
-        '''
-        '''
-        pass
+        #First action, sending command to the plugin client.
+        if self.isIncoming:
+            #Then it's an incoming call.
+            self.serv.notify(COMMAND_INCOMING)
+        elif self.isOutgoingUser:
+            if not self.connectorObj.isClientRunning():
+                self.connectorObj.startClient()
+                while ( not self.initialized ):
+                    time.sleep(2.0)
+                self.initialized = False
+                self.clientStartedByPlugin = True   
+            #Then interface outgoing call.
+            self.serv.notify(COMMAND_OUTGOING_USER)
+        else:
+            #Then user uses TuxBox to make a call. 
+            self.serv.notify(COMMAND_OUTGOING_INTERFACE)
         
+        #Finally, reset values.            
+        self.isIncoming = False
+        self.isOutgoingUser = True        
         
+        
+        
     def ServerOnClientNotification(self, idClient, Message):
         '''
         '''
-        #--------  Incoming IPN messages --------#
-        if Message == 'accept_call':
+        #--------------  Incoming call commands --------------#
+        #Request to get current call partner.
+        if Message == GET_INCOMING_CALL_PARTNER:
+            if self.partner != None:
+                self.serv.notify(GET_INCOMING_CALL_PARTNER + self.partner)
+            else:
+                self.serv.notify(GET_INCOMING_CALL_PARTNER + "unknown")
+                
+        #Request to accept current incoming call
+        elif Message == ACCEPT_INCOMING:
             if self.asynchronious.call != None:
-                #Take call
-                self.serv.notify('command>wait_end_call')
                 thread = threading.Thread(target=self.synchronious.acceptCall, 
args =(self.asynchronious.call, ))
                 thread.start()
-        
-        elif Message == 'reject_call' :
-            #Reject call.
+                
+        #Request to reject current incoming call
+        elif Message == REJECT_INCOMING:
             self.synchronious.rejectCall()
+            self.serv.notify(END)
         
-        #--------  Outgoing IPN messages --------#        
-        elif Message == 'get_contacts':
-            #Outgoing call requested, so getting online list and sending 
outgoing command.
-            self.onlineList = self.synchronious.getOnlineList()
-            #Adding fake 'Quit gadget' contact.
+        #--------------  Common call commands --------------#
+        #Request to close the current call
+        elif Message == CLOSE_CALL:
+            self.synchronious.finishCall()
+        
+        #--------------  Outgoing call commands --------------#
+        elif Message == GET_CONTACTS_LIST:
+            try:
+                self.onlineList = self.synchronious.getOnlineList()
+            except:
+                self.onlineList = []
             self.onlineList.append("Quit gadget")
-            time.sleep(0.4)
-            #And throwing the first online contact in list.
-            self.nextContact()
+            time.sleep(0.3)
             
-        #Outgoing
-        elif Message == 'call_contact':
-            #Proceed call.
-            self.serv.notify('command>wait_end_call')
-            thread = threading.Thread(target=self.callCurrentContact)
-            thread.start()
-   
-        elif Message == 'next_contact':
+            #at least one user find.
+            if len(self.onlineList) > 1:
+                self.currentContact = 0
+                self.serv.notify( COMMAND_TTS + self.onlineList[0] ) 
+                
+        
+        elif Message == NEXT_CONTACT:
             thread = threading.Thread(target=self.nextContact)
             thread.start()
-        
-        elif Message == 'previous_contact':
+        elif Message == PREVIOUS_CONTACT:
             thread = threading.Thread(target=self.previousContact)
             thread.start()
-        
-        elif Message == 'get_outgoing_command':
-            if self.command == 'interface_outgoing_call':
-                #User call someone with Skype client interface.
-                self.serv.notify('command>interface_outgoing_call')
-                self.command = 'interface_outgoing_call'
-            else:
-                #User pick contact from tuxdroid
-                self.serv.notify('command>user_outgoing_call')
-                self.command = 'user_outgoing_call'
-        
-        #--------  Common IPN messages --------#
-        elif Message == 'end_call':
-            self.synchronious.finishCall()
-        
+        elif Message == CALL_CONTACT:
+            thread = threading.Thread(target=self.callCurrentContact)
+            thread.start()
+            
 
 if __name__ == "__main__":
     plugin = SkypePlugin()


------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev 
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn

Reply via email to