record_sensor.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. demeter service
  5. name:record_sensor.py 处理传感器数据
  6. author:rabin
  7. key:sensor/type/gateway/device
  8. """
  9. import time
  10. import json
  11. from demeter.core import *
  12. class Record_sensor(object):
  13. def handle(self, config, value):
  14. model = Demeter.model('device_type')
  15. model.key = config['type']
  16. device_type = model.select(type='fetchone')
  17. if device_type:
  18. show_value = value
  19. model = Demeter.model('device_gateway')
  20. model.hardware_id = config['parent']
  21. gateway = model.select(type='fetchone')
  22. if gateway:
  23. model.id = gateway['id']
  24. model.update(status=True,farm_id=gateway['farm_id'])
  25. model = Demeter.model('device_info')
  26. model.hardware_id = config['child']
  27. device = model.select(type='fetchone')
  28. if device:
  29. #update
  30. model.id = device['id']
  31. model.update(farm_id=gateway['farm_id'], num=value,show_num=show_value,status=True,cdate='time')
  32. else:
  33. model_type = Demeter.model('hardware_type')
  34. model_type.key = config['method']
  35. hard = model_type.select(type='fetchone')
  36. #insert
  37. model.hardware_id = config['child']
  38. model.farm_id = gateway['farm_id']
  39. model.name = device_type['name'] + '#' + str(data['device'])
  40. model.hardware_type = hard['id']
  41. model.gateway_id = gateway['id']
  42. model.type_id = device_type['id']
  43. model.num = value
  44. model.show_num = show_value
  45. model.status = True
  46. id = model.insert()
  47. self.hardware(id, config['child'], gateway['farm_id'],gateway['id'], hard['id'])
  48. data = {}
  49. data['type'] = config['type']
  50. data['gateway'] = config['parent']
  51. data['device'] = config['child']
  52. data['hard'] = config['method']
  53. data['farm'] = gateway['farm_id']
  54. data['source'] = value
  55. data['value'] = show_value
  56. Demeter.model('data', 'tsdb').insert(data)
  57. def hardware(self, id, hardware_id, farm, gateway, hard):
  58. if hardware_id and id > 0:
  59. model = Demeter.model('hardware')
  60. model.hardware_id = hardware_id
  61. info = model.select(type='fetchone')
  62. if not info:
  63. insert = {}
  64. insert['farm_id'] = farm
  65. insert['hardware_id'] = hardware_id
  66. insert['gateway_id'] = gateway
  67. insert['device_id'] = id
  68. insert['hardware_type'] = hard
  69. model.insert()