寻息修改为http订阅
This commit is contained in:
parent
f3355d871a
commit
f0698ce3c2
|
@ -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):
|
||||
"""围栏进入
|
||||
"""
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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': '*'}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue