Quellcode durchsuchen

场景判断时间冲突

locky vor 2 Jahren
Ursprung
Commit
b9cfd49433
2 geänderte Dateien mit 49 neuen und 4 gelöschten Zeilen
  1. 39 0
      Controller/SensorGateway/SmartSceneController.py
  2. 10 4
      Object/ResponseObject.py

+ 39 - 0
Controller/SensorGateway/SmartSceneController.py

@@ -256,6 +256,9 @@ class SmartSceneView(View):
                 if not sub_device_id:
                     return response.json(444, {'error param': 'subDeviceId'})
 
+                if cls.time_conflict(sub_device_id, conditions, is_all_day, request_dict):
+                    return response.json(182)
+
                 device_type = int(conditions_dict['sensor']['device_type'])
                 # 智能按钮不能创建触发条件相同的场景
                 if device_type == SENSOR_TYPE['smart_button']:
@@ -594,6 +597,9 @@ class SmartSceneView(View):
                 if not sub_device_id:
                     return response.json(444, {'error param': 'subDeviceId'})
 
+                if cls.time_conflict(sub_device_id, conditions, is_all_day, request_dict):
+                    return response.json(182)
+
                 device_type = int(conditions_dict['sensor']['device_type'])
                 # 智能按钮不能创建触发条件相同的场景
                 if device_type == SENSOR_TYPE['smart_button']:
@@ -1012,6 +1018,39 @@ class SmartSceneView(View):
             task_list.append(task_temp)
         return task_list
 
+    @staticmethod
+    def time_conflict(sub_device_id, conditions, is_all_day, request_dict):
+        """
+        判断传感器是否创建过条件相同且生效时间冲突的场景
+        @param sub_device_id: 传感器设备id
+        @param conditions: 场景条件
+        @param is_all_day: 全天标识
+        @param request_dict:
+        @return: bool, True: 冲突, False: 不冲突
+        """
+        smart_scene_qs = SmartScene.objects.filter(
+            ~Q(effective_time_id=0),
+            sub_device_id=sub_device_id,
+            conditions=conditions).values('effective_time_id')
+        if not smart_scene_qs.exists():
+            return False
+
+        if is_all_day is None:  # 不设置时间不会冲突
+            return False
+        elif is_all_day == 1:  # 全天必冲突
+            return True
+        elif is_all_day == 2:  # 非全天判断effective_time_id
+            start_time = int(request_dict.get('startTime', None))
+            end_time = int(request_dict.get('endTime', None))
+            int(request_dict.get('repeat', None))
+            for smart_scene in smart_scene_qs:
+                effective_time_id = smart_scene['effective_time_id']
+                effective_time_md = EffectiveTime.objects.get(id=effective_time_id)
+                # 判断时间是否在已设置过的时间范围之内
+                # (新)结束时间大于等于(旧)开始时间,或开始时间小于等于结束时间
+                if end_time >= effective_time_md.start_time or start_time <= effective_time_md.end_time:
+                    return True
+            return False
 
 #
 #                   ___====-_  _-====___

+ 10 - 4
Object/ResponseObject.py

@@ -51,6 +51,7 @@ class ResponseObject(object):
             179: 'Nickname repeated',
             180: 'Smart button scene trigger conditions cannot be repeated',
             181: 'The gateway only bind 3 smart button at most',
+            182: 'Scene effective time conflict',
             201: 'You can only add 3 custom voice at most',
             306: 'The link has expired!',
             309: 'Please ReLogin! errmsg token',
@@ -107,12 +108,14 @@ class ResponseObject(object):
             10038: 'Non device primary user cannot transfer packages',
             10039: 'Activation code has been used',
             10040: 'Invalid activation code',
-            10041: 'This device has purchased a domestic cloud storage package, and cannot purchase a foreign cloud storage package',
+            10041: 'This device has purchased a domestic cloud storage package, '
+                   'and cannot purchase a foreign cloud storage package',
             10042: 'The device has registered a certificate',
             10043: 'The device does not registered a certificate',
             10044: 'Failed to publish MQTT message',
             10045: 'Already the latest version',
-            10046: 'Sorry, users who have activated cloud storage packages do not support logout at the moment, please contact customer service',
+            10046: 'Sorry, users who have activated cloud storage packages do not support logout at the moment, '
+                   'please contact customer service',
             10047: 'Please delete all devices under your account first',
             10048: 'Subscribe to the failure',
             10049: 'The coupon is not available',
@@ -122,11 +125,13 @@ class ResponseObject(object):
             10053: 'The AI service is not purchased',
             10054: 'The AI service has expired',
             10055: 'The AI does not recognize any labels',
-            10056: 'The device has enabled automatic renewal of cloud storage package and cannot be transferred for the time being',
+            10056: 'The device has enabled automatic renewal of cloud storage package and cannot be transferred '
+                   'for the time being',
             10057: "Can't delete",
             10058: 'Default family cannot be deleted',
             10059: 'Order deactivation failure',
-            10060: 'This device has purchased foreign cloud storage package, so it cannot buy domestic cloud storage package',
+            10060: 'This device has purchased foreign cloud storage package, so it cannot buy domestic '
+                   'cloud storage package',
             10061: 'Add the limit reached',
             10062: 'This device has experienced the package',
             10063: 'Failed to claim',
@@ -176,6 +181,7 @@ class ResponseObject(object):
             179: '名称不能重复',
             180: '智能按钮场景触发条件不能重复',
             181: '该网关最多只能绑定3个智能按钮',
+            182: '场景生效时间冲突',
             201: '最多只能添加3条自定义语音',
             306: '链接已超过有效期!',
             309: '请重新登录!',