signal_callback.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import logging
  4. from django.dispatch import receiver
  5. from typing import TYPE_CHECKING
  6. from apps.web.constant import ErrorCode
  7. from apps.web.core.networking import post_operate_device, MessageSender
  8. from apps.web.device.models import MqttErrorLog, DeviceMqttStatics
  9. if TYPE_CHECKING:
  10. from apps.web.device.models import DeviceDict
  11. logger = logging.getLogger(__name__)
  12. @receiver(post_operate_device, sender=MessageSender)
  13. def post_operate_device_callback(sender, **kwargs):
  14. try:
  15. logger.debug('receive post operate device signal. kwargs = {}'.format(kwargs))
  16. device = kwargs['device'] # type: DeviceDict
  17. operation_result = kwargs['operationResult']
  18. payload = kwargs['payload']
  19. # 无rst认为是成功
  20. if 'rst' in operation_result and operation_result['rst'] != ErrorCode.SUCCESS:
  21. MqttErrorLog.new_one(device, payload, operation_result.get('rst', ''))
  22. total, uart, conn, other = 1, 0, 0, 0
  23. rst = operation_result.get('rst', None)
  24. if not rst:
  25. # 无rst认为是成功
  26. pass
  27. else:
  28. if rst == ErrorCode.BOARD_UART_TIMEOUT:
  29. uart = 1
  30. elif rst == ErrorCode.DEVICE_CONN_FAIL:
  31. conn = 1
  32. elif rst != ErrorCode.SUCCESS:
  33. other = 1
  34. DeviceMqttStatics.update(device, total, uart, conn, other)
  35. except Exception as e:
  36. logger.exception(e)