wxr
2024-10-28 505effa88b17ce8c3c7ac5403b76d9e5d0cfc2a5
HDL_ON/Common/ApiUtlis.cs
@@ -1,9 +1,10 @@
using System;
using System.Collections.Generic;
using HDL_ON;
using HDL_ON.DAL;
using System.Net.Http;
using System.Threading.Tasks;
using HDL_ON.DAL.Server;
using HDL_ON.Entity;
using HDL_ON.UI;
using HDL_ON.UI.UI2.FuntionControlView.Video;
using HDL_ON.UI.UI2.Intelligence.Automation;
using Newtonsoft.Json;
@@ -76,7 +77,7 @@
        /// <summary>
        /// token是否有效
        /// </summary>
        public bool tokenInvalid = false;
        public bool IsValidToken = false;
        public bool hadInternet = true;
        /// <summary>
@@ -109,7 +110,7 @@
                try
                {
                    //增加3秒云服务器连接检测,连不上服务器的时候不刷新数据
                    hadInternet = true;
                    //hadInternet = true;
                    var dataList = new List<GlobalRegionListRes>();
                    var requestJson = HttpUtil.GetSignRequestJson(new GetRegionListObj() { regionMark = HttpUtil.RegionMark });
                    var revertObj = HttpUtil.RequestHttpsPost(NewAPI.API_POST_GlobalRegionList, requestJson, HttpUtil.GlobalRequestHttpsHost, "", 3);
@@ -163,6 +164,7 @@
                }
                finally
                {
                    MainPage.Log("服务器连接状态:" + hadInternet);
                    if (!hadInternet)
                    {
                        Application.RunOnMainThread(() =>
@@ -178,9 +180,44 @@
                MainPage.Log($"进入读取云端数据线程");
                //===================刷新Token=======================
                code = Ins.HttpRequest.RefreshToken();
                if (code != StateCode.SUCCESS)
                {
                    DownloadDataComplete = true;
                    IsValidToken= false;
                    try
                    {
                        var spatialInfoDataBytes = FileUtlis.Files.ReadFile("SpatialInfoData");
                        if (spatialInfoDataBytes != null)
                        {
                            var spatialInfoDataString = System.Text.Encoding.UTF8.GetString(spatialInfoDataBytes);
                            if (spatialInfoDataString != null && !string.IsNullOrEmpty(spatialInfoDataString))
                            {
                                var spatialInfoDataList = JsonConvert.DeserializeObject<List<SpatialInfo>>(spatialInfoDataString);
                                SpatialInfo.CurrentSpatial.UpdateSpatialList(spatialInfoDataList);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MainPage.Log("Error", $"{this.GetType()}:加载缓存房间数据异常:{ex.StackTrace}");
                    }
                    try
                    {
                        var fileList = Common.FileUtlis.Files.ReadFiles();
                        foreach (var file in fileList)
                        {
                            FunctionList.List.IniFunctionList(file, true);
                        }
                        Room.CurrentSpatial.InitRoomListFunctions();
                    }
                    catch (Exception ex)
                    {
                        MainPage.Log("Error", $"{this.GetType()}:加载缓存设备数据异常:{ex.StackTrace}");
                    }
                    MainPage.Log($"刷新token失败");
                    if (waitPage != null)
                    {
                        Application.RunOnMainThread(() =>
@@ -194,8 +231,11 @@
                            catch { }
                        });
                    }
                    MainPage.Log($"刷新token失败");
                    return;
                }
                else
                {
                    hadInternet = true;
                }
                MainPage.Log($"刷新token成功");
                //===================刷新住宅信息=======================
@@ -240,26 +280,6 @@
                    return;
                }
                MainPage.Log($"刷新个人信息成功");
                //int count = 0;
                //while (count < 10)
                //{
                //    if (DB_ResidenceData.Instance.CheckWhetherGatewayIdIsNull())
                //    {
                //        System.Threading.Thread.Sleep(200);
                //    }
                //    else
                //    {
                //        break;
                //    }
                //    count++;
                //}
                //if (DB_ResidenceData.Instance.CheckWhetherGatewayIdIsNull())
                //{
                //    return;
                //}
                //return;
                try
                {
@@ -306,7 +326,7 @@
                            {
                                FunctionList.List.ClearDatas();
                                MainPage.Log($"读取设备信息成功");
                                MainPage.Log($"sid列表获取====\r\n", deviceResult.Data.ToString());
                                //MainPage.Log($"sid列表获取====\r\n", deviceResult.Data.ToString());
                                var deviceList = JsonConvert.DeserializeObject<DevcieApiPack>(deviceResult.Data.ToString());
                                if (deviceList == null)
                                {
@@ -377,7 +397,6 @@
                                //}
                                //======================获取可视对讲设备列表====================
                                bool isInterphoneType_HDL = false;//是否为HDLLinphone可视对讲
                                bool isInterphoneType_FREEVIEW = false;//是否为全视通可视对讲
                                var videoInfo = VideoSend.GetVideoInfoList();
                                if (videoInfo.Code == StateCode.SUCCESS)
@@ -393,69 +412,8 @@
                                        {
                                            UI.UI2.PersonalCenter.PirDevice.PirSend.LcSubAccessToken = video.subAccountToken;
                                        }
                                        else if (video.interphoneType == InterphoneType.HDL.ToString())
                                        {
                                            isInterphoneType_HDL = true;
                                            DB_ResidenceData.Instance.SupportFacePass = true;
                                            DB_ResidenceData.Instance.SaveResidenceData();
                                            DB_ResidenceData.Instance.SupportFVDevice = true;
                                        }
                                        else if (video.interphoneType == InterphoneType.FREEVIEW.ToString())
                                        {
#if __IOS__
                                            try
                                            {
                                                FunctionList.List.videoIntercom.Remove(video);
                                                --i;
                                            }catch(Exception ex)
                                            {
                                                MainPage.Log("Error", $"ios 全视通异常 : {ex.StackTrace}");
                                            }
#else
                                            //为全视通可视对讲
                                            DB_ResidenceData.Instance.SupportFacePass = true;
                                            Entity.DB_ResidenceData.Instance.SupportVisitorManage = true;
                                            Entity.DB_ResidenceData.Instance.SupportFVDevice = true;
                                            isInterphoneType_FREEVIEW = true;
#endif
                                        }
                                    }
                                }
                                //======================获取Sip账号并且初始化SDK可视对讲设备列表====================
                                //如果检测线程没启动,才继续下面操作 因为App有可能刚启动马上收到推送了,然后才执行到这里
                                if (HDLLinphone.Current.CheckIncomingCallThreadIsNull())
                                {
                                    //当前住宅是HDL可视对讲类型的设备
                                    if (isInterphoneType_HDL)
                                    {
                                        //HDLLinphone可视对讲,获取Sip账号并初始化SDK
                                        HDLCallVideoInfo callVideoInfo = new HDLCallVideoInfo();
                                        callVideoInfo.HomeId = DB_ResidenceData.Instance.CurrentRegion.id;
                                        callVideoInfo.InterphoneType = InterphoneType.HDL.ToString();
                                        HDLLinphone.Current.GetHDLSipInfoAndInitSDK(callVideoInfo);
                                    }
                                    else if (isInterphoneType_FREEVIEW)
                                    {
                                        //HDLLinphone可视对讲,获取Sip账号并初始化SDK
                                        HDLCallVideoInfo callVideoInfo = new HDLCallVideoInfo();
                                        callVideoInfo.HomeId = DB_ResidenceData.Instance.CurrentRegion.id;
                                        callVideoInfo.InterphoneType = InterphoneType.FREEVIEW.ToString();
                                        HDLLinphone.Current.GetHDLSipInfoAndInitSDK(callVideoInfo);
                                    }
                                    else
                                    {
                                        Utlis.WriteLine("当前住宅不支持HDL可视对讲,退出登录所有账号");
                                        //当前住宅不支持HDL可视对讲
                                        HDLLinphone.Current.LogoutAllAccount();
                                    }
                                }
                                //======================登录全视通====================
                                if (isInterphoneType_FREEVIEW)
                                {
@@ -707,6 +665,34 @@
                        catch (Exception ex){
                            MainPage.Log("Error", $"读取本地数据异常:{ex.StackTrace}");
                        }
                        try
                        {
                            HomePage.LoadEvent_CheckTokenValid();
                            if (IsValidToken && !UserInfo.Current.IsTipedInvalidToken)
                            {
                                UserInfo.Current.IsTipedInvalidToken = true;
                                UserInfo.Current.SaveUserInfo();
                                Application.RunOnMainThread(() =>
                                {
                                    try
                                    {
                                        new PublicAssmebly().TipOptionMsg(StringId.Tip, StringId.TokenInvalidTipMsg, StringId.OfflineControl, StringId.GoLogin, () =>
                                        {
                                            HDLCommon.Current.Logout();
                                        });
                                    }
                                    catch (Exception ex)
                                    {
                                        MainPage.Log("Error", $"提示登录凭证窗口加载异常:{ex.StackTrace}");
                                    }
                                });
                            }
                        }
                        catch (Exception ex)
                        {
                            MainPage.Log("Error", $"更新登录凭证方法调用异常:{ex.StackTrace}");
                        }
                    }
                }
            })
@@ -739,39 +725,50 @@
        {
            lock (FunctionList.List.securities)
            {
                var pack = Ins.HttpRequest.GetSecurityList();
                if (pack.Code == StateCode.SUCCESS)
                try
                {
                    var packList = JsonConvert.DeserializeObject<List<SecurityAlarm>>(pack.Data.ToString());
                    Console.WriteLine("安防总数:" + packList.Count);
                    var sidList = new List<string>();
                    foreach (var function in packList)
                    var pack = Ins.HttpRequest.GetSecurityList();
                    if (pack.Code == StateCode.SUCCESS)
                    {
                        sidList.Add(function.sid);
                    }
                    var infoListPack = Ins.httpRequest.GetSecurityInfo(sidList, new List<string>());
                    var infoList = JsonConvert.DeserializeObject<List<SecurityAlarm>>(infoListPack.Data.ToString());
                    if (infoList != null)
                    {
                        var filePathList = FileUtlis.Files.ReadFiles().FindAll((obj) => obj.StartsWith("SecurityData_"));
                        if (filePathList != null)
                        var packList = JsonConvert.DeserializeObject<List<SecurityAlarm>>(pack.Data.ToString());
                        Console.WriteLine("安防总数:" + packList.Count);
                        var sidList = new List<string>();
                        foreach (var function in packList)
                        {
                            foreach (var file in filePathList)
                            sidList.Add(function.sid);
                        }
                        if (sidList.Count == 0)
                        {
                            return;
                        }
                        var infoListPack = Ins.httpRequest.GetSecurityInfo(sidList, new List<string>());
                        var infoList = JsonConvert.DeserializeObject<List<SecurityAlarm>>(infoListPack.Data.ToString());
                        if (infoList != null)
                        {
                            var filePathList = FileUtlis.Files.ReadFiles().FindAll((obj) => obj.StartsWith("SecurityData_"));
                            if (filePathList != null)
                            {
                                FileUtlis.Files.DeleteFile(file);
                                foreach (var file in filePathList)
                                {
                                    FileUtlis.Files.DeleteFile(file);
                                }
                            }
                            FunctionList.List.securities.Clear();
                            foreach (var function in infoList)
                            {
                                function.SaveFile();
                                FunctionList.List.IniFunctionList(function.savePath, true);
                            }
                        }
                        FunctionList.List.securities.Clear();
                        foreach (var function in infoList)
                        {
                            function.SaveFile();
                            FunctionList.List.IniFunctionList(function.savePath, true);
                        }
                    }
                    else
                    {
                        MainPage.Log($"读取安防列表失败:Code:{pack.Code}; Msg:{pack.message}");
                    }
                }
                else
                catch (Exception ex)
                {
                    MainPage.Log($"读取安防列表失败:Code:{pack.Code}; Msg:{pack.message}");
                    MainPage.Log("Error", $"读取安防列表失败:Code:{ex.StackTrace}");
                }
            }
        }
@@ -784,6 +781,24 @@
            var pack = Ins.HttpRequest.GetSecurityList();
        }
        //public async Task<bool> CanConnectToHost()
        //{
        //    try
        //    {
        //        using (HttpClient client = new HttpClient())
        //        {
        //            client.Timeout = TimeSpan.FromSeconds(3); // 超时时间
        //            HttpResponseMessage response = await client.GetAsync(HttpUtil.GlobalRequestHttpsHost);
        //            return response.IsSuccessStatusCode;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        MainPage.Log(ex.StackTrace);
        //        return false;
        //    }
        //}
    }
}