record.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. model = Demeter.model('device_gateway')
  36. model.hardware_id = config['parent']
  37. gateway = model.select(type='fetchone')
  38. if gateway:
  39. #更新网关状态
  40. self.gateway(config['parent'], gateway['farm_id'], gateway['id'])
  41. model_type = Demeter.model('hardware_type')
  42. model_type.key = config['method']
  43. hard = model_type.select(type='fetchone')
  44. model = Demeter.model('device_info')
  45. model.hardware_id = config['child']
  46. model.hardware_type = hard['id']
  47. device = model.select(type='fetchone')
  48. if device:
  49. model.id = device['id']
  50. if config['method'] == 'control' and (value == '0' or value == 0):
  51. model.update(status=True, cdate='time')
  52. return
  53. #update
  54. id = device['id']
  55. if config['method'] == 'sensor' or config['method'] == 'power':
  56. value = Demeter.exp(device['exp'], value)
  57. model.update(value=value,status=True,cdate='time')
  58. # 上下限判断
  59. # 条件判断
  60. if device['type_id'] > 0 and (hard['id'] == 2 or hard['id'] == 4):
  61. value = float(value)
  62. content = ''
  63. if device['max']:
  64. if value > device['max']:
  65. if device['cxtype'] == 1:
  66. if device['cxnum']:
  67. device['cxnum'] = device['cxnum'] + 1
  68. else:
  69. device['cxnum'] = 1
  70. if device['cxnum'] > 3:
  71. device['cxnum'] = 0
  72. content = '从' + Demeter.date(device['cxdate']) + '到' + Demeter.date(Demeter.time()) + ',' + gateway['name'] + ','+device['name']+'超过'+str(device['max'])+device_type['unit']+',当前值'+str(value)+device_type['unit']+',请及时控制'+device_type['name']+'进行处理。'
  73. self.msg(id, content, gateway['farm_id'], 2)
  74. else:
  75. device['cxtype'] = 1
  76. device['cxnum'] = 1
  77. model.id = device['id']
  78. if device['cxnum'] == 1:
  79. model.update(farm_id=gateway['farm_id'], cxnum=device['cxnum'],cxtype=device['cxtype'], cxdate='time')
  80. else:
  81. model.update(farm_id=gateway['farm_id'], cxnum=device['cxnum'],cxtype=device['cxtype'])
  82. if device['min']:
  83. if value < device['min']:
  84. if device['cxtype'] == 2:
  85. if device['cxnum']:
  86. device['cxnum'] = device['cxnum'] + 1
  87. else:
  88. device['cxnum'] = 1
  89. if device['cxnum'] > 3:
  90. device['cxnum'] = 0
  91. content = '从' + Demeter.date(device['cxdate']) + '到' + Demeter.date(Demeter.time()) + ',' + gateway['name'] + ','+device['name']+'少于'+str(device['min'])+device_type['unit']+',当前值'+str(value)+device_type['unit']+',请及时控制'+device_type['name']+'进行处理。'
  92. self.msg(id, content, gateway['farm_id'], 2)
  93. else:
  94. device['cxtype'] = 2
  95. device['cxnum'] = 1
  96. model.id = device['id']
  97. if device['cxnum'] == 1:
  98. model.update(farm_id=gateway['farm_id'], cxnum=device['cxnum'],cxtype=device['cxtype'], cxdate='time')
  99. else:
  100. model.update(farm_id=gateway['farm_id'], cxnum=device['cxnum'],cxtype=device['cxtype'])
  101. content = ''
  102. condition_model = Demeter.model('device_set_condition')
  103. condition_model.device_id = id
  104. condition_model.status = True
  105. condition = condition_model.select()
  106. if condition:
  107. for cv in condition:
  108. state = False
  109. text = ''
  110. cv['value'] = float(cv['value'])
  111. if cv['condition'] == 1 and value > cv['value']:
  112. state = True
  113. text = '超过'
  114. elif cv['condition'] == 2 and value < cv['value']:
  115. state = True
  116. text = '少于'
  117. elif value == cv['value']:
  118. state = True
  119. text = '等于'
  120. if state:
  121. if cv['notice'] == True:
  122. content = gateway['name'] + ','+device['name']+text+str(cv['value'])+device_type['unit']+',当前值'+str(value)+device_type['unit']+',请及时控制'+device_type['name']+'进行处理。'
  123. self.msg(id, content, gateway['farm_id'], 2)
  124. if cv['oper'] == 2:
  125. cv['oper'] = 1
  126. Demeter.service('device').switchMul(cv)
  127. elif cv['oper'] == 3:
  128. cv['oper'] = 0
  129. Demeter.service('device').switchMul(cv)
  130. else:
  131. #insert
  132. model.hardware_id = config['child']
  133. model.farm_id = gateway['farm_id']
  134. model.name = name
  135. model.hardware_type = hard['id']
  136. model.gateway_id = gateway['id']
  137. if device_type:
  138. model.type_id = device_type['id']
  139. else:
  140. model.type_id = 0
  141. model.value = value
  142. model.status = True
  143. id = model.insert()
  144. self.hardware(id, config['child'], gateway['farm_id'],gateway['id'], hard['id'])
  145. if config['method'] == 'pic':
  146. model_pic = Demeter.model('device_pic')
  147. model_pic.farm_id = gateway['farm_id']
  148. model_pic.gateway_id = gateway['id']
  149. model_pic.device_id = id
  150. model_pic.pic = value
  151. model_pic.insert()
  152. else:
  153. data = {}
  154. data['type'] = config['type']
  155. data['gateway'] = config['parent']
  156. data['device'] = config['child']
  157. data['hard'] = config['method']
  158. data['farm'] = gateway['farm_id']
  159. data['source'] = value
  160. data['value'] = value
  161. data['time'] = Demeter.time()
  162. Demeter.model('data', 'tsdb').insert(data)
  163. def msg(self, device_id, content, farm_id, type_id):
  164. model = Demeter.model('msg')
  165. model.farm_id = farm_id
  166. model.content = content
  167. model.device_id = device_id
  168. model.type_id = type_id
  169. info = model.select(type='fetchone')
  170. if not info:
  171. model.farm_id = farm_id
  172. model.content = content
  173. model.device_id = device_id
  174. model.type_id = type_id
  175. model.insert()
  176. else:
  177. model.id = info['id']
  178. model.update(content=content, cdate='time')
  179. def gateway(self, gateway, farm, id):
  180. # 更新网关状态
  181. hardware_type = 5
  182. model = Demeter.model('device_info')
  183. model.hardware_id = gateway
  184. model.hardware_type = hardware_type
  185. device = model.select(type='fetchone')
  186. if device:
  187. model.id = device['id']
  188. model.update(value='1',status=True,cdate='time')
  189. else:
  190. model.hardware_id = gateway
  191. model.farm_id = farm
  192. model.name = '网关状态'
  193. model.status = True
  194. model.hardware_type = hardware_type
  195. model.gateway_id = id
  196. model.type_id = 0
  197. model.value = '1'
  198. model.insert()
  199. self.hardware(id, gateway, farm, id, 1)
  200. def hardware(self, id, hardware_id, farm, gateway, hard):
  201. # 记录硬件设备
  202. if hardware_id and id > 0:
  203. model = Demeter.model('hardware')
  204. model.hardware_id = hardware_id
  205. info = model.select(type='fetchone')
  206. if not info:
  207. model.farm_id = farm
  208. model.hardware_id = hardware_id
  209. model.gateway_id = gateway
  210. model.device_id = id
  211. model.hardware_type = hard
  212. model.insert()