pub.py 3.0 KB

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