Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2011
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. import traceback
  15. import string
  16.  
  17.  
  18. class EchoBotProtocol(MessageProtocol):
  19.  
  20.     def __init__(self, serial, *args, **kwargs):
  21.         self.serial = serial
  22.         # Set a reference to self on the serial protocol
  23.         self.serial.protocol.echobot = self
  24.         MessageProtocol.__init__(self, *args, **kwargs)
  25.  
  26.     def connectionMade(self):
  27.         print "Connected! : "
  28.         print self.send(AvailablePresence())
  29.         mystatus = AvailablePresence()
  30.         mystatus["to"]="ourroom@conference.jabber.internal/bino02"
  31.         print self.send(mystatus)
  32.    
  33.     def connectionLost(self, reason):
  34.         print "Disconnected!"
  35.  
  36.     def onMessage(self, msg):
  37.         #Will just print the msg body to stdout
  38.         print '<<<<>>>>>>>\n'
  39.         print msg.body
  40.         print '\n<<<<>>>>>>>\n'
  41.         # Send something to serial port
  42.         self.serial.transport.write('some message\n')
  43.            
  44. class ArduinoReceiver(LineReceiver):
  45.     def lineReceived(self, line):
  46.         print "Line Received from Arduino: ", line
  47.         #self.echobot.send('some xmpp message for which I do not know the format')
  48.  
  49. class SerialService(service.Service):
  50.     def startService(self):
  51.         self.serial = serial
  52.  
  53.  
  54. #Create Application
  55. application = service.Application("Serial MultiService Example")
  56.  
  57.  
  58. #just for receive XMPP
  59. xmppclient = XMPPClient(jid.internJID("bino02@jabber.internal/bino02"), "bino02")
  60. xmppclient.logTraffic = True
  61. #Seting Serial
  62. serial = SerialPort(ArduinoReceiver(), '/dev/ttyUSB0', reactor,baudrate=9600)
  63. #Tell EchoBotProtocol to use "serial"
  64. echobot = EchoBotProtocol(serial)
  65. #Set echobot Handler Parent to xmppclient
  66. echobot.setHandlerParent(xmppclient)
  67.  
  68. #Create serialService
  69. #as told at http://www.mentby.com/lucas-taylor/serialport-protocol-as-a-service.html
  70. serialService = SerialService()
  71.  
  72. #Creating Multi Service
  73. multiService = service.MultiService()
  74. #Add our Services to multiservice
  75. serialService.setServiceParent(multiService)
  76. xmppclient.setServiceParent(multiService)
  77.  
  78. #Set created application to be serviceparent of multiservice
  79. multiService.setServiceParent(application)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement