pub.py 531 B

123456789101112131415161718192021222324252627
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. demeter web
  5. name:pub.py
  6. author:rabin
  7. """
  8. from mqtt.pub import *
  9. import random
  10. import time
  11. # 模拟设备数据
  12. gateway = '10086'
  13. def device():
  14. return str(random.randint(100000, 100010))
  15. def val():
  16. return str(random.randint(1, 50))
  17. hard = 'sensor'
  18. pubType = ('temperature', 'humidity')
  19. pub = Pub()
  20. while True:
  21. for v in pubType:
  22. key = hard+'/'+v+'/'+gateway+'/'+device()
  23. value = val()
  24. print 'key:' + key + ' value:' + value
  25. pub.push(key, value)
  26. time.sleep(1)