|
@@ -0,0 +1,126 @@
|
|
|
+# -*- coding: utf-8 -*-
|
|
|
+"""
|
|
|
+# @Author : cheng
|
|
|
+# @Time : 2023/7/10 11:20
|
|
|
+# @File: SmartSwitchController.py
|
|
|
+"""
|
|
|
+
|
|
|
+from django.views import View
|
|
|
+
|
|
|
+from Model.models import SwitchInfo, SwitchDimmingSettings, SwitchChronopher
|
|
|
+from Object.ResponseObject import ResponseObject
|
|
|
+from Service.CommonService import CommonService
|
|
|
+
|
|
|
+
|
|
|
+class SmartSwitchView(View):
|
|
|
+ def get(self, request, *args, **kwargs):
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ operation = kwargs.get('operation')
|
|
|
+ return self.validation(request.GET, request, operation)
|
|
|
+
|
|
|
+ def post(self, request, *args, **kwargs):
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ operation = kwargs.get('operation')
|
|
|
+ return self.validation(request.POST, request, operation)
|
|
|
+
|
|
|
+ def validation(self, request_dict, request, operation):
|
|
|
+ token_code, user_id, response = CommonService.verify_token_get_user_id(request_dict, request)
|
|
|
+ if token_code != 0:
|
|
|
+ return response.json(token_code)
|
|
|
+ if operation == 'get-switch': # 设备获取智能开关数据
|
|
|
+ return self.get_switch_data(request_dict, response)
|
|
|
+ else:
|
|
|
+ return response.json(414)
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def get_switch_info(request_dict, response):
|
|
|
+ """
|
|
|
+ 获取智能开关设备信息
|
|
|
+ @request_dict deviceId: 设备id
|
|
|
+ @param request_dict: 请求参数
|
|
|
+ @param response: 响应对象
|
|
|
+ @return: response
|
|
|
+ """
|
|
|
+ device_id = request_dict.get('deviceId', None)
|
|
|
+ if not device_id:
|
|
|
+ return response.json(444)
|
|
|
+ try:
|
|
|
+ switch_info_qs = SwitchInfo.objects.fileter(device_id=device_id)
|
|
|
+ if not switch_info_qs.exists():
|
|
|
+ return response.json(173)
|
|
|
+ res = {
|
|
|
+ 'model': switch_info_qs[0]['model'],
|
|
|
+ 'hardware_version': switch_info_qs[0]['hardware_version'],
|
|
|
+ 'firmware_version': switch_info_qs[0]['firmware_version'],
|
|
|
+ 'mac': switch_info_qs[0]['mac'],
|
|
|
+ }
|
|
|
+ return response.json(0, res)
|
|
|
+ except Exception as e:
|
|
|
+ print(e)
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def get_switch_setting(request_dict, response):
|
|
|
+ """
|
|
|
+ 获取智能开关设备设置信息
|
|
|
+ @request_dict deviceId: 设备id
|
|
|
+ @param request_dict: 请求参数
|
|
|
+ @param response: 响应对象
|
|
|
+ @return: response
|
|
|
+ """
|
|
|
+ device_id = request_dict.get('deviceId', None)
|
|
|
+ if not device_id:
|
|
|
+ return response.json(444)
|
|
|
+ try:
|
|
|
+ switch_setting_info_qs = SwitchDimmingSettings.objects.fileter(device_id=device_id)
|
|
|
+ if switch_setting_info_qs.exists():
|
|
|
+ res = {
|
|
|
+ 'click_turn_on_speed': switch_setting_info_qs[0]['click_turn_on_speed'],
|
|
|
+ 'click_turn_off_speed': switch_setting_info_qs[0]['click_turn_off_speed'],
|
|
|
+ 'double_click': switch_setting_info_qs[0]['double_click'],
|
|
|
+ 'press': switch_setting_info_qs[0]['press'],
|
|
|
+ 'double_press_click_turn_on_speed': switch_setting_info_qs[0]['double_press_click_turn_on_speed'],
|
|
|
+ 'double_press_click_turn_off_speed': switch_setting_info_qs[0]['double_press_click_turn_off_speed'],
|
|
|
+ 'led': switch_setting_info_qs[0]['led'],
|
|
|
+ 'dimming_correction': switch_setting_info_qs[0]['dimming_correction'],
|
|
|
+ }
|
|
|
+ return response.json(0, res)
|
|
|
+ return response.json(173)
|
|
|
+ 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_setting(request_dict, response):
|
|
|
+ """
|
|
|
+ 获取定时设置
|
|
|
+ @request_dict deviceId: 设备id
|
|
|
+ @param request_dict: 请求参数
|
|
|
+ @param response: 响应对象
|
|
|
+ @return: response
|
|
|
+ """
|
|
|
+ device_id = request_dict.get('deviceId', None)
|
|
|
+ if not device_id:
|
|
|
+ return response.json(444)
|
|
|
+ try:
|
|
|
+ switch_chronopher_qs = SwitchChronopher.objects.fileter(device_id=device_id)
|
|
|
+ if not switch_chronopher_qs:
|
|
|
+ return response.json(173)
|
|
|
+ switch_chronopher_list = []
|
|
|
+ for item in switch_chronopher_qs:
|
|
|
+ switch_chronopher_list.append({
|
|
|
+ 'id': item['id'],
|
|
|
+ 'time_type_radio': item['time_type_radio'],
|
|
|
+ 'time_point': item['time_point'],
|
|
|
+ 'time_quantum_start_time': item['time_quantum_start_time'],
|
|
|
+ 'time_quantum_end_time': item['time_quantum_end_time'],
|
|
|
+ 'time_point_device_will_doing': item['time_point_device_will_doing'],
|
|
|
+ 'time_quantum_device_will_doing': item['time_quantum_device_will_doing'],
|
|
|
+ 'slow_open_or_close_speed': item['slow_open_or_close_speed'],
|
|
|
+ 'repeat': item['repeat'],
|
|
|
+ })
|
|
|
+ return response.json(0, {'list': switch_chronopher_list})
|
|
|
+ except Exception as e:
|
|
|
+ print(e)
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
+
|