record.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. class Record(object):
  11. def topic(self, key):
  12. array = key.split('/')
  13. slen = len(array)
  14. if len >= 4:
  15. return {'method':array[0], 'type':array[1], 'parent':array[2], 'child':array[3]}
  16. else:
  17. return {'method':array[0], 'type':array[1], 'parent':array[2], 'child':array[2]}
  18. def push(self, key, value):
  19. config = self.topic(key)
  20. method = 'record_' + config['method']
  21. cls = Demeter.getClass(method, 'service.')
  22. state = False
  23. if config['method'] != 'pic':
  24. if '{' in value:
  25. value = Demeter.isJson(value)
  26. if value:
  27. state = True
  28. for k,v in value.items():
  29. config['child'] = k
  30. cls().handle(self, config, v)
  31. if state == False:
  32. cls().handle(self, config, value)
  33. def save(self, config, value, name='', device_type=None):
  34. model = Demeter.model('device_gateway')
  35. model.hardware_id = config['parent']
  36. gateway = model.select(type='fetchone')
  37. if gateway:
  38. #更新网关状态
  39. self.gateway(config['parent'], gateway['farm_id'], gateway['id'])
  40. model_type = Demeter.model('hardware_type')
  41. model_type.key = config['method']
  42. hard = model_type.select(type='fetchone')
  43. model = Demeter.model('device_info')
  44. model.hardware_id = config['child']
  45. model.hardware_type = hard['id']
  46. device = model.select(type='fetchone')
  47. if device:
  48. model.id = device['id']
  49. if config['method'] == 'control' and (value == '0' or value == 0):
  50. model.update(status=True, cdate='time')
  51. return
  52. #update
  53. id = device['id']
  54. if config['method'] == 'sensor' or config['method'] == 'power':
  55. value = Demeter.exp(device['exp'], value)
  56. model.update(value=value,status=True,cdate='time')
  57. # 上下限判断
  58. # 条件判断
  59. if device['type_id'] > 0 and (hard['id'] == 2 or hard['id'] == 4):
  60. value = float(value)
  61. content = ''
  62. if device['max']:
  63. if value > device['max']:
  64. if device['cxtype'] == 1:
  65. if device['cxnum']:
  66. device['cxnum'] = device['cxnum'] + 1
  67. else:
  68. device['cxnum'] = 1
  69. if device['cxnum'] > 3:
  70. device['cxnum'] = 0
  71. 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']+'进行处理。'
  72. self.msg(id, content, gateway['farm_id'], 2)
  73. else:
  74. device['cxtype'] = 1
  75. device['cxnum'] = 1
  76. model.id = device['id']
  77. if device['cxnum'] == 1:
  78. model.update(farm_id=gateway['farm_id'], cxnum=device['cxnum'],cxtype=device['cxtype'], cxdate='time')
  79. else:
  80. model.update(farm_id=gateway['farm_id'], cxnum=device['cxnum'],cxtype=device['cxtype'])
  81. if device['min']:
  82. if value < device['min']:
  83. if device['cxtype'] == 2:
  84. if device['cxnum']:
  85. device['cxnum'] = device['cxnum'] + 1
  86. else:
  87. device['cxnum'] = 1
  88. if device['cxnum'] > 3:
  89. device['cxnum'] = 0
  90. 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']+'进行处理。'
  91. self.msg(id, content, gateway['farm_id'], 2)
  92. else:
  93. device['cxtype'] = 2
  94. device['cxnum'] = 1
  95. model.id = device['id']
  96. if device['cxnum'] == 1:
  97. model.update(farm_id=gateway['farm_id'], cxnum=device['cxnum'],cxtype=device['cxtype'], cxdate='time')
  98. else:
  99. model.update(farm_id=gateway['farm_id'], cxnum=device['cxnum'],cxtype=device['cxtype'])
  100. """
  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. """
  131. else:
  132. #insert
  133. model.hardware_id = config['child']
  134. model.farm_id = gateway['farm_id']
  135. model.name = name
  136. model.hardware_type = hard['id']
  137. model.gateway_id = gateway['id']
  138. if device_type:
  139. model.type_id = device_type['id']
  140. else:
  141. model.type_id = 0
  142. model.value = value
  143. model.status = True
  144. id = model.insert()
  145. self.hardware(id, config['child'], gateway['farm_id'],gateway['id'], hard['id'])
  146. if config['method'] == 'pic':
  147. model_pic = Demeter.model('device_pic')
  148. model_pic.farm_id = gateway['farm_id']
  149. model_pic.gateway_id = gateway['id']
  150. model_pic.device_id = id
  151. model_pic.pic = value
  152. model_pic.insert()
  153. else:
  154. data = {}
  155. data['type'] = config['type']
  156. data['gateway'] = config['parent']
  157. data['device'] = config['child']
  158. data['hard'] = config['method']
  159. data['farm'] = gateway['farm_id']
  160. data['source'] = value
  161. data['value'] = value
  162. data['time'] = Demeter.time()
  163. Demeter.model('data', 'tsdb').insert(data)
  164. def msg(self, device_id, content, farm_id, type_id):
  165. model = Demeter.model('msg')
  166. # 2018-05-23更新 增加三个小时之内只能报一次错误
  167. model.farm_id = farm_id
  168. #model.content = content
  169. model.device_id = device_id
  170. model.type_id = type_id
  171. info = model.select(type='fetchone')
  172. state = False
  173. if info:
  174. if Demeter.time() - info['cdate'] > 3600*3:
  175. state = True
  176. if state == True:
  177. model.farm_id = farm_id
  178. model.content = content
  179. model.device_id = device_id
  180. model.type_id = type_id
  181. info = model.select(type='fetchone')
  182. if not info:
  183. model.farm_id = farm_id
  184. model.content = content
  185. model.device_id = device_id
  186. model.type_id = type_id
  187. model.insert()
  188. else:
  189. model.id = info['id']
  190. model.update(content=content, cdate='time')
  191. def gateway(self, gateway, farm, id):
  192. # 更新网关状态
  193. hardware_type = 5
  194. model = Demeter.model('device_info')
  195. model.hardware_id = gateway
  196. model.hardware_type = hardware_type
  197. device = model.select(type='fetchone')
  198. if device:
  199. model.id = device['id']
  200. model.update(value='1',status=True,cdate='time')
  201. else:
  202. model.hardware_id = gateway
  203. model.farm_id = farm
  204. model.name = '网关状态'
  205. model.status = True
  206. model.hardware_type = hardware_type
  207. model.gateway_id = id
  208. model.type_id = 0
  209. model.value = '1'
  210. model.insert()
  211. self.hardware(id, gateway, farm, id, 1)
  212. def hardware(self, id, hardware_id, farm, gateway, hard):
  213. # 记录硬件设备
  214. if hardware_id and id > 0:
  215. model = Demeter.model('hardware')
  216. model.hardware_id = hardware_id
  217. info = model.select(type='fetchone')
  218. if not info:
  219. model.farm_id = farm
  220. model.hardware_id = hardware_id
  221. model.gateway_id = gateway
  222. model.device_id = id
  223. model.hardware_type = hard
  224. model.insert()