From f0698ce3c274accb335c167542f96e68d4a1efcc 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, 13 Sep 2022 17:15:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=BB=E6=81=AF=E4=BF=AE=E6=94=B9=E4=B8=BAht?= =?UTF-8?q?tp=E8=AE=A2=E9=98=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/ecm/service.py | 37 -------------------- apps/third/tapis.py | 12 +++++++ apps/third/views.py | 85 ++++++++++++++++++++++++++++++++++++++------- apps/third/xunxi.py | 10 +++++- 4 files changed, 93 insertions(+), 51 deletions(-) diff --git a/apps/ecm/service.py b/apps/ecm/service.py index b2d2917a..9a09e427 100644 --- a/apps/ecm/service.py +++ b/apps/ecm/service.py @@ -382,43 +382,6 @@ def dispatch_dahua_event(data: dict): notify_event(event) -def dispatch_xunxi_event(data: dict): - """分发寻息事件进行处理 - """ - if data.type == 'rail': - if data.data.type == 1: - # 围栏进入 - rail_in(data=data.data) - elif data.data.type == 2: - # 围栏离开 - rail_out(data=data.data) - elif data.type == 'onKeyAlarm': - # 一键呼救 - handle_xx_event(name='one_key_alarm', data=data) - elif data.type == 'location': - # 定位信息 - loc_change(data=data) - elif data.type == 'onOffLine': - if data.data.online: - # 标签定位在线 - # blt_online(data=data.data) - pass - else: - # 标签定位离线 - # blt_offline(data=data.data) - pass - elif data.type == 'lowpower': - # 低电量 - handle_xx_event(name='low_power', data=data) - elif data.type == 'bltOnOffLineV2': - if data.data.online: - # 标签通信在线 - pass - else: - # 标签通信离线 - pass - - def rail_in(data): """围栏进入 """ diff --git a/apps/third/tapis.py b/apps/third/tapis.py index 6d3b4e19..d61b29fd 100755 --- a/apps/third/tapis.py +++ b/apps/third/tapis.py @@ -160,6 +160,18 @@ xxapis = { "url": "/api/devicesV3/bltInfoByMac", "method": "post" }, + "subscribe": { + "url": "/Subscribe", + "method": "post" + }, + "subscribe_list": { + "url": "/SubscribeList", + "method": "post" + }, + "unsubscribe": { + "url": "/Unsubscribe", + "method": "post" + } } diff --git a/apps/third/views.py b/apps/third/views.py index 0a8d4700..36b08a73 100755 --- a/apps/third/views.py +++ b/apps/third/views.py @@ -1,7 +1,7 @@ import json import logging from rest_framework.exceptions import ParseError, APIException -from apps.ecm.service import dispatch_dahua_event, dispatch_xunxi_event +from apps.ecm.service import dispatch_dahua_event, handle_xx_event, loc_change, rail_in, rail_out from apps.hrm.services import HrmService from apps.third.tapis import dhapis, xxapis, spapis from apps.third.errors import TAPI_CODE_WRONG @@ -16,6 +16,7 @@ from rest_framework.decorators import action from apps.third.serializers import PicSerializer, RequestCommonSerializer from rest_framework import serializers +from django.conf import settings import stomp from django.conf import settings myLogger = logging.getLogger('log') @@ -81,21 +82,21 @@ class XxTestView(APIView): # 寻息事件订阅 -class XxListener(stomp.ConnectionListener): - def on_error(self, frame): - print('received an error "%s"' % frame.body) +# class XxListener(stomp.ConnectionListener): +# def on_error(self, frame): +# print('received an error "%s"' % frame.body) - def on_message(self, frame): - data = json.loads(frame.body) - dispatch_xunxi_event(data) - print('received a message "%s"' % frame.body) +# def on_message(self, frame): +# data = json.loads(frame.body) +# dispatch_xunxi_event(data) +# print('received a message "%s"' % frame.body) -if settings.XX_ENABLED: - c = stomp.Connection([(settings.XX_MQ_HOST, settings.XX_MQ_PORT)]) - c.set_listener('', XxListener()) - c.connect(settings.XX_USERNAME, settings.XX_LICENCE) - c.subscribe(settings.XX_QUEUE, id='') +# if settings.XX_ENABLED: +# c = stomp.Connection([(settings.XX_MQ_HOST, settings.XX_MQ_PORT)]) +# c.set_listener('', XxListener()) +# c.connect(settings.XX_USERNAME, settings.XX_LICENCE) +# c.subscribe(settings.XX_QUEUE, id='') class XxCommonViewSet(CreateModelMixin, CustomGenericViewSet): @@ -142,6 +143,64 @@ class XxCommonViewSet(CreateModelMixin, CustomGenericViewSet): """ return Response({'token': xxClient.token}) + @action(methods=['post'], detail=False, + permission_classes=[IsAdminUser], + serializer_class=serializers.Serializer) + def subscribe(self, request, pk=None): + """寻息事件一键订阅 + + 寻息事件一键订阅 + """ + xxClient.subscribe('location', '/third/xunxi/c_location/') + xxClient.subscribe('rail', '/third/xunxi/c_rail/') + xxClient.subscribe('oneKeyAlarm', '/third/xunxi/c_one_key_alarm/') + xxClient.subscribe('lowpower', '/third/xunxi/c_lowpower/') + return Response() + + @action(methods=['post'], detail=False, + permission_classes=[IsAdminUser], + serializer_class=serializers.Serializer) + def unsubscribe(self, request, pk=None): + """取消订阅 + + 取消订阅 + """ + pass + + @action(methods=['post'], detail=False, + authentication_classes=[], permission_classes=[], + serializer_class=serializers.Serializer, logging_methods=[]) # 日志 + def c_location(self, request, pk=None): + loc_change(data=request.data) + return Response() + + @action(methods=['post'], detail=False, + authentication_classes=[], permission_classes=[], + serializer_class=serializers.Serializer, logging_methods=['POST']) # 日志 + def c_rail(self, request, pk=None): + data = request.data + if data.data.type == 1: + # 围栏进入 + rail_in(data=data.data) + elif data.data.type == 2: + # 围栏离开 + rail_out(data=data.data) + return Response() + + @action(methods=['post'], detail=False, + authentication_classes=[], permission_classes=[], + serializer_class=serializers.Serializer, logging_methods=['POST']) # 日志 + def c_one_key_alarm(self, request, pk=None): + handle_xx_event(name='one_key_alarm', data=request.data) + return Response() + + @action(methods=['post'], detail=False, + authentication_classes=[], permission_classes=[], + serializer_class=serializers.Serializer, logging_methods=[]) # 日志 + def c_lowpower(self, request, pk=None): + handle_xx_event(name='low_power', data=request.data) + return Response() + class DhCommonViewSet(CreateModelMixin, CustomGenericViewSet): perms_map = {'post': '*'} diff --git a/apps/third/xunxi.py b/apps/third/xunxi.py index d254c55f..4cfe2b7a 100644 --- a/apps/third/xunxi.py +++ b/apps/third/xunxi.py @@ -11,7 +11,7 @@ from apps.third.errors import XX_REQUEST_ERROR 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 requests.packages.urllib3.disable_warnings() myLogger = logging.getLogger('log') @@ -74,6 +74,7 @@ class XxClient: params['accessToken'] = self.token json['username'] = self.username json['buildId'] = settings.XX_BUILDID + json['licence'] = settings.XX_LICENCE self.log = {"requested_at": now(), "id": uuid.uuid4(), "path": url, "method": method, "params": params, "body": json, "target": "xunxi", "result": 10} if self.isGetingToken: @@ -126,3 +127,10 @@ class XxClient: "errors": errors }) Tlog(**self.log).save() + + def subscribe(self, type: str, rurl: str): + # 订阅 location, /third/xunxi/c_location + json_data = {"type": type, + "data": {"buildIds": [settings.XX_BUILDID], "serverUrl": settings.BASE_URL_IN + rurl}, + "licence": settings.XX_LICENCE} + self.request(**xxapis['subscribe'], json=json_data)