yf_yzl 2 лет назад
Родитель
Сommit
5ab95f4892
1 измененных файлов с 15 добавлено и 12 удалено
  1. 15 12
      smartfarming/views/device.py

+ 15 - 12
smartfarming/views/device.py

@@ -136,7 +136,8 @@ class CbdPhotoAPIView(APIView):
 
     def post(self, request):
         try:
-            request_data = request.data
+            request_data = request.body
+            request_data = json.loads(request_data)
             logger.warning(f"测报灯图片数据入库原数据: {request_data}")
             device_id = request_data.get("imei")
             device = MongoDevice.objects.filter(device_id=device_id)
@@ -144,12 +145,13 @@ class CbdPhotoAPIView(APIView):
             img_content_url = f'{config_dict.get("image_url").get("image")}/media'
             if device:
                 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}")
+                result_image = request_data.get("Image")
+                addr = config_dict.get("oss") + result_image if not result_image.startswith("http") else result_image
+                inden_path = f"{media}/cbd/{device.device_id}"
                 os.makedirs(inden_path) if not os.path.exists(inden_path) else None 
-                stamp = int(datetime.now().timestamp())
+                stamp = int(datetime.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")
@@ -157,19 +159,20 @@ class CbdPhotoAPIView(APIView):
                 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())
+                result_temp = request_data.get("Result_image")
+                indentify = config_dict.get("oss") + result_temp if not result_temp.startswith("http") else result_temp
+                inden_path_tp = f"{media}/result/cbd/{device.device_id}"
+                os.makedirs(inden_path_tp) if not os.path.exists(inden_path_tp) else None 
+                stamp = int(datetime.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_photo = os.path.join(inden_path_tp, f"{combined_id}.jpg")
                 indentify_remote_content = requests.get(indentify).content
                 with open(indentify_photo, "wb") as f:
                     f.write(indentify_remote_content)
                 data = {
                     "device_id": d_id,
-                    "addr": addr_org.replace(media, img_content_url),
+                    "addr": addr_org.replace(media, "/media"),
                     "indentify_photo":indentify_photo.replace(media, img_content_url),
                     "indentify_result": request_data.get("Result"),
                     "label": request_data.get("Result_code"),
@@ -183,7 +186,7 @@ class CbdPhotoAPIView(APIView):
             else:
                 return Response({"code": 2, "msg": "该项目不存在此设备"})
         except Exception as e:
-            logger.error(f"测报灯图片 {e.args}")
+            logger.error(f"测报灯图片入库失败 {e.args}")
             return Response({"code": 2, "msg": "failer"})