Browse Source

删除设备时删除场景

locky 3 years ago
parent
commit
ec3324feab

+ 2 - 0
Controller/SensorGateway/GatewayDeviceController.py

@@ -160,6 +160,7 @@ class GatewayDeviceView(View):
                         uid_set_qs = UidSetModel.objects.filter(uid=device_qs.first().UID)
                         if uid_set_qs.exists():
                             uid_set_qs.delete()
+                        SmartScene.objects.filter(device_id=device_id).delete()
                         device_qs.delete()
                 elif sub_ids:
                     sub_ids = sub_ids.split(',')
@@ -173,6 +174,7 @@ class GatewayDeviceView(View):
                     gateway_sub_qs = GatewaySubDevice.objects.filter(id__in=ids)
                     if gateway_sub_qs.exists():
                         gateway_sub_qs.delete()
+                    SmartScene.objects.filter(sub_device_id__in=ids).delete()
                 return response.json(0)
         except Exception as e:
             print(e)

+ 5 - 4
Controller/SensorGateway/SubDeviceController.py

@@ -37,7 +37,7 @@ class GatewaySubDeviceView(View):
         elif operation == 'update':  # 更新子设备信息
             return self.sensor_update(request_dict, response)
         elif operation == 'delete':  # 删除子设备
-            return self.delete(request_dict, user_id, response)
+            return self.delete_sub_device(request_dict, response)
         elif operation == 'records/tem-hum':  # 查询温湿度传感器记录
             return self.records_tem_hum(request_dict, response)
         elif operation == 'records':  # 查询其他传感器记录
@@ -173,12 +173,11 @@ class GatewaySubDeviceView(View):
             return response.json(500, repr(e))
 
     @staticmethod
-    def delete(request_dict, user_id, response):
+    def delete_sub_device(request_dict, response):
         """
         更新子设备信息
         @param request_dict: 请求参数
         @request_dict sub_device_id: 子设备id
-        @param user_id: 用户id
         @param response: 响应对象
         @return: response
         """
@@ -187,7 +186,9 @@ class GatewaySubDeviceView(View):
         if not all([sub_device_id]):
             return response.json(444)
         try:
-            GatewaySubDevice.objects.filter(id=sub_device_id).delete()
+            with transaction.atomic():
+                GatewaySubDevice.objects.filter(id=sub_device_id).delete()
+                SmartScene.objects.filter(sub_device_id=sub_device_id).delete()
             return response.json(0)
         except Exception as e:
             return response.json(500, repr(e))