pubs.py 3.0 KB

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