|
@@ -9,9 +9,10 @@ import time
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
|
|
from django.views import View
|
|
|
|
|
|
-from Model.models import FamilyRoomDevice, GatewaySubDevice, FamilyRoom, SmartScene, EffectiveTime
|
|
|
+from Model.models import FamilyRoomDevice, GatewaySubDevice, FamilyRoom, SmartScene, EffectiveTime, Device_Info
|
|
|
from Object.ResponseObject import ResponseObject
|
|
|
from Object.TokenObject import TokenObject
|
|
|
+from Service.CommonService import CommonService
|
|
|
|
|
|
|
|
|
class SmartSceneView(View):
|
|
@@ -39,7 +40,16 @@ class SmartSceneView(View):
|
|
|
return self.task_devices(request_dict, user_id, response)
|
|
|
elif operation == 'create': # 创建智能场景
|
|
|
return self.create_smart_scene(request_dict, user_id, response)
|
|
|
-
|
|
|
+ elif operation == 'scene-list': # 查询智能场景列表
|
|
|
+ return self.scene_list(request_dict, user_id, response)
|
|
|
+ elif operation == 'update-status': # 更新智能场景状态
|
|
|
+ return self.update_status(request_dict, response)
|
|
|
+ elif operation == 'detail': # 查询智能场景详情
|
|
|
+ return self.scene_detail(request_dict, response)
|
|
|
+ elif operation == 'edit': # 编辑智能场景
|
|
|
+ return self.edit_smart_scene(request_dict, response)
|
|
|
+ elif operation == 'delete': # 删除智能场景
|
|
|
+ return self.delete_smart_scene(request_dict, response)
|
|
|
else:
|
|
|
return response.json(414)
|
|
|
|
|
@@ -58,21 +68,24 @@ class SmartSceneView(View):
|
|
|
if not any([device_id, sub_device_id]):
|
|
|
return response.json(444, {'error param': 'deviceId or subDeviceId'})
|
|
|
try:
|
|
|
- if device_id:
|
|
|
- gateway_sub_device_qs = GatewaySubDevice.objects.filter(device_id=device_id)
|
|
|
- else:
|
|
|
- gateway_sub_device_qs = GatewaySubDevice.objects.filter(id=sub_device_id)
|
|
|
+ if sub_device_id:
|
|
|
+ device_id = GatewaySubDevice.objects.get(id=sub_device_id).device_id
|
|
|
+ gateway_sub_device_qs = GatewaySubDevice.objects.filter(device_id=device_id)
|
|
|
if not gateway_sub_device_qs.exists():
|
|
|
return response.json(173)
|
|
|
gateway_sub_device_qs = gateway_sub_device_qs.values('id', 'device_type', 'nickname', 'status')
|
|
|
sub_device_list = []
|
|
|
for gateway_sub_device in gateway_sub_device_qs:
|
|
|
- room_id = FamilyRoomDevice.objects.filter(sub_device=gateway_sub_device['id']).values('room_id')[0][
|
|
|
- 'room_id']
|
|
|
- try:
|
|
|
- gateway_sub_device['room_name'] = FamilyRoom.objects.get(id=room_id).name
|
|
|
- except ObjectDoesNotExist:
|
|
|
+ family_room_device_qs = FamilyRoomDevice.objects.filter(sub_device=gateway_sub_device['id']).\
|
|
|
+ values('room_id')
|
|
|
+ if not family_room_device_qs.exists():
|
|
|
gateway_sub_device['room_name'] = ''
|
|
|
+ else:
|
|
|
+ room_id = family_room_device_qs[0]['room_id']
|
|
|
+ try:
|
|
|
+ gateway_sub_device['room_name'] = FamilyRoom.objects.get(id=room_id).name
|
|
|
+ except ObjectDoesNotExist:
|
|
|
+ gateway_sub_device['room_name'] = ''
|
|
|
gateway_sub_device.pop('id')
|
|
|
sub_device_list.append(gateway_sub_device)
|
|
|
return response.json(0, sub_device_list)
|
|
@@ -80,7 +93,7 @@ class SmartSceneView(View):
|
|
|
return response.json(500, repr(e))
|
|
|
|
|
|
@staticmethod
|
|
|
- def task_devices(request_dict, response):
|
|
|
+ def task_devices(request_dict, user_id, response):
|
|
|
"""
|
|
|
添加任务-查询设备
|
|
|
@param request_dict: 请求参数
|
|
@@ -93,34 +106,9 @@ class SmartSceneView(View):
|
|
|
|
|
|
if not all([device_id]):
|
|
|
return response.json(444)
|
|
|
-
|
|
|
- device_qs = FamilyRoomDevice.objects.filter(device_id=device_id,sub_device=0)
|
|
|
- if not device_qs.exists():
|
|
|
- return response.json(173)
|
|
|
- gate_way = GatewaySubDevice.objects.filter(device_id=device_id)
|
|
|
- if not gate_way.exists():
|
|
|
- return response.json(173)
|
|
|
try:
|
|
|
- gate_way = gate_way.values('id', 'device_type', 'nickname', 'status')
|
|
|
- gate_way_list = []
|
|
|
- for gate_int in gate_way:
|
|
|
- gate_way.exists()
|
|
|
- gate_way_list.append(gate_int)
|
|
|
- device_qs = device_qs.values('family_id', 'device_id', 'room_id', 'device__Type', 'device__NickName',
|
|
|
- 'device__UID')
|
|
|
- device_qs_list = []
|
|
|
- for device_int in device_qs:
|
|
|
- device_qs.exists()
|
|
|
- device_qs_list.append(device_int)
|
|
|
- device_qs = device_qs.first()
|
|
|
- room_id = device_qs['room_id']
|
|
|
- room_qs = FamilyRoom.objects.filter(id=room_id).values('name')
|
|
|
- room_qs_list = []
|
|
|
- for room_int in room_qs:
|
|
|
- room_qs.exists()
|
|
|
- room_qs_list.append(room_int)
|
|
|
- repe = {'device_qs_list': device_qs_list, 'gate_way_list': gate_way_list, 'room_qs_list': room_qs_list}
|
|
|
- return response.json(0, repe)
|
|
|
+ device_info_qs = GatewaySubDevice.objects.filter(device__userID_id=user_id)
|
|
|
+ return response.json(0)
|
|
|
except Exception as e:
|
|
|
return response.json(500, repr(e))
|
|
|
|
|
@@ -131,7 +119,7 @@ class SmartSceneView(View):
|
|
|
@param request_dict: 请求参数
|
|
|
@param user_id: 用户id
|
|
|
@request_dict deviceId: 网关设备id
|
|
|
- @request_dict gatewaySubId: 子设备id
|
|
|
+ @request_dict subDeviceId: 子设备id
|
|
|
@request_dict sceneName: 场景名称
|
|
|
@request_dict conditions: 条件
|
|
|
@request_dict tasks: 任务
|
|
@@ -143,7 +131,7 @@ class SmartSceneView(View):
|
|
|
@return: response
|
|
|
"""
|
|
|
device_id = request_dict.get('deviceId', None)
|
|
|
- sub_device_id = request_dict.get('gatewaySubId', 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)
|
|
@@ -169,27 +157,189 @@ class SmartSceneView(View):
|
|
|
'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
|
|
|
- if is_all_day:
|
|
|
+ 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: # 没传时间
|
|
|
SmartScene.objects.create(**smart_scene_dict)
|
|
|
else:
|
|
|
- start_time = request_dict.get('startTime', None)
|
|
|
- end_time = request_dict.get('endTime', None)
|
|
|
- repeat = 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']
|
|
|
+ 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:
|
|
|
- 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
|
|
|
- SmartScene.objects.create(**smart_scene_dict)
|
|
|
+ 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
|
|
|
+ SmartScene.objects.create(**smart_scene_dict)
|
|
|
+
|
|
|
+ # 发布MQTT消息通知网关设备
|
|
|
+ thing_name = serial_number
|
|
|
+ topic_name = 'loocam/gateway_sensor/{}/smart_scene'.format(serial_number)
|
|
|
+ msg = ''
|
|
|
+ success = CommonService.req_publish_mqtt_msg(thing_name, topic_name, msg)
|
|
|
+ if not success:
|
|
|
+ return response.json(10044)
|
|
|
+
|
|
|
+ return response.json(0)
|
|
|
+ except Exception as e:
|
|
|
+ return response.json(500, repr(e))
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def scene_list(request_dict, user_id, response):
|
|
|
+ """
|
|
|
+ 查询智能场景列表
|
|
|
+ @param request_dict: 请求参数
|
|
|
+ @param user_id: 用户id
|
|
|
+ @request_dict deviceId: 网关设备id
|
|
|
+ @request_dict subDeviceId: 子设备id
|
|
|
+ @param response: 响应对象
|
|
|
+ @return: response
|
|
|
+ """
|
|
|
+ device_id = request_dict.get('deviceId', None)
|
|
|
+ sub_device_id = request_dict.get('subDeviceId', None)
|
|
|
+
|
|
|
+ if not any([device_id, sub_device_id]):
|
|
|
+ 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)
|
|
|
+ else:
|
|
|
+ smart_scene_qs = SmartScene.objects.filter(user_id=user_id, sub_device_id=sub_device_id)
|
|
|
+ if not smart_scene_qs.exists():
|
|
|
+ return response.json(173)
|
|
|
+ smart_scene_qs = smart_scene_qs.values('id', 'scene_name', 'is_enable')
|
|
|
+ return response.json(0, list(smart_scene_qs))
|
|
|
+ except Exception as e:
|
|
|
+ return response.json(500, repr(e))
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def update_status(request_dict, response):
|
|
|
+ """
|
|
|
+ 更新智能场景状态
|
|
|
+ @param request_dict: 请求参数
|
|
|
+ @request_dict smartSceneId: 智能场景id
|
|
|
+ @request_dict isEnable: 状态,True or False
|
|
|
+ @param response: 响应对象
|
|
|
+ @return: response
|
|
|
+ """
|
|
|
+ smart_scene_id = request_dict.get('smartSceneId', None)
|
|
|
+ is_enable = request_dict.get('isEnable', None)
|
|
|
+
|
|
|
+ if not all([smart_scene_id, is_enable]):
|
|
|
+ return response.json(444, {'error param': 'smartSceneId and status'})
|
|
|
+ try:
|
|
|
+ SmartScene.objects.filter(id=smart_scene_id).update(is_enable=is_enable)
|
|
|
+ return response.json(0)
|
|
|
+ except Exception as e:
|
|
|
+ return response.json(500, repr(e))
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def scene_detail(request_dict, response):
|
|
|
+ """
|
|
|
+ 查询智能场景详情
|
|
|
+ @param request_dict: 请求参数
|
|
|
+ @request_dict smartSceneId: 智能场景id
|
|
|
+ @param response: 响应对象
|
|
|
+ @return: response
|
|
|
+ """
|
|
|
+ smart_scene_id = request_dict.get('smartSceneId', None)
|
|
|
+
|
|
|
+ if not smart_scene_id:
|
|
|
+ return response.json(444, {'error param': 'smartSceneId'})
|
|
|
+ try:
|
|
|
+ smart_scene_qs = SmartScene.objects.filter(id=smart_scene_id).values('id', 'scene_name', 'conditions',
|
|
|
+ 'tasks', 'effective_time_id')
|
|
|
+ if not smart_scene_qs.exists():
|
|
|
+ return response.json(173)
|
|
|
+ res = {
|
|
|
+ 'scene_name': smart_scene_qs[0]['scene_name'],
|
|
|
+ 'condition': eval(smart_scene_qs[0]['conditions']),
|
|
|
+ 'task': eval(smart_scene_qs[0]['tasks']),
|
|
|
+ }
|
|
|
+
|
|
|
+ # 如果存在关联的时间数据,组织时间数据
|
|
|
+ try:
|
|
|
+ effective_time_qs = EffectiveTime.objects.get(id=smart_scene_qs[0]['effective_time_id'])
|
|
|
+ except ObjectDoesNotExist:
|
|
|
+ return response.json(0, res)
|
|
|
+
|
|
|
+ if effective_time_qs.is_all_day: # 全天
|
|
|
+ time_dict = {'all_day': 1}
|
|
|
+ else:
|
|
|
+ time_dict = {
|
|
|
+ 'start_time': effective_time_qs.start_time,
|
|
|
+ 'end_time': effective_time_qs.end_time,
|
|
|
+ 'repeat': effective_time_qs.repeat,
|
|
|
+ }
|
|
|
+ res['time'] = time_dict
|
|
|
+ return response.json(0, res)
|
|
|
+ except Exception as e:
|
|
|
+ return response.json(500, repr(e))
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def edit_smart_scene(request_dict, response):
|
|
|
+ """
|
|
|
+ 编辑智能场景
|
|
|
+ @param request_dict: 请求参数
|
|
|
+ @request_dict smartSceneId: 智能场景id
|
|
|
+ @param response: 响应对象
|
|
|
+ @return: response
|
|
|
+ """
|
|
|
+ smart_scene_id = request_dict.get('smartSceneId', None)
|
|
|
+
|
|
|
+ if not smart_scene_id:
|
|
|
+ return response.json(444, {'error param': 'smartSceneId'})
|
|
|
+ try:
|
|
|
+ smart_scene_qs = SmartScene.objects.filter(id=smart_scene_id).values('id', 'scene_name', 'conditions',
|
|
|
+ 'tasks', 'effective_time_id')
|
|
|
+ if not smart_scene_qs.exists():
|
|
|
+ return response.json(173)
|
|
|
+ return response.json(0, list(smart_scene_qs))
|
|
|
+ except Exception as e:
|
|
|
+ return response.json(500, repr(e))
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def delete_smart_scene(request_dict, response):
|
|
|
+ """
|
|
|
+ 删除智能场景
|
|
|
+ @param request_dict: 请求参数
|
|
|
+ @request_dict smartSceneId: 智能场景id
|
|
|
+ @param response: 响应对象
|
|
|
+ @return: response
|
|
|
+ """
|
|
|
+ smart_scene_id = request_dict.get('smartSceneId', None)
|
|
|
+
|
|
|
+ if not smart_scene_id:
|
|
|
+ return response.json(444, {'error param': 'smartSceneId'})
|
|
|
+ try:
|
|
|
+ SmartScene.objects.filter(id=smart_scene_id).delete()
|
|
|
return response.json(0)
|
|
|
except Exception as e:
|
|
|
return response.json(500, repr(e))
|