|
@@ -10,6 +10,7 @@ import json
|
|
from django.core.exceptions import ObjectDoesNotExist
|
|
from django.core.exceptions import ObjectDoesNotExist
|
|
from django.db.models import F, Q
|
|
from django.db.models import F, Q
|
|
from django.views import View
|
|
from django.views import View
|
|
|
|
+from django.db import transaction
|
|
|
|
|
|
from Model.models import FamilyRoomDevice, GatewaySubDevice, FamilyRoom, SmartScene, EffectiveTime, Device_Info
|
|
from Model.models import FamilyRoomDevice, GatewaySubDevice, FamilyRoom, SmartScene, EffectiveTime, Device_Info
|
|
from Service.CommonService import CommonService
|
|
from Service.CommonService import CommonService
|
|
@@ -201,87 +202,89 @@ class SmartSceneView(View):
|
|
conditions_dict = eval(conditions)
|
|
conditions_dict = eval(conditions)
|
|
tasks_list = eval(tasks)
|
|
tasks_list = eval(tasks)
|
|
try:
|
|
try:
|
|
- # 判断是否已存在该场景名
|
|
|
|
- smart_scene_qs = SmartScene.objects.filter(user_id=user_id, scene_name=scene_name)
|
|
|
|
- if smart_scene_qs.exists():
|
|
|
|
- return response.json(174)
|
|
|
|
-
|
|
|
|
- smart_scene_dict = {
|
|
|
|
- 'user_id': user_id,
|
|
|
|
- 'scene_name': scene_name,
|
|
|
|
- 'conditions': conditions,
|
|
|
|
- 'tasks': tasks,
|
|
|
|
- 'created_time': now_time,
|
|
|
|
- 'updated_time': now_time,
|
|
|
|
- }
|
|
|
|
|
|
+ with transaction.atomic():
|
|
|
|
+ # 判断是否已存在该场景名
|
|
|
|
+ smart_scene_qs = SmartScene.objects.filter(user_id=user_id, scene_name=scene_name)
|
|
|
|
+ if smart_scene_qs.exists():
|
|
|
|
+ return response.json(174)
|
|
|
|
+
|
|
|
|
+ smart_scene_dict = {
|
|
|
|
+ 'user_id': user_id,
|
|
|
|
+ 'scene_name': scene_name,
|
|
|
|
+ 'conditions': conditions,
|
|
|
|
+ 'tasks': tasks,
|
|
|
|
+ '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: # 没传时间
|
|
|
|
- smart_scene_qs = SmartScene.objects.create(**smart_scene_dict)
|
|
|
|
- else:
|
|
|
|
- if is_all_day == '0':
|
|
|
|
- effective_time_qs = EffectiveTime.objects.filter(is_all_day=True).values('id')
|
|
|
|
- if effective_time_qs.exists():
|
|
|
|
- effective_time_id = effective_time_qs[0]['id']
|
|
|
|
- else:
|
|
|
|
- effective_time_id = EffectiveTime.objects.create(is_all_day=True).id
|
|
|
|
- smart_scene_dict['effective_time_id'] = effective_time_id
|
|
|
|
- SmartScene.objects.create(**smart_scene_dict)
|
|
|
|
|
|
+ # 处理传网关设备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:
|
|
else:
|
|
- start_time = int(request_dict.get('startTime', None))
|
|
|
|
- end_time = int(request_dict.get('endTime', None))
|
|
|
|
- repeat = int(request_dict.get('repeat', None))
|
|
|
|
- if not all([start_time, end_time, repeat]):
|
|
|
|
- return response.json(444, {'error param': 'startTime and endTime and repeat'})
|
|
|
|
- effective_time_qs = EffectiveTime.objects.filter(start_time=start_time, end_time=end_time,
|
|
|
|
- repeat=repeat).values('id')
|
|
|
|
- if effective_time_qs.exists():
|
|
|
|
- effective_time_id = effective_time_qs[0]['id']
|
|
|
|
- else:
|
|
|
|
- effective_time_id = EffectiveTime.objects.create(start_time=start_time, end_time=end_time,
|
|
|
|
- repeat=repeat).id
|
|
|
|
- smart_scene_dict['effective_time_id'] = effective_time_id
|
|
|
|
|
|
+ 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: # 没传时间
|
|
smart_scene_qs = SmartScene.objects.create(**smart_scene_dict)
|
|
smart_scene_qs = SmartScene.objects.create(**smart_scene_dict)
|
|
- 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[0].id,
|
|
|
|
- 'sensor_type': conditions_dict['sensor']['device_type'],
|
|
|
|
- 'sensor_src': int(sub_device_qs['src_addr'], 16),
|
|
|
|
- 'sensor_status': conditions_dict['sensor']['eventValues'][0]['value'],
|
|
|
|
- }
|
|
|
|
- task_list = []
|
|
|
|
- for task in tasks_list:
|
|
|
|
- sub_device_id = task['subDeviceId']
|
|
|
|
|
|
+ else:
|
|
|
|
+ if is_all_day == '0':
|
|
|
|
+ effective_time_qs = EffectiveTime.objects.filter(is_all_day=True).values('id')
|
|
|
|
+ if effective_time_qs.exists():
|
|
|
|
+ effective_time_id = effective_time_qs[0]['id']
|
|
|
|
+ else:
|
|
|
|
+ effective_time_id = EffectiveTime.objects.create(is_all_day=True).id
|
|
|
|
+ smart_scene_dict['effective_time_id'] = effective_time_id
|
|
|
|
+ SmartScene.objects.create(**smart_scene_dict)
|
|
|
|
+ else:
|
|
|
|
+ start_time = int(request_dict.get('startTime', None))
|
|
|
|
+ end_time = int(request_dict.get('endTime', None))
|
|
|
|
+ repeat = int(request_dict.get('repeat', None))
|
|
|
|
+ if not all([start_time, end_time, repeat]):
|
|
|
|
+ return response.json(444, {'error param': 'startTime and endTime and repeat'})
|
|
|
|
+ effective_time_qs = EffectiveTime.objects.filter(start_time=start_time, end_time=end_time,
|
|
|
|
+ repeat=repeat).values('id')
|
|
|
|
+ if effective_time_qs.exists():
|
|
|
|
+ effective_time_id = effective_time_qs[0]['id']
|
|
|
|
+ else:
|
|
|
|
+ effective_time_id = EffectiveTime.objects.create(start_time=start_time, end_time=end_time,
|
|
|
|
+ repeat=repeat).id
|
|
|
|
+ smart_scene_dict['effective_time_id'] = effective_time_id
|
|
|
|
+ smart_scene_qs = SmartScene.objects.create(**smart_scene_dict)
|
|
|
|
+ sub_device_id = conditions_dict['sensor']['subDeviceId']
|
|
sub_device_qs = GatewaySubDevice.objects.filter(id=sub_device_id).values('src_addr').first()
|
|
sub_device_qs = GatewaySubDevice.objects.filter(id=sub_device_id).values('src_addr').first()
|
|
- task_temp = {
|
|
|
|
- 'sensor_type': task['device_type'],
|
|
|
|
|
|
+ msg = {
|
|
|
|
+ 'smart_scene_id': smart_scene_qs.id,
|
|
|
|
+ 'sensor_type': conditions_dict['sensor']['device_type'],
|
|
'sensor_src': int(sub_device_qs['src_addr'], 16),
|
|
'sensor_src': int(sub_device_qs['src_addr'], 16),
|
|
- 'sensor_action': task['event_type']
|
|
|
|
|
|
+ 'sensor_status': conditions_dict['sensor']['eventValues'][0]['value'],
|
|
}
|
|
}
|
|
- task_list.append(task_temp)
|
|
|
|
- msg['task'] = task_list
|
|
|
|
- smart_scene_qs.update(device_data=json.dumps(msg))
|
|
|
|
- # 发布MQTT消息通知网关设备
|
|
|
|
- thing_name = serial_number
|
|
|
|
- topic_name = 'loocam/gateway_sensor/{}/smart_scene'.format(serial_number)
|
|
|
|
-
|
|
|
|
- success = CommonService.req_publish_mqtt_msg(thing_name, topic_name, msg)
|
|
|
|
- if not success:
|
|
|
|
- return response.json(10044)
|
|
|
|
|
|
+ task_list = []
|
|
|
|
+ for task in tasks_list:
|
|
|
|
+ task_temp = {
|
|
|
|
+ 'sensor_type': task['device_type'],
|
|
|
|
+ 'sensor_action': task['event_type']
|
|
|
|
+ }
|
|
|
|
+ if 'subDeviceId' in task:
|
|
|
|
+ sub_device_id = task['subDeviceId']
|
|
|
|
+ sub_device_qs = GatewaySubDevice.objects.filter(id=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
|
|
|
|
+ smart_scene_qs.update(device_data=json.dumps(msg))
|
|
|
|
+ # 发布MQTT消息通知网关设备
|
|
|
|
+ thing_name = serial_number
|
|
|
|
+ topic_name = 'loocam/gateway_sensor/{}/smart_scene'.format(serial_number)
|
|
|
|
+
|
|
|
|
+ success = CommonService.req_publish_mqtt_msg(thing_name, topic_name, msg)
|
|
|
|
+ if not success:
|
|
|
|
+ return response.json(10044)
|
|
|
|
|
|
return response.json(0)
|
|
return response.json(0)
|
|
except Exception as e:
|
|
except Exception as e:
|