Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from twisted.words.xish import domish
- from wokkel.xmppim import MessageProtocol, AvailablePresence
- from twisted.words.protocols.jabber import jid
- from wokkel.client import XMPPClient
- from twisted.application import internet, service
- from twisted.web import server, xmlrpc
- from twisted.internet import ssl
- from twisted.internet import reactor
- from twisted.internet.serialport import SerialPort
- from twisted.protocols.basic import LineReceiver
- import myxml
- import traceback
- import linecache, random
- import string
- import ConfigParser, io
- """
- ATTENTION : This one using ConfigParser !!
- """
- config = ConfigParser.RawConfigParser()
- class EchoBotProtocol(MessageProtocol):
- def __init__(self, serial, *args, **kwargs):
- self.serial = serial
- # Set a reference to self on the serial protocol
- self.serial.protocol.echobot = self
- MessageProtocol.__init__(self, *args, **kwargs)
- def connectionMade(self):
- print "Connected! : "
- print self.send(AvailablePresence())
- mystatus = AvailablePresence()
- print self.send(mystatus)
- def connectionLost(self, reason):
- print "Disconnected!"
- def onMessage(self, msg):
- #Will just print the msg body to stdout
- print '<<<<>>>>>>>\n'
- print msg.body
- print '\n<<<<>>>>>>>\n'
- # Send something to serial port
- self.serial.transport.write('some message\n')
- class MyRPC(xmlrpc.XMLRPC):
- def __init__(self):
- self.allowNone = True
- self.useDateTime = False
- #a var just as container of XMPP Client
- self.xc = None
- def xmlrpc_dispatch(self, msg):
- myto = msg['to']
- myxbody = msg['xbody']
- mybody = xmlrpclib.dumps((myxbody,))
- mytype = msg['type']
- if mytype == None:
- mytype == 'chat'
- if self.xc <> None:
- self.m = domish.Element((None, "message"))
- self.m["to"] = myto
- self.m["type"] = mytype
- self.m.addElement("body", content=mybody)
- self.xc.send(self.m)
- application = service.Application("newdispatch")
- svcCollection = service.IServiceCollection(application)
- xmppclient.logTraffic = True
- # Protocol to handle serial port communication
- class ArduinoReceiver(LineReceiver):
- def lineReceived(self, line):
- print "Line Received from Arduino"
- self.echobot.send('some xmpp message for which I do not know the format')
- # Note there are more parameters to create the SerialPort (baudrate, etc.)
- serial = SerialPort(ArduinoReceiver, '/dev/tty.usbserial', reactor)
- #just for receive XMPP
- echobot = EchoBotProtocol(serial)
- echobot.setHandlerParent(xmppclient)
- #Make xmlrpc site, and tell xmppclient to use
- rpc = MyRPC()
- rpc.xc = xmppclient
- site = server.Site(rpc)
- i = internet.TCPServer(50008, site, interface='localhost')
- #Add both service to collection
- i.setServiceParent(svcCollection)
- xmppclient.setServiceParent(svcCollection)
- serialService.setServiceParent(svcCollection)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement