# -*- encoding: utf-8 -*- """ @File : SmartSocketController.py @Time : 2023/3/17 11:52 @Author : stephen @Email : zhangdongming@asj6.wecom.work @Software: PyCharm """ import logging import time from django.db import transaction from django.http import QueryDict from django.views import View from Model.models import SocketInfo from Object.ResponseObject import ResponseObject from Service.CommonService import CommonService LOGGER = logging.getLogger('info') class SmartSocketView(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 delete(self, request, *args, **kwargs): request.encoding = 'utf-8' operation = kwargs.get('operation') delete = QueryDict(request.body) if not delete: delete = request.GET return self.validation(delete, request, operation) def put(self, request, *args, **kwargs): request.encoding = 'utf-8' operation = kwargs.get('operation') put = QueryDict(request.body) return self.validation(put, request, operation) def validation(self, request_dict, request, operation): response = ResponseObject('cn') if operation == 'getSocketDetails': return self.getSocketDetails(request_dict, response) elif operation == 'publishSocketInfo': device_id = '123' serial_number = '0013UC11A' status = 1 type_switch = 0 # self.save_socket_switch(device_id, serial_number, status, type_switch) is_open = 0 count_down_time = 1679131969 self.save_socket_count_down(device_id, serial_number, status, is_open, count_down_time) return response.json(0) return response.json(404) @classmethod def getSocketDetails(cls, request_dict, response): return response.json(0) @staticmethod def save_socket_switch(device_id, serial_number, status, type_switch=0): """ 保存插座开关信息 @param device_id: 设备ID @param serial_number: 序列号 @param status: 状态 0关,1开 @param type_switch: 0:总开关,1倒计时开关 @return: """ if not device_id: return False socket_info_qs = SocketInfo.objects.filter(device_id=device_id, type_switch=type_switch) now_time = int(time.time()) try: with transaction.atomic(): # 创建插座开关信息 if not socket_info_qs.exists(): socket_dict = {"device_id": device_id, "serial_number": serial_number, "status": status, "type_switch": type_switch, "created_time": now_time, "updated_time": now_time, "online": True} SocketInfo.objects.create(**socket_dict) return True if socket_info_qs.first().status == status: return True socket_info_qs.update(status=status, updated_time=now_time) # 主题名称 topic_name = 'loocam/smart-socket/{}'.format(serial_number) # 发布消息内容 msg = {'type': 1, 'data': {'deviceSwitch': status}} result = CommonService.req_publish_mqtt_msg(serial_number, topic_name, msg) LOGGER.info('智能插座开关设置发布MQTT消息结果{}'.format(result)) return True except Exception as e: LOGGER.info('智能插座异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e))) return False @staticmethod def save_socket_count_down(device_id, serial_number, status, is_open, count_down_time, type_switch=1): """ 保存插座倒计时信息 @param count_down_time: 倒计时时间戳 @param is_open: 是否开始 0:关停,1:开始 @param device_id: 设备ID @param serial_number: 序列号 @param status: 电源状态 0关,1开 @param type_switch: 0:总开关,1倒计时开关 @return: """ if not device_id: return False socket_info_qs = SocketInfo.objects.filter(device_id=device_id, type_switch=type_switch) now_time = int(time.time()) try: with transaction.atomic(): # 创建插座倒计时信息 if not socket_info_qs.exists(): socket_dict = {"device_id": device_id, "serial_number": serial_number, "status": status, "type_switch": type_switch, "created_time": now_time, "updated_time": now_time, "online": True, "count_down_time": int(count_down_time)} socket_info_qs = SocketInfo.objects.create(**socket_dict) else: socket_info_qs.update(status=status, count_down_time=int(count_down_time), updated_time=now_time) count_down_id = socket_info_qs.first().id # 主题名称 topic_name = 'loocam/smart-socket/{}'.format(serial_number) # 发布消息内容 msg = {'type': 2, 'data': {'powerType': status, 'countDownId': count_down_id, 'time': int(count_down_time), 'start': is_open}} result = CommonService.req_publish_mqtt_msg(serial_number, topic_name, msg) LOGGER.info('智能插座倒计时发布MQTT消息结果{}'.format(result)) return True except Exception as e: LOGGER.info('智能插座异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e))) return False