Преглед изворни кода

s3对象添加复制方法、用户反馈识别失败处理接口

peng пре 2 година
родитељ
комит
333c582070
3 измењених фајлова са 113 додато и 5 уклоњено
  1. 99 5
      Controller/FeedBack.py
  2. 1 0
      Model/models.py
  3. 13 0
      Object/AWS/AmazonS3Util.py

+ 99 - 5
Controller/FeedBack.py

@@ -15,16 +15,19 @@
 import json
 import time
 
+import boto3
 import oss2
 from django.db import transaction
 from django.views.generic.base import View
 
-from Ansjer.config import OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET
-from Model.models import FeedBackModel, StatResModel
+from Ansjer.config import OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY
+from Model.models import FeedBackModel, StatResModel, EquipmentInfoMonday, EquipmentInfoTuesday, EquipmentInfoWednesday, \
+    EquipmentInfoThursday, EquipmentInfoFriday, EquipmentInfoSaturday, EquipmentInfoSunday, PushInaccurateFeedback
 from Object.ResponseObject import ResponseObject
 from Object.TokenObject import TokenObject
 from Service.CommonService import CommonService
 from Service.ModelService import ModelService
+from Object.AWS.AmazonS3Util import AmazonS3Util
 
 
 class FeedBackView(View):
@@ -304,7 +307,98 @@ class FeedBackView(View):
 
     @staticmethod
     def ai_recognition(user_id, request_dict, response):
-        equipment_info_id = request_dict.get('equipment_info_id', None)
-        if not equipment_info_id:
+        equipment_info_id = request_dict.get('equipmentInfoId', None)
+        region_id = request_dict.get('regionId', None)
+        if not all([equipment_info_id, region_id]):
             return response.json(444)
-        return response.json(0)
+        region_id = int(region_id)
+        week = int(equipment_info_id[0])
+        equipment_info_id = equipment_info_id[1:]
+        now_time = int(time.time())
+        try:
+            if region_id == 1:  # 国内
+                s3 = AmazonS3Util(
+                    aws_access_key_id=AWS_ACCESS_KEY_ID[0],
+                    secret_access_key=AWS_SECRET_ACCESS_KEY[0],
+                    region_name='cn-northwest-1'
+                )
+            else:  # 国外
+                s3 = AmazonS3Util(
+                    aws_access_key_id=AWS_ACCESS_KEY_ID[1],
+                    secret_access_key=AWS_SECRET_ACCESS_KEY[1],
+                    region_name='us-east-1'
+                )
+            if week == 1:
+                equipment_info_qs = EquipmentInfoMonday.objects.filter(id=equipment_info_id).values('device_uid',
+                                                                                                    'channel',
+                                                                                                    'event_time',
+                                                                                                    'event_type',
+                                                                                                    'is_st')
+            elif week == 2:
+                equipment_info_qs = EquipmentInfoMonday.objects.filter(id=equipment_info_id).values('device_uid',
+                                                                                                    'channel',
+                                                                                                    'event_time',
+                                                                                                    'event_type',
+                                                                                                    'is_st')
+
+            elif week == 3:
+                equipment_info_qs = EquipmentInfoWednesday.objects.filter(id=equipment_info_id).values('device_uid',
+                                                                                                       'channel',
+                                                                                                       'event_time',
+                                                                                                       'event_type',
+                                                                                                       'is_st')
+
+            elif week == 4:
+                equipment_info_qs = EquipmentInfoThursday.objects.filter(id=equipment_info_id).values('device_uid',
+                                                                                                      'channel',
+                                                                                                      'event_time',
+                                                                                                      'event_type',
+                                                                                                      'is_st')
+
+            elif week == 5:
+                equipment_info_qs = EquipmentInfoFriday.objects.filter(id=equipment_info_id).values('device_uid',
+                                                                                                    'channel',
+                                                                                                    'event_time',
+                                                                                                    'event_type',
+                                                                                                    'is_st')
+
+            elif week == 6:
+                equipment_info_qs = EquipmentInfoSaturday.objects.filter(id=equipment_info_id).values('device_uid',
+                                                                                                      'channel',
+                                                                                                      'event_time',
+                                                                                                      'event_type',
+                                                                                                      'is_st')
+
+            else:
+                equipment_info_qs = EquipmentInfoSunday.objects.filter(id=equipment_info_id).values('device_uid',
+                                                                                                    'channel',
+                                                                                                    'event_time',
+                                                                                                    'event_type',
+                                                                                                    'is_st')
+            if not equipment_info_qs.exists():
+                return response.json(173)
+            equipment_info = equipment_info_qs.first()
+            uid = equipment_info['device_uid']
+            channel = equipment_info['channel']
+            event_time = equipment_info['event_time']
+            event_type = equipment_info['event_type']
+            is_st = equipment_info['is_st']
+            if is_st != 3:
+                file_path = '{uid}/{channel}/{event_time}.jpeg'.format(uid=uid,
+                                                                       channel=channel,
+                                                                       event_time=event_time)
+                s3.copy_obj('push', 'push-inaccurate', file_path)
+            else:
+                for index in range(3):
+                    file_path = '{uid}/{channel}/{event_time}_{index}.jpeg'.format(uid=uid,
+                                                                                   channel=channel,
+                                                                                   event_time=event_time,
+                                                                                   index=index)
+                    s3.copy_obj('push', 'push-inaccurate', file_path)
+            PushInaccurateFeedback.objects.create(equipment_info_id=equipment_info_id,
+                                                  user_id=user_id, event_type=event_type,
+                                                  uid=uid, channel=channel, add_time=now_time,
+                                                  is_st=is_st)
+            return response.json(0)
+        except Exception as e:
+            return response.json(500, repr(e))

+ 1 - 0
Model/models.py

@@ -572,6 +572,7 @@ class PushInaccurateFeedback(models.Model):
     # 事件类型, 1,2,3,3:aws rekognition识别,其他为设备识别
     event_type = models.IntegerField(default=0, verbose_name='事件类型')
     add_time = models.IntegerField(default=0, verbose_name='添加时间')
+    is_st = models.SmallIntegerField(default=0, verbose_name='是否截图(0:否,1:图片,2:视频)')  # 0 否,1 是图,2,视频
 
     class Meta:
         db_table = 'push_inaccurate_feedback'

+ 13 - 0
Object/AWS/AmazonS3Util.py

@@ -110,3 +110,16 @@ class AmazonS3Util:
                 "存储桶 {} 不存在,或者你没有权限.".format(bucket_name))
             exists = False
         return exists
+
+    def copy_obj(self, source_bucket, to_bucket, file_key):
+        """
+        复制对象
+        @param source_bucket: 原存储桶
+        @param file_key: 文件名称
+        @param to_bucket: 新存储桶
+        """
+        source_dict = {
+            'Bucket': source_bucket,
+            'Key': file_key
+        }
+        self.session_conn.meta.client.copy(source_dict, to_bucket, file_key)