359 lines
13 KiB
Python
359 lines
13 KiB
Python
from apps.third.filters import TDeviceFilterSet
|
|
from apps.third.models import BltBind, TDevice, Tlog
|
|
from apps.third.serializers import BindAreaSerializer, BltBindCreateSerializer, BltQuerySerializer, BltSerializer, LabelLocationSerializer, TDeviceSerializer, 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.tapis import xxapis, dhapis, spapis
|
|
from rest_framework.response import Response
|
|
from rest_framework.serializers import Serializer
|
|
from rest_framework.decorators import action
|
|
from apps.am.models import Area
|
|
from rest_framework.views import APIView
|
|
from rest_framework.exceptions import ParseError
|
|
from django.db import transaction
|
|
from rest_framework.mixins import ListModelMixin, CreateModelMixin, DestroyModelMixin
|
|
|
|
|
|
class BltViewSet(CustomGenericViewSet):
|
|
"""定位标签列表
|
|
|
|
定位标签列表
|
|
"""
|
|
queryset = TDevice.objects.filter(type=TDevice.DEVICE_BLT)
|
|
serializer_class = TDeviceSerializer
|
|
|
|
@action(methods=['get'], detail=False, perms_map={'get': '*'})
|
|
def count_bind(self, request):
|
|
"""统计绑定定位卡人/设备数
|
|
|
|
统计绑定定位卡人/设备数
|
|
"""
|
|
qs = self.queryset.exclude(employee=None)
|
|
ret = {}
|
|
ret['total'] = qs.count()
|
|
ret['count_employee'] = qs.filter(employee__type='employee').count()
|
|
ret['count_remployee'] = qs.filter(employee__type='remployee').count()
|
|
ret['count_visitor'] = qs.filter(employee__type='visitor').count()
|
|
return Response(ret)
|
|
|
|
@action(methods=['get'], detail=False, perms_map={'get': '*'})
|
|
def count_now(self, request):
|
|
"""统计在厂且绑定标签数
|
|
|
|
统计在厂且绑定标签数
|
|
"""
|
|
json = {
|
|
"pageNum": 1,
|
|
"numPerPage": 2000,
|
|
"online": "online"
|
|
}
|
|
_, res = xxClient.request(**xxapis['blt_list'], json=json)
|
|
ret = {}
|
|
ret['total_blt'] = res['totalCount']
|
|
blt_list = res['recordList']
|
|
macs = []
|
|
for i in blt_list:
|
|
macs.append(i['mac'])
|
|
qs = self.queryset.filter(code__in=macs).exclude(employee=None)
|
|
ret['total'] = qs.count()
|
|
ret['count_employee'] = qs.filter(employee__type='employee').count()
|
|
ret['count_remployee'] = qs.filter(employee__type='remployee').count()
|
|
ret['count_visitor'] = qs.filter(employee__type='visitor').count()
|
|
return Response(ret)
|
|
|
|
@action(methods=['post'], detail=False, perms_map={'post': '*'},
|
|
serializer_class=BltQuerySerializer)
|
|
def all(self, request):
|
|
"""全部在线标签列表信息
|
|
|
|
全部在线标签列表信息
|
|
"""
|
|
data = request.data
|
|
area = None
|
|
if data.get('area', None):
|
|
try:
|
|
area = Area.objects.get(id=data['area'])
|
|
railId = area.third_info['xx_rail']['id']
|
|
except Exception:
|
|
raise ParseError('区域信息不正确')
|
|
json = {
|
|
"railId": railId,
|
|
"type": ""
|
|
}
|
|
_, res = xxClient.request(**xxapis['rail_ibeacon_list'], json=json)
|
|
else:
|
|
json = {
|
|
"pageNum": 1,
|
|
"numPerPage": 2000,
|
|
"online": "online"
|
|
}
|
|
_, res = xxClient.request(**xxapis['blt_list'], json=json)
|
|
count_people = 0
|
|
blt_list = res['recordList']
|
|
macs = []
|
|
for i in blt_list:
|
|
macs.append(i['mac'])
|
|
qs = self.queryset.filter(code__in=macs).exclude(employee=None)
|
|
qs_data = BltSerializer(instance=qs, many=True).data
|
|
qs_dict = {}
|
|
for i in qs_data:
|
|
qs_dict[i['code']] = i
|
|
for i in blt_list:
|
|
i['my_info'] = {}
|
|
if i['mac'] in qs_dict:
|
|
i['my_info'] = qs_dict[i['mac']]
|
|
if area:
|
|
# 更新区域下的总人数
|
|
count_people = qs.filter(obj_cate='people').count()
|
|
area.count_people = count_people
|
|
area.save()
|
|
return Response(blt_list)
|
|
|
|
|
|
class TDeviceViewSet(ListModelMixin, DestroyModelMixin, CustomGenericViewSet):
|
|
"""
|
|
三方设备接口
|
|
"""
|
|
queryset = TDevice.objects.all()
|
|
serializer_class = TDeviceSerializer
|
|
ordering = ['-create_time']
|
|
filterset_class = TDeviceFilterSet
|
|
select_related_fields = ['employee', 'area', 'employee__post', 'employee__belong_dept']
|
|
|
|
def list(self, request, *args, **kwargs):
|
|
queryset = self.filter_queryset(self.get_queryset())
|
|
page = self.paginate_queryset(queryset)
|
|
if page is not None:
|
|
serializer = self.get_serializer(page, many=True)
|
|
else:
|
|
serializer = self.get_serializer(queryset, many=True)
|
|
data = serializer.data
|
|
typex = self.request.query_params.get('type', None)
|
|
if typex == str(TDevice.DEVICE_BLT): # 如果是定位标签加载三方信息
|
|
macs = []
|
|
for i in data:
|
|
macs.append(i['code'])
|
|
json = {
|
|
"macList": macs
|
|
}
|
|
_, res = xxClient.request(**xxapis['blt_info'], json=json)
|
|
macs_dict = {}
|
|
for i in res['success']:
|
|
macs_dict[i['mac']] = i
|
|
for i in res['error']:
|
|
macs_dict[i['mac']] = i
|
|
for i in data:
|
|
i['third_info'] = macs_dict.get(i['code'], {})
|
|
if page is not None:
|
|
return self.get_paginated_response(data)
|
|
else:
|
|
return Response(data)
|
|
|
|
@action(methods=['post'], detail=False, perms_map={'post': '*'},
|
|
serializer_class=Serializer)
|
|
@transaction.atomic
|
|
def blt_sync(self, request):
|
|
"""同步标签mac至平台
|
|
|
|
同步标签mac至平台
|
|
"""
|
|
json = {
|
|
"pageNum": 1,
|
|
"numPerPage": 1000
|
|
}
|
|
_, res = xxClient.request(**xxapis['blt_list'], json=json)
|
|
blt_list = res['recordList']
|
|
for i in blt_list:
|
|
TDevice.objects.get_or_create(code=i['mac'], defaults={"code": i['mac'], "type": TDevice.DEVICE_BLT})
|
|
return Response()
|
|
|
|
@action(methods=['post'], detail=False, perms_map={'post': '*'},
|
|
serializer_class=BltBindCreateSerializer)
|
|
@transaction.atomic
|
|
def blt_bind(self, request):
|
|
"""绑定/解绑定位卡
|
|
|
|
绑定/解绑定位卡
|
|
"""
|
|
serializer = BltBindCreateSerializer(data=request.data)
|
|
serializer.is_valid(raise_exception=True)
|
|
vdata = serializer.validated_data
|
|
blt = vdata['blt']
|
|
if vdata['type'] == BltBind.BLT_BIND and vdata['employee']:
|
|
if blt.employee:
|
|
raise ParseError('该定位卡已绑定人员,请先解绑')
|
|
else:
|
|
blt.employee = vdata['employee']
|
|
blt.obj_cate = 'people'
|
|
blt.save()
|
|
vdata['obj_cate'] = 'people'
|
|
BltBind.objects.create(**vdata)
|
|
elif vdata['type'] == BltBind.BLT_UNBIND:
|
|
if blt.employee:
|
|
blt.employee = None
|
|
blt.save()
|
|
vdata['obj_cate'] = 'people'
|
|
BltBind.objects.create(**vdata)
|
|
return Response()
|
|
|
|
@action(methods=['post'], detail=False, perms_map={'post': '*'},
|
|
serializer_class=Serializer)
|
|
def blt(self, request):
|
|
"""定位标签列表
|
|
|
|
定位标签列表
|
|
"""
|
|
_, res = xxClient.request(**xxapis['blt_list'], json=request.data)
|
|
macs = []
|
|
for i in res['recordList']:
|
|
macs.append(i['mac'])
|
|
qs = self.queryset.filter(code__in=macs)
|
|
qs_data = BltSerializer(instance=qs, many=True).data
|
|
qs_dict = {}
|
|
for i in qs_data:
|
|
qs_dict[i['code']] = i
|
|
for i in res['recordList']:
|
|
i['my_info'] = {}
|
|
if i['mac'] in qs_dict:
|
|
i['my_info'] = qs_dict[i['mac']]
|
|
return Response(res)
|
|
|
|
@action(methods=['post'], detail=False, perms_map={'post': '*'},
|
|
serializer_class=Serializer)
|
|
def vchannel(self, request):
|
|
"""
|
|
视频通道列表
|
|
|
|
视频通道列表
|
|
"""
|
|
request.data.update({'channelTypeList': ["1"]})
|
|
_, res = dhClient.request(**dhapis['channel_list'], json=request.data)
|
|
codes = []
|
|
if res.get('pageData', None):
|
|
for i in res['pageData']:
|
|
codes.append(i['channelCode'])
|
|
tds_info = TDeviceSerializer(instance=TDevice.objects.filter(code__in=codes), many=True).data
|
|
tds_dict = {}
|
|
for i in tds_info:
|
|
tds_dict[i['code']] = i
|
|
for i in res['pageData']:
|
|
i['my_info'] = {}
|
|
if i['channelCode'] in tds_dict:
|
|
i['my_info'] = tds_dict[i['channelCode']]
|
|
return Response(res)
|
|
|
|
@action(methods=['post'], detail=False, perms_map={'post': '*'},
|
|
serializer_class=Serializer)
|
|
def speaker(self, request):
|
|
"""
|
|
喇叭列表
|
|
|
|
喇叭列表
|
|
"""
|
|
_, res = spClient.request(**spapis['device_list'], params=request.data)
|
|
codes = []
|
|
for i in res['rows']:
|
|
codes.append(i['sn'])
|
|
tds_info = TDeviceSerializer(instance=TDevice.objects.filter(code__in=codes), many=True).data
|
|
tds_dict = {}
|
|
for i in tds_info:
|
|
tds_dict[i['code']] = i
|
|
for i in res['rows']:
|
|
i['my_info'] = {}
|
|
if i['sn'] in tds_dict:
|
|
i['my_info'] = tds_dict[i['sn']]
|
|
return Response(res)
|
|
|
|
@action(methods=['post'], detail=False, perms_map={'post': '*'},
|
|
serializer_class=Serializer)
|
|
def dchannel(self, request):
|
|
"""
|
|
闸机通道列表
|
|
|
|
闸机通道列表
|
|
"""
|
|
request.data.update({'channelTypeList': ["7"]})
|
|
_, res = dhClient.request(**dhapis['channel_list'], json=request.data)
|
|
codes = []
|
|
if res.get('pageData', None):
|
|
for i in res['pageData']:
|
|
codes.append(i['channelCode'])
|
|
tds_info = TDeviceSerializer(instance=TDevice.objects.filter(code__in=codes), many=True).data
|
|
tds_dict = {}
|
|
for i in tds_info:
|
|
tds_dict[i['code']] = i
|
|
for i in res['pageData']:
|
|
i['my_info'] = {}
|
|
if i['channelCode'] in tds_dict:
|
|
i['my_info'] = tds_dict[i['channelCode']]
|
|
return Response(res)
|
|
|
|
@action(methods=['post'], detail=False, perms_map={'post': 'tdevice:label_location'},
|
|
serializer_class=LabelLocationSerializer)
|
|
def label_location(self, request):
|
|
"""
|
|
标注坐标位置
|
|
|
|
标注坐标位置
|
|
"""
|
|
serializer = self.get_serializer(data=request.data)
|
|
serializer.is_valid(raise_exception=True)
|
|
vdata = serializer.validated_data
|
|
td = TDevice.objects.filter(code=vdata['code']).first()
|
|
if td:
|
|
td.location = vdata['location']
|
|
td.type = vdata['type']
|
|
td.area = vdata['area']
|
|
td.name = vdata['name']
|
|
td.update_by = request.user
|
|
td.save()
|
|
td.areas.clear()
|
|
for i in vdata['areas']:
|
|
td.areas.add(i)
|
|
else:
|
|
td = TDevice()
|
|
td.type = vdata['type']
|
|
td.code = vdata['code']
|
|
td.location = vdata['location']
|
|
td.area = vdata['area']
|
|
td.name = vdata['name']
|
|
td.create_by = request.user
|
|
td.save()
|
|
td.areas.clear()
|
|
for i in vdata['areas']:
|
|
td.areas.add(i)
|
|
return Response()
|
|
|
|
@action(methods=['post'], detail=False, perms_map={'post': 'tdevice:bind_area'},
|
|
serializer_class=BindAreaSerializer)
|
|
def bind_area(self, request):
|
|
"""
|
|
批量绑定所在区域
|
|
|
|
批量绑定所在区域
|
|
"""
|
|
serializer = self.get_serializer(data=request.data)
|
|
serializer.is_valid(raise_exception=True)
|
|
vdata = serializer.vdata
|
|
area = Area.objects.get(id=vdata['area'])
|
|
for i in vdata['codes']:
|
|
td = TDevice.objects.filter(code=i['code']).first()
|
|
if td:
|
|
td.area = area
|
|
td.update_by = request.user
|
|
td.save()
|
|
else:
|
|
td = TDevice()
|
|
td.type = TDevice.DEVICE_VCHANNEL
|
|
td.code = i['code']
|
|
td.area = area
|
|
td.create_by = request.user
|
|
td.save()
|
|
return Response()
|
|
|
|
|
|
class TlogViewSet(ListModelMixin, CustomGenericViewSet):
|
|
queryset = Tlog.objects.all()
|
|
serializer_class = TlogSerializer
|