AlexaController.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # @Author : Rocky
  2. # @File : AlexaController.py
  3. # @Time : 2025/7/22 17:20
  4. import threading
  5. import time
  6. from typing import Dict, Union
  7. import requests
  8. from django.views import View
  9. from Model.models import AlexaPush
  10. from Object.RedisObject import RedisObject
  11. from Object.ResponseObject import ResponseObject
  12. from AnsjerPush.config import CONFIG_INFO, CONFIG_EUR, ALEXA_DOMAIN, ERROR_INFO_LOGGER
  13. from Service.DevicePushService import DevicePushService
  14. class AlexaView(View):
  15. def get(self, request, *args, **kwargs):
  16. request.encoding = 'utf-8'
  17. operation = kwargs.get('operation')
  18. return self.validation(request.GET, request, operation)
  19. def post(self, request, *args, **kwargs):
  20. request.encoding = 'utf-8'
  21. operation = kwargs.get('operation')
  22. return self.validation(request.POST, request, operation)
  23. def validation(self, request_dict, request, operation):
  24. response = ResponseObject()
  25. if operation == 'ObjectDetectionOrSnapshot':
  26. return self.object_detection_or_snapshot(request_dict, response)
  27. else:
  28. return response.json(414)
  29. @classmethod
  30. def object_detection_or_snapshot(cls, request_dict, response):
  31. uid = request_dict.get('uid', None)
  32. channel = request_dict.get('channel', '1')
  33. event_type = request_dict.get('event_type', None)
  34. event_time = request_dict.get('event_time', None)
  35. if not all([uid, event_type, event_time]):
  36. return response.json(444)
  37. # 欧洲: OCI伦敦, 其他: OCI凤凰城
  38. storage_location: int = 4 if CONFIG_INFO == CONFIG_EUR else 3
  39. kwargs = {
  40. 'uid': uid,
  41. 'channel': channel,
  42. 'event_type': int(event_type),
  43. 'event_time': event_time,
  44. 'storage_location': storage_location,
  45. }
  46. # 保存数据和发送Alexa事件
  47. thread = threading.Thread(
  48. target=cls.save_data_and_send_alexa_event,
  49. kwargs=kwargs)
  50. thread.start()
  51. kwargs.pop('event_type')
  52. res_data: str = DevicePushService.get_oci_req_url(**kwargs)
  53. return response.json(0, res_data)
  54. @staticmethod
  55. def save_data_and_send_alexa_event(**kwargs):
  56. uid: str = kwargs['uid']
  57. try:
  58. now_time: int = int(time.time())
  59. kwargs['add_time']: int = now_time
  60. AlexaPush.objects.create(**kwargs)
  61. # 请求Alexa服务器发送 ObjectDetection 或 Snapshot 事件
  62. url: str = ALEXA_DOMAIN + 'snapshot/asynEventResponse'
  63. storage_location: str = kwargs['storage_location']
  64. bucket: str = 'foreignpush'
  65. redis_obj: RedisObject = RedisObject()
  66. obj_name: str = '{}/{}/{}.jpeg'.format(uid, kwargs['channel'], kwargs['event_time'])
  67. image_uri: str = DevicePushService.oci_object_url(
  68. uid=uid, redis_obj=redis_obj, storage_location=storage_location, bucket=bucket, obj_name=obj_name)
  69. print(image_uri)
  70. # 快照
  71. if kwargs['event_type'] == 0:
  72. data: Dict[str, Union[str, int]] = {
  73. 'uid': uid,
  74. 'image_uri': image_uri,
  75. 'time_sample': now_time
  76. }
  77. res = requests.post(url=url, data=data, timeout=30)
  78. assert res.status_code == 200
  79. print(res.json())
  80. except Exception as e:
  81. ERROR_INFO_LOGGER.info(
  82. '发送Alexa事件线程异常,uid:{},error_line:{},error_msg:{}'.format(uid, e.__traceback__.tb_lineno, repr(e)))