device.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. insert = {}
  30. insert['farm_id'] = farm_id
  31. insert['name'] = '电源状态'
  32. insert['hardware_id'] = hardware_id
  33. insert['gateway_id'] = gateway_id
  34. insert['hardware_type'] = 4
  35. insert['value'] = '0'
  36. insert['type_id'] = 8
  37. insert['status'] = False
  38. Demeter.service('common').update('device_info', None, insert)
  39. # 批量处理开关
  40. def switchMul(self, value):
  41. if value['devices']:
  42. model = Demeter.model('device_info')
  43. value['devices'] = tuple(eval(value['devices']))
  44. if value['oper'] == 1:
  45. switch = 1
  46. else:
  47. switch = 2
  48. for v in value['devices']:
  49. self.switch(v, switch)
  50. # 处理开关-向设备发送请求
  51. def switch(self, device_id, switch=1, mul=False, queue=False):
  52. model = Demeter.model('device_info')
  53. model.id = device_id
  54. info = model.select(type='fetchone')
  55. # 如果有批量控制,还要保存mul的id
  56. if mul:
  57. queue_model = Demeter.model('device_mul_queue')
  58. queue_model.mul_id = mul
  59. queue_model.device_id = info['id']
  60. queue_model.status = 1
  61. queue_model.value = switch
  62. if info['inorder']:
  63. queue_model.inorder = info['inorder']
  64. else:
  65. queue_model.inorder = 100
  66. queue_model.udate = info['udate']
  67. queue_model.hardware_id = info['hardware_id']
  68. queue_model.insert()
  69. return
  70. if info and info['status'] == True:
  71. """
  72. if queue:
  73. oper = self.msg(info['type_id'], switch)
  74. queue_model = Demeter.model('device_mul_queue')
  75. queue_model.id = queue
  76. queue_model.update(oper='正在' + oper + info['name'])
  77. """
  78. # 设置为loading状态,cxnum保存当前的值
  79. model.id = device_id
  80. model.update(exp='-1', cxnum=switch, oper=True, operdate=Demeter.time())
  81. model = Demeter.model('device_gateway')
  82. model.id = info['gateway_id']
  83. gateway_info = model.select(type='fetchone')
  84. if info['type_id'] > 0:
  85. model = Demeter.model('device_type')
  86. model.id = info['type_id']
  87. type_info = model.select(type='fetchone')
  88. if type_info:
  89. info['hardware_id'] = str(info['hardware_id'])
  90. switch = str(switch)
  91. gateway_info['hardware_id'] = str(gateway_info['hardware_id'])
  92. # 这里要向服务器发送pub请求了,暂时使用临时文件代替
  93. #Demeter.temp(key=type_info['key'], name=info['hardware_id'], value=switch)
  94. #pub
  95. pub = Pub()
  96. key = type_info['key'] + '/' + gateway_info['hardware_id'] + '/' + info['hardware_id']
  97. update = 'update/' + key
  98. feedback = 'status/' + key
  99. value = switch
  100. #pub.push(update, value, qos=0, callback=self.switchAction, param={'info':info, 'key':feedback, 'switch':switch}, feedback=True)
  101. pub.push(update, value)
  102. else:
  103. msg = self.notice('offline')
  104. model.id = device_id
  105. model.update(exp=msg, cxnum=switch, oper=True, operdate=Demeter.time())
  106. self.mul(info, info['name'] + '失败:' + msg, False)
  107. #Demeter.error('offline')
  108. # 处理开关-响应
  109. def switchAction(self, param, client, userdata, mid, msg='ok'):
  110. info = param['info']
  111. if info and info['status'] == True:
  112. param['switch'] = info['cxnum']
  113. model = Demeter.model('device_gateway')
  114. model.id = info['gateway_id']
  115. gateway = model.select(type='fetchone')
  116. if msg == 'ok':
  117. model = Demeter.model('device_info')
  118. model.id = info['id']
  119. model.update(value=param['switch'], exp='1', oper=False, operdate=Demeter.time())
  120. # 如果有批量控制
  121. self.mul(info, info['name'] + '成功', True)
  122. else:
  123. msg = self.notice(msg)
  124. model = Demeter.model('device_info')
  125. model.id = info['id']
  126. model.update(exp=msg, oper=True, operdate=Demeter.time())
  127. # 如果有批量控制
  128. self.mul(info, info['name'] + '失败:' + msg, False)
  129. oper = self.msg(info['type_id'], param['switch'])
  130. content = oper + gateway['name'] + '下的'+ info['name'] + '失败,错误提示:' + msg
  131. Demeter.service('record').msg(info['id'], content, info['farm_id'], 3)
  132. #Demeter.error(msg)
  133. def notice(self, msg):
  134. model = Demeter.model('notice_type')
  135. model.key = msg
  136. info = model.select(type='fetchone')
  137. if info:
  138. msg = info['name']
  139. return msg
  140. def msg(self, type_id, switch):
  141. oper = ''
  142. type_model = Demeter.model('device_type')
  143. type_model.id = type_id
  144. type_info = type_model.select(type='fetchone')
  145. if type_info['unit'] == 'button':
  146. if switch == 1:
  147. oper = '升起'
  148. elif switch == 2:
  149. oper = '降下'
  150. elif switch == 5:
  151. oper = '停止'
  152. else:
  153. if switch == 1:
  154. oper = '开启'
  155. else:
  156. oper = '关闭'
  157. return oper
  158. def mul(self, info, msg, state):
  159. model = Demeter.model('device_mul_queue')
  160. mul_model = Demeter.model('device_mul')
  161. model.device_id = info['id']
  162. model.status = 1
  163. queue = model.select()
  164. if queue:
  165. for v in queue:
  166. model.status = 1
  167. model.device_id = info['id']
  168. oper = self.msg(info['type_id'], v['value']) + msg
  169. if state:
  170. model.update(status=2, oper=oper, operdate=Demeter.time())
  171. else:
  172. model.update(status=3, oper=oper, operdate=Demeter.time())