using System; using Shared.IO; using Shared.Net; using System.Text; using System.Collections.Generic; // // 程序主入口 // //using SystemConfiguration; using System.Net; using System.IO; using Shared.SimpleControl.Phone; namespace Shared.SimpleControl { public class AirQuality { public string city; public string airQTemp; public string airQHumidity; public string airQAirPM25; public string airQAirWeather; } /// /// 主页面 /// public static class MainPage { /// /// 主页面,根界面 /// public static FrameLayout MainFrameLayout { get; internal set; } public static bool Showed; public static bool IsAppStoreVersionNewer = true; static Loading loading; public static AirQuality AirQuality; /// /// 显示加载界面 /// public static Loading Loading { get { if (loading == null) { loading = new Loading (); } return loading; } } public static string sendTime = ""; public static string recTime = ""; #if wallon public static string CodeIDString = "202006021"; #else public static string CodeIDString = "2.506021"; #endif public static UserInfo LoginUser; public static string RequestSource = "1"; //0:Zigbee、1:HdlOn、2:GraphicsMonitor、3:BusproSoft、4:AgentWeb public static string SoftPadTitle = "HDL ON"; public static string SoftTitle = "ON"; public static int SoftSmsType = 0; public static string LogoString = @"Logo/Logo.png"; public static string RegisterLogoString = @"Logo/Register_logo.png"; public static string SeviceIP = "115.29.251.24"; public static string RequestHttpsHost = "https://developer.hdlcontrol.com/api/"; //public static string RequestHttpsHost = "http://172.16.2.152:8000/"; /// /// 获取view的计算高度,ON软件为了适配全面特殊使用的比例方法,由于一些背景图片是直接全部填充的,改动成三层的(view-button-button)的工作量很大,不禁需要增加一个view,还需要修改图片 /// /// The view height. public static int GetDesignHeight (int height) { int ss = Convert.ToInt32(height / PixelScale); return Application.GetRealHeight(ss); } public static double PixelScale { get { #if __IOS__ switch (Application.PhoneType) { case 3: case 4: case 5: case 6: case 7: case 8: case 9: return 1.0; case 10: return 1.1; case 11: case 12: return 1.2; } var widthScale = Application.CurrentWidth / 640.0f; var heightScale = Application.CurrentHeight / 1136.0f; var scale = Math.Round (Math.Abs (widthScale - heightScale), 2); return scale + 1; #else return 1; #endif } } /// /// 显示当前界面 /// public static void Show () { try { Showed = true; if (Shared.Application.IsPad) { Application.DesignWidth = 2048; Application.DesignHeight = 1536; } else { Application.DesignWidth = 640; Application.DesignHeight =1136; } Shared.BusSocket.Start (6000); CommonPage.InitReceiveEvent (); MainFrameLayout = Application.MainPage; MainFrameLayout.AddChidren (Loading); if (Shared.Application.IsPad) { //Pad.UserHomePage.Init (); } else { Phone.UserHomePage.Init (); } //ezviz摄像头场景通知触发 EZMonitor.CommonList.EditorAction = (data) => { System.Console.WriteLine ("MonitorSceneData"); //加载摄像头编辑场景的界面 var monitorAddSceneListView = new Phone.MonitorAddSceneList (); Phone.UserMiddle.DevicePageView.AddChidren (monitorAddSceneListView); monitorAddSceneListView.showVideoMonitoring (data); Phone.UserMiddle.DevicePageView.PageIndex = 2; }; EZMonitor.CommonList.ControlMonitorSceneAction = (data) => { new ScenePhoneMethod ().ControlScene (data.FileName); }; } catch (Exception ex) { //new Alert ("Eorr", ex.Message, "Close").Show(); } } static Button btnTipMsg; public static void AddTip (string tip, int time = 3000) { if (MainFrameLayout == null) { return; } Application.RunOnMainThread (() => { try { if (btnTipMsg == null) { btnTipMsg = new Button () { Height = Application.GetRealHeight (90), BackgroundColor = SkinStyle.Current.AddTipButtonColor, Y = Application.GetRealHeight (36), TextAlignment = TextAlignment.Center, }; } btnTipMsg.Text = tip; MainFrameLayout.AddChidren (btnTipMsg); btnTipMsg.MouseUpEventHandler += (sender, e) => { if (btnTipMsg.Parent != null) btnTipMsg.RemoveFromParent (); }; System.Threading.Tasks.Task.Run (() => { System.Threading.Thread.Sleep (time); Application.RunOnMainThread (() => { if (btnTipMsg.Parent != null) btnTipMsg.RemoveFromParent (); }); }); } catch { } }); } //static Button btnTipMsg = new Button () { // Height = Application.GetRealHeight (126), BackgroundColor = SkinStyle.Current.AddTipButtonColor, // Y = Application.GetRealHeight(30), // TextAlignment = TextAlignment.Center, //}; //public static void AddTip (string tip, int time = 3000) //{ // if (MainFrameLayout == null) { // return; // } // Application.RunOnMainThread (() => { // btnTipMsg.Text = tip; // MainFrameLayout.AddChidren (btnTipMsg); // btnTipMsg.MouseUpEventHandler += (sender, e) => { // if (btnTipMsg.Parent != null) // btnTipMsg.RemoveFromParent (); // }; // System.Threading.Tasks.Task.Run (() => { // System.Threading.Thread.Sleep (time); // Application.RunOnMainThread (() => { // if (btnTipMsg.Parent != null) // btnTipMsg.RemoveFromParent (); // }); // }); // }); //} /// /// 请求服务器方法 /// 指定一个Url,和请求方法,数据,Cookie,得到响应的数据 /// /// 请求Url /// 请求的的数据 /// 得到响应的数据 public static async System.Threading.Tasks.Task RequestHttpsResultAsync (string requestUrl, byte [] byteData) { try { //初始化新的webRequst //1. 创建httpWebRequest对象 var webRequest = (HttpWebRequest)WebRequest.Create (new Uri ($"{RequestHttpsHost}{requestUrl}")); //2. 初始化HttpWebRequest对象 webRequest.Method = "POST"; webRequest.Timeout = 10 * 1000; webRequest.ContentType = "application/json"; webRequest.ContentLength = byteData.Length; webRequest.Headers.Add (HttpRequestHeader.Authorization, LoginUser.LoginTokenString); //3. 附加要POST给服务器的数据到HttpWebRequest对象(附加POST数据的过程比较特殊,它并没有提供一个属性给用户存取,需要写入HttpWebRequest对象提供的一个stream里面。) var newStream = webRequest.GetRequestStream ();//创建一个Stream,赋值是写入HttpWebRequest对象提供的一个stream里面 newStream.Write (byteData, 0, byteData.Length); newStream.Flush (); newStream.Close (); //4. 读取服务器的返回信息 var response = (HttpWebResponse)webRequest.GetResponse (); var stream = response.GetResponseStream (); if (stream == null) { return null; } var result = new byte [stream.Length]; //已接收的长度 int receivedLength = 0; //注意,当数据比较大时,可能一次性没读完,需要重复读取 //当完成时可能返回的结果为 0 while (receivedLength < result.Length) { var rece = await stream.ReadAsync (result, receivedLength, result.Length - receivedLength); receivedLength += rece; if (rece == 0) { break; } } stream.Close (); response.Close (); return result; } catch (Exception e) { System.Console.WriteLine (e.Message); return null; } } /// /// 请求服务器方法 /// /// The https. /// 请求方法. /// 请求的参数. /// 是否需要包头. public static ResponsePack RequestHttps (string methodType, string dataStringJson, bool needHeaders = true, bool needTip = true,string urlHead = "") { ResponsePack revertObj = null; var webClient = new WebClient (); #region if (urlHead == "") { urlHead = $"{RequestHttpsHost}"; } else { webClient.Headers.Add ("CONTENT-TYPE", "application/json"); } string url = urlHead + methodType; if (needHeaders && LoginUser != null) {/* 如果不需要验证Token可以不用传入 */ webClient.Headers.Add ("Authorization", LoginUser.LoginTokenString); } byte[] bytes =null; try { bytes = webClient.UploadData (url, Encoding.UTF8.GetBytes (dataStringJson)); } catch (Exception ex){ Console.WriteLine (ex.Message); revertObj = new ResponsePack () { StateCode="Net_Error" }; } string responseString = null; if (bytes != null) { responseString = Encoding.UTF8.GetString (bytes); } if (responseString != null) { try { revertObj = Newtonsoft.Json.JsonConvert.DeserializeObject (responseString); if (revertObj.StateCode != "SUCCESS") { if (needTip) { Application.RunOnMainThread (() => { new Alert (Language.StringByID (R.MyInternationalizationString.Tip), revertObj.ErrorInfo, Language.StringByID (R.MyInternationalizationString.Close)).Show (); }); } } } catch (Exception ex) { Console.WriteLine (ex.ToString ()); } } return revertObj; #endregion } public static bool checeInternet () { byte [] bytes = null; var dateTime = DateTime.Now; System.Threading.Tasks.Task.Run (() => { bytes = new System.Net.WebClient { Proxy = null }.DownloadData ("http://www.baidu.com"); }); while ((DateTime.Now - dateTime).TotalMilliseconds < 8000) { System.Threading.Thread.Sleep (100); if (bytes != null) { return true; } } return false; } /// /// 连接服务器失败 /// public static void FailureToServer () { Application.RunOnMainThread (() => { new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.FailureToServer), Language.StringByID (R.MyInternationalizationString.Close)).Show (); }); } /// /// 设备不在线,无响应 /// public static void DeviceNotOnline () { Application.RunOnMainThread (() => { new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.TipEquipmentNotOnline), Language.StringByID (R.MyInternationalizationString.Close)).Show (); MainPage.Loading.Hide (); }); } public static void LoadingStart (string showString = "") { Application.RunOnMainThread (() => { MainPage.Loading.Start (showString); }); } public static void LoadingHide () { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); }); } public static void RestroeSkin () { SkinStyle.Current.ChangeColor (); Application.Skin = UserConfig.Instance.CurrentSkinName; SharedMethod.SharedMethod.ChangeSkin (); UserHomePage.FrameLayoutMain.BackgroundColor = SkinStyle.Current.ViewColor; UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.LinkStatusTipColor; UserMiddle.SettingBodyView.BackgroundColor = SkinStyle.Current.MainColor; UserMiddle.SettingView.BackgroundColor = SkinStyle.Current.ViewColor; //UserMiddle.btnVersion.BackgroundColor = SkinStyle.Current.ViewColor; //UserMiddle.btnVersion.TextColor = SkinStyle.Current.TextColor1; UserMiddle.UserMenuItem.BackgroundColor = SkinStyle.Current.MainColor; UserMiddle.btnFavorite.BackgroundColor = SkinStyle.Current.MainColor; UserMiddle.btnFavorite.SelectedBackgroundColor = SkinStyle.Current.UserMenuItemButton; UserMiddle.btnFavorite.TextColor = SkinStyle.Current.TextColor1; UserMiddle.btnFavorite.SelectedTextColor = SkinStyle.Current.SelectedColor; UserMiddle.btnDevice.BackgroundColor = SkinStyle.Current.MainColor; UserMiddle.btnDevice.SelectedBackgroundColor = SkinStyle.Current.UserMenuItemButton; UserMiddle.btnDevice.TextColor = SkinStyle.Current.TextColor1; UserMiddle.btnDevice.SelectedTextColor = SkinStyle.Current.SelectedColor; UserMiddle.btnRoom.BackgroundColor = SkinStyle.Current.MainColor; UserMiddle.btnRoom.SelectedBackgroundColor = SkinStyle.Current.UserMenuItemButton; UserMiddle.btnRoom.TextColor = SkinStyle.Current.TextColor1; UserMiddle.btnRoom.SelectedTextColor = SkinStyle.Current.SelectedColor; UserMiddle.btnSetting.BackgroundColor = SkinStyle.Current.MainColor; UserMiddle.btnSetting.SelectedBackgroundColor = SkinStyle.Current.UserMenuItemButton; UserMiddle.btnSetting.TextColor = SkinStyle.Current.TextColor1; UserMiddle.btnSetting.SelectedTextColor = SkinStyle.Current.SelectedColor; } } }