Parcourir la source

修改消息推送开关V2、设备版本一键同步其它服

zhangdongming il y a 2 mois
Parent
commit
ef993b21ca

+ 66 - 4
AdminController/DeviceManagementController.py

@@ -17,8 +17,9 @@ from django.forms.models import model_to_dict
 from django.utils import timezone
 from django.views.generic.base import View
 
+from Ansjer.cn_config.config_test import CONFIG_INFO
 from Ansjer.config import LOGGER, SERIAL_DOMAIN_NAME, HUAWEICLOUD_AK, HUAWEICLOUD_SK, HUAWEICLOUD_OBS_SERVER, \
-    HUAWEICLOUD_BAIDU_BIG_MODEL_LICENSE_BUKET
+    HUAWEICLOUD_BAIDU_BIG_MODEL_LICENSE_BUKET, CONFIG_TEST, CONFIG_CN, CONFIG_US, CONFIG_EUR
 from Ansjer.config import OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, \
     AWS_SES_ACCESS_REGION
 from Ansjer.config import SERVER_DOMAIN_TEST, SERVER_DOMAIN_CN, SERVER_DOMAIN_US, SERVER_DOMAIN_EUR
@@ -124,7 +125,7 @@ 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 == 'syncDeviceVersion':  # 删除设备型号版本
+            elif operation == 'syncDeviceVersion':  # 一键同步
                 return self.sync_device_version(request_dict, response)
 
             # 设备语音设置
@@ -1752,7 +1753,7 @@ class DeviceManagement(View):
             version_config = version_config_qs.first()
 
             # 将数据库字段映射为驼峰命名的响应数据
-            response_data = {
+            req_data = {
                 'dCode': version_config.d_code,
                 'softwareVer': version_config.software_ver,
                 'firmwareVer': version_config.firmware_ver,
@@ -1778,7 +1779,8 @@ class DeviceManagement(View):
                 'electricityStatistics': version_config.electricity_statistics,
                 'supportsPetTracking': version_config.supports_pet_tracking
             }
-
+            config_thread = threading.Thread(target=DeviceManagement.sync_device_version_config, kwargs=req_data)
+            config_thread.start()
             # 返回成功响应,包含设备版本配置信息
             return response.json(0)
 
@@ -1787,6 +1789,66 @@ class DeviceManagement(View):
             LOGGER.exception("同步设备配置失败")
             return response.json(500, f'Server error: {str(e)}')
 
+    @staticmethod
+    def sync_device_version_config(**req_data):
+        """
+           同步设备版本信息到本地数据库及外部服务器(含异常跳过逻辑)
+           """
+        try:
+            target_servers = []
+            if CONFIG_INFO == CONFIG_TEST:
+                target_servers = [
+                    "https://www.zositechc.cn/open/device/configuration/syncVerConfig",
+                    "https://www.dvema.com/open/device/configuration/syncVerConfig",
+                    "https://api.zositeche.com/open/device/configuration/syncVerConfig"
+                ]
+            elif CONFIG_INFO == CONFIG_CN:
+                target_servers = [
+                    "https://test.zositechc.cn/open/device/configuration/syncVerConfig",
+                    "https://www.dvema.com/open/device/configuration/syncVerConfig",
+                    "https://api.zositeche.com/open/device/configuration/syncVerConfig"
+                ]
+            elif CONFIG_INFO == CONFIG_US:
+                target_servers = [
+                    "https://test.zositechc.cn/open/device/configuration/syncVerConfig",
+                    "https://www.zositechc.cn/open/device/configuration/syncVerConfig",
+                    "https://api.zositeche.com/open/device/configuration/syncVerConfig"
+                ]
+            elif CONFIG_INFO == CONFIG_EUR:
+                target_servers = [
+                    "https://test.zositechc.cn/open/device/configuration/syncVerConfig",
+                    "https://www.zositechc.cn/open/device/configuration/syncVerConfig",
+                    "https://www.dvema.com/open/device/configuration/syncVerConfig"
+                ]
+
+            for server_url in target_servers:
+                try:
+                    # 发送请求并设置超时(10秒)
+                    headers = {'Content-Type': 'application/json'}
+                    resp = requests.post(
+                        server_url,
+                        json=req_data,
+                        headers=headers,
+                        timeout=10  # 超时时间
+                    )
+                    resp.raise_for_status()  # 抛出HTTP错误(如400/500)
+                    LOGGER.info(f"[{server_url}] 同步成功,响应:{resp.json()}")
+
+                except requests.exceptions.ConnectTimeout:
+                    LOGGER.error(f"[{server_url}] 连接超时,跳过")
+                    continue
+                except requests.exceptions.ReadTimeout:
+                    LOGGER.error(f"[{server_url}] 读取超时,跳过")
+                    continue
+                except requests.exceptions.RequestException as e:
+                    LOGGER.error(f"[{server_url}] 同步失败:{str(e)}")
+                    continue  # 跳过当前服务器
+            return True
+
+        except Exception as e:
+            LOGGER.exception("同步设备版本信息失败error{}".format(repr(e)))
+            return False
+
     @staticmethod
     def get_device_voice(request_dict, response):
         """

+ 11 - 1
Controller/DetectControllerV2.py

@@ -17,8 +17,9 @@ from Ansjer.config import DETECT_PUSH_DOMAIN, DETECT_PUSH_DOMAINS, DETECT_PUSH_D
 from Ansjer.config import PUSH_BUCKET, CONFIG_INFO, CONFIG_CN, CONFIG_EUR, CONFIG_US
 from Ansjer.config import PUSH_REDIS_ADDRESS
 from Model.models import Device_Info, Equipment_Info, UidSetModel, UidPushModel, CompanyModel, SysMsgModel, \
-    AiService, VodBucketModel
+    AiService, VodBucketModel, Device_User
 from Object.ETkObject import ETkObject
+from Object.Enums.RedisKeyConstant import RedisKeyConstant
 from Object.OCIObjectStorage import OCIObjectStorage
 from Object.RedisObject import RedisObject
 from Object.ResponseObject import ResponseObject
@@ -182,6 +183,7 @@ class DetectControllerViewV2(View):
             return response.json(444, 'appBundleId,app_type,token_val,uid,m_code')
 
         try:
+            LOGGER.info(f'v2消息推送uid:{uid},s:{status},tv:{token_val},ab{appBundleId},pt:{push_type}')
             # 判断用户是否拥有设备
             device_info_qs = Device_Info.objects.filter(userID_id=userID, UID=uid)
             if not device_info_qs.exists():
@@ -264,6 +266,14 @@ class DetectControllerViewV2(View):
                 uid_set_qs = UidSetModel.objects.create(**uid_set_data)
                 uid_set_id = uid_set_qs.id
 
+            if CONFIG_INFO == CONFIG_EUR:
+                user_qs = Device_User.objects.filter(userID=userID, region_country=91).values('userID')
+                if user_qs.exists():
+                    redisObj = RedisObject(host=PUSH_REDIS_ADDRESS)
+                    storage_key = RedisKeyConstant.PUSH_STORAGE_CONFIG_UID.value + uid
+                    redisObj.set_data(storage_key, 2, 3600 * 24 * 180)
+                    LOGGER.info(f'RU俄罗斯用户推送配置已更新UID:{uid}')
+
             # 初始化UidPushModel推送表
             if electricity_status:
                 if m_code != 0 and m_code != '0':

+ 1 - 1
Object/Enums/RedisKeyConstant.py

@@ -32,7 +32,7 @@ class RedisKeyConstant(Enum):
     # 活动信息
     ACTIVITY_INFO = 'ACTIVITY:INFO'
     # 推送存储位置
-    PUSH_STORAGE_LOCATION = 'PUSH:STORAGE:LOCATION:UID:'
+    PUSH_STORAGE_CONFIG_UID = 'push:storage:config:uid:'
 
     # Redis 过期时间常量 (秒)
     EXPIRE_TIME_60_SECONDS = 60    # 60秒