|
@@ -12,7 +12,9 @@ from django.db import transaction
|
|
|
from django.db.models import F, Q, Count
|
|
|
from django.views import View
|
|
|
|
|
|
-from Ansjer.Config.gatewaySensorConfig import SMART_SCENE_TOPIC, SENSOR_TYPE, EVENT_TYPE
|
|
|
+from Ansjer.Config.gatewaySensorConfig import SMART_SCENE_TOPIC, SENSOR_TYPE, EVENT_TYPE, SCENE_EVENT_CREATE, \
|
|
|
+ SCENE_EVENT_EDIT, SCENE_EVENT_DELETE, SCENE_STATUS_ON, SCENE_STATUS_OFF, SCENE_EVENT_EDIT_STATUS, \
|
|
|
+ VOICE_AUDITION_TOPIC
|
|
|
from Model.models import FamilyRoomDevice, GatewaySubDevice, FamilyRoom, SmartScene, EffectiveTime, Device_Info, \
|
|
|
SceneLog
|
|
|
from Object.ResponseObject import ResponseObject
|
|
@@ -58,6 +60,8 @@ class SmartSceneView(View):
|
|
|
return self.scene_log(request_dict, response)
|
|
|
elif operation == 'log-date': # 查询智能场景日志日期
|
|
|
return self.scene_log_date(request_dict, response)
|
|
|
+ elif operation == 'voice-audition': # 智能场景音频试听
|
|
|
+ return self.voice_audition(request_dict, response)
|
|
|
else:
|
|
|
return response.json(414)
|
|
|
|
|
@@ -84,7 +88,7 @@ class SmartSceneView(View):
|
|
|
res = cls.get_sub_device_room_name(gateway_sub_device_qs)
|
|
|
return response.json(0, res)
|
|
|
except Exception as e:
|
|
|
- return response.json(500, repr(e))
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
@classmethod
|
|
|
def task_devices(cls, request_dict, response):
|
|
@@ -114,8 +118,9 @@ class SmartSceneView(View):
|
|
|
else: # 智能按钮返回网关,门磁和人体传感器(如果存在)
|
|
|
gateway_data = cls.get_gateway_data(device_id)
|
|
|
sub_device_qs = GatewaySubDevice.objects.filter(
|
|
|
- Q(device_id=device_id) & Q(device_type=SENSOR_TYPE['door_magnet']) | Q(
|
|
|
- device_type=SENSOR_TYPE['body_sensor'])).values('id', 'nickname', 'status', 'device_type')
|
|
|
+ Q(Q(device_id=device_id) & Q(device_type=SENSOR_TYPE['door_magnet'])) |
|
|
|
+ Q(Q(device_id=device_id) & Q(device_type=SENSOR_TYPE['body_sensor']))
|
|
|
+ ).values('id', 'nickname', 'status', 'device_type')
|
|
|
if sub_device_qs.exists():
|
|
|
res = cls.get_sub_device_room_name(sub_device_qs, gateway_data)
|
|
|
else:
|
|
@@ -123,7 +128,7 @@ class SmartSceneView(View):
|
|
|
|
|
|
return response.json(0, res)
|
|
|
except Exception as e:
|
|
|
- return response.json(500, repr(e))
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
@staticmethod
|
|
|
def get_sub_device_room_name(sub_device_qs, gateway_data=None):
|
|
@@ -161,13 +166,15 @@ class SmartSceneView(View):
|
|
|
@param device_id: 网关设备id
|
|
|
@return: res
|
|
|
"""
|
|
|
- device_info_qs = Device_Info.objects.filter(id=device_id).values('NickName', 'Type')
|
|
|
+ device_info_qs = Device_Info.objects.filter(id=device_id).values('NickName', 'Type', 'serial_number')
|
|
|
nickname = device_info_qs[0]['NickName']
|
|
|
device_type = device_info_qs[0]['Type']
|
|
|
+ serial_number = device_info_qs[0]['serial_number']
|
|
|
room_id = FamilyRoomDevice.objects.filter(device_id=device_id).values('room_id')[0]['room_id']
|
|
|
room_id_qs = FamilyRoom.objects.filter(id=room_id).values('name')
|
|
|
room_name = room_id_qs.first()['name'] if room_id_qs.exists() else ''
|
|
|
res = {
|
|
|
+ 'serialNumber': serial_number,
|
|
|
'deviceNickName': nickname,
|
|
|
'deviceType': device_type,
|
|
|
'roomName': room_name,
|
|
@@ -175,8 +182,8 @@ class SmartSceneView(View):
|
|
|
}
|
|
|
return res
|
|
|
|
|
|
- @staticmethod
|
|
|
- def create_smart_scene(request_dict, user_id, response):
|
|
|
+ @classmethod
|
|
|
+ def create_smart_scene(cls, request_dict, user_id, response):
|
|
|
"""
|
|
|
创建智能场景
|
|
|
@param request_dict: 请求参数
|
|
@@ -222,7 +229,8 @@ class SmartSceneView(View):
|
|
|
'updated_time': now_time,
|
|
|
}
|
|
|
msg = {
|
|
|
- 'scene_status': 1
|
|
|
+ 'scene_event': SCENE_EVENT_CREATE,
|
|
|
+ 'scene_status': SCENE_STATUS_ON
|
|
|
}
|
|
|
# 处理设置时间
|
|
|
if is_all_day is not None:
|
|
@@ -238,10 +246,19 @@ class SmartSceneView(View):
|
|
|
if not device_info_qs.exists():
|
|
|
return response.json(173)
|
|
|
serial_number = device_info_qs[0]['serial_number']
|
|
|
+
|
|
|
+ # 网关数据
|
|
|
+ msg['sensor_type'] = 200
|
|
|
+ msg['sensor_status'] = 2002
|
|
|
+ msg['sensor_ieee_addr'] = 'FFFFFFFFFFFFFFFF'
|
|
|
+
|
|
|
else: # 子设备设置场景
|
|
|
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']:
|
|
@@ -265,13 +282,13 @@ class SmartSceneView(View):
|
|
|
|
|
|
smart_scene_dict['sub_device_id'] = sub_device_id
|
|
|
sub_device_qs = GatewaySubDevice.objects.filter(id=sub_device_id).values('device__serial_number',
|
|
|
- 'src_addr')
|
|
|
+ 'ieee_addr')
|
|
|
if not sub_device_qs.exists():
|
|
|
return response.json(173)
|
|
|
serial_number = sub_device_qs[0]['device__serial_number']
|
|
|
|
|
|
msg['sensor_type'] = int(conditions_dict['sensor']['device_type'])
|
|
|
- msg['sensor_src'] = int(sub_device_qs[0]['src_addr'], 16)
|
|
|
+ msg['sensor_ieee_addr'] = sub_device_qs[0]['ieee_addr']
|
|
|
msg['sensor_status'] = int(conditions_dict['sensor']['eventValues'][0]['event_type'])
|
|
|
|
|
|
with transaction.atomic():
|
|
@@ -313,24 +330,10 @@ class SmartSceneView(View):
|
|
|
return response.json(444, {'error param': 'invalid isAllDay'})
|
|
|
|
|
|
msg['time'] = time_dict
|
|
|
- msg['smart_scene_id'] = smart_scene_qs.id
|
|
|
- task_list = []
|
|
|
- for task in tasks_list:
|
|
|
- task_temp = {
|
|
|
- 'sensor_type': int(task['device_type']),
|
|
|
- 'sensor_action': int(task['event_type'])
|
|
|
- }
|
|
|
-
|
|
|
- # 延时
|
|
|
- if 'delay_time' in task and task['delay_time'] != 0:
|
|
|
- task_temp['sensor_delay'] = task['delay_time']
|
|
|
+ msg['scene_id'] = smart_scene_qs.id
|
|
|
|
|
|
- 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_temp['sensor_src'] = int(sub_device_qs['src_addr'], 16)
|
|
|
- task_list.append(task_temp)
|
|
|
- msg['task'] = task_list
|
|
|
+ # 获取设备任务数据
|
|
|
+ msg['task'] = cls.get_msg_task_list(tasks_list)
|
|
|
|
|
|
smart_scene_qs.device_data = json.dumps(msg)
|
|
|
smart_scene_qs.save()
|
|
@@ -346,7 +349,7 @@ class SmartSceneView(View):
|
|
|
|
|
|
return response.json(0)
|
|
|
except Exception as e:
|
|
|
- return response.json(500, repr(e))
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
@staticmethod
|
|
|
def scene_list(request_dict, user_id, response):
|
|
@@ -394,7 +397,7 @@ class SmartSceneView(View):
|
|
|
smart_scene_list.append(smart_scene_dict)
|
|
|
return response.json(0, smart_scene_list)
|
|
|
except Exception as e:
|
|
|
- return response.json(500, repr(e))
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
@staticmethod
|
|
|
def smart_button_scene_list(request_dict, user_id, response):
|
|
@@ -447,7 +450,7 @@ class SmartSceneView(View):
|
|
|
})
|
|
|
return response.json(0, scene_list)
|
|
|
except Exception as e:
|
|
|
- return response.json(500, repr(e))
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
@staticmethod
|
|
|
def update_status(request_dict, response):
|
|
@@ -466,8 +469,9 @@ class SmartSceneView(View):
|
|
|
return response.json(444, {'error param': 'smartSceneId and status'})
|
|
|
try:
|
|
|
smart_scene_id = int(smart_scene_id)
|
|
|
- scene_status = 1 if is_enable == 'True' else 0
|
|
|
+ scene_status = SCENE_STATUS_ON if is_enable == 'True' else SCENE_STATUS_OFF
|
|
|
msg = {
|
|
|
+ 'scene_event': SCENE_EVENT_EDIT_STATUS,
|
|
|
'scene_id': smart_scene_id,
|
|
|
'scene_status': scene_status
|
|
|
}
|
|
@@ -493,7 +497,7 @@ class SmartSceneView(View):
|
|
|
return response.json(10044)
|
|
|
return response.json(0)
|
|
|
except Exception as e:
|
|
|
- return response.json(500, repr(e))
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
@staticmethod
|
|
|
def scene_detail(request_dict, response):
|
|
@@ -537,10 +541,10 @@ class SmartSceneView(View):
|
|
|
return response.json(0, res)
|
|
|
|
|
|
except Exception as e:
|
|
|
- return response.json(500, repr(e))
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
- @staticmethod
|
|
|
- def edit_smart_scene(request_dict, user_id, response):
|
|
|
+ @classmethod
|
|
|
+ def edit_smart_scene(cls, request_dict, user_id, response):
|
|
|
"""
|
|
|
编辑智能场景
|
|
|
@param request_dict: 请求参数
|
|
@@ -585,13 +589,17 @@ class SmartSceneView(View):
|
|
|
|
|
|
scene_status = 1 if smart_scene_qs[0].is_enable else 0
|
|
|
msg = {
|
|
|
- 'smart_scene_id': smart_scene_id,
|
|
|
+ 'scene_id': smart_scene_id,
|
|
|
+ 'scene_event': SCENE_EVENT_EDIT,
|
|
|
'scene_status': scene_status
|
|
|
}
|
|
|
if conditions_dict['type'] == 2: # 条件为选择子设备
|
|
|
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, smart_scene_id):
|
|
|
+ return response.json(182)
|
|
|
+
|
|
|
device_type = int(conditions_dict['sensor']['device_type'])
|
|
|
# 智能按钮不能创建触发条件相同的场景
|
|
|
if device_type == SENSOR_TYPE['smart_button']:
|
|
@@ -615,13 +623,13 @@ class SmartSceneView(View):
|
|
|
msg['sensor_data'] = float(value)
|
|
|
|
|
|
device_id = ''
|
|
|
- sub_device_qs = GatewaySubDevice.objects.filter(id=sub_device_id).values('src_addr',
|
|
|
+ sub_device_qs = GatewaySubDevice.objects.filter(id=sub_device_id).values('ieee_addr',
|
|
|
'device__serial_number')
|
|
|
if not sub_device_qs.exists():
|
|
|
return response.json(173)
|
|
|
serial_number = sub_device_qs[0]['device__serial_number']
|
|
|
msg['sensor_type'] = int(conditions_dict['sensor']['device_type'])
|
|
|
- msg['sensor_src'] = int(sub_device_qs[0]['src_addr'], 16)
|
|
|
+ msg['sensor_ieee_addr'] = sub_device_qs[0]['ieee_addr']
|
|
|
msg['sensor_status'] = int(conditions_dict['sensor']['eventValues'][0]['event_type'])
|
|
|
else:
|
|
|
if not device_id:
|
|
@@ -631,24 +639,13 @@ class SmartSceneView(View):
|
|
|
if not device_qs.exists():
|
|
|
return response.json(173)
|
|
|
serial_number = device_qs[0]['serial_number']
|
|
|
+ # 网关数据
|
|
|
+ msg['sensor_type'] = 200
|
|
|
+ msg['sensor_status'] = 2002
|
|
|
+ msg['sensor_ieee_addr'] = 'FFFFFFFFFFFFFFFF'
|
|
|
|
|
|
- task_list = []
|
|
|
- for task in tasks_list:
|
|
|
- task_temp = {
|
|
|
- 'sensor_type': int(task['device_type']),
|
|
|
- 'sensor_action': int(task['event_type'])
|
|
|
- }
|
|
|
-
|
|
|
- # 延时
|
|
|
- if 'delay_time' in task and task['delay_time'] != 0:
|
|
|
- task_temp['sensor_delay'] = task['delay_time']
|
|
|
-
|
|
|
- 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
|
|
|
+ # 获取设备任务数据
|
|
|
+ msg['task'] = cls.get_msg_task_list(tasks_list)
|
|
|
|
|
|
with transaction.atomic():
|
|
|
smart_scene_qs.update(scene_name=scene_name, conditions=conditions, tasks=tasks,
|
|
@@ -705,7 +702,7 @@ class SmartSceneView(View):
|
|
|
return response.json(0, res)
|
|
|
|
|
|
except Exception as e:
|
|
|
- return response.json(500, repr(e))
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
@staticmethod
|
|
|
def delete_smart_scene(request_dict, response):
|
|
@@ -737,7 +734,8 @@ class SmartSceneView(View):
|
|
|
for smart_scene_id in smart_scene_id_list:
|
|
|
# 通知设备删除场景id
|
|
|
msg = {
|
|
|
- 'smart_scene_delete': int(smart_scene_id)
|
|
|
+ 'scene_event': SCENE_EVENT_DELETE,
|
|
|
+ 'scene_id': int(smart_scene_id)
|
|
|
}
|
|
|
success = CommonService.req_publish_mqtt_msg(serial_number, topic_name, msg)
|
|
|
try:
|
|
@@ -747,7 +745,7 @@ class SmartSceneView(View):
|
|
|
time.sleep(0.3)
|
|
|
return response.json(0)
|
|
|
except Exception as e:
|
|
|
- return response.json(500, repr(e))
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
@staticmethod
|
|
|
def scene_log(request_dict, response):
|
|
@@ -783,17 +781,19 @@ class SmartSceneView(View):
|
|
|
device_list.append(device['device'])
|
|
|
if device['sub_device']:
|
|
|
sub_device_list.append(device['sub_device'])
|
|
|
- elif sub_device_id:
|
|
|
- family_room_device_qs = FamilyRoomDevice.objects.filter(family_id=family_id, sub_device=sub_device_id)
|
|
|
+ elif sub_device_id: # 查询子设备
|
|
|
+ family_room_device_qs = FamilyRoomDevice.objects.filter(family_id=family_id,
|
|
|
+ sub_device=sub_device_id).values('device_id')
|
|
|
+ for device in family_room_device_qs:
|
|
|
+ device_list.append(device['device_id'])
|
|
|
sub_device_list.append(sub_device_id)
|
|
|
- else:
|
|
|
- family_room_device_qs = FamilyRoomDevice.objects.filter(family_id=family_id, device=device_id).values(
|
|
|
+ else: # 查询网关
|
|
|
+ family_room_device_qs = FamilyRoomDevice.objects.filter(Q(family_id=family_id) & Q(device=device_id) &
|
|
|
+ ~Q(sub_device=0)).values(
|
|
|
'sub_device')
|
|
|
device_list.append(device_id)
|
|
|
for device in family_room_device_qs:
|
|
|
sub_device_list.append(device['sub_device'])
|
|
|
- if not family_room_device_qs.exists():
|
|
|
- return response.json(173)
|
|
|
try:
|
|
|
page, size = int(page), int(size)
|
|
|
scene_log_qs = SceneLog.objects.filter(Q(device_id__in=device_list) | Q(sub_device_id__in=sub_device_list))
|
|
@@ -828,11 +828,13 @@ class SmartSceneView(View):
|
|
|
else:
|
|
|
device_qs = Device_Info.objects.filter(id=item['device_id']).values('Type')
|
|
|
item['device_type'] = device_qs[0]['Type'] if device_qs.exists() else ''
|
|
|
- item['tasks'] = eval(item['tasks'])
|
|
|
- return response.json(0, list(scene_log_qs))
|
|
|
+ if item['tasks'] != '':
|
|
|
+ item['tasks'] = eval(item['tasks'])
|
|
|
+ scene_log_list = list(scene_log_qs)
|
|
|
+ return response.json(0, scene_log_list)
|
|
|
except Exception as e:
|
|
|
- print(repr(e))
|
|
|
- return response.json(500, repr(e))
|
|
|
+ print('error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
@staticmethod
|
|
|
def scene_log_date(request_dict, response):
|
|
@@ -886,7 +888,7 @@ class SmartSceneView(View):
|
|
|
})
|
|
|
return response.json(0, log_date_list)
|
|
|
except Exception as e:
|
|
|
- return response.json(500, repr(e))
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
@staticmethod
|
|
|
def get_scene_data(request_dict, response):
|
|
@@ -931,14 +933,14 @@ class SmartSceneView(View):
|
|
|
# 下发智能按钮数据
|
|
|
smart_button_qs = GatewaySubDevice.objects.filter(device_id=device_id,
|
|
|
device_type=SENSOR_TYPE['smart_button']).values(
|
|
|
- 'src_addr', 'is_tampered')
|
|
|
+ 'ieee_addr', 'is_tampered')
|
|
|
if smart_button_qs.exists():
|
|
|
sos_count = smart_button_qs.count()
|
|
|
for index, smart_button in enumerate(smart_button_qs):
|
|
|
msg = {
|
|
|
'sos_count': sos_count, # 该网关下的智能按钮数量
|
|
|
'sos_num': index + 1, # 第几个按钮
|
|
|
- 'sensor_src': int(smart_button['src_addr'], 16),
|
|
|
+ 'sensor_ieee_addr': smart_button['ieee_addr'],
|
|
|
'sos_select': smart_button['is_tampered']
|
|
|
}
|
|
|
success = CommonService.req_publish_mqtt_msg(serial_number, topic_name, msg)
|
|
@@ -950,7 +952,133 @@ class SmartSceneView(View):
|
|
|
|
|
|
return response.json(0)
|
|
|
except Exception as e:
|
|
|
- return response.json(500, repr(e))
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def voice_audition(request_dict, response):
|
|
|
+ """
|
|
|
+ 智能场景音频试听
|
|
|
+ @param request_dict: 请求参数
|
|
|
+ @request_dict serial_number: 序列号
|
|
|
+ @request_dict voiceId: 音频id
|
|
|
+ @param response: 响应对象
|
|
|
+ @return: response
|
|
|
+ """
|
|
|
+ serial_number = request_dict.get('serial_number', None)
|
|
|
+ voice_id = request_dict.get('voiceId', None)
|
|
|
+ if not all([serial_number, voice_id]):
|
|
|
+ return response.json(444)
|
|
|
+ try:
|
|
|
+ topic_name = VOICE_AUDITION_TOPIC.format(serial_number)
|
|
|
+ msg = {'voice_id': int(voice_id)}
|
|
|
+ success = CommonService.req_publish_mqtt_msg(serial_number, topic_name, msg)
|
|
|
+ try:
|
|
|
+ assert success
|
|
|
+ except AssertionError:
|
|
|
+ return response.json(10044)
|
|
|
+ return response.json(0)
|
|
|
+ except Exception as e:
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def get_msg_task_list(tasks_list):
|
|
|
+ """
|
|
|
+ 获取设备任务数据
|
|
|
+ @param tasks_list: app任务列表
|
|
|
+ @return: task_list
|
|
|
+ """
|
|
|
+ task_list = []
|
|
|
+ for task in tasks_list:
|
|
|
+ sensor_type = int(task['device_type'])
|
|
|
+ task_temp = {
|
|
|
+ 'sensor_type': sensor_type,
|
|
|
+ 'sensor_delay': 0
|
|
|
+ }
|
|
|
+
|
|
|
+ # 延时
|
|
|
+ if 'delay_time' in task and task['delay_time'] != 0:
|
|
|
+ task_temp['sensor_delay'] = task['delay_time']
|
|
|
+
|
|
|
+ # 不为-1时需要其他数据
|
|
|
+ if sensor_type != -1:
|
|
|
+ task_temp['sensor_action'] = int(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('ieee_addr').first()
|
|
|
+ task_temp['sensor_ieee_addr'] = sub_device_qs['ieee_addr']
|
|
|
+ # 网关添加报警类型数据
|
|
|
+ else:
|
|
|
+ task_temp['voice_type'] = task.get('voice_type')
|
|
|
+ task_temp['voice_id'] = task.get('voice_id')
|
|
|
+ task_temp['count'] = task.get('count')
|
|
|
+ task_temp['delay_time'] = task.get('delay_time')
|
|
|
+ task_temp['duration'] = task.get('duration')
|
|
|
+ task_temp['value_type'] = task.get('value_type')
|
|
|
+ task_list.append(task_temp)
|
|
|
+ return task_list
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ 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: 不冲突
|
|
|
+ """
|
|
|
+ # 不设置时间不会冲突
|
|
|
+ if is_all_day is None:
|
|
|
+ return False
|
|
|
+
|
|
|
+ # 查询设置过时间的数据
|
|
|
+ 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
|
|
|
+ # 非全天
|
|
|
+ elif is_all_day == 2:
|
|
|
+ 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_qs = EffectiveTime.objects.filter(id=effective_time_id).\
|
|
|
+ values('start_time', 'end_time', 'repeat')
|
|
|
+ if effective_time_qs.exists():
|
|
|
+ old_start_time = effective_time_qs[0]['start_time']
|
|
|
+ old_end_time = effective_time_qs[0]['end_time']
|
|
|
+ old_repeat = effective_time_qs[0]['repeat']
|
|
|
+ # 每天重复
|
|
|
+ if repeat == 127:
|
|
|
+ # 判断时间是否在已设置过的时间范围之内
|
|
|
+ if old_start_time <= start_time <= old_end_time or \
|
|
|
+ old_start_time <= end_time <= old_end_time:
|
|
|
+ return True
|
|
|
+ else:
|
|
|
+ # 有相同的重复天
|
|
|
+ if repeat & old_repeat != 0:
|
|
|
+ # 判断时间是否在已设置过的时间范围之内
|
|
|
+ if old_start_time <= start_time <= old_end_time or \
|
|
|
+ old_start_time <= end_time <= old_end_time:
|
|
|
+ return True
|
|
|
+ return False
|
|
|
|
|
|
#
|
|
|
# ___====-_ _-====___
|