# -*- coding: utf-8 -*- # !/usr/bin/env python import logging from django.dispatch import receiver from typing import TYPE_CHECKING from apps.web.constant import ErrorCode from apps.web.core.networking import post_operate_device, MessageSender from apps.web.device.models import MqttErrorLog, DeviceMqttStatics if TYPE_CHECKING: from apps.web.device.models import DeviceDict logger = logging.getLogger(__name__) @receiver(post_operate_device, sender=MessageSender) def post_operate_device_callback(sender, **kwargs): try: logger.debug('receive post operate device signal. kwargs = {}'.format(kwargs)) device = kwargs['device'] # type: DeviceDict operation_result = kwargs['operationResult'] payload = kwargs['payload'] # 无rst认为是成功 if 'rst' in operation_result and operation_result['rst'] != ErrorCode.SUCCESS: MqttErrorLog.new_one(device, payload, operation_result.get('rst', '')) total, uart, conn, other = 1, 0, 0, 0 rst = operation_result.get('rst', None) if not rst: # 无rst认为是成功 pass else: if rst == ErrorCode.BOARD_UART_TIMEOUT: uart = 1 elif rst == ErrorCode.DEVICE_CONN_FAIL: conn = 1 elif rst != ErrorCode.SUCCESS: other = 1 DeviceMqttStatics.update(device, total, uart, conn, other) except Exception as e: logger.exception(e)