|
@@ -211,25 +211,34 @@ class SmartSceneView(View):
|
|
|
'created_time': now_time,
|
|
|
'updated_time': now_time,
|
|
|
}
|
|
|
-
|
|
|
+ msg = {}
|
|
|
# 处理设置时间
|
|
|
if is_all_day is not None:
|
|
|
is_all_day = int(is_all_day)
|
|
|
smart_scene_dict['is_all_day'] = is_all_day
|
|
|
|
|
|
# 处理传网关设备id和子设备id的情况
|
|
|
- if device_id:
|
|
|
+ if conditions_dict['type'] == 1: # 网关设置时间
|
|
|
+ if not device_id:
|
|
|
+ return response.json(444, {'error param': 'deviceId'})
|
|
|
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:
|
|
|
+ else: # 子设备设置场景
|
|
|
+ if not sub_device_id:
|
|
|
+ return response.json(444, {'error param': 'subDeviceId'})
|
|
|
smart_scene_dict['sub_device_id'] = sub_device_id
|
|
|
- sub_device_qs = GatewaySubDevice.objects.filter(id=sub_device_id).values('device__serial_number')
|
|
|
+ sub_device_qs = GatewaySubDevice.objects.filter(id=sub_device_id).values('device__serial_number',
|
|
|
+ 'src_addr')
|
|
|
if not sub_device_qs.exists():
|
|
|
return response.json(173)
|
|
|
serial_number = sub_device_qs[0]['device__serial_number']
|
|
|
+ msg['scene_status'] = 1
|
|
|
+ msg['sensor_type'] = conditions_dict['sensor']['device_type']
|
|
|
+ msg['sensor_src'] = int(sub_device_qs[0]['src_addr'], 16)
|
|
|
+ msg['sensor_status'] = int(conditions_dict['sensor']['eventValues'][0]['value'])
|
|
|
|
|
|
with transaction.atomic():
|
|
|
if is_all_day is None: # 不设置时间
|
|
@@ -276,18 +285,8 @@ class SmartSceneView(View):
|
|
|
else:
|
|
|
return response.json(444, {'error param': 'invalid isAllDay'})
|
|
|
|
|
|
- msg = {
|
|
|
- 'time': time_dict
|
|
|
- }
|
|
|
- if conditions_dict['type'] == 2: # 条件为选择子设备
|
|
|
- sub_device_id = conditions_dict['sensor']['subDeviceId']
|
|
|
- sub_device_qs = GatewaySubDevice.objects.filter(id=sub_device_id).values('src_addr').first()
|
|
|
- msg['smart_scene_id'] = smart_scene_qs.id
|
|
|
- msg['scene_status'] = 1
|
|
|
- msg['sensor_type'] = conditions_dict['sensor']['device_type']
|
|
|
- msg['sensor_src'] = int(sub_device_qs['src_addr'], 16)
|
|
|
- msg['sensor_status'] = int(conditions_dict['sensor']['eventValues'][0]['value'])
|
|
|
-
|
|
|
+ msg['time'] = time_dict
|
|
|
+ msg['smart_scene_id'] = smart_scene_qs.id
|
|
|
task_list = []
|
|
|
for task in tasks_list:
|
|
|
task_temp = {
|
|
@@ -335,7 +334,9 @@ class SmartSceneView(View):
|
|
|
return response.json(444, {'error param': 'deviceId or subDeviceId'})
|
|
|
try:
|
|
|
if device_id:
|
|
|
- smart_scene_qs = SmartScene.objects.filter(user_id=user_id, device_id=device_id)
|
|
|
+ sub_device_id = GatewaySubDevice.objects.filter(device_id=device_id).values('id')
|
|
|
+ smart_scene_qs = SmartScene.objects.filter(
|
|
|
+ Q(user_id=user_id) & Q(device_id=device_id) | Q(sub_device_id__in=sub_device_id))
|
|
|
else:
|
|
|
smart_scene_qs = SmartScene.objects.filter(user_id=user_id, sub_device_id=sub_device_id)
|
|
|
if not smart_scene_qs.exists():
|
|
@@ -420,6 +421,8 @@ class SmartSceneView(View):
|
|
|
@return: response
|
|
|
"""
|
|
|
smart_scene_id = request_dict.get('smartSceneId', None)
|
|
|
+ device_id = request_dict.get('deviceId', None)
|
|
|
+ sub_device_id = request_dict.get('subDeviceId', None)
|
|
|
scene_name = request_dict.get('sceneName', None)
|
|
|
conditions = request_dict.get('conditions', None)
|
|
|
tasks = request_dict.get('tasks', None)
|
|
@@ -427,6 +430,7 @@ class SmartSceneView(View):
|
|
|
|
|
|
conditions_dict = eval(conditions)
|
|
|
tasks_list = eval(tasks)
|
|
|
+ now_time = int(time.time())
|
|
|
res = {
|
|
|
'scene_name': scene_name,
|
|
|
'conditions': conditions_dict,
|
|
@@ -446,15 +450,22 @@ class SmartSceneView(View):
|
|
|
smart_scene_qs = SmartScene.objects.filter(id=smart_scene_id)
|
|
|
if not smart_scene_qs.exists():
|
|
|
return response.json(173)
|
|
|
+
|
|
|
msg = {}
|
|
|
if conditions_dict['type'] == 2: # 条件为选择子设备
|
|
|
- sub_device_id = conditions_dict['sensor']['subDeviceId']
|
|
|
+ if not sub_device_id:
|
|
|
+ return response.json(444, {'error param': 'subDeviceId'})
|
|
|
+ device_id = ''
|
|
|
sub_device_qs = GatewaySubDevice.objects.filter(id=sub_device_id).values('src_addr').first()
|
|
|
msg['smart_scene_id'] = smart_scene_id
|
|
|
msg['scene_status'] = 1
|
|
|
msg['sensor_type'] = conditions_dict['sensor']['device_type']
|
|
|
msg['sensor_src'] = int(sub_device_qs['src_addr'], 16)
|
|
|
msg['sensor_status'] = int(conditions_dict['sensor']['eventValues'][0]['event_type'])
|
|
|
+ else:
|
|
|
+ if not device_id:
|
|
|
+ return response.json(444, {'error param': 'deviceId'})
|
|
|
+ sub_device_id = 0
|
|
|
|
|
|
task_list = []
|
|
|
for task in tasks_list:
|
|
@@ -462,16 +473,19 @@ class SmartSceneView(View):
|
|
|
'sensor_type': task['device_type'],
|
|
|
'sensor_action': task['event_type']
|
|
|
}
|
|
|
- sub_device_id = task.get('subDeviceId', None)
|
|
|
- if sub_device_id:
|
|
|
- sub_device_qs = GatewaySubDevice.objects.filter(id=sub_device_id).values('src_addr').first()
|
|
|
+ task_sub_device_id = task.get('subDeviceId', None)
|
|
|
+ if task_sub_device_id:
|
|
|
+ sub_device_qs = GatewaySubDevice.objects.filter(id=task_sub_device_id).values('src_addr').first()
|
|
|
task_temp['sensor_src'] = int(sub_device_qs['src_addr'], 16)
|
|
|
task_list.append(task_temp)
|
|
|
msg['task'] = task_list
|
|
|
|
|
|
+
|
|
|
with transaction.atomic():
|
|
|
smart_scene_qs.update(scene_name=scene_name, conditions=conditions, tasks=tasks,
|
|
|
- device_data=json.dumps(msg))
|
|
|
+ device_data=json.dumps(msg), updated_time=now_time, device_id=device_id,
|
|
|
+ sub_device_id=sub_device_id)
|
|
|
+
|
|
|
if is_all_day == 0 or is_all_day == 1: # 不设置时间或全天
|
|
|
smart_scene_qs.update(effective_time_id=0, is_all_day=is_all_day)
|
|
|
else:
|