279 lines
		
	
	
		
			8.8 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
			
		
		
	
	
			279 lines
		
	
	
		
			8.8 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
| import json
 | |
| import logging
 | |
| from rest_framework.exceptions import ParseError, APIException
 | |
| from apps.ecm.service import dispatch_dahua_event, dispatch_xunxi_event
 | |
| 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.utils.mixins import MyLoggingMixin
 | |
| from rest_framework.response import Response
 | |
| from rest_framework.views import APIView
 | |
| from rest_framework.permissions import IsAuthenticated, IsAdminUser
 | |
| from apps.utils.viewsets import CustomGenericViewSet
 | |
| from rest_framework.mixins import CreateModelMixin
 | |
| from rest_framework.decorators import action
 | |
| 
 | |
| from apps.third.serializers import PicSerializer, RequestCommonSerializer
 | |
| from rest_framework import serializers
 | |
| import stomp
 | |
| from django.conf import settings
 | |
| myLogger = logging.getLogger('log')
 | |
| # Create your views here.
 | |
| 
 | |
| 
 | |
| class DahuaTestView(MyLoggingMixin, APIView):
 | |
|     """大华测试接口
 | |
| 
 | |
|     大华测试接口
 | |
|     """
 | |
|     permission_classes = [IsAuthenticated]
 | |
| 
 | |
|     def get(self, request, *args, **kwargs):
 | |
|         # res = dhClient.face_search(path='/media/2022/07/13/three.jpg')
 | |
|         res = dhClient.face_deploy()
 | |
|         return Response(res)
 | |
| 
 | |
| 
 | |
| class SpTestView(APIView):
 | |
|     """
 | |
|     音响测试接口
 | |
|     """
 | |
|     permission_classes = [IsAuthenticated]
 | |
| 
 | |
|     def get(self, request, *args, **kwargs):
 | |
|         """
 | |
|         音响测试接口
 | |
| 
 | |
|         音响测试接口
 | |
|         """
 | |
|         # spClient.speak('http://1.203.161.103:2800/media/2022/07/15/alarm2.mp3', ['ls20://0201874E9530'], v_num=2)
 | |
|         # return Response()
 | |
|         # params = {"page": 1, "pageSize": 10000}
 | |
|         # _, res = spClient.request(**spapis['device_list'], params=params)
 | |
|         _, res = spClient.speak(path='http://192.168.10.249:20309/media/2022/09/01/test.mp3',
 | |
|                                 sns=['ls20://020277AA7D06'], v_num=5)
 | |
|         return Response(res)
 | |
| 
 | |
| 
 | |
| class XxTestView(APIView):
 | |
|     """
 | |
|     寻息测试接口
 | |
|     """
 | |
|     permission_classes = [IsAuthenticated]
 | |
| 
 | |
|     def get(self, request, *args, **kwargs):
 | |
|         json = {
 | |
|             "railId": "bc0ed1151524490589a9a519685f188f",
 | |
|             "type": ""
 | |
|         }
 | |
|         _, res = xxClient.request(**xxapis['rail_ibeacon_list'], json=json)
 | |
|         return Response(res)
 | |
|         # ok, res = xxClient.request(
 | |
|         #     url='/api/application/build/buildListV2', json={})
 | |
|         # if ok == 'success':
 | |
|         #     return Response(res)
 | |
|         # elif ok == 'fail':
 | |
|         #     raise ParseError(**res)
 | |
|         # else:
 | |
|         #     raise APIException(**res)
 | |
| 
 | |
| # 寻息事件订阅
 | |
| 
 | |
| 
 | |
| 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)
 | |
| 
 | |
| 
 | |
| # 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):
 | |
|     perms_map = {'post': '*'}
 | |
|     serializer_class = RequestCommonSerializer
 | |
| 
 | |
|     def create(self, request, *args, **kwargs):
 | |
|         """
 | |
|         寻息通用调用接口
 | |
| 
 | |
|         寻息通用调用接口
 | |
|         """
 | |
|         serializer = self.get_serializer(data=request.data)
 | |
|         serializer.is_valid(raise_exception=True)
 | |
|         vdata = serializer.validated_data
 | |
|         if vdata.get('code', ''):
 | |
|             xxapi = xxapis.get(vdata['code'], None)
 | |
|             if xxapi is None:
 | |
|                 raise ParseError(**TAPI_CODE_WRONG)
 | |
|             vdata['url'] = xxapi['url']
 | |
|             vdata['method'] = xxapi['method']
 | |
|         _, res = xxClient.request(
 | |
|             url=vdata['url'],
 | |
|             method=vdata.get('method', 'post'),
 | |
|             params=vdata.get('params', {}),
 | |
|             json=vdata.get('json', {}))
 | |
|         return Response(res)
 | |
| 
 | |
|     @action(methods=['get'], detail=False,
 | |
|             permission_classes=[IsAuthenticated])
 | |
|     def codes(self, request, pk=None):
 | |
|         """获取请求短标识
 | |
| 
 | |
|         获取请求短标识
 | |
|         """
 | |
|         return Response(xxapis)
 | |
| 
 | |
|     @action(methods=['get'], detail=False,
 | |
|             permission_classes=[IsAuthenticated])
 | |
|     def token(self, request, pk=None):
 | |
|         """获取token
 | |
| 
 | |
|         获取token
 | |
|         """
 | |
|         return Response({'token': xxClient.token})
 | |
| 
 | |
| 
 | |
| class DhCommonViewSet(CreateModelMixin, CustomGenericViewSet):
 | |
|     perms_map = {'post': '*'}
 | |
|     serializer_class = RequestCommonSerializer
 | |
| 
 | |
|     def create(self, request, *args, **kwargs):
 | |
|         """
 | |
|         大华通用调用接口
 | |
| 
 | |
|         大华通用调用接口
 | |
|         """
 | |
|         serializer = self.get_serializer(data=request.data)
 | |
|         serializer.is_valid(raise_exception=True)
 | |
|         vdata = serializer.validated_data
 | |
|         if vdata.get('code', ''):
 | |
|             xxapi = dhapis.get(vdata['code'], None)
 | |
|             if xxapi is None:
 | |
|                 raise ParseError(**TAPI_CODE_WRONG)
 | |
|             vdata['url'] = xxapi['url']
 | |
|             vdata['method'] = xxapi['method']
 | |
|         _, res = dhClient.request(
 | |
|             url=vdata['url'],
 | |
|             method=vdata.get('method', 'post'),
 | |
|             params=vdata.get('params', {}),
 | |
|             json=vdata.get('json', {}))
 | |
|         return Response(res)
 | |
| 
 | |
|     @action(methods=['get'], detail=False,
 | |
|             permission_classes=[IsAuthenticated])
 | |
|     def codes(self, request, pk=None):
 | |
|         """获取请求短标识
 | |
| 
 | |
|         获取请求短标识
 | |
|         """
 | |
|         return Response(dhapis)
 | |
| 
 | |
|     @action(methods=['get'], detail=False,
 | |
|             permission_classes=[IsAuthenticated])
 | |
|     def token(self, request, pk=None):
 | |
|         """获取token
 | |
| 
 | |
|         获取token
 | |
|         """
 | |
|         return Response({'token': dhClient.token})
 | |
| 
 | |
|     @action(methods=['post'], detail=False,
 | |
|             permission_classes=[IsAuthenticated],
 | |
|             serializer_class=PicSerializer)
 | |
|     def full_pic(self, request, pk=None):
 | |
|         """获取完整图片地址
 | |
| 
 | |
|         获取完整图片地址
 | |
|         """
 | |
|         return Response({'url': dhClient.get_full_pic(path=request.data.get('path'))})
 | |
| 
 | |
|     @action(methods=['post'], detail=False,
 | |
|             permission_classes=[IsAdminUser],
 | |
|             serializer_class=serializers.Serializer)
 | |
|     def subscribe(self, request, pk=None):
 | |
|         """事件订阅
 | |
| 
 | |
|         事件订阅
 | |
|         """
 | |
|         json_data = {
 | |
|             "param": {
 | |
|                 "monitors": [
 | |
|                     {
 | |
|                         "monitor": settings.BASE_URL + '/api/third/dahua/mq/',
 | |
|                         "monitorType": "url",
 | |
|                         "events": [
 | |
|                             # {
 | |
|                             #     "category": "business",
 | |
|                             # },
 | |
|                             {
 | |
|                                 "category": "alarm",
 | |
|                             },
 | |
|                             # {
 | |
|                             #     "category": "state",
 | |
|                             # }
 | |
|                         ]
 | |
|                     }
 | |
|                 ],
 | |
|                 "subsystem": {
 | |
|                     "subsystemType": 0,
 | |
|                     "name": settings.DAHUA_SUBSCRIBE,
 | |
|                     "magic": settings.DAHUA_SUBSCRIBE
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
|         dhClient.request(**dhapis['mq_subscribe'], json=json_data)
 | |
|         return Response()
 | |
| 
 | |
|     @action(methods=['delete'], detail=False,
 | |
|             permission_classes=[IsAdminUser],
 | |
|             serializer_class=serializers.Serializer)
 | |
|     def unsubscribe(self, request, pk=None):
 | |
|         """取消事件订阅
 | |
| 
 | |
|         取消事件订阅
 | |
|         """
 | |
|         dhClient.request(**dhapis['mq_unsubscribe'],
 | |
|                          params={'name': settings.DAHUA_SUBSCRIBE})
 | |
|         return Response()
 | |
| 
 | |
|     @action(methods=['post'], detail=False,
 | |
|             authentication_classes=[], permission_classes=[],
 | |
|             serializer_class=serializers.Serializer, logging_methods=['POST'])  # 日志
 | |
|     def mq(self, request, pk=None):
 | |
|         """大华事件处理
 | |
| 
 | |
|         大华事件处理
 | |
|         """
 | |
|         # data = json.loads(request.body)
 | |
|         data = request.data
 | |
|         method = data['method']
 | |
|         category = data['category']
 | |
|         subsystem = data.get('subsystem', None)
 | |
|         # info = data.get('info', {})
 | |
|         if method == 'department.update':
 | |
|             pass
 | |
|         elif method == 'person.update':
 | |
|             pass
 | |
|         elif method == 'alarm.msg' and subsystem == 'evo-accesscontrol':
 | |
|             """
 | |
|             刷卡事件
 | |
|             """
 | |
|             HrmService.swipe(data=data)
 | |
|         elif category == 'alarm':
 | |
|             """
 | |
|             其他报警转到事件派发
 | |
|             """
 | |
|             # myLogger.info(data)
 | |
|             dispatch_dahua_event(data=data)
 | |
|         return Response()
 |