using System; using System.Collections.Generic; using HDL_ON; using HDL_ON.DAL; using HDL_ON.DAL.Server; using HDL_ON.Entity; using HDL_ON.UI.UI2.Intelligence.Automation; using Newtonsoft.Json; using Shared; namespace HDL_ON.Common { public class ApiUtlis { public ApiUtlis() { } static ApiUtlis apiUtlis; public static ApiUtlis Ins { get { if (apiUtlis == null) { apiUtlis = new ApiUtlis(); } return apiUtlis; } } HttpServerRequest httpRequest; public HttpServerRequest HttpRequest { get { if (httpRequest == null) { httpRequest = new HttpServerRequest(); } return httpRequest; } } /// /// 读取设备功能数据完成 /// bool complateDevice = false; /// /// 读取场景数据完成 /// bool complateScene = false; //线程结束时间 DateTime endTime1; /// /// 校验网关是否在线 /// public bool GatewayOnlineVerification() { bool result = DB_ResidenceData.Instance.HomeGateway.gatewayStatus; DriverLayer.Control.Ins.GatewayOnline_Cloud = result; if (!result) { } return result; } /// /// 下载数据完成标记 /// public bool DownloadDataComplete = false; /// /// 下载数据 /// public void DownloadData() { if (MainPage.InternetStatus == 0 || MainPage.NoLoginMode) return; endTime1 = DateTime.Now.AddSeconds(25); complateDevice = complateScene = false; MainPage.Log($"开始同步云端数据"); DownloadDataComplete = false; var waitPage = new Loading(); MainPage.BaseView.AddChidren(waitPage); waitPage.Start(Language.StringByID(StringId.PleaseWait)); string code = StateCode.SUCCESS; Inverter.Ins.H5Page = null;//重置光伏页面 var downloadDataThread = new System.Threading.Thread(() => { try { MainPage.Log($"进入读取云端数据线程"); //===================刷新Token======================= code = Ins.HttpRequest.RefreshToken(); if (code != StateCode.SUCCESS) { MainPage.Log($"刷新token失败"); return; } MainPage.Log($"刷新token成功"); //===================刷新住宅信息======================= code = Ins.HttpRequest.GetHomePager(); if (code != StateCode.SUCCESS) { MainPage.Log($"刷新住宅信息失败"); return; } MainPage.Log($"刷新住宅信息成功"); //===================刷新个人信息======================= code = Ins.HttpRequest.GetUserInfo(); if (code != StateCode.SUCCESS) { MainPage.Log($"刷新个人信息失败"); return; } MainPage.Log($"刷新个人信息成功"); } catch (Exception ex) { MainPage.Log($"刷新基础数据失败"); } if (string.IsNullOrEmpty( DB_ResidenceData.Instance.CurrentRegion.id)) { return; } try { //===================房间======================= var roomResult = Ins.HttpRequest.GetRoomList(); if (roomResult.Code == StateCode.SUCCESS) { MainPage.Log($"读取房间信息成功"); var revData = Newtonsoft.Json.JsonConvert.DeserializeObject(roomResult.Data.ToString()); if (revData == null) { revData = new SpatialApiPack(); } { SpatialInfo.CurrentSpatial.UpdateSpatialList(revData.list); } } else { MainPage.Log($"读取房间数据失败:Code:{roomResult.Code}; msg:{roomResult.message}"); } //===================设备======================= new System.Threading.Thread(() => { try { complateDevice = false; var deviceResult = Ins.HttpRequest.GetDeviceList("30", "1"); if (deviceResult.Code == StateCode.SUCCESS) { FunctionList.List.ClearDatas(); MainPage.Log($"============设备============开始" + FunctionList.List.Functions.Count); //MainPage.Log($"读取设备信息成功"); //MainPage.Log($"sid列表获取====" + deviceResult.Data.ToString()); var deviceList = JsonConvert.DeserializeObject(deviceResult.Data.ToString()); if (deviceList == null) { deviceList = new DevcieApiPack(); } try { while (deviceList.totalPage != deviceList.pageNo) { var deviceResult2 = Ins.HttpRequest.GetDeviceList("30", Convert.ToInt32(deviceList.pageNo) + 1 + ""); if (deviceResult2.Code == StateCode.SUCCESS) { var deviceList2 = JsonConvert.DeserializeObject(deviceResult.Data.ToString()); deviceList.pageNo = deviceList2.pageNo; deviceList.list.AddRange(deviceList2.list); } } } catch (Exception ex) { } finally { } string delFile = ""; if (FunctionList.List.GetDeviceFunctionList().Count > 0) { for (int i = 0; i < FunctionList.List.GetDeviceFunctionList().Count;) { var localFunction = FunctionList.List.GetDeviceFunctionList()[i]; //if (SPK.MusicSpkList().Contains(localFunction.spk)) //{ // i++; // continue; //} var newFunction = deviceList.list.Find((obj) => obj.deviceId == localFunction.deviceId); if (delFile == localFunction.savePath) { i++; continue; } delFile = localFunction.savePath; FunctionList.List.DeleteFunction(localFunction); } } //处理剩下的新增功能 int iiii = 0; foreach (var newFunction in deviceList.list) { newFunction.AssembleStatus(); newFunction.SaveFunctionFile(); FunctionList.List.IniFunctionList(newFunction.savePath,true); MainPage.Log($"============设备============{iiii++}"); } } else { MainPage.Log($"读取云端设备数据失败:Code:{deviceResult.Code}; Msg:{deviceResult.message}"); } }catch (Exception ex) { MainPage.Log($"============设备============{ex.Message}"); } finally { complateDevice = true; MainPage.Log($"============设备============完成" + FunctionList.List.Functions.Count); } //}) //{ IsBackground = true }.Start(); ////===================场景========================== //new System.Threading.Thread(() => //{ try { complateScene = false; var pack = Ins.HttpRequest.GetSceneList(); if (pack.Code == StateCode.SUCCESS) { MainPage.Log($"读取场景数据成功"); var sceneList = JsonConvert.DeserializeObject>(pack.Data.ToString()); if (sceneList == null) { sceneList = new List(); } FunctionList.List.scenes.Clear(); //处理剩下的新增功能 foreach (var tempScene in sceneList) { tempScene.SaveSceneFile(); FunctionList.List.scenes.Add(tempScene); } //如果是一端口需要提前下载场景数据,否则控制不了一端口场景 if (DB_ResidenceData.Instance.GatewayType == 0) { var sceneIds = new List(); foreach (var tempScene in FunctionList.List.scenes) { sceneIds.Add(tempScene.userSceneId); } var packInfo = HttpRequest.GetSceneListInfo(sceneIds); if (packInfo.Code == StateCode.SUCCESS) { var serverTempList = JsonConvert.DeserializeObject>(packInfo.Data.ToString()); foreach (var localScene in FunctionList.List.scenes) { var serverTemp = serverTempList.Find((obj) => obj.userSceneId == localScene.userSceneId); if (serverTemp != null) { localScene.functions = serverTemp.functions; localScene.SaveSceneFile(); } } } else { IMessageCommon.Current.ShowErrorInfoAlter(packInfo.Code); } } } else { MainPage.Log($"读取云端场景数据失败:Code:{pack.Code}; Msg:{pack.message}"); } }catch(Exception ex) { MainPage.Log($"===场景==={ex.Message}"); } finally { complateScene = true; } }) { IsBackground = true }.Start(); //===================读取逻辑列表========================== Logic.LogicList.Clear();//主页下拉强制刷新自动化列表 //======================安防==================== GetSecurityList(); //======================注册推送==================== httpRequest.RegisteredPush(); } catch (Exception ex) { MainPage.Log($"数据初始化失败:{ex.Message}"); } finally { Application.RunOnMainThread(() => { MainPage.Log($"结束读取云端数据线程"); if (waitPage != null) { waitPage.RemoveFromParent(); waitPage = null; } }); //不考虑网关情况,直接订阅 DAL.Mqtt.MqttClient.InitState(); try { FunctionList.List.Read3tyFunctionStatus(); }catch(Exception ex) { MainPage.Log("读取第三方设备状态失败:"+ex.Message); } } }); downloadDataThread.Start(); //网络卡顿,终止下载线程 new System.Threading.Thread(() => { try { while (endTime1 > DateTime.Now) { if ( complateDevice && complateScene) { break; } System.Threading.Thread.Sleep(100); } DownloadDataComplete = true; Application.RunOnMainThread(() => { if (waitPage != null) { waitPage.RemoveFromParent(); waitPage = null; } }); } catch { } finally { if (DB_ResidenceData.Instance.CurrentRegion != null) { #region 读取本地数据 //搜索网关 DriverLayer.Control.Ins.SearchLoaclGateway(); //重新连接mqtt DAL.Mqtt.MqttClient.DisConnectRemote("刷新数据,重连mqtt",false); #endregion } } }) { IsBackground = true }.Start(); } /// /// 获取功能信息 /// public void GetFunctionInfo(string functionId) { var deviceResult = Ins.HttpRequest.GetDeviceInfo(functionId); if (deviceResult.Code == StateCode.SUCCESS) { MainPage.Log($"读取设备信息成功"); var packList = JsonConvert.DeserializeObject(deviceResult.Data.ToString()); foreach(var function in packList.list) { function.SaveFunctionFile(); FunctionList.List.IniFunctionList(function.savePath,true); } } } /// /// 获取安防列表 /// public void GetSecurityList() { var pack = Ins.HttpRequest.GetSecurityList(); if (pack.Code == StateCode.SUCCESS) { var packList = JsonConvert.DeserializeObject>(pack.Data.ToString()); var sidList = new List(); foreach (var function in packList) { sidList.Add(function.sid); } var infoListPack = Ins.httpRequest.GetSecurityInfo(sidList, new List()); var infoList = JsonConvert.DeserializeObject>(infoListPack.Data.ToString()); if (infoList != null) { var filePathList = FileUtlis.Files.ReadFiles().FindAll((obj) => obj.StartsWith("SecurityData_")); if (filePathList != null) { 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); } } } else { MainPage.Log($"读取安防列表失败:Code:{pack.Code}; Msg:{pack.message}"); } } /// /// 获取SIP账号信息 /// public void GetSipInfo() { var pack = Ins.HttpRequest.GetSecurityList(); } } }