pub.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. demeter web
  5. name:pub.py
  6. author:rabin
  7. """
  8. from demeter.mqtt import *
  9. from demeter.tcp import *
  10. import random
  11. import time
  12. import base64
  13. def sensor():
  14. # 模拟设备数据
  15. gateway = '10086'
  16. def device():
  17. return str(random.randint(100000, 100010))
  18. def val():
  19. return str(random.randint(1, 50))
  20. hard = 'sensor'
  21. pubType = ('temperature', 'humidity')
  22. pub = Pub()
  23. while True:
  24. for v in pubType:
  25. key = hard+'/'+v+'/'+gateway+'/'+device()
  26. value = val()
  27. print 'key:' + key + ' value:' + value
  28. pub.push(key, value)
  29. time.sleep(1)
  30. def pic():
  31. gateway = '10086'
  32. pub = Pub()
  33. key = 'pic/'
  34. key = 'pic/pic_'
  35. gateway = '/' + gateway + '/200001'
  36. with open('admin/static/images/banner.png', 'rb') as f:
  37. data = f.readlines()
  38. total = len(data)
  39. key = key + str(total) + '_'
  40. i = 0
  41. print key
  42. for value in data:
  43. k = key + str(i) + gateway
  44. pub.push(k, value)
  45. print k
  46. i = i+1
  47. #sensor()
  48. #pic()
  49. def tcp_sensor():
  50. # 模拟设备数据
  51. gateway = '10086'
  52. def device():
  53. return str(random.randint(100000, 100010))
  54. def val():
  55. return str(random.randint(1, 50))
  56. hard = 'sensor'
  57. pubType = ('temperature', 'humidity')
  58. client = Client('0.0.0.0', 8000)
  59. while True:
  60. for v in pubType:
  61. key = hard+'/'+v+'/'+gateway+'/'+device()
  62. value = val()
  63. print 'key:' + key + ' value:' + value
  64. client.send(key+'|:|'+value)
  65. time.sleep(1)
  66. def tcp_pic():
  67. gateway = '10086'
  68. client = Client('0.0.0.0', 8000)
  69. key = 'pic/'
  70. key = 'pic/pic_'
  71. gateway = '/' + gateway + '/200001'
  72. with open('admin/static/images/banner.png', 'rb') as f:
  73. data = f.readlines()
  74. total = len(data)
  75. key = key + str(total) + '_'
  76. i = 0
  77. #print key
  78. for value in data:
  79. k = key + str(i) + gateway
  80. #print value
  81. #print value
  82. print k
  83. client.send(k + '|:|' +value)
  84. #print k
  85. i = i+1
  86. client.close()
  87. tcp_sensor()
  88. #tcp_pic()