|
@@ -1,21 +1,115 @@
|
|
|
-from rest_framework.views import APIView
|
|
|
|
|
-from rest_framework.viewsets import GenericViewSet
|
|
|
|
|
|
|
+from rest_framework import status
|
|
|
|
|
+from rest_framework.viewsets import ModelViewSet
|
|
|
from rest_framework.response import Response
|
|
from rest_framework.response import Response
|
|
|
|
|
+from rest_framework.decorators import action
|
|
|
|
|
+from rest_framework.serializers import ValidationError
|
|
|
|
|
+
|
|
|
|
|
+import requests
|
|
|
|
|
+import json
|
|
|
|
|
|
|
|
from .serializers import PlatSimInfoSerializer
|
|
from .serializers import PlatSimInfoSerializer
|
|
|
from .models import PlatSimInfo
|
|
from .models import PlatSimInfo
|
|
|
|
|
+from utils.paginations import CustomPagination
|
|
|
|
|
+from utils.utils import get_equip_list, Get_SIM_info
|
|
|
|
|
+
|
|
|
|
|
|
|
|
# Create your views here.
|
|
# Create your views here.
|
|
|
|
|
|
|
|
-class GetBaseType(APIView):
|
|
|
|
|
- def get(self, request):
|
|
|
|
|
|
|
+
|
|
|
|
|
+class PlatformIOTCardViewSet(ModelViewSet):
|
|
|
|
|
+ serializer_class = PlatSimInfoSerializer
|
|
|
|
|
+ pagination_class = CustomPagination
|
|
|
|
|
+ queryset = PlatSimInfo.objects.all().order_by("expiry_date")
|
|
|
|
|
+
|
|
|
|
|
+ @action(methods=['get'], detail=False, url_path='get_device_type', url_name='get_device_type')
|
|
|
|
|
+ def get_device_type(self, request, *args, **kwargs):
|
|
|
queryset = PlatSimInfo.objects.raw("SELECT id,device_type FROM plat_sim_info GROUP BY device_type;")
|
|
queryset = PlatSimInfo.objects.raw("SELECT id,device_type FROM plat_sim_info GROUP BY device_type;")
|
|
|
data = []
|
|
data = []
|
|
|
for i in queryset:
|
|
for i in queryset:
|
|
|
data.append(i.device_type)
|
|
data.append(i.device_type)
|
|
|
return Response(data)
|
|
return Response(data)
|
|
|
|
|
|
|
|
|
|
+ def list(self, request, *args, **kwargs):
|
|
|
|
|
+ device_type = request.query_params.get("device_type")
|
|
|
|
|
+ deviceId = request.query_params.get("deviceId")
|
|
|
|
|
+ simId = request.query_params.get("simId")
|
|
|
|
|
+ expire_start_time = request.query_params.get("expire_start_time")
|
|
|
|
|
+ expire_end_time = request.query_params.get("expire_end_time")
|
|
|
|
|
+ queryset = self.get_queryset()
|
|
|
|
|
+ if device_type:
|
|
|
|
|
+ queryset = queryset.filter(device_type=device_type)
|
|
|
|
|
+ if deviceId:
|
|
|
|
|
+ if len(deviceId.strip()) < 4:
|
|
|
|
|
+ raise ValidationError("设备号长度不能小于4位")
|
|
|
|
|
+ queryset = queryset.filter(deviceId__icontains=deviceId.strip())
|
|
|
|
|
+ if simId:
|
|
|
|
|
+ if len(simId.strip()) < 4:
|
|
|
|
|
+ raise ValidationError("物联网卡号长度不能小于4位")
|
|
|
|
|
+ queryset = queryset.filter(simId__icontains=simId.strip())
|
|
|
|
|
+ if expire_start_time:
|
|
|
|
|
+ queryset = queryset.filter(expiry_date__range=(expire_start_time, expire_end_time))
|
|
|
|
|
+ page = self.paginate_queryset(queryset)
|
|
|
|
|
+ serializer = self.get_serializer(page, many=True)
|
|
|
|
|
+ return self.get_paginated_response(serializer.data)
|
|
|
|
|
+
|
|
|
|
|
+ def create(self, request, *args, **kwargs):
|
|
|
|
|
+ device_id = request.data.get("device_id").strip()
|
|
|
|
|
+ simId = request.data.get("simId").strip()
|
|
|
|
|
+ if len(simId) > 20 or len(simId) < 19:
|
|
|
|
|
+ raise ValidationError("物联网卡长度介于19-20位")
|
|
|
|
|
+
|
|
|
|
|
+ if self.get_queryset().filter(simId=simId).exists():
|
|
|
|
|
+ raise ValidationError("该物联网卡号已存在,请核查!")
|
|
|
|
|
+
|
|
|
|
|
+ if self.get_queryset().filter(deviceId=device_id, input_type=2).exists():
|
|
|
|
|
+ raise ValidationError("该设备已存在手动录入物联网卡,请核查!")
|
|
|
|
|
+
|
|
|
|
|
+ device_info_list = get_equip_list(
|
|
|
|
|
+ d_id=device_id,
|
|
|
|
|
+ isfullId=1
|
|
|
|
|
+ )
|
|
|
|
|
+ if device_info_list:
|
|
|
|
|
+ device_info = device_info_list[0]
|
|
|
|
|
+ device_type = device_info["device_type"]
|
|
|
|
|
+ platform = device_info["plat"]
|
|
|
|
|
+ else:
|
|
|
|
|
+ device_type = ""
|
|
|
|
|
+ platform = ""
|
|
|
|
|
+ if device_type not in ["测报灯", "孢子仪"]:
|
|
|
|
|
+ raise ValidationError("不支持该类型设备手动录入物联网卡!")
|
|
|
|
|
+ if platform == "四情平台":
|
|
|
|
|
+ platformid = 1
|
|
|
|
|
+ res = requests.post(url="http://www.yfzhwlw.com/equip_simiccid", data={"e_id": device_id, "iccid": simId})
|
|
|
|
|
+ if res.text != "0":
|
|
|
|
|
+ raise ValidationError("对应平台添加异常!")
|
|
|
|
|
+ elif platform == "大数据平台":
|
|
|
|
|
+ platformid = 2
|
|
|
|
|
+ res = requests.post(url="http://8.136.98.49:8002/api/api_gateway?method=forecast.send_control.device_sim",
|
|
|
|
|
+ data={"device_id": device_id, "iccid": simId, "type": "change"})
|
|
|
|
|
+ res_json = json.loads(res.text)
|
|
|
|
|
+ if res_json["message"] != "":
|
|
|
|
|
+ raise ValidationError("对应平台添加异常!")
|
|
|
|
|
+ else:
|
|
|
|
|
+ raise ValidationError("平台不存在该设备ID,请核查!")
|
|
|
|
|
|
|
|
-class PlatformIOTCardViewSet(GenericViewSet):
|
|
|
|
|
- queryset = PlatSimInfo.objects.all()
|
|
|
|
|
- serializer_class = PlatSimInfoSerializer
|
|
|
|
|
|
|
+ sim_operators, account_status, active_date, data_plan, data_usage, data_balance, expiry_date = Get_SIM_info(
|
|
|
|
|
+ simId).get_sim_info()
|
|
|
|
|
+ request_data = {
|
|
|
|
|
+ "simId": simId,
|
|
|
|
|
+ "sim_operators": sim_operators,
|
|
|
|
|
+ "input_type": 2,
|
|
|
|
|
+ "deviceId": device_id,
|
|
|
|
|
+ "device_type": device_type,
|
|
|
|
|
+ "platform": platformid,
|
|
|
|
|
+ "account_status": account_status,
|
|
|
|
|
+ "active_date": active_date,
|
|
|
|
|
+ "data_plan": data_plan,
|
|
|
|
|
+ "data_usage": data_usage,
|
|
|
|
|
+ "data_balance": data_balance,
|
|
|
|
|
+ "expiry_date": expiry_date
|
|
|
|
|
+ }
|
|
|
|
|
+ serializer = self.get_serializer(data=request_data)
|
|
|
|
|
+ serializer.is_valid(raise_exception=True)
|
|
|
|
|
+ self.perform_create(serializer)
|
|
|
|
|
+ headers = self.get_success_headers(serializer.data)
|
|
|
|
|
+ return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
|