record.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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 len >= 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. config = self.topic(key)
  21. method = 'record_' + config['method']
  22. cls = Demeter.getClass(method, 'service.')
  23. state = False
  24. if config['method'] != 'pic':
  25. if '{' in value:
  26. value = Demeter.isJson(value)
  27. if value:
  28. state = True
  29. for k,v in value.items():
  30. config['child'] = k
  31. cls().handle(self, config, v)
  32. if state == False:
  33. cls().handle(self, config, value)
  34. def save(self, config, value, name='', device_type=None):
  35. Demeter.service('device').save(config,value, name, device_type)