Advertisement
Guest User

Twisted Serial <--> XMPP sketch

a guest
Dec 21st, 2011
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.97 KB | None | 0 0
  1. from twisted.words.xish import domish
  2. from wokkel.xmppim import MessageProtocol, AvailablePresence
  3. from twisted.words.protocols.jabber import jid
  4. from wokkel.client import XMPPClient
  5.  
  6. from twisted.application import internet, service
  7. from twisted.web import server, xmlrpc
  8. from twisted.internet import ssl
  9.  
  10. from twisted.internet import reactor
  11. from twisted.internet.serialport import SerialPort
  12. from twisted.protocols.basic import LineReceiver
  13.  
  14.  
  15. import myxml
  16. import traceback
  17. import linecache, random
  18. import string
  19. import ConfigParser, io
  20. """
  21. ATTENTION : This one using ConfigParser !!
  22. """
  23. config = ConfigParser.RawConfigParser()
  24.  
  25.  
  26. class EchoBotProtocol(MessageProtocol):
  27.  
  28.     def __init__(self, serial, *args, **kwargs):
  29.         self.serial = serial
  30.         # Set a reference to self on the serial protocol
  31.         self.serial.protocol.echobot = self
  32.         MessageProtocol.__init__(self, *args, **kwargs)
  33.  
  34.     def connectionMade(self):
  35.         print "Connected! : "
  36.         print self.send(AvailablePresence())
  37.  
  38.  
  39.         mystatus = AvailablePresence()
  40.         mystatus["to"]="ourroom@conference.myjabber.internal/bino02"
  41.         print self.send(mystatus)
  42.        
  43.     def connectionLost(self, reason):
  44.         print "Disconnected!"
  45.  
  46.     def onMessage(self, msg):
  47.         #Will just print the msg body to stdout
  48.         print '<<<<>>>>>>>\n'
  49.         print msg.body
  50.         print '\n<<<<>>>>>>>\n'
  51.        
  52.         # Send something to serial port
  53.         self.serial.transport.write('some message\n')
  54.            
  55. class MyRPC(xmlrpc.XMLRPC):
  56.     def __init__(self):
  57.         self.allowNone = True
  58.         self.useDateTime = False
  59.  
  60.         #a var just as container of XMPP Client
  61.         self.xc = None
  62.        
  63.     def xmlrpc_dispatch(self, msg):
  64.         myto = msg['to']
  65.         myxbody = msg['xbody']
  66.         mybody = xmlrpclib.dumps((myxbody,))
  67.         mytype = msg['type']
  68.  
  69.         if mytype == None:
  70.             mytype == 'chat'
  71.         if self.xc <> None:
  72.             self.m = domish.Element((None, "message"))
  73.             self.m["to"] = myto
  74.             self.m["type"] = mytype
  75.             self.m.addElement("body", content=mybody)
  76.             self.xc.send(self.m)
  77.  
  78.  
  79. application = service.Application("newdispatch")
  80. svcCollection = service.IServiceCollection(application)
  81.  
  82. xmppclient = XMPPClient(jid.internJID("bino02@myjabber.internal/bino02"), "bino002")
  83. xmppclient.logTraffic = True
  84.  
  85.  
  86. # Protocol to handle serial port communication
  87. class ArduinoReceiver(LineReceiver):
  88.     def lineReceived(self, line):
  89.         print "Line Received from Arduino"
  90.         self.echobot.send('some xmpp message for which I do not know the format')
  91.  
  92. # Note there are more parameters to create the SerialPort (baudrate, etc.)
  93. serial = SerialPort(ArduinoReceiver, '/dev/tty.usbserial', reactor)
  94.  
  95. #just for receive XMPP
  96. echobot = EchoBotProtocol(serial)
  97. echobot.setHandlerParent(xmppclient)
  98.  
  99.  
  100. #Make xmlrpc site, and tell xmppclient to use
  101. rpc = MyRPC()
  102. rpc.xc = xmppclient
  103. site = server.Site(rpc)
  104. i = internet.TCPServer(50008, site, interface='localhost')
  105.  
  106.  
  107. #Add both service to collection
  108. i.setServiceParent(svcCollection)
  109. xmppclient.setServiceParent(svcCollection)
  110. serialService.setServiceParent(svcCollection)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement