|
@@ -272,8 +272,15 @@ class SmartSceneView(View):
|
|
|
is_all_day = int(is_all_day)
|
|
|
smart_scene_dict['is_all_day'] = is_all_day
|
|
|
|
|
|
- # 处理传网关设备id和子设备id的情况
|
|
|
- if conditions_dict['type'] == 1: # 网关设置时间
|
|
|
+ # 判断条件是否为设置时间
|
|
|
+ is_set_time, minutes, repeat = False, 0, 0
|
|
|
+ if conditions_dict['type'] == 1:
|
|
|
+ is_set_time = True
|
|
|
+ minutes = conditions_dict['time']['minutes']
|
|
|
+ repeat = conditions_dict['time']['repeat']
|
|
|
+
|
|
|
+ # 条件为设置时间
|
|
|
+ if is_set_time:
|
|
|
if not device_id:
|
|
|
return response.json(444, {'error param': 'deviceId'})
|
|
|
smart_scene_dict['device_id'] = device_id
|
|
@@ -287,7 +294,8 @@ class SmartSceneView(View):
|
|
|
msg['sensor_status'] = 2002
|
|
|
msg['sensor_ieee_addr'] = 'FFFFFFFFFFFFFFFF'
|
|
|
|
|
|
- else: # 子设备设置场景
|
|
|
+ # 条件为传感器设备
|
|
|
+ else:
|
|
|
if not sub_device_id:
|
|
|
return response.json(444, {'error param': 'subDeviceId'})
|
|
|
|
|
@@ -374,7 +382,8 @@ class SmartSceneView(View):
|
|
|
msg['scene_id'] = smart_scene_qs.id
|
|
|
|
|
|
# 获取设备任务数据
|
|
|
- msg['task'], scene_data = cls.get_task_list_and_scene_data(tasks_list, conditions_dict, tz, now_time)
|
|
|
+ msg['task'], scene_data = cls.get_task_list_and_scene_data(
|
|
|
+ conditions_dict, is_set_time, minutes, repeat, tz, now_time, tasks_list)
|
|
|
|
|
|
smart_scene_qs.device_data = json.dumps(msg)
|
|
|
if scene_data:
|
|
@@ -640,6 +649,7 @@ class SmartSceneView(View):
|
|
|
|
|
|
if not all([smart_scene_id, scene_name, conditions, tasks]):
|
|
|
return response.json(444, {'error param': 'smartSceneId,sceneName,conditions or tasks'})
|
|
|
+
|
|
|
try:
|
|
|
smart_scene_qs = SmartScene.objects.filter(id=smart_scene_id)
|
|
|
if not smart_scene_qs.exists():
|
|
@@ -654,7 +664,28 @@ class SmartSceneView(View):
|
|
|
'scene_event': SCENE_EVENT_EDIT,
|
|
|
'scene_status': scene_status
|
|
|
}
|
|
|
- if conditions_dict['type'] == 2: # 条件为选择子设备
|
|
|
+
|
|
|
+ # 判断条件是否为设置时间
|
|
|
+ is_set_time, minutes, repeat = False, 0, 0
|
|
|
+ if conditions_dict['type'] == 1:
|
|
|
+ is_set_time = True
|
|
|
+ minutes = conditions_dict['time']['minutes']
|
|
|
+ repeat = conditions_dict['time']['repeat']
|
|
|
+ # 条件为设置时间
|
|
|
+ if is_set_time:
|
|
|
+ if not device_id:
|
|
|
+ return response.json(444, {'error param': 'deviceId'})
|
|
|
+ sub_device_id = 0
|
|
|
+ device_qs = Device_Info.objects.filter(id=device_id).values('serial_number')
|
|
|
+ if not device_qs.exists():
|
|
|
+ return response.json(173)
|
|
|
+ serial_number = device_qs[0]['serial_number']
|
|
|
+ # 网关数据
|
|
|
+ msg['sensor_type'] = DEVICE_TYPE['gateway']
|
|
|
+ msg['sensor_status'] = 2002
|
|
|
+ msg['sensor_ieee_addr'] = 'FFFFFFFFFFFFFFFF'
|
|
|
+ # 条件为选择子设备
|
|
|
+ else:
|
|
|
if not sub_device_id:
|
|
|
return response.json(444, {'error param': 'subDeviceId'})
|
|
|
|
|
@@ -692,22 +723,10 @@ class SmartSceneView(View):
|
|
|
msg['sensor_type'] = int(conditions_dict['sensor']['device_type'])
|
|
|
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:
|
|
|
- return response.json(444, {'error param': 'deviceId'})
|
|
|
- sub_device_id = 0
|
|
|
- device_qs = Device_Info.objects.filter(id=device_id).values('serial_number')
|
|
|
- if not device_qs.exists():
|
|
|
- return response.json(173)
|
|
|
- serial_number = device_qs[0]['serial_number']
|
|
|
- # 网关数据
|
|
|
- msg['sensor_type'] = DEVICE_TYPE['gateway']
|
|
|
- msg['sensor_status'] = 2002
|
|
|
- msg['sensor_ieee_addr'] = 'FFFFFFFFFFFFFFFF'
|
|
|
|
|
|
# 获取设备任务数据
|
|
|
- msg['task'], scene_data = cls.get_task_list_and_scene_data(tasks_list, conditions_dict, tz, now_time,
|
|
|
- scene_data_dict)
|
|
|
+ msg['task'], scene_data = cls.get_task_list_and_scene_data(
|
|
|
+ conditions_dict, is_set_time, minutes, repeat, tz, now_time, tasks_list, scene_data_dict)
|
|
|
if scene_data:
|
|
|
scene_data = json.dumps(scene_data)
|
|
|
|
|
@@ -1057,11 +1076,15 @@ class SmartSceneView(View):
|
|
|
return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
@classmethod
|
|
|
- def get_task_list_and_scene_data(cls, tasks_list, conditions_dict, tz, now_time, scene_data_dict=None):
|
|
|
+ def get_task_list_and_scene_data(cls, conditions_dict, is_set_time, minutes, repeat, tz, now_time, tasks_list,
|
|
|
+ scene_data_dict=None):
|
|
|
"""
|
|
|
获取设备任务数据和场景数据
|
|
|
@param tasks_list: app任务列表
|
|
|
@param conditions_dict: 条件
|
|
|
+ @param is_set_time: 条件是否为设置时间
|
|
|
+ @param minutes: 时间分钟数
|
|
|
+ @param repeat: 重复星期周期的十进制
|
|
|
@param tz: 时区
|
|
|
@param now_time: 当前时间
|
|
|
@param scene_data_dict: 场景数据
|
|
@@ -1070,11 +1093,11 @@ class SmartSceneView(View):
|
|
|
# 删除旧的定时任务
|
|
|
cls.del_aps_job(scene_data_dict)
|
|
|
task_list = []
|
|
|
- mqtt_task_list = []
|
|
|
+ scene_task_list = []
|
|
|
total_delay_time = 0
|
|
|
|
|
|
# 组织条件数据
|
|
|
- condition = cls.get_condition(conditions_dict, now_time, tz)
|
|
|
+ condition = cls.get_condition(conditions_dict, is_set_time, minutes, repeat, now_time, tz)
|
|
|
|
|
|
for task in tasks_list:
|
|
|
sensor_type = int(task['device_type'])
|
|
@@ -1092,14 +1115,11 @@ class SmartSceneView(View):
|
|
|
'serial_number': serial_number
|
|
|
}
|
|
|
|
|
|
- # 如果条件为设置时间,创建或修改定时任务
|
|
|
- if conditions_dict['type'] == 1:
|
|
|
- minutes = conditions_dict['time']['minutes']
|
|
|
- repeat = conditions_dict['time']['repeat']
|
|
|
-
|
|
|
+ # 如果条件为设置时间,创建定时任务
|
|
|
+ if is_set_time:
|
|
|
task_temp['task_id'] = cls.create_aps_job(
|
|
|
condition, minutes, total_delay_time, tz, repeat, sensor_type, event_type, serial_number)
|
|
|
- mqtt_task_list.append(task_temp)
|
|
|
+ scene_task_list.append(task_temp)
|
|
|
else:
|
|
|
task_temp = {
|
|
|
'sensor_type': sensor_type,
|
|
@@ -1128,32 +1148,32 @@ class SmartSceneView(View):
|
|
|
task_temp['value_type'] = task.get('value_type')
|
|
|
task_list.append(task_temp)
|
|
|
|
|
|
- # mqtt_task_list不为空,组织完整场景数据
|
|
|
+ # scene_task_list不为空,组织完整场景数据
|
|
|
scene_data = ''
|
|
|
- if mqtt_task_list:
|
|
|
+ if scene_task_list:
|
|
|
scene_data = {
|
|
|
'condition': condition,
|
|
|
- 'task_list': mqtt_task_list
|
|
|
+ 'task_list': scene_task_list
|
|
|
}
|
|
|
|
|
|
return task_list, scene_data
|
|
|
|
|
|
@classmethod
|
|
|
- def get_condition(cls, conditions_dict, now_time, tz):
|
|
|
+ def get_condition(cls, conditions_dict, is_set_time, minutes, repeat, now_time, tz):
|
|
|
"""
|
|
|
获取场景条件
|
|
|
@param conditions_dict: 条件数据
|
|
|
+ @param is_set_time: 条件是否为设置时间
|
|
|
+ @param minutes: 时间分钟数
|
|
|
+ @param repeat: 重复星期周期的十进制
|
|
|
@param now_time: 当前时间
|
|
|
@param tz: 时区
|
|
|
@return: condition
|
|
|
"""
|
|
|
condition = {}
|
|
|
# 条件为设置时间
|
|
|
- if conditions_dict['type'] == 1:
|
|
|
- minutes = conditions_dict['time']['minutes']
|
|
|
+ if is_set_time:
|
|
|
hour, minute = divmod(minutes, 60)
|
|
|
- repeat = conditions_dict['time']['repeat']
|
|
|
-
|
|
|
# 一次性任务
|
|
|
if repeat == 0:
|
|
|
condition['time'] = 'date'
|
|
@@ -1177,12 +1197,12 @@ class SmartSceneView(View):
|
|
|
}
|
|
|
condition['time_dict'] = time_dict
|
|
|
else:
|
|
|
- device_type = conditions_dict['sensor']['device_type']
|
|
|
+ device_type = int(conditions_dict['sensor']['device_type'])
|
|
|
event_type = conditions_dict['sensor']['eventValues'][0]['event_type']
|
|
|
condition['event_type'] = event_type
|
|
|
# 温湿度传感器,取值
|
|
|
if device_type == SENSOR_TYPE['tem_hum_sensor']:
|
|
|
- condition['value'] = event_type = conditions_dict['sensor']['eventValues']['value']
|
|
|
+ condition['value'] = event_type = conditions_dict['sensor']['eventValues'][0]['value']
|
|
|
return condition
|
|
|
|
|
|
@staticmethod
|