|
@@ -10,8 +10,7 @@ import time
|
|
|
|
|
|
from django.views import View
|
|
|
|
|
|
-from Model.models import SwitchDimmingSettings, SwitchChronopher, Device_Info, SceneLog, FamilyRoomDevice, \
|
|
|
- SwitchOperateLog
|
|
|
+from Model.models import SwitchDimmingSettings, SwitchScheduler, Device_Info, SceneLog, FamilyRoomDevice
|
|
|
from Object.RedisObject import RedisObject
|
|
|
from Service.CommonService import CommonService
|
|
|
from Object.CeleryBeatObject import CeleryBeatObj
|
|
@@ -37,10 +36,8 @@ class SmartSwitchView(View):
|
|
|
|
|
|
def validation(self, request_dict, request, operation):
|
|
|
token_code, user_id, response = CommonService.verify_token_get_user_id(request_dict, request)
|
|
|
- if operation == 'switch-chronopher-log': # 设备上报排程日志
|
|
|
- return self.create_chronopher_log(request_dict, response)
|
|
|
- if operation == 'switch-operate-log': # 设备上报操作日志
|
|
|
- return self.create_operate_log(request_dict, response)
|
|
|
+ if operation == 'switch-scheduler-log': # 设备上报排程日志
|
|
|
+ return self.create_scheduler_log(request_dict, response)
|
|
|
elif operation == 'reset': # 设备重置
|
|
|
return self.reset(request_dict, response)
|
|
|
else:
|
|
@@ -48,14 +45,14 @@ class SmartSwitchView(View):
|
|
|
return response.json(token_code)
|
|
|
if operation == 'get-dimming-setting': # 获取智能开关调光设置
|
|
|
return self.get_dimming_setting(request_dict, response)
|
|
|
- elif operation == 'get-chronopher-setting': # 获取排程计划
|
|
|
- return self.get_chronopher_setting(request_dict, response)
|
|
|
- elif operation == 'add-or-edit-chronopher': # 添加/编辑排程计划
|
|
|
- return self.add_or_edit_chronopher(request_dict, response)
|
|
|
- elif operation == 'edit-chronopher-status': # 修改排程计划状态
|
|
|
- return self.edit_chronopher_status(request_dict, response)
|
|
|
- elif operation == 'delete-chronopher': # 删除排程计划
|
|
|
- return self.delete_chronopher(request_dict, response)
|
|
|
+ elif operation == 'get-scheduler-setting': # 获取排程计划
|
|
|
+ return self.get_scheduler_setting(request_dict, response)
|
|
|
+ elif operation == 'add-or-edit-scheduler': # 添加/编辑排程计划
|
|
|
+ return self.add_or_edit_scheduler(request_dict, response)
|
|
|
+ elif operation == 'edit-scheduler-status': # 修改排程计划状态
|
|
|
+ return self.edit_scheduler_status(request_dict, response)
|
|
|
+ elif operation == 'delete-scheduler': # 删除排程计划
|
|
|
+ return self.delete_scheduler(request_dict, response)
|
|
|
elif operation == 'get-timer-setting': # 获取计时器
|
|
|
return self.get_timer_setting(request_dict, response)
|
|
|
elif operation == 'add-or-edit-timer': # 添加/编辑计时器
|
|
@@ -64,10 +61,8 @@ class SmartSwitchView(View):
|
|
|
return self.edit_dimming_correction(request_dict, response)
|
|
|
elif operation == 'edit-dimming-setting': # 修改智能开关调光设置
|
|
|
return self.edit_dimming_setting(request_dict, response)
|
|
|
- elif operation == 'get-chronopher-log': # 查询排程日志
|
|
|
- return self.get_chronopher_log(request_dict, response)
|
|
|
- elif operation == 'get-operate-log': # 查询普通操作日志
|
|
|
- return self.get_operate_log(request_dict, response)
|
|
|
+ elif operation == 'get-scheduler-log': # 查询排程日志
|
|
|
+ return self.get_scheduler_log(request_dict, response)
|
|
|
else:
|
|
|
return response.json(414)
|
|
|
|
|
@@ -165,7 +160,7 @@ class SmartSwitchView(View):
|
|
|
return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
@staticmethod
|
|
|
- def get_chronopher_setting(request_dict, response):
|
|
|
+ def get_scheduler_setting(request_dict, response):
|
|
|
"""
|
|
|
获取排程计划设置
|
|
|
@param request_dict: 请求参数
|
|
@@ -177,40 +172,41 @@ class SmartSwitchView(View):
|
|
|
if not device_id:
|
|
|
return response.json(444)
|
|
|
try:
|
|
|
- switch_chronopher_qs = SwitchChronopher.objects.filter(device_id=device_id).values()
|
|
|
- if not switch_chronopher_qs.exists():
|
|
|
+ switch_scheduler_qs = SwitchScheduler.objects.filter(device_id=device_id).values()
|
|
|
+ if not switch_scheduler_qs.exists():
|
|
|
return response.json(173)
|
|
|
- switch_chronopher_list = []
|
|
|
- for item in switch_chronopher_qs:
|
|
|
- switch_chronopher_list.append({
|
|
|
- 'chronopherId': item['id'],
|
|
|
+ switch_scheduler_list = []
|
|
|
+ for item in switch_scheduler_qs:
|
|
|
+ switch_scheduler_list.append({
|
|
|
+ 'schedulerId': item['id'],
|
|
|
'timeTypeRadio': item['time_type_radio'],
|
|
|
'timePoint': item['time_point'],
|
|
|
- 'timeQuantumStartTime': item['time_quantum_start_time'],
|
|
|
- 'timeQuantumEndTime': item['time_quantum_end_time'],
|
|
|
- 'timePointDeviceWillDoing': item['time_point_device_will_doing'],
|
|
|
- 'timeQuantumDeviceWillDoing': item['time_quantum_device_will_doing'],
|
|
|
+ 'startTime': item['start_time'],
|
|
|
+ 'endTime': item['end_time'],
|
|
|
+ 'actionsType': item['actions_type'],
|
|
|
+ 'actions': item['actions'],
|
|
|
'slowOpenOrCloseSpeed': item['slow_open_or_close_speed'],
|
|
|
'repeat': item['repeat'],
|
|
|
+ 'isExecute': item['is_execute'],
|
|
|
})
|
|
|
- return response.json(0, {'list': switch_chronopher_list})
|
|
|
+ return response.json(0, {'list': switch_scheduler_list})
|
|
|
except Exception as e:
|
|
|
print(e)
|
|
|
return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
@staticmethod
|
|
|
- def add_or_edit_chronopher(request_dict, response):
|
|
|
+ def add_or_edit_scheduler(request_dict, response):
|
|
|
"""
|
|
|
添加/编辑排程计划
|
|
|
@param request_dict: 请求参数
|
|
|
@request_dict deviceId: 设备id
|
|
|
- @request_dict chronopherId: 排程计划id
|
|
|
+ @request_dict schedulerId: 排程计划id
|
|
|
@request_dict timeTypeRadio: 切换时间点/时间段
|
|
|
@request_dict timePoint: 时间点
|
|
|
- @request_dict timeQuantumStartTime: 时间段开始时间
|
|
|
- @request_dict timeQuantumEndTime: 时间段结束时间
|
|
|
- @request_dict timePointDeviceWillDoing: 设备将会
|
|
|
- @request_dict timeQuantumDeviceWillDoing: 设备将会
|
|
|
+ @request_dict startTime: 时间段开始时间
|
|
|
+ @request_dict endTime: 时间段结束时间
|
|
|
+ @request_dict actions: 排程操作
|
|
|
+ @request_dict actionsType: 操作类型
|
|
|
@request_dict slowOpenOrCloseSpeed: 缓慢开/关速度
|
|
|
@request_dict repeat: 重复周期
|
|
|
@param response: 响应对象
|
|
@@ -218,14 +214,14 @@ class SmartSwitchView(View):
|
|
|
"""
|
|
|
is_edit = request_dict.get('isEdit', None)
|
|
|
device_id = request_dict.get('deviceId', None)
|
|
|
- chronopher_id = request_dict.get('chronopherId', None)
|
|
|
+ scheduler_id = request_dict.get('schedulerId', None)
|
|
|
time_type_radio = int(request_dict.get('timeTypeRadio', 0))
|
|
|
time_point = request_dict.get('timePoint', None)
|
|
|
- time_quantum_start_time = request_dict.get('timeQuantumStartTime', None)
|
|
|
- time_quantum_end_time = request_dict.get('timeQuantumEndTime', None)
|
|
|
- time_point_device_will_doing = request_dict.get('timePointDeviceWillDoing', None)
|
|
|
- time_quantum_device_will_doing = request_dict.get('timeQuantumDeviceWillDoing', None)
|
|
|
- slow_open_or_close_speed = request_dict.get('slowOpenOrCloseSpeed', None)
|
|
|
+ start_time = request_dict.get('startTime', None)
|
|
|
+ end_time = request_dict.get('endTime', None)
|
|
|
+ actions = request_dict.get('actions', None)
|
|
|
+ actions_type = request_dict.get('actionsType', None)
|
|
|
+ slow_speed = request_dict.get('slowSpeed', None)
|
|
|
repeat = request_dict.get('repeat', None)
|
|
|
|
|
|
if not all([device_id, repeat]):
|
|
@@ -234,27 +230,30 @@ class SmartSwitchView(View):
|
|
|
if not device_qs.exists():
|
|
|
return response.json(173)
|
|
|
if time_type_radio == 1: # 时间点
|
|
|
- if not all([time_point, slow_open_or_close_speed]):
|
|
|
- return response.json(444, {'param': 'timePoint,slowOpenOrCloseSpeed'})
|
|
|
- chronopher_data = {
|
|
|
+ if not all([time_point, slow_speed]):
|
|
|
+ return response.json(444, {'param': 'timePoint,slowSpeed'})
|
|
|
+ scheduler_data = {
|
|
|
'device_id': device_id,
|
|
|
'time_type_radio': time_type_radio,
|
|
|
'time_point': time_point,
|
|
|
- 'time_point_device_will_doing': time_point_device_will_doing,
|
|
|
- 'slow_open_or_close_speed': slow_open_or_close_speed,
|
|
|
+ 'actions': actions,
|
|
|
+ 'actions_type': actions_type,
|
|
|
+ 'slow_speed': slow_speed,
|
|
|
'repeat': repeat
|
|
|
}
|
|
|
elif time_type_radio == 2: # 时间段
|
|
|
- if not all([time_quantum_start_time, time_quantum_end_time]):
|
|
|
- return response.json(444, {'param': 'timeQuantumStartTime,timeQuantumEndTime'})
|
|
|
- time_quantum_start_time = int(time_quantum_start_time)
|
|
|
- time_quantum_end_time = int(time_quantum_end_time)
|
|
|
- chronopher_data = {
|
|
|
+ if not all([start_time, end_time]):
|
|
|
+ return response.json(444, {'param': 'startTime,endTime'})
|
|
|
+ start_time = int(start_time)
|
|
|
+ end_time = int(end_time)
|
|
|
+ scheduler_data = {
|
|
|
'device_id': device_id,
|
|
|
'time_type_radio': time_type_radio,
|
|
|
- 'time_quantum_start_time': time_quantum_start_time,
|
|
|
- 'time_quantum_end_time': time_quantum_end_time,
|
|
|
- 'time_quantum_device_will_doing': time_quantum_device_will_doing,
|
|
|
+ 'start_time': start_time,
|
|
|
+ 'end_time': end_time,
|
|
|
+ 'actions': actions,
|
|
|
+ 'actions_type': actions_type,
|
|
|
+ 'slow_speed': slow_speed,
|
|
|
'repeat': repeat
|
|
|
}
|
|
|
else:
|
|
@@ -263,18 +262,18 @@ class SmartSwitchView(View):
|
|
|
with transaction.atomic():
|
|
|
celery_obj = CeleryBeatObj()
|
|
|
if is_edit:
|
|
|
- if not chronopher_id:
|
|
|
- return response.json(444, {'param': 'chronopherId'})
|
|
|
- update_flag = SwitchChronopher.objects.filter(device_id=device_id, id=chronopher_id).update(
|
|
|
- **chronopher_data)
|
|
|
+ if not scheduler_id:
|
|
|
+ return response.json(444, {'param': 'schedulerId'})
|
|
|
+ update_flag = SwitchScheduler.objects.filter(device_id=device_id, id=scheduler_id).update(
|
|
|
+ **scheduler_data)
|
|
|
if not update_flag:
|
|
|
return response.json(173)
|
|
|
- celery_obj.del_task('switchchronopher_{}'.format(chronopher_id))
|
|
|
- celery_obj.del_task('switchchronopher_{}_1'.format(chronopher_id))
|
|
|
- celery_obj.del_task('switchchronopher_{}_2'.format(chronopher_id))
|
|
|
+ celery_obj.del_task('switchscheduler_{}'.format(scheduler_id))
|
|
|
+ celery_obj.del_task('switchscheduler_{}_1'.format(scheduler_id))
|
|
|
+ celery_obj.del_task('switchscheduler_{}_2'.format(scheduler_id))
|
|
|
else:
|
|
|
- switch_qs = SwitchChronopher.objects.create(**chronopher_data)
|
|
|
- chronopher_id = switch_qs.id
|
|
|
+ switch_qs = SwitchScheduler.objects.create(**scheduler_data)
|
|
|
+ scheduler_id = switch_qs.id
|
|
|
|
|
|
# 设置排程任务
|
|
|
serial_number = device_qs[0]['serial_number']
|
|
@@ -282,45 +281,48 @@ class SmartSwitchView(View):
|
|
|
tz = CommonService.get_user_tz(user_id)
|
|
|
topic_name = APSCHEDULER_TOPIC_NAME.format(serial_number)
|
|
|
if time_type_radio == 1: # 时间点任务
|
|
|
- task_id = 'switchchronopher_{}'.format(chronopher_id)
|
|
|
- if time_point_device_will_doing in ['0', '1']: # 开启或关闭
|
|
|
+ task_id = 'switchscheduler_{}'.format(scheduler_id)
|
|
|
+ if actions_type == '1': # 开启或关闭
|
|
|
msg = {
|
|
|
- "taskId": chronopher_id,
|
|
|
- "deviceSwitch": int(time_point_device_will_doing), # 设备开关-1:反转,0:关,1:开,2:预设亮度
|
|
|
- "slowTime": slow_open_or_close_speed
|
|
|
+ "task_id": scheduler_id,
|
|
|
+ "device_switch": int(actions), # 设备开关-1:反转,0:关,1:开,2:预设亮度
|
|
|
+ "slow_time": slow_speed
|
|
|
}
|
|
|
- else: # 开启且设置亮度
|
|
|
+ elif actions_type == '2': # 开启且设置亮度
|
|
|
msg = {
|
|
|
- "taskId": chronopher_id,
|
|
|
- "deviceSwitch": 2,
|
|
|
- "pwmControl": int(time_point_device_will_doing),
|
|
|
- 'slowTime': slow_open_or_close_speed
|
|
|
+ "task_id": scheduler_id,
|
|
|
+ "device_switch": 2,
|
|
|
+ "pwm_control": int(actions),
|
|
|
+ 'slow_time': slow_speed
|
|
|
}
|
|
|
+ else:
|
|
|
+ return response.json(444, {'param': 'timeTypeRadio'})
|
|
|
+
|
|
|
time_str = datetime.datetime.fromtimestamp(int(time_point))
|
|
|
celery_obj.creat_crontab_task(tz, task_id, MQTT_TASK, time_str.minute,
|
|
|
time_str.hour, repeat, args=[serial_number, topic_name, msg, task_id])
|
|
|
else: # 时间段任务
|
|
|
- start_hour = int(time_quantum_start_time / 60 // 60)
|
|
|
- start_minute = int(time_quantum_start_time / 60 % 60)
|
|
|
- end_hour = int(time_quantum_end_time / 60 // 60)
|
|
|
- end_minute = int(time_quantum_end_time / 60 % 60)
|
|
|
- if time_quantum_device_will_doing in ['0', '1']:
|
|
|
- begin_task_id = 'switchchronopher_{}_1'.format(chronopher_id) # 开始任务id
|
|
|
- end_task_id = 'switchchronopher_{}_2'.format(chronopher_id) # 结束任务id
|
|
|
- msg = {"taskId": chronopher_id,
|
|
|
- "deviceSwitch": int(time_quantum_device_will_doing)}
|
|
|
+ start_hour = int(start_time / 60 // 60)
|
|
|
+ start_minute = int(start_time / 60 % 60)
|
|
|
+ end_hour = int(end_time / 60 // 60)
|
|
|
+ end_minute = int(end_time / 60 % 60)
|
|
|
+ if actions == '1':
|
|
|
+ begin_task_id = 'switchscheduler_{}_1'.format(scheduler_id) # 开始任务id
|
|
|
+ end_task_id = 'switchscheduler_{}_2'.format(scheduler_id) # 结束任务id
|
|
|
+ msg = {"task_id": scheduler_id,
|
|
|
+ "device_switch": int(actions)}
|
|
|
celery_obj.creat_crontab_task(tz, begin_task_id, MQTT_TASK, start_minute, start_hour, repeat,
|
|
|
args=[serial_number, topic_name, msg, begin_task_id])
|
|
|
- msg = {"taskId": chronopher_id,
|
|
|
- "deviceSwitch": 0 if int(time_quantum_device_will_doing) == 1 else 1}
|
|
|
+ msg = {"task_id": scheduler_id,
|
|
|
+ "device_switch": 0 if int(actions) == 1 else 1}
|
|
|
celery_obj.creat_crontab_task(tz, end_task_id, MQTT_TASK, end_minute, end_hour, repeat,
|
|
|
args=[serial_number, topic_name, msg, end_task_id])
|
|
|
|
|
|
- else: # 间隔任务
|
|
|
- minute = int(time_quantum_device_will_doing)
|
|
|
- task_id = 'switchchronopher_{}'.format(chronopher_id) # 开始任务id
|
|
|
- msg = {"taskId": chronopher_id,
|
|
|
- "deviceSwitch": -1}
|
|
|
+ elif actions == '3': # 间隔任务
|
|
|
+ minute = int(actions)
|
|
|
+ task_id = 'switchscheduler_{}'.format(scheduler_id) # 开始任务id
|
|
|
+ msg = {"task_id": scheduler_id,
|
|
|
+ "device_switch": -1}
|
|
|
if minute >= 60:
|
|
|
hour = '{}-{}/{}'.format(start_hour, end_hour, minute // 60)
|
|
|
minute = start_minute
|
|
@@ -335,60 +337,60 @@ class SmartSwitchView(View):
|
|
|
return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
@staticmethod
|
|
|
- def edit_chronopher_status(request_dict, response):
|
|
|
+ def edit_scheduler_status(request_dict, response):
|
|
|
"""
|
|
|
修改排程计划状态
|
|
|
@param request_dict: 请求参数
|
|
|
@request_dict deviceId: 设备id
|
|
|
- @request_dict chronopherId: 排程计划id
|
|
|
+ @request_dict schedulerId: 排程计划id
|
|
|
@request_dict isExecute: 修改状态
|
|
|
@param response: 响应对象
|
|
|
@return: response
|
|
|
"""
|
|
|
device_id = request_dict.get('deviceId', None)
|
|
|
- chronopher_id = request_dict.get('chronopherId', None)
|
|
|
+ scheduler_id = request_dict.get('schedulerId', None)
|
|
|
is_execute = request_dict.get('isExecute', None)
|
|
|
- if not all([device_id, chronopher_id, is_execute]):
|
|
|
- return response.json(444, {'param': 'deviceId,chronopherId,isExecute'})
|
|
|
+ if not all([device_id, scheduler_id, is_execute]):
|
|
|
+ return response.json(444, {'param': 'deviceId,schedulerId,isExecute'})
|
|
|
try:
|
|
|
is_execute = int(is_execute)
|
|
|
celery_obj = CeleryBeatObj()
|
|
|
if is_execute:
|
|
|
- celery_obj.enable_task('switchchronopher_{}'.format(chronopher_id))
|
|
|
- celery_obj.enable_task('switchchronopher_{}_1'.format(chronopher_id))
|
|
|
- celery_obj.enable_task('switchchronopher_{}_2'.format(chronopher_id))
|
|
|
+ celery_obj.enable_task('switchscheduler_{}'.format(scheduler_id))
|
|
|
+ celery_obj.enable_task('switchscheduler_{}_1'.format(scheduler_id))
|
|
|
+ celery_obj.enable_task('switchscheduler_{}_2'.format(scheduler_id))
|
|
|
else:
|
|
|
- celery_obj.disable_task('switchchronopher_{}'.format(chronopher_id))
|
|
|
- celery_obj.disable_task('switchchronopher_{}_1'.format(chronopher_id))
|
|
|
- celery_obj.disable_task('switchchronopher_{}_2'.format(chronopher_id))
|
|
|
- SwitchChronopher.objects.filter(device_id=device_id, id=chronopher_id).update(is_execute=is_execute)
|
|
|
+ celery_obj.disable_task('switchscheduler_{}'.format(scheduler_id))
|
|
|
+ celery_obj.disable_task('switchscheduler_{}_1'.format(scheduler_id))
|
|
|
+ celery_obj.disable_task('switchscheduler_{}_2'.format(scheduler_id))
|
|
|
+ SwitchScheduler.objects.filter(device_id=device_id, id=scheduler_id).update(is_execute=is_execute)
|
|
|
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 delete_chronopher(request_dict, response):
|
|
|
+ def delete_scheduler(request_dict, response):
|
|
|
"""
|
|
|
删除排程计划
|
|
|
@param request_dict: 请求参数
|
|
|
@request_dict deviceId: 设备id
|
|
|
- @request_dict chronopherId: 排程计划id
|
|
|
+ @request_dict schedulerId: 排程计划id
|
|
|
@param response: 响应对象
|
|
|
@return: response
|
|
|
"""
|
|
|
device_id = request_dict.get('deviceId', None)
|
|
|
- chronopher_id = request_dict.get('chronopherId', None)
|
|
|
+ scheduler_id = request_dict.get('schedulerId', None)
|
|
|
|
|
|
- if not chronopher_id:
|
|
|
+ if not scheduler_id:
|
|
|
return response.json(444, {'error param': 'deviceId or chronopherId'})
|
|
|
try:
|
|
|
- delete_flag = SwitchChronopher.objects.filter(device_id=device_id, id=chronopher_id).delete()
|
|
|
+ delete_flag = SwitchScheduler.objects.filter(device_id=device_id, id=scheduler_id).delete()
|
|
|
if not delete_flag[0]:
|
|
|
return response.json(173)
|
|
|
celery_obj = CeleryBeatObj()
|
|
|
- celery_obj.del_task('switchchronopher_{}'.format(chronopher_id)) # 删除排程任务
|
|
|
- celery_obj.del_task('switchchronopher_{}_1'.format(chronopher_id))
|
|
|
- celery_obj.del_task('switchchronopher_{}_2'.format(chronopher_id))
|
|
|
+ celery_obj.del_task('switchscheduler_{}'.format(scheduler_id)) # 删除排程任务
|
|
|
+ celery_obj.del_task('switchscheduler_{}_1'.format(scheduler_id))
|
|
|
+ celery_obj.del_task('switchscheduler_{}_2'.format(scheduler_id))
|
|
|
return response.json(0)
|
|
|
except Exception as e:
|
|
|
print(e)
|
|
@@ -411,7 +413,7 @@ class SmartSwitchView(View):
|
|
|
redis_obj = RedisObject()
|
|
|
timer_info = redis_obj.get_all_hash_data(key)
|
|
|
if not timer_info:
|
|
|
- res = {'timePoint': -1, 'countdownTime': -1, 'timePointDeviceWillDoing': -1, 'timerStatus': -1}
|
|
|
+ res = {'timePoint': -1, 'countdownTime': -1, 'actions': -1, 'timerStatus': -1}
|
|
|
else:
|
|
|
res = timer_info
|
|
|
return response.json(0, res)
|
|
@@ -435,11 +437,11 @@ class SmartSwitchView(View):
|
|
|
is_edit = request_dict.get('isEdit', None)
|
|
|
device_id = request_dict.get('deviceId', None)
|
|
|
countdown_time = request_dict.get('countdownTime', None)
|
|
|
- time_point_device_will_doing = request_dict.get('timePointDeviceWillDoing', None)
|
|
|
+ actions = request_dict.get('actions', None)
|
|
|
timer_status = request_dict.get('timerStatus', None)
|
|
|
|
|
|
- if not all([device_id, countdown_time, time_point_device_will_doing]):
|
|
|
- return response.json(444, {'param': 'deviceId, countdownTime, timePointDeviceWillDoing'})
|
|
|
+ if not all([device_id, countdown_time, actions]):
|
|
|
+ return response.json(444, {'param': 'deviceId, countdownTime, actions'})
|
|
|
device_qs = Device_Info.objects.filter(id=device_id).values('serial_number', 'userID')
|
|
|
if not device_qs.exists():
|
|
|
return response.json(173)
|
|
@@ -457,7 +459,7 @@ class SmartSwitchView(View):
|
|
|
implement_time = now_time + countdown_time
|
|
|
redis_dict = {'timePoint': implement_time,
|
|
|
'countdownTime': countdown_time,
|
|
|
- 'timePointDeviceWillDoing': time_point_device_will_doing,
|
|
|
+ 'actions': actions,
|
|
|
'timerStatus': timer_status}
|
|
|
with transaction.atomic():
|
|
|
if is_edit:
|
|
@@ -472,8 +474,7 @@ class SmartSwitchView(View):
|
|
|
return response.json(0)
|
|
|
redis_obj.set_hash_data(key, redis_dict)
|
|
|
redis_obj.set_expire(key, countdown_time)
|
|
|
- msg = {'device_switch': time_point_device_will_doing, 'implement_time': implement_time,
|
|
|
- 'task_id': task_id}
|
|
|
+ msg = {'device_switch': actions, 'implement_time': implement_time, 'task_id': task_id}
|
|
|
celery_obj.creat_clocked_task(task_id, MQTT_TASK, implement_time, tz,
|
|
|
args=[serial_number, topic_name, msg, task_id])
|
|
|
return response.json(0)
|
|
@@ -482,40 +483,45 @@ class SmartSwitchView(View):
|
|
|
return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
@staticmethod
|
|
|
- def create_chronopher_log(request_dict, response):
|
|
|
+ def create_scheduler_log(request_dict, response):
|
|
|
"""
|
|
|
生成执行日志
|
|
|
@param request_dict: 请求参数
|
|
|
@request_dict serialNumber: 设备序列号
|
|
|
- @request_dict chronopherId: 排程id
|
|
|
+ @request_dict schedulerId: 排程id
|
|
|
@request_dict status: 执行状态
|
|
|
@param response: 响应对象
|
|
|
@return: response
|
|
|
"""
|
|
|
serial_number = request_dict.get('serial_number', None)
|
|
|
- chronopher_id = request_dict.get('task_id', None)
|
|
|
+ scheduler_id = request_dict.get('task_id', None)
|
|
|
operate_status = request_dict.get('status', None)
|
|
|
switch_status = request_dict.get('switch_status', None)
|
|
|
implement_time = request_dict.get('implement_time', None)
|
|
|
brightness = request_dict.get('brightness', None)
|
|
|
|
|
|
- if not all([serial_number, chronopher_id, operate_status, switch_status, implement_time, brightness]):
|
|
|
+ if not all([serial_number, scheduler_id, operate_status, switch_status, implement_time, brightness]):
|
|
|
return response.json(444, {
|
|
|
- 'error param': 'serial_number, chronopher_id, status, switch_status, implement_time, brightness'})
|
|
|
+ 'error param': 'serial_number, task_id, status, switch_status, implement_time, brightness'})
|
|
|
device_qs = Device_Info.objects.filter(serial_number=serial_number).values('id')
|
|
|
if not device_qs.exists():
|
|
|
return response.json(173)
|
|
|
device_id = device_qs[0]['id']
|
|
|
- chronopher_qs = SwitchChronopher.objects.filter(device_id=device_id, id=chronopher_id).values(
|
|
|
- 'time_type_radio', 'time_point', 'time_quantum_start_time', 'time_quantum_end_time',
|
|
|
- 'time_point_device_will_doing', 'time_quantum_device_will_doing', 'slow_open_or_close_speed', 'repeat')
|
|
|
- if not chronopher_qs.exists():
|
|
|
+ scheduler_qs = SwitchScheduler.objects.filter(device_id=device_id, id=scheduler_id).values('time_type_radio',
|
|
|
+ 'time_point',
|
|
|
+ 'start_time',
|
|
|
+ 'end_time',
|
|
|
+ 'actions',
|
|
|
+ 'actions_type',
|
|
|
+ 'slow_speed',
|
|
|
+ 'repeat')
|
|
|
+ if not scheduler_qs.exists():
|
|
|
return response.json(173)
|
|
|
try:
|
|
|
scene_log = {
|
|
|
'scene_id': chronopher_id,
|
|
|
'device_id': device_id,
|
|
|
- 'tasks': json.dumps(chronopher_qs[0]),
|
|
|
+ 'tasks': json.dumps(scheduler_qs[0]),
|
|
|
'status': operate_status,
|
|
|
'created_time': implement_time,
|
|
|
}
|
|
@@ -523,23 +529,14 @@ class SmartSwitchView(View):
|
|
|
scene_id=chronopher_id)
|
|
|
if not scene_qs.exists():
|
|
|
SceneLog.objects.create(**scene_log)
|
|
|
- operate_log = {
|
|
|
- 'device_id': device_id,
|
|
|
- 'status': switch_status,
|
|
|
- 'operate_type': 2,
|
|
|
- 'brightness': brightness,
|
|
|
- 'created_time': implement_time
|
|
|
- }
|
|
|
- operate_qs = SwitchOperateLog.objects.filter(**operate_log)
|
|
|
- if not operate_qs.exists():
|
|
|
- SwitchOperateLog.objects.create(**operate_log)
|
|
|
+
|
|
|
return response.json(0)
|
|
|
except Exception as e:
|
|
|
print(e)
|
|
|
return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
@staticmethod
|
|
|
- def get_chronopher_log(request_dict, response):
|
|
|
+ def get_scheduler_log(request_dict, response):
|
|
|
"""
|
|
|
查询排程执行日志
|
|
|
@param request_dict: 请求参数
|
|
@@ -584,15 +581,15 @@ class SmartSwitchView(View):
|
|
|
try:
|
|
|
# 删除智能开关数据
|
|
|
SwitchDimmingSettings.objects.filter(device_id=device_id).delete()
|
|
|
- chronopher_qs = SwitchChronopher.objects.filter(device_id=device_id)
|
|
|
- if chronopher_qs.exists():
|
|
|
+ scheduler_qs = SwitchScheduler.objects.filter(device_id=device_id)
|
|
|
+ if scheduler_qs.exists():
|
|
|
celery_obj = CeleryBeatObj()
|
|
|
- for chronopher in chronopher_qs:
|
|
|
- chronopher_id = chronopher.id
|
|
|
- celery_obj.del_task('switchchronopher_{}'.format(chronopher_id)) # 删除排程任务
|
|
|
- celery_obj.del_task('switchchronopher_{}_1'.format(chronopher_id))
|
|
|
- celery_obj.del_task('switchchronopher_{}_2'.format(chronopher_id))
|
|
|
- chronopher_qs.delete()
|
|
|
+ for scheduler in scheduler_qs:
|
|
|
+ scheduler_id = scheduler.id
|
|
|
+ celery_obj.del_task('switchscheduler_{}'.format(scheduler_id)) # 删除排程任务
|
|
|
+ celery_obj.del_task('switchscheduler_{}_1'.format(scheduler_id))
|
|
|
+ celery_obj.del_task('switchscheduler_{}_2'.format(scheduler_id))
|
|
|
+ scheduler_qs.delete()
|
|
|
SceneLog.objects.filter(device_id=device_id).delete()
|
|
|
FamilyRoomDevice.objects.filter(device_id=device_id).delete()
|
|
|
Device_Info.objects.filter(id=device_id).delete()
|
|
@@ -610,15 +607,15 @@ class SmartSwitchView(View):
|
|
|
"""
|
|
|
try:
|
|
|
SwitchDimmingSettings.objects.filter(device_id=device_id).delete()
|
|
|
- chronopher_qs = SwitchChronopher.objects.filter(device_id=device_id)
|
|
|
- if chronopher_qs.exists():
|
|
|
+ scheduler_qs = SwitchScheduler.objects.filter(device_id=device_id)
|
|
|
+ if scheduler_qs.exists():
|
|
|
celery_obj = CeleryBeatObj()
|
|
|
- for chronopher in chronopher_qs:
|
|
|
- chronopher_id = chronopher.id
|
|
|
- celery_obj.del_task('switchchronopher_{}'.format(chronopher_id)) # 删除排程任务
|
|
|
- celery_obj.del_task('switchchronopher_{}_1'.format(chronopher_id))
|
|
|
- celery_obj.del_task('switchchronopher_{}_2'.format(chronopher_id))
|
|
|
- chronopher_qs.delete()
|
|
|
+ for scheduler in scheduler_qs:
|
|
|
+ scheduler_id = scheduler.id
|
|
|
+ celery_obj.del_task('switchscheduler_{}'.format(scheduler_id)) # 删除排程任务
|
|
|
+ celery_obj.del_task('switchscheduler_{}_1'.format(scheduler_id))
|
|
|
+ celery_obj.del_task('switchscheduler_{}_2'.format(scheduler_id))
|
|
|
+ scheduler_qs.delete()
|
|
|
SceneLog.objects.filter(device_id=device_id).delete()
|
|
|
msg = {
|
|
|
"device_reset": 1 # 重置智能开关
|
|
@@ -629,57 +626,3 @@ class SmartSwitchView(View):
|
|
|
except Exception as e:
|
|
|
print(e)
|
|
|
LOGGER.info('error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
-
|
|
|
- @staticmethod
|
|
|
- def create_operate_log(request_dict, response):
|
|
|
- """
|
|
|
- 设备上报排程日志
|
|
|
- @param request_dict: 请求参数
|
|
|
- @request_dict serialNumber: 设备序列号
|
|
|
- @param response: 响应对象
|
|
|
- @return: response
|
|
|
- """
|
|
|
- serial_number = request_dict.get('serial_number', None)
|
|
|
- status = request_dict.get('switch_status', None)
|
|
|
- implement_time = request_dict.get('implement_time', None)
|
|
|
-
|
|
|
- if not all([serial_number, status, implement_time]):
|
|
|
- return response.json(444, {'error param': 'serial_number, switch_status, implement_time'})
|
|
|
- device_qs = Device_Info.objects.filter(serial_number=serial_number).values('id')
|
|
|
- if not device_qs.exists():
|
|
|
- return response.json(173)
|
|
|
- device_id = device_qs[0]['id']
|
|
|
-
|
|
|
- try:
|
|
|
- operate_log = {
|
|
|
- 'device_id': device_id,
|
|
|
- 'status': status,
|
|
|
- 'created_time': implement_time,
|
|
|
- }
|
|
|
- operate_qs = SwitchOperateLog.objects.filter(**operate_log)
|
|
|
- if not operate_qs.exists():
|
|
|
- SwitchOperateLog.objects.create(**operate_log)
|
|
|
- return response.json(0)
|
|
|
- except Exception as e:
|
|
|
- print(e)
|
|
|
- return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
-
|
|
|
- @staticmethod
|
|
|
- def get_operate_log(request_dict, response):
|
|
|
- """
|
|
|
- 查询普通操作日志
|
|
|
- @param request_dict: 请求参数
|
|
|
- @request_dict deviceId: 设备id
|
|
|
- @param response: 响应对象
|
|
|
- @return: response
|
|
|
- """
|
|
|
- device_id = request_dict.get('deviceId', None)
|
|
|
- if not device_id:
|
|
|
- return response.json(444, {'error param': 'deviceId'})
|
|
|
- try:
|
|
|
- scene_qs = SwitchOperateLog.objects.filter(device_id=device_id).values('status', 'created_time', 'id',
|
|
|
- 'operate_type')
|
|
|
- return response.json(0, list(scene_qs))
|
|
|
- except Exception as e:
|
|
|
- print(e)
|
|
|
- return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|