|
@@ -597,7 +597,7 @@ 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):
|
|
|
+ if cls.time_conflict(sub_device_id, conditions, is_all_day, request_dict, smart_scene_id):
|
|
|
return response.json(182)
|
|
|
|
|
|
device_type = int(conditions_dict['sensor']['device_type'])
|
|
@@ -1019,13 +1019,14 @@ class SmartSceneView(View):
|
|
|
return task_list
|
|
|
|
|
|
@staticmethod
|
|
|
- def time_conflict(sub_device_id, conditions, is_all_day, request_dict):
|
|
|
+ def time_conflict(sub_device_id, conditions, is_all_day, request_dict, smart_scene_id=None):
|
|
|
"""
|
|
|
判断传感器是否创建过条件相同且生效时间冲突的场景
|
|
|
@param sub_device_id: 传感器设备id
|
|
|
@param conditions: 场景条件
|
|
|
@param is_all_day: 全天标识
|
|
|
@param request_dict:
|
|
|
+ @param smart_scene_id: 场景id,编辑场景时传
|
|
|
@return: bool, True: 冲突, False: 不冲突
|
|
|
"""
|
|
|
# 不设置时间不会冲突
|
|
@@ -1033,28 +1034,45 @@ class SmartSceneView(View):
|
|
|
return False
|
|
|
|
|
|
# 查询设置过时间的数据
|
|
|
- smart_scene_qs = SmartScene.objects.filter(
|
|
|
- ~Q(is_all_day=0),
|
|
|
- sub_device_id=sub_device_id,
|
|
|
- conditions=conditions).values('effective_time_id')
|
|
|
+ if smart_scene_id is None: # 创建场景
|
|
|
+ smart_scene_qs = SmartScene.objects.filter(
|
|
|
+ ~Q(is_all_day=0),
|
|
|
+ sub_device_id=sub_device_id,
|
|
|
+ conditions=conditions).values('effective_time_id')
|
|
|
+ else: # 编辑场景,过滤本身场景数据
|
|
|
+ smart_scene_qs = SmartScene.objects.filter(
|
|
|
+ ~Q(id=smart_scene_id),
|
|
|
+ ~Q(is_all_day=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 == 1:
|
|
|
return True
|
|
|
- # 非全天判断effective_time_id
|
|
|
+ # 非全天
|
|
|
elif is_all_day == 2:
|
|
|
- start_time = int(request_dict.get('startTime', None))
|
|
|
- end_time = int(request_dict.get('endTime', None))
|
|
|
- int(request_dict.get('repeat', None))
|
|
|
+ start_time = int(request_dict.get('startTime'))
|
|
|
+ end_time = int(request_dict.get('endTime'))
|
|
|
+ repeat = int(request_dict.get('repeat'))
|
|
|
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 effective_time_md.start_time <= start_time <= effective_time_md.end_time or \
|
|
|
- effective_time_md.start_time <= end_time <= effective_time_md.end_time:
|
|
|
- return True
|
|
|
+ # 每天重复
|
|
|
+ if repeat == 127:
|
|
|
+ # 判断时间是否在已设置过的时间范围之内
|
|
|
+ if effective_time_md.start_time <= start_time <= effective_time_md.end_time or \
|
|
|
+ effective_time_md.start_time <= end_time <= effective_time_md.end_time:
|
|
|
+ return True
|
|
|
+ else:
|
|
|
+ # 有相同的重复天
|
|
|
+ if repeat & EffectiveTime.repeat != 0:
|
|
|
+ # 判断时间是否在已设置过的时间范围之内
|
|
|
+ if effective_time_md.start_time <= start_time <= effective_time_md.end_time or \
|
|
|
+ effective_time_md.start_time <= end_time <= effective_time_md.end_time:
|
|
|
+ return True
|
|
|
return False
|
|
|
|
|
|
#
|