|
@@ -5,10 +5,12 @@ import threading
|
|
import time
|
|
import time
|
|
from collections import OrderedDict
|
|
from collections import OrderedDict
|
|
from datetime import datetime
|
|
from datetime import datetime
|
|
|
|
+from gc import enable
|
|
|
|
|
|
import requests
|
|
import requests
|
|
from django.http import JsonResponse
|
|
from django.http import JsonResponse
|
|
from django.shortcuts import render
|
|
from django.shortcuts import render
|
|
|
|
+from django.views import View
|
|
from django.views.generic import TemplateView
|
|
from django.views.generic import TemplateView
|
|
|
|
|
|
from azoauth.config import *
|
|
from azoauth.config import *
|
|
@@ -18,6 +20,7 @@ from object.ResObject import ResObject
|
|
from object.ResponseObject import ResponseObject
|
|
from object.ResponseObject import ResponseObject
|
|
from object.tkObject import tkObject
|
|
from object.tkObject import tkObject
|
|
from service.CommonService import CommonService
|
|
from service.CommonService import CommonService
|
|
|
|
+import uuid
|
|
|
|
|
|
|
|
|
|
class authView(TemplateView):
|
|
class authView(TemplateView):
|
|
@@ -979,3 +982,112 @@ class VesseTest(TemplateView):
|
|
except Exception as e:
|
|
except Exception as e:
|
|
print(e)
|
|
print(e)
|
|
return response.json(500, repr(e))
|
|
return response.json(500, repr(e))
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+logger = logging.getLogger('django')
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def get_uuid():
|
|
|
|
+ return str(uuid.uuid4())
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+class GetSnapshotView(View):
|
|
|
|
+ def post(self, request, *args, **kwargs):
|
|
|
|
+ request.encoding = 'utf-8'
|
|
|
|
+ request_dict = request.POST
|
|
|
|
+ return self.validate(request_dict)
|
|
|
|
+
|
|
|
|
+ def get(self, request, *args, **kwargs):
|
|
|
|
+ request.encoding = 'utf-8'
|
|
|
|
+ request_dict = request.GET
|
|
|
|
+ return self.validate(request_dict)
|
|
|
|
+
|
|
|
|
+ def validate(self, request_dict):
|
|
|
|
+ try:
|
|
|
|
+ uid = request_dict.get("uid", None)
|
|
|
|
+ access_token = request_dict.get("access_token", None)
|
|
|
|
+ correlation_token = request_dict.get('correlation_token', '')
|
|
|
|
+
|
|
|
|
+ if not uid or not access_token:
|
|
|
|
+ raise ValueError("endpoint_id or access_token is empty")
|
|
|
|
+
|
|
|
|
+ # 2. 验证用户和设备
|
|
|
|
+ user = UserModel.objects.get(access_token=access_token)
|
|
|
|
+ if not user.uid_rtsp.filter(uid=uid).exists():
|
|
|
|
+ raise PermissionError("Device not belong to user")
|
|
|
|
+
|
|
|
|
+ device = user.uid_rtsp.get(uid=uid)
|
|
|
|
+ region = getattr(device, 'region', 'US')
|
|
|
|
+ threading.Thread(
|
|
|
|
+ target=self._send_mqtt_snapshot_command,
|
|
|
|
+ args=(uid, region),
|
|
|
|
+ daemon=True
|
|
|
|
+ ).start()
|
|
|
|
+
|
|
|
|
+ # 5. 返回Alexa延迟响应
|
|
|
|
+ return JsonResponse({
|
|
|
|
+ "event": {
|
|
|
|
+ "header": {
|
|
|
|
+ "namespace": "Alexa",
|
|
|
|
+ "name": "DeferredResponse",
|
|
|
|
+ "messageId": get_uuid(),
|
|
|
|
+ "correlationToken": correlation_token,
|
|
|
|
+ "payloadVersion": "3"
|
|
|
|
+ },
|
|
|
|
+ "endpoint": {
|
|
|
|
+ "endpointId": uid
|
|
|
|
+ },
|
|
|
|
+ "payload": {
|
|
|
|
+ "estimatedDeferralInSeconds": 7
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ except Exception as e:
|
|
|
|
+ logger.error(f"Error: {str(e)}")
|
|
|
|
+ return JsonResponse({'error': 'INTERNAL_ERROR'}, status=500)
|
|
|
|
+
|
|
|
|
+ @staticmethod
|
|
|
|
+ def _send_mqtt_snapshot_command(uid,region):
|
|
|
|
+ thing_name = CommonService.query_serial_with_uid(uid)
|
|
|
|
+ topic_name='ansjer/generic/{}'.format(thing_name)
|
|
|
|
+
|
|
|
|
+ msg = {
|
|
|
|
+ "commandType": "alexaSnapshot",
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ result = CommonService.req_publish_mqtt_msg(thing_name, topic_name, msg,qos=1)
|
|
|
|
+ logger.info('快照指令下发结果 {}: {}'.format(uid, result))
|
|
|
|
+
|
|
|
|
+ if result:
|
|
|
|
+ return True
|
|
|
|
+
|
|
|
|
+ if region == 'EU':
|
|
|
|
+ domain_name = SERVER_PREFIX_EU
|
|
|
|
+ elif region == 'CN':
|
|
|
|
+ domain_name = SERVER_PREFIX_TEST
|
|
|
|
+ else:
|
|
|
|
+ domain_name = SERVER_PREFIX
|
|
|
|
+
|
|
|
|
+ url = '{}/iot/requestPublishMessage'.format(domain_name)
|
|
|
|
+ request_data = {'UID': uid, 'commandType': 'alexaSnapshot'}
|
|
|
|
+
|
|
|
|
+ try:
|
|
|
|
+ response = requests.post(url, data=request_data, timeout=10)
|
|
|
|
+ if response.status_code == 200:
|
|
|
|
+ result = response.json()
|
|
|
|
+ logger.info('远程MQTT接口返回: %s', result)
|
|
|
|
+ if result.get('result_code') == 0:
|
|
|
|
+ return True
|
|
|
|
+ else:
|
|
|
|
+ logger.error('远程MQTT接口业务失败, result_code=%s, reason=%s',
|
|
|
|
+ result.get('result_code'), result.get('reason'))
|
|
|
|
+ return False
|
|
|
|
+ else:
|
|
|
|
+ logger.error('远程MQTT接口调用失败, HTTP status=%s', response.status_code)
|
|
|
|
+ return False
|
|
|
|
+
|
|
|
|
+ except Exception as e:
|
|
|
|
+ logger.error('调用远程MQTT接口异常: %s', e)
|
|
|
|
+ return False
|