pub.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. demeter web
  5. name:pub.py
  6. author:rabin
  7. """
  8. from mqtt.pub import *
  9. import random
  10. import time
  11. def sensor():
  12. # 模拟设备数据
  13. gateway = '10086'
  14. def device():
  15. return str(random.randint(100000, 100010))
  16. def val():
  17. return str(random.randint(1, 50))
  18. hard = 'sensor'
  19. pubType = ('temperature', 'humidity')
  20. pub = Pub()
  21. while True:
  22. for v in pubType:
  23. key = hard+'/'+v+'/'+gateway+'/'+device()
  24. value = val()
  25. print 'key:' + key + ' value:' + value
  26. pub.push(key, value)
  27. time.sleep(1)
  28. def pic():
  29. gateway = '10086'
  30. pub = Pub()
  31. key = 'pic/'
  32. key = 'pic/pic_'
  33. gateway = '/' + gateway + '/100001'
  34. with open('admin/static/images/banner.png', 'rb') as f:
  35. data = f.readlines()
  36. total = len(data)
  37. key = key + str(total) + '_'
  38. i = 0
  39. print key
  40. for value in data:
  41. k = key + str(i) + gateway
  42. pub.push(k, value)
  43. print k
  44. i = i+1
  45. #with open('test.png', 'w') as f:
  46. #f.writelines(data)
  47. """
  48. handle = open('admin/static/images/banner.png', 'rb')
  49. img = handle.read(1)
  50. handle.close()
  51. data = struct.pack('c', img)
  52. print data
  53. """
  54. #pub.push(key, value)
  55. #sensor()
  56. pic()