pub.py 2.9 KB

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