Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from twisted.internet.protocol import DatagramProtocol
- from twisted.internet import reactor
- import time
- class Echo(DatagramProtocol):
- def datagramReceived(self, data, (host, port)):
- print "received %r from %s:%d" % (data, host, port)
- # Login
- if data.startswith('$01PW'):
- self.transport.write(">01\r", (host, port))
- return
- # Request IO status
- if data.startswith('$01C'):
- self.transport.write("!01" + "000000000000" + "000000000000" + "000000000000" + "\r", (host, port))
- return
- # Request IO input status
- if data.startswith('$016'):
- seconds = int(time.time()) % 60
- print seconds
- if seconds > 20 and seconds < 24:
- print "trigger!"
- self.transport.write("!01"+"00"+"FFFF\r", (host, port))
- else:
- self.transport.write("!01"+"01"+"FFFF\r", (host, port))
- return
- # Request set IO output
- if data.startswith('#011'):
- print "Channel[0x" + data[4:6]+ "] = " + data[6:7]
- self.transport.write(">\r", (host, port))
- return
- print "unknow command!"
- #self.transport.write(data, (host, port))
- print "starting ..."
- reactor.listenUDP(1025, Echo())
- reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement