|
@@ -0,0 +1,90 @@
|
|
|
|
+# -*- encoding: utf-8 -*-
|
|
|
|
+"""
|
|
|
|
+@File : GatewayFamilyRoomController.py
|
|
|
|
+@Time : 2022/5/24 19:43
|
|
|
|
+@Author : stephen
|
|
|
|
+@Email : zhangdongming@asj6.wecom.work
|
|
|
|
+@Software: PyCharm
|
|
|
|
+"""
|
|
|
|
+
|
|
|
|
+from django.db import transaction
|
|
|
|
+from django.views.generic.base import View
|
|
|
|
+
|
|
|
|
+from Model.models import FamilyRoomDevice, FamilyRoom
|
|
|
|
+from Object.ResponseObject import ResponseObject
|
|
|
|
+from Object.TokenObject import TokenObject
|
|
|
|
+from SensorGateway.EquipmentFamilyController import EquipmentFamilyView
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+# 家庭房间管理
|
|
|
|
+class GatewayFamilyRoomView(View):
|
|
|
|
+
|
|
|
|
+ def get(self, request, *args, **kwargs):
|
|
|
|
+ request.encoding = 'utf-8'
|
|
|
|
+ operation = kwargs.get('operation')
|
|
|
|
+ return self.validation(request.GET, request, operation)
|
|
|
|
+
|
|
|
|
+ def post(self, request, *args, **kwargs):
|
|
|
|
+ request.encoding = 'utf-8'
|
|
|
|
+ operation = kwargs.get('operation')
|
|
|
|
+ return self.validation(request.POST, request, operation)
|
|
|
|
+
|
|
|
|
+ def validation(self, request_dict, request, operation):
|
|
|
|
+ token = request.META.get('HTTP_AUTHORIZATION')
|
|
|
|
+ token = TokenObject(token)
|
|
|
|
+ lang = request_dict.get('lang', None)
|
|
|
|
+ response = ResponseObject(lang) if lang else ResponseObject(token.lang)
|
|
|
|
+ if token.code != 0:
|
|
|
|
+ return response.json(token.code)
|
|
|
|
+ app_user_id = token.userID
|
|
|
|
+ # 添加设备关联房间
|
|
|
|
+ if operation == 'device-changes':
|
|
|
|
+ return self.room_device_save(app_user_id, request_dict, response)
|
|
|
|
+ # 房间排序
|
|
|
|
+ elif operation == 'sort':
|
|
|
|
+ return self.room_sort_save(request_dict, response)
|
|
|
|
+
|
|
|
|
+ @classmethod
|
|
|
|
+ def room_device_save(cls, app_user_id, request_dict, response):
|
|
|
|
+ """
|
|
|
|
+ 房间加入设备or移除设备
|
|
|
|
+ @param app_user_id:
|
|
|
|
+ @param request_dict:
|
|
|
|
+ @param response:
|
|
|
|
+ @return:
|
|
|
|
+ """
|
|
|
|
+ family_id = request_dict.get('familyId', None)
|
|
|
|
+ device_ids = request_dict.getlist('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]):
|
|
|
|
+ return response.json(444)
|
|
|
|
+ operate = int(operate)
|
|
|
|
+ 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)
|
|
|
|
+ if qs.exists():
|
|
|
|
+ qs.update(room_id=0) if operate == 1 else qs.update(room_id=int(room_id))
|
|
|
|
+ return response.json(0)
|
|
|
|
+
|
|
|
|
+ @classmethod
|
|
|
|
+ def room_sort_save(cls, request_dict, response):
|
|
|
|
+ """
|
|
|
|
+ 房间排序
|
|
|
|
+ @param request_dict: 请求参数
|
|
|
|
+ @param response: 响应参数
|
|
|
|
+ @return:
|
|
|
|
+ """
|
|
|
|
+ ids = request_dict.getlist('ids', None)
|
|
|
|
+ if not ids:
|
|
|
|
+ return response.json(444)
|
|
|
|
+ for i, item in ids:
|
|
|
|
+ id_sort = item[i]
|
|
|
|
+ print(id_sort)
|
|
|
|
+ return response.json(0)
|