yf_yzl 2 лет назад
Родитель
Сommit
4322c109d9
3 измененных файлов с 43 добавлено и 8 удалено
  1. 8 1
      formal
  2. 34 6
      smartfarming/views/device.py
  3. 1 1
      test

+ 8 - 1
formal

@@ -21,6 +21,7 @@
     "cbd": 3,
     "jiankong": 1
   },
+  "oss": "https://bigdata-image.oss-cn-hangzhou.aliyuncs.com/Basics/cbd/",
   "dashuju_pub_key":"dashuju_pub_redis_key",
   "ftp_config": {
     "/data/ftp/photo/dh_cbd/": ["dh_cbd_users", "yf123456", "/data/ftp/photo/dh_cbd/"],
@@ -54,7 +55,8 @@
   },
   "image_url": {
     "image":"http://60.14.40.10:8002",
-    "discern": "http://60.14.40.10:8002"
+    "discern": "http://60.14.40.10:8002",
+    "identify_url": "http://60.166.14.184:9143/upload"
   },
   "camera": {
     "lc_addr": "https://openapi.lechange.cn/openapi/bindDeviceLive",
@@ -69,5 +71,10 @@
     "lc_kit_token": "https://openapi.lechange.cn/openapi/getKitToken", 
     "ys_open": "https://open.ys7.com/api/lapp/live/video/open",
     "ys_get": "https://open.ys7.com/api/lapp/live/address/get"
+  },
+  "mqtt_config":{
+    "http": "http://8.136.98.49:8080/api/v4/mqtt/publish",
+    "user":"mq_yfkj",
+    "secret_key" : "Mjk2NjEyMDM4MDk3MjYyNjY3MzAyNzAyOTg5NDY1MzU0MjE"
   }
 }

+ 34 - 6
smartfarming/views/device.py

@@ -3,8 +3,11 @@ from rest_framework.response import Response
 from django.conf import settings
 from django.core.paginator import Paginator
 import time
-import json
+import os
+import datetime
+import uuid
 import logging
+import requests
 from smartfarming.utils import get_addr_by_lag_lng
 from smartfarming.serializers.device_serializers import DeviceSerializers
 from smartfarming.models.device import MongoDevice, MongoCBDData, MongoSCDData, MongoXYCBData
@@ -21,8 +24,9 @@ from django.core.paginator import Paginator
 
 
 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):
@@ -123,13 +127,37 @@ class CbdPhotoAPIView(APIView):
             logger.info(f"测报灯图片数据入库原数据: {request_data}")
             device_id = request_data.get("imei")
             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:
                 device = device.first()
                 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 = {
                     "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"),
                     "label": request_data.get("Result_code"),
                     "photo_status": 1,
@@ -140,7 +168,7 @@ class CbdPhotoAPIView(APIView):
                 photo.save()
                 return Response({"code": 0, "msg": "success"}) 
             else:
-                return Response({"code": 2, "msg": "该设备不存在本机器"})
+                return Response({"code": 2, "msg": "该项目不存在此设备"})
         except Exception as e:
             logger.error(f"测报灯图片 {e.args}")
             return Response({"code": 2, "msg": "failer"})

+ 1 - 1
test

@@ -21,8 +21,8 @@
       "scd": 2,
       "cbd": 3,
       "jiankong": 1
-
     },
+    "oss": "https://bigdata-image.oss-cn-hangzhou.aliyuncs.com/Basics/cbd/",
     "dashuju_pub_key":"dashuju_pub_redis_key",
     "ftp_config": {
       "/data/ftp/photo/dh_cbd/": ["dh_cbd_users", "yf123456", "/data/ftp/photo/dh_cbd/"],