1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- """
- demeter service
- name:record_sensor.py 处理传感器数据
- author:rabin
- key:sensor/type/gateway/device
- """
- import time
- import json
- from demeter.core import *
- class Record_sensor(object):
- def handle(self, config, value):
- model = Demeter.model('device_type')
- model.key = config['type']
- device_type = model.select(type='fetchone')
- if device_type:
- show_value = value
- model = Demeter.model('device_gateway')
- model.hardware_id = config['parent']
- gateway = model.select(type='fetchone')
- if gateway:
- model.id = gateway['id']
- model.update(status=True,farm_id=gateway['farm_id'])
- model = Demeter.model('device_info')
- model.hardware_id = config['child']
- device = model.select(type='fetchone')
- if device:
- #update
- model.id = device['id']
- model.update(farm_id=gateway['farm_id'], num=value,show_num=show_value,status=True,cdate='time')
- else:
- model_type = Demeter.model('hardware_type')
- model_type.key = config['method']
- hard = model_type.select(type='fetchone')
- #insert
- model.hardware_id = config['child']
- model.farm_id = gateway['farm_id']
- model.name = device_type['name'] + '#' + str(data['device'])
- model.hardware_type = hard['id']
- model.gateway_id = gateway['id']
- model.type_id = device_type['id']
- model.num = value
- model.show_num = show_value
- model.status = True
- id = model.insert()
- self.hardware(id, config['child'], gateway['farm_id'],gateway['id'], hard['id'])
- data = {}
- data['type'] = config['type']
- data['gateway'] = config['parent']
- data['device'] = config['child']
- data['hard'] = config['method']
- data['farm'] = gateway['farm_id']
- data['source'] = value
- data['value'] = show_value
- Demeter.model('data', 'tsdb').insert(data)
- def hardware(self, id, hardware_id, farm, gateway, hard):
- if hardware_id and id > 0:
- model = Demeter.model('hardware')
- model.hardware_id = hardware_id
- info = model.select(type='fetchone')
- if not info:
- insert = {}
- insert['farm_id'] = farm
- insert['hardware_id'] = hardware_id
- insert['gateway_id'] = gateway
- insert['device_id'] = id
- insert['hardware_type'] = hard
- model.insert()
-
|