device.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. demeter service
  5. name:device.py 处理设备
  6. author:rabin
  7. """
  8. from demeter.core import *
  9. from demeter.mqtt import *
  10. class Device(object):
  11. type_info = []
  12. # 新增网关时,增加状态和电源
  13. def upGatewayStatus(self, farm_id, gateway_id, hardware_id):
  14. if hardware_id and gateway_id > 0:
  15. info = Demeter.service('common').list('device_info', search={'hardware_id':hardware_id})
  16. if not info:
  17. # 网关状态
  18. insert = {}
  19. insert['farm_id'] = farm_id
  20. insert['name'] = '网关状态'
  21. insert['hardware_id'] = hardware_id
  22. insert['gateway_id'] = gateway_id
  23. insert['hardware_type'] = 5
  24. insert['value'] = '2'
  25. insert['type_id'] = 0
  26. insert['status'] = False
  27. Demeter.service('common').update('device_info', None, insert)
  28. # 电源
  29. """
  30. insert = {}
  31. insert['farm_id'] = farm_id
  32. insert['name'] = '电源状态'
  33. insert['hardware_id'] = hardware_id
  34. insert['gateway_id'] = gateway_id
  35. insert['hardware_type'] = 4
  36. insert['value'] = '0'
  37. insert['type_id'] = 8
  38. insert['status'] = False
  39. Demeter.service('common').update('device_info', None, insert)
  40. """
  41. # 批量处理开关
  42. def switchMul(self, value):
  43. if value['devices']:
  44. model = Demeter.model('device_info')
  45. value['devices'] = tuple(eval(value['devices']))
  46. if value['oper'] == 1:
  47. switch = 1
  48. else:
  49. switch = 2
  50. # 直到执行成功才执行下一个
  51. for v in value['devices']:
  52. feedback = self.switch(v, switch)
  53. if feedback:
  54. state = False
  55. while state == False:
  56. state = self.switchCheck(v)
  57. # 验证按钮执行状态 1为成功了
  58. def switchCheck(self, device_id):
  59. model = Demeter.model('device_info')
  60. model.id = device_id
  61. #model.exp = '1'
  62. model.exp.nq('-1')
  63. info = model.select(type='fetchone')
  64. if info:
  65. return True
  66. return False
  67. # 处理开关-向设备发送请求
  68. def switch(self, device_id, switch=1, mul=False, queue=False):
  69. model = Demeter.model('device_info')
  70. model.id = device_id
  71. info = model.select(type='fetchone')
  72. # 如果有批量控制,还要保存mul的id
  73. if mul:
  74. queue_model = Demeter.model('device_mul_queue')
  75. queue_model.mul_id = mul
  76. queue_model.device_id = info['id']
  77. queue_model.status = 1
  78. queue_model.value = switch
  79. if info['inorder']:
  80. queue_model.inorder = info['inorder']
  81. else:
  82. queue_model.inorder = 100
  83. queue_model.udate = info['udate']
  84. queue_model.hardware_id = info['hardware_id']
  85. queue_model.insert()
  86. return
  87. if info and info['status'] == True:
  88. """
  89. if queue:
  90. oper = self.msg(info['type_id'], switch)
  91. queue_model = Demeter.model('device_mul_queue')
  92. queue_model.id = queue
  93. queue_model.update(oper='正在' + oper + info['name'])
  94. """
  95. # 设置为loading状态,cxnum保存当前的值
  96. model.id = device_id
  97. model.update(exp='-1', cxnum=switch, oper=True, operdate=Demeter.time())
  98. model = Demeter.model('device_gateway')
  99. model.id = info['gateway_id']
  100. gateway_info = model.select(type='fetchone')
  101. if info['type_id'] > 0:
  102. model = Demeter.model('device_type')
  103. model.id = info['type_id']
  104. type_info = model.select(type='fetchone')
  105. if type_info:
  106. info['hardware_id'] = str(info['hardware_id'])
  107. switch = str(switch)
  108. gateway_info['hardware_id'] = str(gateway_info['hardware_id'])
  109. # 这里要向服务器发送pub请求了,暂时使用临时文件代替
  110. #Demeter.temp(key=type_info['key'], name=info['hardware_id'], value=switch)
  111. #pub
  112. pub = Pub()
  113. key = type_info['key'] + '/' + gateway_info['hardware_id'] + '/' + info['hardware_id']
  114. update = 'update/' + key
  115. feedback = 'status/' + key
  116. value = switch
  117. #pub.push(update, value, qos=0, callback=self.switchAction, param={'info':info, 'key':feedback, 'switch':switch}, feedback=True)
  118. pub.push(update, value)
  119. return feedback
  120. else:
  121. msg = self.notice('offline')
  122. model.id = device_id
  123. model.update(exp=msg, cxnum=switch, oper=True, operdate=Demeter.time())
  124. self.mul(info, info['name'] + '失败:' + msg, False)
  125. #Demeter.error('offline')
  126. return ''
  127. # 处理开关-响应
  128. def switchAction(self, param, client, userdata, mid, msg='ok'):
  129. info = param['info']
  130. if info and info['status'] == True:
  131. param['switch'] = info['cxnum']
  132. model = Demeter.model('device_gateway')
  133. model.id = info['gateway_id']
  134. gateway = model.select(type='fetchone')
  135. if msg == 'ok':
  136. model = Demeter.model('device_info')
  137. model.id = info['id']
  138. model.update(value=param['switch'], exp='1', oper=False, operdate=Demeter.time())
  139. # 如果有批量控制
  140. self.mul(info, info['name'] + '成功', True)
  141. else:
  142. msg = self.notice(msg)
  143. model = Demeter.model('device_info')
  144. model.id = info['id']
  145. model.update(exp=msg, oper=True, operdate=Demeter.time())
  146. # 如果有批量控制
  147. self.mul(info, info['name'] + '失败:' + msg, False)
  148. oper = self.msg(info['type_id'], param['switch'])
  149. content = oper + gateway['name'] + '下的'+ info['name'] + '失败,错误提示:' + msg
  150. Demeter.service('record').msg(info['id'], content, info['farm_id'], 3)
  151. #Demeter.error(msg)
  152. def notice(self, msg):
  153. model = Demeter.model('notice_type')
  154. model.key = msg
  155. info = model.select(type='fetchone')
  156. if info:
  157. msg = info['name']
  158. return msg
  159. def msg(self, type_id, switch):
  160. oper = ''
  161. type_model = Demeter.model('device_type')
  162. type_model.id = type_id
  163. type_info = type_model.select(type='fetchone')
  164. if type_info['unit'] == 'button':
  165. if switch == 1:
  166. oper = '升起'
  167. elif switch == 2:
  168. oper = '降下'
  169. elif switch == 5:
  170. oper = '停止'
  171. else:
  172. if switch == 1:
  173. oper = '开启'
  174. else:
  175. oper = '关闭'
  176. return oper
  177. def mul(self, info, msg, state):
  178. model = Demeter.model('device_mul_queue')
  179. mul_model = Demeter.model('device_mul')
  180. model.device_id = info['id']
  181. model.status = 1
  182. queue = model.select()
  183. if queue:
  184. for v in queue:
  185. model.status = 1
  186. model.device_id = info['id']
  187. oper = self.msg(info['type_id'], v['value']) + msg
  188. if state:
  189. model.update(status=2, oper=oper, operdate=Demeter.time())
  190. else:
  191. model.update(status=3, oper=oper, operdate=Demeter.time())