Browse Source

请求Alexa服务器发送Snapshot事件

locky 2 weeks ago
parent
commit
a8d21dcb59
3 changed files with 40 additions and 8 deletions
  1. 3 0
      AnsjerPush/config.py
  2. 36 7
      Controller/AlexaController.py
  3. 1 1
      Object/RedisObject.py

+ 3 - 0
AnsjerPush/config.py

@@ -11,6 +11,7 @@ from firebase_admin import credentials
 
 # 日志配置
 LOGGER = logging.getLogger('info')
+ERROR_INFO_LOGGER = logging.getLogger('error_info')
 
 # 配置信息
 CONFIG_TEST = 'test'
@@ -71,10 +72,12 @@ AWS_ARN = ['arn:aws-cn:s3', 'arn:aws:s3']
 AWS_ACCESS_KEY_ID = ['AKIA2MMWBR4DSFG67DTG', 'AKIA2E67UIMD45Y3HL53']  # 0国内, 1国外
 AWS_SECRET_ACCESS_KEY = ['aI9gxcAKPmiGgPy9axrtFKzjYGbvpuytEX4xWweL', 'ckYLg4Lo9ZXJIcJEAKkzf2rWvs8Xth1FCjqiAqUw']
 
+# 服务器域名
 NGINX_RTMP_STAT = 'http://www.dvema.com/stat'
 SERVER_DOMAIN = 'http://www.dvema.com/'
 SERVER_DOMAIN_SSL = 'https://www.dvema.com/'
 DOMAIN_HOST = 'www.dvema.com'
+ALEXA_DOMAIN = 'https://www.zositech.xyz/'
 
 PAYPAL_CRD = {
     "mode": "live",  # sandbox or live

+ 36 - 7
Controller/AlexaController.py

@@ -3,12 +3,15 @@
 # @Time      : 2025/7/22 17:20
 import threading
 import time
+from typing import Dict, Union
 
+import requests
 from django.views import View
 
 from Model.models import AlexaPush
+from Object.RedisObject import RedisObject
 from Object.ResponseObject import ResponseObject
-from AnsjerPush.config import CONFIG_INFO, CONFIG_EUR
+from AnsjerPush.config import CONFIG_INFO, CONFIG_EUR, ALEXA_DOMAIN, ERROR_INFO_LOGGER
 from Service.DevicePushService import DevicePushService
 
 
@@ -51,10 +54,11 @@ class AlexaView(View):
             'event_time': event_time,
             'storage_location': storage_location,
         }
-        # 异步推送消息和保存数据
-        threading.Thread(
+        # 保存数据和发送Alexa事件
+        thread = threading.Thread(
             target=cls.save_data_and_send_alexa_event,
-            kwargs=kwargs).start()
+            kwargs=kwargs)
+        thread.start()
 
         kwargs.pop('event_type')
         res_data: str = DevicePushService.get_oci_req_url(**kwargs)
@@ -63,6 +67,31 @@ class AlexaView(View):
 
     @staticmethod
     def save_data_and_send_alexa_event(**kwargs):
-        kwargs['add_time'] = int(time.time())
-        AlexaPush.objects.create(**kwargs)
-        # 请求Alexa服务器发送 ObjectDetection 或 Snapshot 事件
+        uid: str = kwargs['uid']
+        try:
+            now_time: int = int(time.time())
+            kwargs['add_time']: int = now_time
+            AlexaPush.objects.create(**kwargs)
+
+            # 请求Alexa服务器发送 ObjectDetection 或 Snapshot 事件
+            url: str = ALEXA_DOMAIN + 'snapshot/asynEventResponse'
+            storage_location: str = kwargs['storage_location']
+            bucket: str = 'foreignpush'
+            redis_obj: RedisObject = RedisObject()
+            obj_name: str = '{}/{}/{}.jpeg'.format(uid, kwargs['channel'], kwargs['event_time'])
+            image_uri: str = DevicePushService.oci_object_url(
+                uid=uid, redis_obj=redis_obj, storage_location=storage_location, bucket=bucket, obj_name=obj_name)
+            print(image_uri)
+            # 快照
+            if kwargs['event_type'] == 0:
+                data: Dict[str, Union[str, int]] = {
+                    'uid': uid,
+                    'image_uri': image_uri,
+                    'time_sample': now_time
+                }
+                res = requests.post(url=url, data=data, timeout=30)
+                assert res.status_code == 200
+                print(res.json())
+        except Exception as e:
+            ERROR_INFO_LOGGER.info(
+                '发送Alexa事件线程异常,uid:{},error_line:{},error_msg:{}'.format(uid, e.__traceback__.tb_lineno, repr(e)))

+ 1 - 1
Object/RedisObject.py

@@ -2,7 +2,7 @@ import redis
 from AnsjerPush.config import REDIS_ADDRESS, CONFIG_INFO, CONFIG_US
 
 # 本地调试把注释打开
-# REDIS_ADDRESS = '127.0.0.1'
+REDIS_ADDRESS = '127.0.0.1'
 
 
 class RedisObject: