record.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. demeter service
  5. name:record.py 用于记录设备过来的数据
  6. author:rabin
  7. """
  8. from demeter.core import *
  9. import json
  10. import time
  11. class Record(object):
  12. def topic(self, key):
  13. array = key.split('/')
  14. slen = len(array)
  15. if slen >= 4:
  16. return {'method':array[0], 'type':array[1], 'parent':array[2], 'child':array[3]}
  17. else:
  18. return {'method':array[0], 'type':array[1], 'parent':array[2], 'child':array[2]}
  19. def push(self, key, value):
  20. value = value.replace("b'", '')
  21. value = value.replace("'", '')
  22. config = self.topic(key)
  23. method = 'record_' + config['method']
  24. cls = Demeter.service(method, 'mqtt')
  25. state = False
  26. if config['method'] != 'pic':
  27. if '{' in value:
  28. value = Demeter.isJson(value)
  29. if value:
  30. state = True
  31. for k,v in value.items():
  32. config['child'] = k
  33. cls.handle(self, config, v)
  34. if state == False:
  35. cls.handle(self, config, value)
  36. def save(self, config, value, name='', device_type=None):
  37. Demeter.service('device').save(config,value, name, device_type)