SHOW:
|
|
- or go back to the newest paste.
| 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 myxml |
| 15 | + | |
| 16 | ||
| 17 | - | import linecache, random |
| 17 | + | |
| 18 | class EchoBotProtocol(MessageProtocol): | |
| 19 | - | import ConfigParser, io |
| 19 | + | |
| 20 | - | """ |
| 20 | + | def __init__(self, serial, *args, **kwargs): |
| 21 | - | ATTENTION : This one using ConfigParser !! |
| 21 | + | self.serial = serial |
| 22 | - | """ |
| 22 | + | # Set a reference to self on the serial protocol |
| 23 | - | config = ConfigParser.RawConfigParser() |
| 23 | + | self.serial.protocol.echobot = self |
| 24 | MessageProtocol.__init__(self, *args, **kwargs) | |
| 25 | ||
| 26 | def connectionMade(self): | |
| 27 | print "Connected! : " | |
| 28 | - | def __init__(self, serial, *args, **kwargs): |
| 28 | + | print self.send(AvailablePresence()) |
| 29 | - | self.serial = serial |
| 29 | + | mystatus = AvailablePresence() |
| 30 | - | # Set a reference to self on the serial protocol |
| 30 | + | mystatus["to"]="[email protected]/bino02" |
| 31 | - | self.serial.protocol.echobot = self |
| 31 | + | print self.send(mystatus) |
| 32 | - | MessageProtocol.__init__(self, *args, **kwargs) |
| 32 | + | |
| 33 | def connectionLost(self, reason): | |
| 34 | - | def connectionMade(self): |
| 34 | + | print "Disconnected!" |
| 35 | - | print "Connected! : " |
| 35 | + | |
| 36 | - | print self.send(AvailablePresence()) |
| 36 | + | def onMessage(self, msg): |
| 37 | #Will just print the msg body to stdout | |
| 38 | print '<<<<>>>>>>>\n' | |
| 39 | - | mystatus = AvailablePresence() |
| 39 | + | print msg.body |
| 40 | - | mystatus["to"]="[email protected]/bino02" |
| 40 | + | print '\n<<<<>>>>>>>\n' |
| 41 | - | print self.send(mystatus) |
| 41 | + | # Send something to serial port |
| 42 | - | |
| 42 | + | self.serial.transport.write('some message\n')
|
| 43 | - | def connectionLost(self, reason): |
| 43 | + | |
| 44 | - | print "Disconnected!" |
| 44 | + | |
| 45 | def lineReceived(self, line): | |
| 46 | - | def onMessage(self, msg): |
| 46 | + | print "Line Received from Arduino: ", line |
| 47 | #self.echobot.send('some xmpp message for which I do not know the format')
| |
| 48 | - | print '<<<<>>>>>>>\n' |
| 48 | + | |
| 49 | - | print msg.body |
| 49 | + | class SerialService(service.Service): |
| 50 | - | print '\n<<<<>>>>>>>\n' |
| 50 | + | def startService(self): |
| 51 | - | |
| 51 | + | self.serial = serial |
| 52 | - | # Send something to serial port |
| 52 | + | |
| 53 | - | self.serial.transport.write('some message\n')
|
| 53 | + | |
| 54 | #Create Application | |
| 55 | - | class MyRPC(xmlrpc.XMLRPC): |
| 55 | + | application = service.Application("Serial MultiService Example")
|
| 56 | - | def __init__(self): |
| 56 | + | |
| 57 | - | self.allowNone = True |
| 57 | + | |
| 58 | - | self.useDateTime = False |
| 58 | + | |
| 59 | xmppclient = XMPPClient(jid.internJID("[email protected]/bino02"), "bino02")
| |
| 60 | - | #a var just as container of XMPP Client |
| 60 | + | |
| 61 | - | self.xc = None |
| 61 | + | #Seting Serial |
| 62 | - | |
| 62 | + | serial = SerialPort(ArduinoReceiver(), '/dev/ttyUSB0', reactor,baudrate=9600) |
| 63 | - | def xmlrpc_dispatch(self, msg): |
| 63 | + | #Tell EchoBotProtocol to use "serial" |
| 64 | - | myto = msg['to'] |
| 64 | + | |
| 65 | - | myxbody = msg['xbody'] |
| 65 | + | #Set echobot Handler Parent to xmppclient |
| 66 | - | mybody = xmlrpclib.dumps((myxbody,)) |
| 66 | + | |
| 67 | - | mytype = msg['type'] |
| 67 | + | |
| 68 | #Create serialService | |
| 69 | - | if mytype == None: |
| 69 | + | #as told at http://www.mentby.com/lucas-taylor/serialport-protocol-as-a-service.html |
| 70 | - | mytype == 'chat' |
| 70 | + | serialService = SerialService() |
| 71 | - | if self.xc <> None: |
| 71 | + | |
| 72 | - | self.m = domish.Element((None, "message")) |
| 72 | + | #Creating Multi Service |
| 73 | - | self.m["to"] = myto |
| 73 | + | multiService = service.MultiService() |
| 74 | - | self.m["type"] = mytype |
| 74 | + | #Add our Services to multiservice |
| 75 | - | self.m.addElement("body", content=mybody)
|
| 75 | + | serialService.setServiceParent(multiService) |
| 76 | - | self.xc.send(self.m) |
| 76 | + | xmppclient.setServiceParent(multiService) |
| 77 | ||
| 78 | #Set created application to be serviceparent of multiservice | |
| 79 | - | application = service.Application("newdispatch")
|
| 79 | + | multiService.setServiceParent(application) |