Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. from twisted.internet.protocol import DatagramProtocol
  2. from twisted.internet import reactor
  3. import time
  4.  
  5. class Echo(DatagramProtocol):
  6.  
  7. def datagramReceived(self, data, (host, port)):
  8. print "received %r from %s:%d" % (data, host, port)
  9.  
  10. # Login
  11. if data.startswith('$01PW'):
  12. self.transport.write(">01\r", (host, port))
  13. return
  14.  
  15. # Request IO status
  16. if data.startswith('$01C'):
  17. self.transport.write("!01" + "000000000000" + "000000000000" + "000000000000" + "\r", (host, port))
  18. return
  19.  
  20. # Request IO input status
  21. if data.startswith('$016'):
  22. seconds = int(time.time()) % 60
  23. print seconds
  24. if seconds > 20 and seconds < 24:
  25. print "trigger!"
  26. self.transport.write("!01"+"00"+"FFFF\r", (host, port))
  27. else:
  28. self.transport.write("!01"+"01"+"FFFF\r", (host, port))
  29. return
  30.  
  31. # Request set IO output
  32. if data.startswith('#011'):
  33. print "Channel[0x" + data[4:6]+ "] = " + data[6:7]
  34. self.transport.write(">\r", (host, port))
  35. return
  36.  
  37. print "unknow command!"
  38. #self.transport.write(data, (host, port))
  39.  
  40. print "starting ..."
  41. reactor.listenUDP(1025, Echo())
  42. reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement