Browse Source

优化添加条件-查询设备接口

locky 3 years ago
parent
commit
036d95da0f
1 changed files with 46 additions and 5 deletions
  1. 46 5
      Controller/SensorGateway/SmartSceneController.py

+ 46 - 5
Controller/SensorGateway/SmartSceneController.py

@@ -9,9 +9,10 @@ import time
 from django.core.exceptions import ObjectDoesNotExist
 from django.views import View
 
-from Model.models import FamilyRoomDevice, GatewaySubDevice, FamilyRoom, SmartScene, EffectiveTime
+from Model.models import FamilyRoomDevice, GatewaySubDevice, FamilyRoom, SmartScene, EffectiveTime, Device_Info
 from Object.ResponseObject import ResponseObject
 from Object.TokenObject import TokenObject
+from Service.CommonService import CommonService
 
 
 class SmartSceneView(View):
@@ -47,6 +48,8 @@ class SmartSceneView(View):
             return self.scene_detail(request_dict, response)
         elif operation == 'edit':  # 编辑智能场景
             return self.edit_smart_scene(request_dict, response)
+        elif operation == 'delete':  # 删除智能场景
+            return self.delete_smart_scene(request_dict, response)
         else:
             return response.json(414)
 
@@ -65,10 +68,9 @@ class SmartSceneView(View):
         if not any([device_id, sub_device_id]):
             return response.json(444, {'error param': 'deviceId or subDeviceId'})
         try:
-            if device_id:
-                gateway_sub_device_qs = GatewaySubDevice.objects.filter(device_id=device_id)
-            else:
-                gateway_sub_device_qs = GatewaySubDevice.objects.filter(id=sub_device_id)
+            if sub_device_id:
+                device_id = GatewaySubDevice.objects.get(id=sub_device_id).device_id
+            gateway_sub_device_qs = GatewaySubDevice.objects.filter(device_id=device_id)
             if not gateway_sub_device_qs.exists():
                 return response.json(173)
             gateway_sub_device_qs = gateway_sub_device_qs.values('id', 'device_type', 'nickname', 'status')
@@ -151,10 +153,21 @@ class SmartSceneView(View):
                 'created_time': now_time,
                 'updated_time': now_time,
             }
+
+            # 处理传网关设备id和子设备id的情况
             if device_id:
                 smart_scene_dict['device_id'] = device_id
+                device_info_qs = Device_Info.objects.filter(id=device_id).values('serial_number')
+                if not device_info_qs.exists():
+                    return response.json(173)
+                serial_number = device_info_qs[0]['serial_number']
             else:
                 smart_scene_dict['sub_device_id'] = sub_device_id
+                sub_device_qs = GatewaySubDevice.objects.filter(id=sub_device_id).values('device__serial_number')
+                if not sub_device_qs.exists():
+                    return response.json(173)
+                serial_number = sub_device_qs[0]['device__serial_number']
+
             if not is_all_day:  # 没传时间
                 SmartScene.objects.create(**smart_scene_dict)
             else:
@@ -181,6 +194,15 @@ class SmartSceneView(View):
                                                                          repeat=repeat).id
                     smart_scene_dict['effective_time_id'] = effective_time_id
                     SmartScene.objects.create(**smart_scene_dict)
+
+            # 发布MQTT消息通知网关设备
+            thing_name = serial_number
+            topic_name = 'loocam/gateway_sensor/{}/smart_scene'.format(serial_number)
+            msg = ''
+            success = CommonService.req_publish_mqtt_msg(thing_name, topic_name, msg)
+            if not success:
+                return response.json(10044)
+
             return response.json(0)
         except Exception as e:
             return response.json(500, repr(e))
@@ -298,3 +320,22 @@ class SmartSceneView(View):
             return response.json(0, list(smart_scene_qs))
         except Exception as e:
             return response.json(500, repr(e))
+
+    @staticmethod
+    def delete_smart_scene(request_dict, response):
+        """
+        删除智能场景
+        @param request_dict: 请求参数
+        @request_dict smartSceneId: 智能场景id
+        @param response: 响应对象
+        @return: response
+        """
+        smart_scene_id = request_dict.get('smartSceneId', None)
+
+        if not smart_scene_id:
+            return response.json(444, {'error param': 'smartSceneId'})
+        try:
+            SmartScene.objects.filter(id=smart_scene_id).delete()
+            return response.json(0)
+        except Exception as e:
+            return response.json(500, repr(e))