pub.py 2.6 KB

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