From 3a38ee28b93b9e9f8b0b64aef0a51524112f4ee5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=B9=E5=89=8D=E6=98=8E?= <909355014@qq.com> Date: Tue, 4 Oct 2022 11:19:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=95=E4=BE=8B=E6=A8=A1=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/am/views.py | 2 +- apps/develop/views.py | 4 ++-- apps/ecm/service.py | 5 +++-- apps/ecm/tasks.py | 3 +-- apps/hrm/serializers.py | 2 +- apps/hrm/services.py | 2 +- apps/hrm/views.py | 2 +- apps/rpm/serializers.py | 2 +- apps/third/clients.py | 6 ------ apps/third/dahua.py | 5 +++++ apps/third/speaker.py | 5 +++++ apps/third/views.py | 4 +++- apps/third/views_d.py | 4 +++- apps/third/xunxi.py | 5 +++++ apps/utils/tools.py | 10 ++++++++++ apps/vm/serializers.py | 2 +- 16 files changed, 43 insertions(+), 20 deletions(-) delete mode 100755 apps/third/clients.py diff --git a/apps/am/views.py b/apps/am/views.py index 5bc97458..70261da2 100755 --- a/apps/am/views.py +++ b/apps/am/views.py @@ -5,7 +5,7 @@ from apps.utils.viewsets import CustomModelViewSet, CustomGenericViewSet from django.db import transaction from rest_framework.decorators import action from rest_framework import serializers -from apps.third.clients import xxClient +from apps.third.xunxi import xxClient from apps.third.tapis import xxapis from rest_framework.response import Response from rest_framework.mixins import ListModelMixin, CreateModelMixin, DestroyModelMixin diff --git a/apps/develop/views.py b/apps/develop/views.py index 3f5b6809..8051191a 100755 --- a/apps/develop/views.py +++ b/apps/develop/views.py @@ -14,11 +14,11 @@ from rest_framework.exceptions import APIException from apps.ecm.service import check_not_in_place, create_remind, handle_xx_event, loc_change, notify_event, rail_in, snap_and_analyse from apps.ecm.tasks import opl_task, update_count_people from apps.opm.models import Opl -from apps.third.clients import dhClient +from apps.third.dahua import dhClient +from apps.third.speaker import spClient from apps.third.models import TDevice from apps.utils.sms import send_sms from apps.utils.speech import generate_voice -from apps.third.clients import spClient from apps.utils.viewsets import CustomGenericViewSet from apps.utils.wx import wxClient diff --git a/apps/ecm/service.py b/apps/ecm/service.py index eae94ec2..9c3a4cb3 100644 --- a/apps/ecm/service.py +++ b/apps/ecm/service.py @@ -15,14 +15,15 @@ from apps.utils.queryset import get_child_queryset2, get_parent_queryset from django.core.cache import cache import time from shapely.geometry import Point -from apps.third.clients import dhClient, spClient +from apps.third.dahua import dhClient +from apps.third.xunxi import xxClient +from apps.third.speaker import spClient from django.utils import timezone from django.conf import settings import os from apps.utils.speech import generate_voice from threading import Thread from apps.utils.tools import timestamp_to_time -from apps.third.clients import xxClient from apps.third.tapis import xxapis import logging myLogger = logging.getLogger('log') diff --git a/apps/ecm/tasks.py b/apps/ecm/tasks.py index 5769ae72..cad858d7 100644 --- a/apps/ecm/tasks.py +++ b/apps/ecm/tasks.py @@ -9,8 +9,7 @@ from apps.ecm.models import EventCate, Eventdo, Event from apps.ecm.service import notify_event, snap_and_analyse from apps.hrm.models import Employee from apps.opm.models import Opl -from apps.third.clients import xxClient -from apps.third.clients import dhClient +from apps.third.xunxi import xxClient from apps.third.models import TDevice from apps.third.tapis import xxapis from django.utils import timezone diff --git a/apps/hrm/serializers.py b/apps/hrm/serializers.py index f5e6440b..5b982f06 100755 --- a/apps/hrm/serializers.py +++ b/apps/hrm/serializers.py @@ -11,7 +11,7 @@ from apps.utils.constants import EXCLUDE_FIELDS from apps.hrm.models import Certificate, ClockRecord, Employee, NotWorkRemark from apps.system.serializers import DeptSimpleSerializer, UserSimpleSerializer from django.db import transaction -from apps.third.clients import dhClient +from apps.third.dahua import dhClient from apps.third.tapis import dhapis from django.conf import settings from django.core.cache import cache diff --git a/apps/hrm/services.py b/apps/hrm/services.py index 817522e4..3fad11d9 100755 --- a/apps/hrm/services.py +++ b/apps/hrm/services.py @@ -5,7 +5,7 @@ from apps.hrm.models import ClockRecord, Employee from apps.third.errors import DH_REQUEST_ERROR from apps.third.models import TDevice from apps.third.tapis import dhapis -from apps.third.clients import dhClient +from apps.third.dahua import dhClient from apps.utils.tools import rannum, ranstr from datetime import datetime from django.utils import timezone diff --git a/apps/hrm/views.py b/apps/hrm/views.py index fa5ba7b2..9dc442b2 100755 --- a/apps/hrm/views.py +++ b/apps/hrm/views.py @@ -21,7 +21,7 @@ from apps.hrm.serializers import (CertificateCreateUpdateSerializer, Certificate NotWorkRemarkListSerializer) from apps.hrm.services import HrmService -from apps.third.clients import dhClient +from apps.third.dahua import dhClient from apps.third.tapis import dhapis from apps.utils.viewsets import CustomGenericViewSet, CustomModelViewSet from rest_framework.permissions import IsAuthenticated diff --git a/apps/rpm/serializers.py b/apps/rpm/serializers.py index e9fc7d34..bffc8c71 100644 --- a/apps/rpm/serializers.py +++ b/apps/rpm/serializers.py @@ -10,7 +10,7 @@ from apps.system.serializers import DictSerializer, DictSimpleSerializer, FileSe from rest_framework import serializers from rest_framework.exceptions import ParseError from django.db import transaction -from apps.third.clients import dhClient +from apps.third.dahua import dhClient from apps.third.tapis import dhapis from apps.utils.tools import check_id_number_e, check_phone_e from apps.wf.serializers import TicketSimpleSerializer diff --git a/apps/third/clients.py b/apps/third/clients.py deleted file mode 100755 index c0f55aa9..00000000 --- a/apps/third/clients.py +++ /dev/null @@ -1,6 +0,0 @@ -from apps.third.xunxi import XxClient -from apps.third.dahua import DhClient -from apps.third.speaker import SpClient -dhClient = DhClient() -xxClient = XxClient() -spClient = SpClient() diff --git a/apps/third/dahua.py b/apps/third/dahua.py index 54813ef6..bea5c6c3 100644 --- a/apps/third/dahua.py +++ b/apps/third/dahua.py @@ -15,11 +15,13 @@ from apps.utils.my_rsa import encrypt_data from apps.utils.tools import convert_to_base64, print_roundtrip from django.utils.timezone import now from apps.third.tapis import dhapis +from apps.utils.tools import singleton requests.packages.urllib3.disable_warnings() myLogger = logging.getLogger('log') +@singleton class DhClient: """ 大华 @@ -278,3 +280,6 @@ class DhClient: "chnId": i['id'] } self.request(**dhapis['face_deploy'], json=json_data) + + +dhClient = DhClient() diff --git a/apps/third/speaker.py b/apps/third/speaker.py index 34e11179..4ce03d1a 100644 --- a/apps/third/speaker.py +++ b/apps/third/speaker.py @@ -13,11 +13,13 @@ 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 +from apps.utils.tools import singleton requests.packages.urllib3.disable_warnings() myLogger = logging.getLogger('log') +@singleton class SpClient: """ 音响接口 @@ -128,3 +130,6 @@ class SpClient: "errors": errors }) Tlog(**self.log).save() + + +spClient = SpClient() diff --git a/apps/third/views.py b/apps/third/views.py index ee8c56e1..0666ef7e 100755 --- a/apps/third/views.py +++ b/apps/third/views.py @@ -5,7 +5,9 @@ from apps.ecm.service import dispatch_dahua_event, handle_xx_event, loc_change, from apps.hrm.services import HrmService from apps.third.tapis import dhapis, xxapis, spapis from apps.third.errors import TAPI_CODE_WRONG -from apps.third.clients import dhClient, spClient, xxClient +from apps.third.dahua import dhClient +from apps.third.xunxi import xxClient +from apps.third.speaker import spClient from apps.utils.mixins import MyLoggingMixin from rest_framework.response import Response from rest_framework.views import APIView diff --git a/apps/third/views_d.py b/apps/third/views_d.py index a883c606..01ec0207 100644 --- a/apps/third/views_d.py +++ b/apps/third/views_d.py @@ -3,7 +3,9 @@ from apps.third.models import BltBind, TDevice, Tlog from apps.third.serializers import BindAreaSerializer, BltBindCreateSerializer, BltQuerySerializer, BltSerializer, LabelLocationSerializer, TDeviceSerializer, TDeviceUpdateSerializer, TlogSerializer from apps.utils.viewsets import CustomGenericViewSet from rest_framework.mixins import ListModelMixin, CreateModelMixin -from apps.third.clients import xxClient, dhClient, spClient +from apps.third.dahua import dhClient +from apps.third.xunxi import xxClient +from apps.third.speaker import spClient from apps.third.tapis import xxapis, dhapis, spapis from rest_framework.response import Response from rest_framework.serializers import Serializer diff --git a/apps/third/xunxi.py b/apps/third/xunxi.py index 97f79926..7712246f 100644 --- a/apps/third/xunxi.py +++ b/apps/third/xunxi.py @@ -12,10 +12,12 @@ from apps.third.models import Tlog from apps.utils.tools import print_roundtrip from django.utils.timezone import now from apps.third.tapis import xxapis +from apps.utils.tools import singleton requests.packages.urllib3.disable_warnings() myLogger = logging.getLogger('log') +@singleton class XxClient: """ 寻息 @@ -100,3 +102,6 @@ class XxClient: "data": {"buildIds": [settings.XX_BUILDID], "serverUrl": settings.BASE_URL_IN + rurl}, "licence": settings.XX_LICENCE} self.request(**xxapis['subscribe'], json=json_data) + + +xxClient = XxClient() diff --git a/apps/utils/tools.py b/apps/utils/tools.py index 8dace665..eacb1cdb 100755 --- a/apps/utils/tools.py +++ b/apps/utils/tools.py @@ -10,6 +10,16 @@ from io import BytesIO from rest_framework.serializers import ValidationError +def singleton(cls): + _instance = {} + + def inner(): + if cls not in _instance: + _instance[cls] = cls() + return _instance[cls] + return inner + + def print_roundtrip(response, *args, **kwargs): def format_headers(d): return '\n'.join(f'{k}: {v}' for k, v in d.items()) print(textwrap.dedent(''' diff --git a/apps/vm/serializers.py b/apps/vm/serializers.py index 4dda34ee..90348214 100644 --- a/apps/vm/serializers.py +++ b/apps/vm/serializers.py @@ -8,7 +8,7 @@ from apps.vm.models import Visit, Visitor, Vpeople from rest_framework import serializers from rest_framework.exceptions import ParseError from django.db import transaction -from apps.third.clients import dhClient +from apps.third.dahua import dhClient from apps.third.tapis import dhapis from apps.system.serializers import UserSimpleSerializer from apps.wf.serializers import TicketSerializer