pub.py 2.8 KB

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