GatewayFamilyRoomController.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. # -*- encoding: utf-8 -*-
  2. """
  3. @File : GatewayFamilyRoomController.py
  4. @Time : 2022/5/24 19:43
  5. @Author : stephen
  6. @Email : zhangdongming@asj6.wecom.work
  7. @Software: PyCharm
  8. """
  9. from django.db import transaction
  10. from django.db.models import Q, Count, F
  11. from django.views.generic.base import View
  12. from Controller.SensorGateway.EquipmentFamilyController import EquipmentFamilyView
  13. from Model.models import FamilyRoomDevice, FamilyRoom
  14. from Object.ResponseObject import ResponseObject
  15. from Object.TokenObject import TokenObject
  16. # 家庭房间管理
  17. class GatewayFamilyRoomView(View):
  18. def get(self, request, *args, **kwargs):
  19. request.encoding = 'utf-8'
  20. operation = kwargs.get('operation')
  21. return self.validation(request.GET, request, operation)
  22. def post(self, request, *args, **kwargs):
  23. request.encoding = 'utf-8'
  24. operation = kwargs.get('operation')
  25. return self.validation(request.POST, request, operation)
  26. def validation(self, request_dict, request, operation):
  27. token = request.META.get('HTTP_AUTHORIZATION')
  28. token = TokenObject(token)
  29. lang = request_dict.get('lang', token.lang)
  30. response = ResponseObject(lang)
  31. if token.code != 0:
  32. return response.json(token.code)
  33. app_user_id = token.userID
  34. # 添加设备关联房间
  35. if operation == 'device-changes':
  36. return self.room_device_save(app_user_id, request_dict, response)
  37. # 房间排序
  38. elif operation == 'sort':
  39. return self.room_sort_save(request_dict, response)
  40. # 房间删除
  41. elif operation == 'del':
  42. return self.room_del(app_user_id, request_dict, response)
  43. # 房间详情
  44. elif operation == 'details':
  45. return self.get_room_details(app_user_id, request_dict, response)
  46. elif operation == 'all-devices': # 家庭所有设备
  47. return self.all_devices(request_dict, response)
  48. @classmethod
  49. def room_device_save(cls, app_user_id, request_dict, response):
  50. """
  51. 房间加入设备or移除设备
  52. @param app_user_id:
  53. @param request_dict:
  54. @param response:
  55. @return:
  56. """
  57. family_id = request_dict.get('familyId', None)
  58. device_ids = request_dict.get('deviceIds', None)
  59. room_id = request_dict.get('roomId', None)
  60. if not all([family_id, room_id]):
  61. return response.json(444)
  62. family_id = int(family_id)
  63. room_id = int(room_id)
  64. is_owner = EquipmentFamilyView.get_family_owner(app_user_id, family_id)
  65. if not is_owner:
  66. return response.json(404)
  67. try:
  68. with transaction.atomic():
  69. room_qs = FamilyRoom.objects.filter(family_id=family_id, id=room_id)
  70. if not room_qs.exists():
  71. return response.json(173)
  72. qs = FamilyRoomDevice.objects.filter(family_id=family_id, room_id=room_id)
  73. if qs.exists():
  74. qs.update(room_id=0, sort=0)
  75. if device_ids:
  76. device_ids = device_ids.split(',')
  77. for i, item in enumerate(device_ids):
  78. device_id = int(item)
  79. device_qs = FamilyRoomDevice.objects.filter(device_id=device_id)
  80. if device_qs.exists():
  81. device_qs.update(room_id=room_id, sort=i)
  82. return response.json(0)
  83. except Exception as e:
  84. print(e)
  85. return response.json(177, repr(e))
  86. @classmethod
  87. def room_del(cls, user_id, request_dict, response):
  88. """
  89. 房间多选删除
  90. @param user_id: 当前登录用户id
  91. @param request_dict: 请求参数
  92. @param response: 响应参数
  93. @return:
  94. """
  95. ids = request_dict.get('roomIds', None)
  96. if not ids:
  97. return response.json(444)
  98. ids = ids.split(',')
  99. room_id = ids[0]
  100. room_info = FamilyRoom.objects.filter(id=room_id)
  101. if not room_info.exists():
  102. return response.json(173)
  103. is_owner = EquipmentFamilyView.get_family_owner(user_id, room_info.first().family_id)
  104. if not is_owner:
  105. return response.json(404)
  106. try:
  107. with transaction.atomic():
  108. for item in ids:
  109. room_id = int(item)
  110. room_device = FamilyRoomDevice.objects.filter(room_id=room_id)
  111. if room_device.exists():
  112. room_device.update(room_id=0)
  113. FamilyRoom.objects.filter(id=room_id).delete()
  114. return response.json(0)
  115. except Exception as e:
  116. print(e)
  117. return response.json(177, repr(e))
  118. @classmethod
  119. def room_sort_save(cls, request_dict, response):
  120. """
  121. 房间排序
  122. @param request_dict: 请求参数
  123. @param response: 响应参数
  124. @return:
  125. """
  126. ids = request_dict.get('ids', None)
  127. if not ids:
  128. return response.json(444)
  129. items = ids.split(',')
  130. for item in items:
  131. item = item.split('-')
  132. room_id, sort = item[0], item[1]
  133. family_room = FamilyRoom.objects.filter(id=int(room_id))
  134. if family_room.exists():
  135. family_room.update(sort=int(sort))
  136. return response.json(0)
  137. @classmethod
  138. def get_room_details(cls, app_user_id, request_dict, response):
  139. """
  140. 房间设备详情(所在当前房间下,和所在家庭不在当前房间下的主设备)
  141. @param app_user_id:
  142. @param request_dict:
  143. @param response:
  144. @return:
  145. """
  146. family_id = request_dict.get('familyId', None)
  147. room_id = request_dict.get('roomId', None)
  148. if not all([family_id, room_id]):
  149. return response.json(444)
  150. is_owner = EquipmentFamilyView.get_family_owner(app_user_id, family_id)
  151. if not is_owner:
  152. return response.json(404)
  153. family_id = int(family_id)
  154. room_id = int(room_id)
  155. room_device_qs = FamilyRoomDevice.objects.filter(family_id=family_id, room_id=room_id).order_by('sort').values(
  156. 'device_id').annotate(count=Count('device_id')).values('device_id', 'device__Type', 'device__NickName')
  157. device_room = []
  158. if room_device_qs.exists():
  159. room_name = FamilyRoom.objects.filter(id=room_id)
  160. for item in room_device_qs:
  161. device_room.append({
  162. 'deviceId': item['device_id'],
  163. 'deviceType': item['device__Type'],
  164. 'nickName': item['device__NickName'],
  165. 'roomName': room_name.first().name if room_name.exists() else '',
  166. })
  167. device_not_room = []
  168. device_not_room_qs = FamilyRoomDevice.objects.filter(family_id=family_id)
  169. device_not_room_qs = device_not_room_qs.filter(~Q(room_id=room_id)).values('device_id').annotate(
  170. count=Count('device_id')).values('room_id', 'device_id', 'device__Type', 'device__NickName')
  171. if device_not_room_qs.exists():
  172. for item in device_not_room_qs:
  173. name = ''
  174. if room_device_qs.exists():
  175. family_room_qs = FamilyRoom.objects.filter(id=item['room_id'])
  176. if family_room_qs.exists():
  177. name = family_room_qs.first().name
  178. device_not_room.append({
  179. 'deviceId': item['device_id'],
  180. 'deviceType': item['device__Type'],
  181. 'nickName': item['device__NickName'],
  182. 'roomName': name
  183. })
  184. return response.json(0, {'deviceRooms': device_room, 'deviceNotRooms': device_not_room})
  185. @staticmethod
  186. def all_devices(request_dict, response):
  187. """
  188. 家庭所有设备(网关和摄像头设备)
  189. @param request_dict: 请求参数
  190. @request_dict familyId: 家庭id
  191. @param response: 响应参数
  192. @return:
  193. """
  194. family_id = request_dict.get('familyId', None)
  195. if not family_id:
  196. return response.json(444)
  197. try:
  198. family_room_device_qs = FamilyRoomDevice.objects.filter(family_id=family_id, sub_device=0).\
  199. annotate(device_id=F('device__id'), type=F('device__Type'), nickname=F('device__NickName')).\
  200. values('device_id', 'type', 'nickname', 'room_id').order_by('sort')
  201. if not family_room_device_qs.exists():
  202. return response.json(0)
  203. # 查询房间名称
  204. for device in family_room_device_qs:
  205. room_id = device['room_id']
  206. if room_id == 0:
  207. device['room'] = ''
  208. else:
  209. family_room_qs = FamilyRoom.objects.filter(id=room_id).values('name')
  210. device['room'] = family_room_qs[0]['name'] if family_room_qs.exists() else ''
  211. device.pop('room_id')
  212. return response.json(0, list(family_room_device_qs))
  213. except Exception as e:
  214. return response.json(500, repr(e))