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