linhaohong 5 сар өмнө
parent
commit
7c3ad0c8c9

+ 241 - 2
AdminController/DeviceManagementController.py

@@ -9,7 +9,7 @@ import oss2
 import requests
 from django.core.paginator import Paginator
 from django.db import transaction
-from django.db.models import Q, F, Sum
+from django.db.models import Q, F, Sum, OuterRef, Min, Subquery
 from django.forms.models import model_to_dict
 from django.views.generic.base import View
 
@@ -20,7 +20,7 @@ from Ansjer.config import SERVER_DOMAIN_TEST, SERVER_DOMAIN_CN, SERVER_DOMAIN_US
 from Model.models import Device_Info, UidSetModel, LogModel, UID_Bucket, Unused_Uid_Meal, StsCrdModel, \
     VodHlsModel, ExperienceContextModel, DeviceTypeModel, UidUserModel, ExperienceAiModel, AiService, \
     AppBundle, App_Info, AppDeviceType, DeviceNameLanguage, UIDCompanySerialModel, UidPushModel, \
-    CustomCustomerOrderInfo, CustomCustomerDevice, DeviceVersionInfo
+    CustomCustomerOrderInfo, CustomCustomerDevice, DeviceVersionInfo, VoicePromptModel, DeviceAlgorithmExplain
 from Object.AWS.AmazonS3Util import AmazonS3Util
 from Object.Enums.RedisKeyConstant import RedisKeyConstant
 from Object.RedisObject import RedisObject
@@ -116,6 +116,20 @@ class DeviceManagement(View):
                 return self.edit_device_ver_info(request_dict, response)
             elif operation == 'delDeviceVerInfo':  # 删除设备型号版本
                 return self.del_device_ver_info(request_dict, response)
+
+            # 设备语音设置
+            elif operation == 'getDeviceVoice':  # 获取设备音频
+                return self.get_device_voice(request_dict, response)
+            elif operation == 'algorithmTypeList':  # 添加设备型号版本
+                return self.algorithm_type_list(response)
+            elif operation == 'addDeviceVoice':  # 获取设备音频列表
+                return self.add_device_voice(request, request_dict, response)
+            elif operation == 'editDeviceVoice':
+                return self.edit_device_voice(request_dict, response)
+            elif operation == 'delDeviceVoice':
+                return self.del_device_voice(request_dict, response)
+            elif operation == 'deviceTypeList':
+                return self.device_type_list(response)
             else:
                 return response.json(444, 'operation')
 
@@ -1661,3 +1675,228 @@ class DeviceManagement(View):
         device_version_info_qs.delete()
         return response.json(0)
 
+    @staticmethod
+    def get_device_voice(request_dict, response):
+        """
+        获取设备音频
+        @param request_dict:
+        @param response:
+        @return:
+        """
+        title = request_dict.get('title', None)
+        voice_type = request_dict.get('voiceType', None)
+        classification = request_dict.get('classification', None)
+        uid = request_dict.get('uid', None)
+        algorithm_type = request_dict.get('algorithmType', None)
+        status = request_dict.get('status', None)
+        page = request_dict.get('page', 1)
+        page_size = request_dict.get('pageSize', 20)
+        try:
+            voice_prompt_qs = VoicePromptModel.objects.all()
+            if title:
+                voice_prompt_qs = voice_prompt_qs.filter(title__icontains=title)
+            if voice_type:
+                voice_prompt_qs = voice_prompt_qs.filter(type=voice_type)
+            if classification:
+                voice_prompt_qs = voice_prompt_qs.filter(classification=classification)
+            if uid:
+                voice_prompt_qs = voice_prompt_qs.filter(uid=uid)
+            if algorithm_type:
+                voice_prompt_qs = voice_prompt_qs.filter(algorithm_type=algorithm_type)
+            if status:
+                voice_prompt_qs = voice_prompt_qs.filter(status=status)
+
+            # 分页
+            paginator = Paginator(voice_prompt_qs.order_by("-add_time"), page_size)  # 每页显示 page_size 条
+            voice_prompt_page = paginator.get_page(page)  # 获取当前页的数据
+
+            # 上传文件到阿里云OSS
+            auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
+            bucket = oss2.Bucket(auth, 'https://oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
+            voice_prompt_list = []
+            for voice_prompt in voice_prompt_page:
+                filename = voice_prompt.filename
+                obj = 'voice_prompt/system/' + filename
+                filename_url = bucket.sign_url('GET', obj, 3600)
+                voice_prompt_list.append({
+                    "id": voice_prompt.id,
+                    "title": voice_prompt.title,
+                    "type": voice_prompt.type,
+                    "classification": voice_prompt.classification,
+                    "filename": filename,
+                    "filenameUrl": filename_url,
+                    "language": voice_prompt.language,
+                    "status": voice_prompt.status,
+                    "addTime": voice_prompt.add_time,
+                    "algorithmType": voice_prompt.algorithm_type,
+                    "uid": voice_prompt.uid,
+                    "device_types": voice_prompt.device_types
+                })
+            return response.json(0, {'list': voice_prompt_list, 'total': paginator.count})
+        except Exception as e:
+            print(e)
+            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+
+    @staticmethod
+    def algorithm_type_list(response):
+        """
+        算法类型列表
+        @param response:
+        @return:
+        """
+        try:
+            device_algorithm_explain_qs = DeviceAlgorithmExplain.objects.filter(lang='cn').values('title', 'algorithm_type_id')
+            device_algorithm_explain = list(device_algorithm_explain_qs)
+            device_algorithm_explain.append({"title": "无", "algorithm_type_id": 99})
+            return response.json(0, device_algorithm_explain)
+        except Exception as e:
+            print(e)
+            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+
+    @staticmethod
+    def add_device_voice(request, request_dict, response):
+        """
+        添加设备音频
+        @param request
+        @param request_dict:
+        @param response:
+        @return:
+        """
+        title = request_dict.get('title', None)
+        voice_type = request_dict.get('voiceType', None)
+        classification = request_dict.get('classification', None)
+        algorithm_type = request_dict.get('algorithmType', None)
+        status = request_dict.get('status', None)
+        language = request_dict.get('language', None)
+        uid = request_dict.get('uid', None)
+        channel = request_dict.get('channel', 1)
+        device_types = request_dict.get('deviceTypes', "")
+        voice_file = request.FILES.get('voiceFile', None)
+
+        if not all([title, voice_type, classification, algorithm_type, status, language, voice_file]):
+            return response.json(444)
+        try:
+            device_type_list = device_types.split(',')
+            device_type_list = list(map(int, device_type_list))
+            classification = int(classification)
+            auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
+            bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
+            filename = CommonService.createOrderID() + ".g711a"
+            voice_prompt_dict = {
+                "title": title,
+                "type": voice_type,
+                "classification": classification,
+                "filename": filename,
+                "language": language,
+                "status": status,
+                "algorithm_type": algorithm_type,
+                "add_time": int(time.time()),
+                "device_types": device_type_list
+            }
+            if uid and classification == 1:
+                voice_prompt_dict["uid"] = uid
+                obj = 'voice_prompt/{uid}/{channel}/'.format(uid=uid, channel=channel) + filename
+                bucket.put_object(obj, voice_file)
+            elif uid and classification == 0:
+                return response.json(178)
+            else:
+                obj = 'voice_prompt/system/' + filename
+                bucket.put_object(obj, voice_file)
+            VoicePromptModel.objects.create(**voice_prompt_dict)
+            return response.json(0)
+        except Exception as e:
+            print(e)
+            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+
+    @staticmethod
+    def edit_device_voice(request_dict, response):
+        """
+        添加设备音频
+        @param request:
+        @param request_dict:
+        @param response:
+        @return:
+        """
+        device_voice_id = request_dict.get('deviceVoiceId', None)
+        title = request_dict.get('title', None)
+        voice_type = request_dict.get('voiceType', None)
+        classification = request_dict.get('classification', None)
+        algorithm_type = request_dict.get('algorithmType', None)
+        status = request_dict.get('status', None)
+        language = request_dict.get('language', None)
+        device_types = request_dict.get('deviceTypes', "")
+        try:
+            voice_prompt = VoicePromptModel.objects.get(pk=device_voice_id)
+            if title:
+                voice_prompt.title = title
+            if voice_type:
+                voice_prompt.type = voice_type
+            if classification:
+                voice_prompt.classification = classification
+            if algorithm_type:
+                voice_prompt.algorithm_type = algorithm_type
+            if status:
+                voice_prompt.status = status
+            if language:
+                voice_prompt.language = language
+            if device_types:
+                device_type_list = device_types.split(',')
+                device_type_list = list(map(int, device_type_list))
+                voice_prompt.device_types = device_type_list
+            voice_prompt.save()
+            return response.json(0)
+        except Exception as e:
+            print(e)
+            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+
+
+    @staticmethod
+    def del_device_voice(request_dict, response):
+        """
+        删除设备音频
+        @param request_dict:
+        @param response:
+        @return:
+        """
+        device_voice_id = request_dict.get('deviceVoiceId', None)
+        try:
+            voice_prompt_qs = VoicePromptModel.objects.filter(id=device_voice_id)
+            filename = voice_prompt_qs[0].filename
+            classification = voice_prompt_qs[0].classification
+            uid = voice_prompt_qs[0].uid
+            channel = voice_prompt_qs[0].channel
+            auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
+            bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
+            if classification == 1 and uid:
+                obj = 'voice_prompt/{uid}/{channel}/'.format(uid=uid, channel=channel) + filename
+                bucket.delete_object(obj)
+            else:
+                obj = 'voice_prompt/system/' + filename
+                bucket.delete_object(obj)
+            voice_prompt_qs.delete()
+            return response.json(0)
+        except Exception as e:
+            print(e)
+            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+
+    @staticmethod
+    def device_type_list(response):
+        """
+        获取设备类型
+        @param response:
+        @return:
+        """
+        try:
+            subquery = DeviceTypeModel.objects.filter(
+                type=OuterRef('type')
+            ).values('type').annotate(
+                min_id=Min('id')
+            ).values('min_id')
+            # 根据最小 id 获取对应的 name 和 type
+            device_type_qs = DeviceTypeModel.objects.filter(
+                id__in=Subquery(subquery)
+            ).values("name", "type")
+            return response.json(0, list(device_type_qs))
+        except Exception as e:
+            print(e)
+            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))