AlexaController.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # @Author : Rocky
  2. # @File : AlexaController.py
  3. # @Time : 2025/7/22 17:20
  4. import threading
  5. import time
  6. from django.views import View
  7. from Model.models import AlexaPush
  8. from Object.ResponseObject import ResponseObject
  9. from AnsjerPush.config import CONFIG_INFO, CONFIG_EUR
  10. from Service.DevicePushService import DevicePushService
  11. class AlexaView(View):
  12. def get(self, request, *args, **kwargs):
  13. request.encoding = 'utf-8'
  14. operation = kwargs.get('operation')
  15. return self.validation(request.GET, request, operation)
  16. def post(self, request, *args, **kwargs):
  17. request.encoding = 'utf-8'
  18. operation = kwargs.get('operation')
  19. return self.validation(request.POST, request, operation)
  20. def validation(self, request_dict, request, operation):
  21. response = ResponseObject()
  22. if operation == 'ObjectDetectionOrSnapshot':
  23. return self.object_detection_or_snapshot(request_dict, response)
  24. else:
  25. return response.json(414)
  26. @classmethod
  27. def object_detection_or_snapshot(cls, request_dict, response):
  28. uid = request_dict.get('uid', None)
  29. channel = request_dict.get('channel', '1')
  30. event_type = request_dict.get('event_type', None)
  31. event_time = request_dict.get('event_time', None)
  32. if not all([uid, event_type, event_time]):
  33. return response.json(444)
  34. # 欧洲: OCI伦敦, 其他: OCI凤凰城
  35. storage_location: int = 4 if CONFIG_INFO == CONFIG_EUR else 3
  36. kwargs = {
  37. 'uid': uid,
  38. 'channel': channel,
  39. 'event_type': int(event_type),
  40. 'event_time': event_time,
  41. 'storage_location': storage_location,
  42. }
  43. # 异步推送消息和保存数据
  44. threading.Thread(
  45. target=cls.save_data_and_send_alexa_event,
  46. kwargs=kwargs).start()
  47. kwargs.pop('event_type')
  48. res_data: str = DevicePushService.get_oci_req_url(**kwargs)
  49. return response.json(0, res_data)
  50. @staticmethod
  51. def save_data_and_send_alexa_event(**kwargs):
  52. kwargs['add_time'] = int(time.time())
  53. AlexaPush.objects.create(**kwargs)
  54. # 请求Alexa服务器发送 ObjectDetection 或 Snapshot 事件