|
@@ -3,8 +3,11 @@ from rest_framework.response import Response
|
|
|
from django.conf import settings
|
|
from django.conf import settings
|
|
|
from django.core.paginator import Paginator
|
|
from django.core.paginator import Paginator
|
|
|
import time
|
|
import time
|
|
|
-import json
|
|
|
|
|
|
|
+import os
|
|
|
|
|
+import datetime
|
|
|
|
|
+import uuid
|
|
|
import logging
|
|
import logging
|
|
|
|
|
+import requests
|
|
|
from smartfarming.utils import get_addr_by_lag_lng
|
|
from smartfarming.utils import get_addr_by_lag_lng
|
|
|
from smartfarming.serializers.device_serializers import DeviceSerializers
|
|
from smartfarming.serializers.device_serializers import DeviceSerializers
|
|
|
from smartfarming.models.device import MongoDevice, MongoCBDData, MongoSCDData, MongoXYCBData
|
|
from smartfarming.models.device import MongoDevice, MongoCBDData, MongoSCDData, MongoXYCBData
|
|
@@ -21,8 +24,9 @@ from django.core.paginator import Paginator
|
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger("myapp")
|
|
logger = logging.getLogger("myapp")
|
|
|
-device_type_en = (settings.CONFIG).get("device_type_en")
|
|
|
|
|
-device_type_zh = (settings.CONFIG).get("device_type_zh")
|
|
|
|
|
|
|
+config_dict = settings.CONFIG
|
|
|
|
|
+device_type_en = config_dict.get("device_type_en")
|
|
|
|
|
+device_type_zh = config_dict.get("device_type_zh")
|
|
|
|
|
|
|
|
|
|
|
|
|
class CbdScdXyDeviceSaveAPIView(APIView):
|
|
class CbdScdXyDeviceSaveAPIView(APIView):
|
|
@@ -123,13 +127,37 @@ class CbdPhotoAPIView(APIView):
|
|
|
logger.info(f"测报灯图片数据入库原数据: {request_data}")
|
|
logger.info(f"测报灯图片数据入库原数据: {request_data}")
|
|
|
device_id = request_data.get("imei")
|
|
device_id = request_data.get("imei")
|
|
|
device = MongoDevice.objects.filter(device_id=device_id)
|
|
device = MongoDevice.objects.filter(device_id=device_id)
|
|
|
|
|
+ media = config_dict.get("media")
|
|
|
|
|
+ img_content_url = f'{config_dict.get("image_url").get("image")}/media'
|
|
|
if device:
|
|
if device:
|
|
|
device = device.first()
|
|
device = device.first()
|
|
|
d_id = device.id
|
|
d_id = device.id
|
|
|
|
|
+ # 把原图下载到本地
|
|
|
|
|
+ addr = config_dict.get("oss") + request_data.get("Result_image")
|
|
|
|
|
+ inden_path = os.path.join(media, f"/cbd/{device.device_id}")
|
|
|
|
|
+ os.makedirs(inden_path) if not os.path.exists(inden_path) else None
|
|
|
|
|
+ stamp = int(datetime.now().timestamp())
|
|
|
|
|
+ unique_id = uuid.uuid4()
|
|
|
|
|
+ combined_id = str(stamp) + "-" + str(unique_id)
|
|
|
|
|
+ addr_org = os.path.join(inden_path, f"{combined_id}.jpg")
|
|
|
|
|
+ remote_content = requests.get(addr).content
|
|
|
|
|
+ with open(addr_org, "wb") as f:
|
|
|
|
|
+ f.write(remote_content)
|
|
|
|
|
+ # 把识别后的图片下载到本地
|
|
|
|
|
+ indentify = config_dict.get("oss") + request_data.get("Result")
|
|
|
|
|
+ inden_path = os.path.join(media, f"/result/cbd/{device.device_id}")
|
|
|
|
|
+ os.makedirs(inden_path) if not os.path.exists(inden_path) else None
|
|
|
|
|
+ stamp = int(datetime.now().timestamp())
|
|
|
|
|
+ unique_id = uuid.uuid4()
|
|
|
|
|
+ combined_id = str(stamp) + "-" + str(unique_id)
|
|
|
|
|
+ indentify_photo = os.path.join(inden_path, f"{combined_id}.jpg")
|
|
|
|
|
+ indentify_remote_content = requests.get(indentify).content
|
|
|
|
|
+ with open(indentify_photo, "wb") as f:
|
|
|
|
|
+ f.write(indentify_remote_content)
|
|
|
data = {
|
|
data = {
|
|
|
"device_id": d_id,
|
|
"device_id": d_id,
|
|
|
- "addr": request_data.get("Image"),
|
|
|
|
|
- "indentify_photo": request_data.get("Result_image"),
|
|
|
|
|
|
|
+ "addr": addr_org.replace(media, img_content_url),
|
|
|
|
|
+ "indentify_photo":indentify_photo.replace(media, img_content_url),
|
|
|
"indentify_result": request_data.get("Result"),
|
|
"indentify_result": request_data.get("Result"),
|
|
|
"label": request_data.get("Result_code"),
|
|
"label": request_data.get("Result_code"),
|
|
|
"photo_status": 1,
|
|
"photo_status": 1,
|
|
@@ -140,7 +168,7 @@ class CbdPhotoAPIView(APIView):
|
|
|
photo.save()
|
|
photo.save()
|
|
|
return Response({"code": 0, "msg": "success"})
|
|
return Response({"code": 0, "msg": "success"})
|
|
|
else:
|
|
else:
|
|
|
- return Response({"code": 2, "msg": "该设备不存在本机器"})
|
|
|
|
|
|
|
+ return Response({"code": 2, "msg": "该项目不存在此设备"})
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
logger.error(f"测报灯图片 {e.args}")
|
|
logger.error(f"测报灯图片 {e.args}")
|
|
|
return Response({"code": 2, "msg": "failer"})
|
|
return Response({"code": 2, "msg": "failer"})
|