import time from threading import Thread import traceback import uuid import requests from rest_framework.exceptions import APIException, ParseError from django.conf import settings from apps.third.errors import SP_REQUEST_ERROR from apps.third.models import Tlog from apps.utils.tools import print_roundtrip, ranstr from apps.third.tapis import spapis from django.utils.timezone import now requests.packages.urllib3.disable_warnings() class SpClient: """ 音响接口 """ def __init__(self, username=settings.SP_USERNAME, password=settings.SP_PASSWORD) -> None: if not settings.SP_ENABLED: return None self.username = username self.password = password self.headers = {} self.isGetingToken = False self.isRuning = True self.t = None # 线程 self.log = {} self.setup() def _get_token_loop(self): while self.isRuning: params = { "user": { "email": self.username, "password": self.password } } r = requests.post(json=params, url=settings.SP_BASE_URL + '/api/users/login', verify=False) if r.status_code == 200: ret = r.json() self.headers['Authorization'] = 'Bearer ' + ret['user']['token'] time.sleep(3600) def get_token(self): self.isGetingToken = True params = { 'grant_type': 'client_credentials', 'client_id': self.client_id, 'client_secret': self.client_secret } r = requests.post(params=params, url=settings.SP_BASE_URL + '/evo-apigw/evo-oauth/oauth/token', verify=False) if r.status_code == 200: ret = r.json() self.headers['Authorization'] = 'Bearer ' + ret['user']['token'] self.isGetingToken = False def setup(self): t = Thread(target=self._get_token_loop, args=(), daemon=True) t.start() def __del__(self): """ 自定义销毁 """ self.isRuning = False # self.t.join() def request(self, url: str, method: str, params=dict(), json=dict(), timeout=10, file_path_rela=None, raise_exception=True): if not settings.SP_ENABLED: raise ParseError('音响对接未启用') self.log = {"requested_at": now(), "id": uuid.uuid4(), "path": url, "method": method, "params": params, "body": json, "target": "dahua", "result": 10} files = None if file_path_rela: # 相对路径 files = {'file': open(settings.BASE_DIR + file_path_rela, 'rb')} try: if params: url = url.format(**params) r = getattr(requests, method)('{}{}'.format(settings.SP_BASE_URL, url), headers=self.headers, params=params, json=json, timeout=timeout, files=files, verify=False) except Exception: self.handle_log(result='error', errors=traceback.format_exc()) if raise_exception: raise APIException(**SP_REQUEST_ERROR) return 'error', SP_REQUEST_ERROR # if settings.DEBUG: # print_roundtrip(r) if r.status_code == 200: ret = r.json() if 'code' in ret and ret['code'] not in ['0', '100', '00000', '1000', 0, 100, 1000]: detail = '音响错误:{}'.format(str(ret.get('msg', ''))) err_detail = dict(detail=detail, code='sp_'+str(ret['code'])) self.handle_log(result='fail', response=ret) if raise_exception: raise ParseError(**err_detail) return 'fail', dict(detail=detail, code='sp_'+str(ret['code'])) # self.handle_log(result='success', response=ret) return 'success', ret self.handle_log(result='error', errors=traceback.format_exc()) if raise_exception: raise APIException(**SP_REQUEST_ERROR) return 'error', SP_REQUEST_ERROR def speak(self, path: str, sns: list, v_num: int): """播放报警声 Args: path (str): 资源本地地址 sns (list): 喇叭sn队列 v_num (int): 播放次数 """ uri = path if not uri.startswith('http'): uri = settings.BASE_URL + path json = { "sns": sns, "type": "req", "name": "songs_queue_append", "params": { "tid": ranstr(6), "vol": 100, "urls": [{ "name": "alarm.mp3", "uri": uri }] } } while v_num > 0: self.request(**spapis['send_to_device'], json=json) v_num = v_num - 1 return def _get_response_ms(self): """ Get the duration of the request response cycle is milliseconds. In case of negative duration 0 is returned. """ response_timedelta = now() - self.log["requested_at"] response_ms = int(response_timedelta.total_seconds() * 1000) return max(response_ms, 0) def handle_log(self, result, response=None, errors=None): self.log.update({ "result": result, "response": response, "response_ms": self._get_response_ms(), "errors": errors }) Tlog(**self.log).save()