speaker.py 优化

This commit is contained in:
caoqianming 2022-12-23 17:42:04 +08:00
parent 76b1fa0b82
commit d388871e7c
1 changed files with 24 additions and 21 deletions

View File

@ -7,6 +7,7 @@ import logging
import requests import requests
from rest_framework.exceptions import APIException, ParseError from rest_framework.exceptions import APIException, ParseError
from django.conf import settings from django.conf import settings
from requests.exceptions import RequestException
from apps.third.errors import SP_REQUEST_ERROR from apps.third.errors import SP_REQUEST_ERROR
from apps.third.models import Tlog from apps.third.models import Tlog
@ -61,27 +62,29 @@ class SpClient:
files = {'file': open(settings.BASE_DIR + file_path_rela, 'rb')} files = {'file': open(settings.BASE_DIR + file_path_rela, 'rb')}
if params: if params:
url = url.format(**params) url = url.format(**params)
r = getattr(requests, method)('{}{}'.format(settings.SP_BASE_URL, url), try:
headers=self.headers, params=params, json=json, r = getattr(requests, method)('{}{}'.format(settings.SP_BASE_URL, url),
timeout=timeout, files=files, verify=False) headers=self.headers, params=params, json=json,
# if settings.DEBUG: timeout=timeout, files=files, verify=False)
# print_roundtrip(r) # if settings.DEBUG:
ret = r.text # print_roundtrip(r)
if 300 > r.status_code >= 200: ret = r.text
ret = r.json() if 300 > r.status_code >= 200:
if 'code' in ret and ret['code'] not in ['0', '100', '00000', '1000', 0, 100, 1000]: ret = r.json()
detail = '音响错误:{}'.format(str(ret.get('msg', ''))) if 'code' in ret and ret['code'] not in ['0', '100', '00000', '1000', 0, 100, 1000]:
err_detail = dict(detail=detail, code='sp_'+str(ret['code'])) detail = '音响错误:{}'.format(str(ret.get('msg', '')))
self.handle_log(result='fail', response=ret) err_detail = dict(detail=detail, code='sp_'+str(ret['code']))
if raise_exception: self.handle_log(result='fail', response=ret)
raise ParseError(**err_detail) if raise_exception:
return 'fail', err_detail raise ParseError(**err_detail)
# self.handle_log(result='success', response=ret) return 'fail', err_detail
return 'success', ret # self.handle_log(result='success', response=ret)
self.handle_log(result='error', response=ret) return 'success', ret
if raise_exception: except RequestException:
raise APIException(**SP_REQUEST_ERROR) self.handle_log(result='error', response=ret)
return 'error', SP_REQUEST_ERROR if raise_exception:
raise APIException(**SP_REQUEST_ERROR)
return 'error', SP_REQUEST_ERROR
def speak(self, path: str, sns: list, v_num: int): def speak(self, path: str, sns: list, v_num: int):
"""播放报警声 """播放报警声