|
@@ -0,0 +1,111 @@
|
|
|
|
+# -*- coding: utf-8 -*-
|
|
|
|
+"""
|
|
|
|
+@Author : Rocky
|
|
|
|
+@Time : 2022/6/29 9:31
|
|
|
|
+@File :SmartSceneController.py
|
|
|
|
+"""
|
|
|
|
+from django.core.exceptions import ObjectDoesNotExist
|
|
|
|
+from django.views import View
|
|
|
|
+
|
|
|
|
+from Model.models import FamilyRoomDevice, GatewaySubDevice, FamilyRoom
|
|
|
|
+from Object.ResponseObject import ResponseObject
|
|
|
|
+from Object.TokenObject import TokenObject
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+class SmartSceneView(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_obj = TokenObject(request.META.get('HTTP_AUTHORIZATION'))
|
|
|
|
+ lang = request_dict.get('lang', None)
|
|
|
|
+ response = ResponseObject(lang if lang else token_obj.lang)
|
|
|
|
+
|
|
|
|
+ if token_obj.code != 0:
|
|
|
|
+ return response.json(token_obj.code)
|
|
|
|
+ user_id = token_obj.userID
|
|
|
|
+ if operation == 'condition-devices': # 添加条件-查询设备
|
|
|
|
+ return self.condition_devices(request_dict, response)
|
|
|
|
+ if operation == 'task-devices': # 添加任务-查询设备
|
|
|
|
+ return self.task_devices(request_dict, user_id, response)
|
|
|
|
+ elif operation == 'create': # 创建智能场景
|
|
|
|
+ return self.create_smart_scene(request_dict, response)
|
|
|
|
+ else:
|
|
|
|
+ return response.json(414)
|
|
|
|
+
|
|
|
|
+ @staticmethod
|
|
|
|
+ def condition_devices(request_dict, response):
|
|
|
|
+ """
|
|
|
|
+ 添加条件-查询设备
|
|
|
|
+ @param request_dict: 请求参数
|
|
|
|
+ @request_dict deviceId: 网关设备id
|
|
|
|
+ @param response: 响应对象
|
|
|
|
+ @return: response
|
|
|
|
+ """
|
|
|
|
+ device_id = request_dict.get('deviceId', None)
|
|
|
|
+
|
|
|
|
+ if not all([device_id]):
|
|
|
|
+ return response.json(444)
|
|
|
|
+ try:
|
|
|
|
+ gateway_sub_device_qs = GatewaySubDevice.objects.filter(device_id=device_id).values('id', 'device_type',
|
|
|
|
+ 'nickname', 'status')
|
|
|
|
+ if not gateway_sub_device_qs.exists():
|
|
|
|
+ return response.json(173)
|
|
|
|
+ sub_device_list = []
|
|
|
|
+ for gateway_sub_device in gateway_sub_device_qs:
|
|
|
|
+ room_id = FamilyRoomDevice.objects.filter(sub_device=gateway_sub_device['id']).values('room_id')[0][
|
|
|
|
+ 'room_id']
|
|
|
|
+ try:
|
|
|
|
+ gateway_sub_device['room_name'] = FamilyRoom.objects.get(id=room_id).name
|
|
|
|
+ except ObjectDoesNotExist:
|
|
|
|
+ gateway_sub_device['room_name'] = ''
|
|
|
|
+ gateway_sub_device.pop('id')
|
|
|
|
+ sub_device_list.append(gateway_sub_device)
|
|
|
|
+ return response.json(0, sub_device_list)
|
|
|
|
+ except Exception as e:
|
|
|
|
+ return response.json(500, repr(e))
|
|
|
|
+
|
|
|
|
+ @staticmethod
|
|
|
|
+ def task_devices(request_dict, user_id, response):
|
|
|
|
+ """
|
|
|
|
+ 添加任务-查询设备
|
|
|
|
+ @param request_dict: 请求参数
|
|
|
|
+ @param user_id: 用户id
|
|
|
|
+ @request_dict deviceId: 网关设备id
|
|
|
|
+ @param response: 响应对象
|
|
|
|
+ @return: response
|
|
|
|
+ """
|
|
|
|
+ device_id = request_dict.get('deviceId', None)
|
|
|
|
+
|
|
|
|
+ if not all([device_id]):
|
|
|
|
+ return response.json(444)
|
|
|
|
+ try:
|
|
|
|
+ device_info_qs = GatewaySubDevice.objects.filter(device__userID_id=user_id)
|
|
|
|
+ return response.json(0)
|
|
|
|
+ except Exception as e:
|
|
|
|
+ return response.json(500, repr(e))
|
|
|
|
+
|
|
|
|
+ @staticmethod
|
|
|
|
+ def create_smart_scene(request_dict, response):
|
|
|
|
+ """
|
|
|
|
+ 创建智能场景
|
|
|
|
+ @param request_dict: 请求参数
|
|
|
|
+ @request_dict gatewaySubId: 子设备id
|
|
|
|
+ @param response: 响应对象
|
|
|
|
+ @return: response
|
|
|
|
+ """
|
|
|
|
+ sub_device_id = request_dict.get('gatewaySubId', None)
|
|
|
|
+
|
|
|
|
+ if not all([sub_device_id]):
|
|
|
|
+ return response.json(444)
|
|
|
|
+ try:
|
|
|
|
+ return response.json(0)
|
|
|
|
+ except Exception as e:
|
|
|
|
+ return response.json(500, repr(e))
|