from rest_framework.exceptions import ParseError, APIException from apps.third.tapis import dhapis, xxapis from apps.third.erros import TAPI_CODE_WRONG from apps.third.clients import dhClient from apps.utils.errors import XX_REQUEST_ERROR from apps.utils.mixins import MyLoggingMixin from apps.third.clients import xxClient 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 RequestCommonSerializer from rest_framework import serializers import stomp from django.conf import settings # Create your views here. class DahuaTestView(MyLoggingMixin, APIView): """ 大华测试接口 """ permission_classes = [IsAuthenticated] def get(self, request, *args, **kwargs): # file_path_rela = '/media/default/avatar.png' # _, res = dhClient.request(**dhapis['person_img_upload'], file_path_rela=file_path_rela) _, res = dhClient.request( url='/evo-apigw/evo-brm/1.0.0/person/subsystem/{}'.format(2059335), method='get') return Response(res) class XxTestView(APIView): """ 寻息测试接口 """ permission_classes = [IsAuthenticated] def get(self, request, *args, **kwargs): 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): 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', ''): try: dhapi = dhapis[vdata['code']] except: raise ParseError(**TAPI_CODE_WRONG) vdata['url'] = dhapi['url'] vdata['method'] = dhapi['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) 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', ''): try: xxapi = xxapis[vdata['code']] except: 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=['post'], detail=False, permission_classes=[IsAdminUser], serializer_class=serializers.Serializer) def subscribe(self, request, pk=None): """事件订阅 事件订阅 """ json_data = { "param": { "monitors": [ { "monitor": settings.BASE_URL + request.path, "monitorType": "url", "events": [ { "category": "business", }, { "category": "alarm", }, { "category": "state", } ] } ], "subsystem": { "subsystemType": 0, "name": "127.0.0.1_8000", "magic": "127.0.0.1_8000" } } } 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':'127.0.0.1_8000'}) return Response() @action(methods=['post'], detail=False, authentication_classes=[], permission_classes=[], serializer_class=serializers.Serializer) def mq(self, request, pk=None): """大华事件处理 大华事件处理 """ data = request.data method = data['method'] 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': """ 刷卡事件 """ return Response()