|
@@ -11,10 +11,10 @@ from django.db import transaction
|
|
|
from django.db.models import Q, Count
|
|
|
from django.views.generic.base import View
|
|
|
|
|
|
+from Controller.SensorGateway.EquipmentFamilyController import EquipmentFamilyView
|
|
|
from Model.models import FamilyRoomDevice, FamilyRoom
|
|
|
from Object.ResponseObject import ResponseObject
|
|
|
from Object.TokenObject import TokenObject
|
|
|
-from Controller.SensorGateway.EquipmentFamilyController import EquipmentFamilyView
|
|
|
|
|
|
|
|
|
# 家庭房间管理
|
|
@@ -49,7 +49,7 @@ class GatewayFamilyRoomView(View):
|
|
|
return self.room_del(app_user_id, request_dict, response)
|
|
|
# 房间详情
|
|
|
elif operation == 'details':
|
|
|
- return self.get_room_details(app_user_id, request_dict, response)
|
|
|
+ return self.get_room_details(request_dict, response)
|
|
|
|
|
|
@classmethod
|
|
|
def room_device_save(cls, app_user_id, request_dict, response):
|
|
@@ -61,24 +61,34 @@ class GatewayFamilyRoomView(View):
|
|
|
@return:
|
|
|
"""
|
|
|
family_id = request_dict.get('familyId', None)
|
|
|
- device_ids = request_dict.getlist('deviceIds', None)
|
|
|
+ device_ids = request_dict.get('deviceIds', None)
|
|
|
room_id = request_dict.get('roomId', None)
|
|
|
- operate = request_dict.get('operate', None)
|
|
|
- if not all([family_id, device_ids, operate, room_id]):
|
|
|
+ if not all([family_id, room_id]):
|
|
|
return response.json(444)
|
|
|
- operate = int(operate)
|
|
|
+ family_id = int(family_id)
|
|
|
+ room_id = int(room_id)
|
|
|
is_owner = EquipmentFamilyView.get_family_owner(app_user_id, family_id)
|
|
|
if not is_owner:
|
|
|
return response.json(404)
|
|
|
- with transaction.atomic():
|
|
|
- room_qs = FamilyRoom.objects.filter(family_id=family_id, id=room_id)
|
|
|
- if not room_qs.exists():
|
|
|
- return response.json(173)
|
|
|
- for item in device_ids:
|
|
|
- qs = FamilyRoomDevice.objects.filter(family_id=family_id, device_id=item)
|
|
|
+ try:
|
|
|
+ with transaction.atomic():
|
|
|
+ room_qs = FamilyRoom.objects.filter(family_id=family_id, id=room_id)
|
|
|
+ if not room_qs.exists():
|
|
|
+ return response.json(173)
|
|
|
+ qs = FamilyRoomDevice.objects.filter(family_id=family_id, room_id=room_id)
|
|
|
if qs.exists():
|
|
|
- qs.update(room_id=0) if operate == 1 else qs.update(room_id=int(room_id))
|
|
|
- return response.json(0)
|
|
|
+ qs.update(room_id=0, sort=0)
|
|
|
+ if device_ids:
|
|
|
+ device_ids = device_ids.split(',')
|
|
|
+ for i, item in enumerate(device_ids):
|
|
|
+ device_id = int(item)
|
|
|
+ device_qs = FamilyRoomDevice.objects.filter(device_id=device_id)
|
|
|
+ if device_qs.exists():
|
|
|
+ device_qs.update(room_id=room_id, sort=i)
|
|
|
+ return response.json(0)
|
|
|
+ except Exception as e:
|
|
|
+ print(e)
|
|
|
+ return response.json(177, repr(e))
|
|
|
|
|
|
@classmethod
|
|
|
def room_del(cls, user_id, request_dict, response):
|
|
@@ -135,61 +145,47 @@ class GatewayFamilyRoomView(View):
|
|
|
return response.json(0)
|
|
|
|
|
|
@classmethod
|
|
|
- def get_room_details(cls, app_user_id, request_dict, response):
|
|
|
+ def get_room_details(cls, request_dict, response):
|
|
|
"""
|
|
|
- 房间设备详情
|
|
|
- @param app_user_id:
|
|
|
+ 房间设备详情(所在当前房间下,和所在家庭不在当前房间下的主设备)
|
|
|
@param request_dict:
|
|
|
@param response:
|
|
|
@return:
|
|
|
"""
|
|
|
family_id = request_dict.get('familyId', None)
|
|
|
room_id = request_dict.get('roomId', None)
|
|
|
- page_no = request_dict.get('pageNo', None)
|
|
|
- page_size = request_dict.get('pageSize', None)
|
|
|
- if not all([family_id, room_id, page_no, page_size]):
|
|
|
+ if not all([family_id, room_id]):
|
|
|
return response.json(444)
|
|
|
- room_count = FamilyRoomDevice.objects.filter(family_id=int(family_id), room_id=int(room_id)).values(
|
|
|
- 'device_id').annotate(count=Count('device_id')).count()
|
|
|
+ family_id = int(family_id)
|
|
|
+ room_id = int(room_id)
|
|
|
+ room_device_qs = FamilyRoomDevice.objects.filter(family_id=family_id, room_id=room_id).order_by('sort').values(
|
|
|
+ 'device_id').annotate(count=Count('device_id')).values('device_id', 'device__Type', 'device__NickName')
|
|
|
device_room = []
|
|
|
- # 房间设备列表
|
|
|
- if room_count > 0:
|
|
|
- device_room_list = EquipmentFamilyView.get_family_device_list(user_id=app_user_id, page_no=1,
|
|
|
- page_size=room_count,
|
|
|
- family_id=int(family_id),
|
|
|
- room_id=int(room_id))
|
|
|
-
|
|
|
- if device_room_list:
|
|
|
- room_name = FamilyRoom.objects.get(id=room_id).name
|
|
|
+ room_name = FamilyRoom.objects.get(id=room_id).name
|
|
|
+ if room_device_qs.exists():
|
|
|
+ for item in room_device_qs:
|
|
|
+ device_room.append({
|
|
|
+ 'deviceId': item['device_id'],
|
|
|
+ 'deviceType': item['device__Type'],
|
|
|
+ 'nickName': item['device__NickName'],
|
|
|
+ 'roomName': room_name,
|
|
|
+ })
|
|
|
|
|
|
- for item in device_room_list:
|
|
|
- device_room.append({
|
|
|
- 'deviceId': item['id'],
|
|
|
- 'deviceType': item['Type'],
|
|
|
- 'nickName': item['NickName'],
|
|
|
- 'roomName': room_name,
|
|
|
- })
|
|
|
device_not_room = []
|
|
|
- device_not_room_count = FamilyRoomDevice.objects.filter(family_id=int(family_id), room_id=0).values(
|
|
|
- 'device_id').annotate(count=Count('device_id')).count()
|
|
|
- if device_not_room_count > 0:
|
|
|
- not_room_device_list = EquipmentFamilyView.get_family_device_list(user_id=app_user_id, page_no=int(page_no),
|
|
|
- page_size=int(page_size),
|
|
|
- family_id=int(family_id), room_id=0,
|
|
|
- is_room_other=True)
|
|
|
- if not_room_device_list:
|
|
|
- for item in not_room_device_list:
|
|
|
- room_device_qs = FamilyRoomDevice.objects.filter(family_id=int(family_id))
|
|
|
- room_device_qs = room_device_qs.filter(~Q(room_id=0))
|
|
|
- name = ''
|
|
|
- if room_device_qs.exists():
|
|
|
- family_room_qs = FamilyRoom.objects.filter(id=room_device_qs.first().room_id)
|
|
|
- if family_room_qs.exists():
|
|
|
- name = family_room_qs.first().name
|
|
|
- device_not_room.append({
|
|
|
- 'deviceId': item['id'],
|
|
|
- 'deviceType': item['Type'],
|
|
|
- 'nickName': item['NickName'],
|
|
|
- 'roomName': name
|
|
|
- })
|
|
|
+ device_not_room_qs = FamilyRoomDevice.objects.filter(family_id=family_id)
|
|
|
+ device_not_room_qs = device_not_room_qs.filter(~Q(room_id=room_id)).values('device_id').annotate(
|
|
|
+ count=Count('device_id')).values('room_id', 'device_id', 'device__Type', 'device__NickName')
|
|
|
+ if device_not_room_qs.exists():
|
|
|
+ for item in device_not_room_qs:
|
|
|
+ name = ''
|
|
|
+ if room_device_qs.exists():
|
|
|
+ family_room_qs = FamilyRoom.objects.filter(id=item['room_id'])
|
|
|
+ if family_room_qs.exists():
|
|
|
+ name = family_room_qs.first().name
|
|
|
+ device_not_room.append({
|
|
|
+ 'deviceId': item['device_id'],
|
|
|
+ 'deviceType': item['device__Type'],
|
|
|
+ 'nickName': item['device__NickName'],
|
|
|
+ 'roomName': name
|
|
|
+ })
|
|
|
return response.json(0, {'deviceRooms': device_room, 'deviceNotRooms': device_not_room})
|