pub.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 ConfigParser
  11. import random
  12. import time
  13. import base64
  14. #import oss2
  15. def push():
  16. # 模拟传感器设备数据
  17. gateway = 10086
  18. def device(min):
  19. max = min + 9
  20. return str(random.randint(min, max))
  21. def val(min, max):
  22. return str(round(random.uniform(min, max), 2))
  23. def switch():
  24. return str(random.randint(0, 1))
  25. hard = 'sensor'
  26. pubType = (
  27. {'hard':'sensor','type':'temprature','device':100000,'value':(-20, 30)}
  28. ,{'hard':'sensor','type':'humidity','device':100010,'value':(30, 80)}
  29. ,{'hard':'sensor','type':'light','device':100020,'value':(3000, 13000)}
  30. ,{'hard':'power','type':'power','device':100090,'value':(1, 50)}
  31. #,{'hard':'control','type':'valve','device':100100}
  32. #,{'hard':'control','type':'blower','device':100110}
  33. ,{'hard':'power','type':'power','device':10086,'value':(1, 50)}
  34. )
  35. pub = Pub()
  36. while True:
  37. config = Demeter.temp()
  38. for v in pubType:
  39. if v['device'] == gateway:
  40. id = v['device']
  41. else:
  42. id = device(v['device'])
  43. key = v['hard']+'/'+v['type']+'/'+str(gateway)+'/'+str(id)
  44. if 'value' in v:
  45. value = val(v['value'][0], v['value'][1])
  46. else:
  47. value = config[v['type']][id]
  48. print 'key:' + key + ' value:' + value
  49. pub.push(key, value)
  50. time.sleep(1)
  51. def pic():
  52. gateway = '10086'
  53. pub = Pub()
  54. key = 'pic/pic_'
  55. gateway = '/' + gateway + '/200001'
  56. with open('front/static/pc/images/376a00e6.tmp2.jpg', 'rb') as f:
  57. data = f.readlines()
  58. total = len(data)
  59. key = key + str(total) + '_'
  60. i = 0
  61. print key
  62. for value in data:
  63. k = key + str(i) + gateway
  64. pub.push(k, value)
  65. print k
  66. i = i+1
  67. push()
  68. #pic()
  69. def tcp_sensor():
  70. # 模拟设备数据
  71. gateway = '10086'
  72. def device():
  73. return str(random.randint(100000, 100010))
  74. def val():
  75. return str(random.randint(1, 50))
  76. hard = 'sensor'
  77. pubType = ('temperature', 'humidity')
  78. client = Client('0.0.0.0', 8000)
  79. while True:
  80. for v in pubType:
  81. key = hard+'/'+v+'/'+gateway+'/'+device()
  82. value = val()
  83. print 'key:' + key + ' value:' + value
  84. client.send(key+'|:|'+value)
  85. time.sleep(1)
  86. def tcp_pic():
  87. gateway = '10086'
  88. client = Client('0.0.0.0', 8000)
  89. key = 'pic/'
  90. key = 'pic/pic_'
  91. gateway = '/' + gateway + '/200001'
  92. with open('admin/static/images/banner.png', 'rb') as f:
  93. data = f.readlines()
  94. total = len(data)
  95. key = key + str(total) + '_'
  96. i = 0
  97. #print key
  98. for value in data:
  99. k = key + str(i) + gateway
  100. #print value
  101. #print value
  102. print k
  103. client.send(k + '|:|' +value)
  104. #print k
  105. i = i+1
  106. client.close()
  107. #tcp_sensor()
  108. #tcp_pic()