| | |
| | | |
| | | using System; |
| | | using Shared.IO; |
| | | using Shared.Net; |
| | | using System.Text; |
| | | using System.Collections.Generic; |
| | | using System.Net; |
| | | using System.IO; |
| | | using Shared.SimpleControl.Phone; |
| | | // <summary> |
| | | // 程序主入口 |
| | | // </summary> |
| | | //using SystemConfiguration; |
| | | using System.Net; |
| | | using System.IO; |
| | | using Shared.SimpleControl.Phone; |
| | | namespace Shared.SimpleControl |
| | | { |
| | | /// <summary> |
| | |
| | | |
| | | //public static Button LogoButton = new Button (); |
| | | |
| | | public static string CodeIDString = "20191223"; |
| | | public static string RequestVersion = "20200402"; |
| | | public static UserInfo LoginUser; |
| | | /// <summary> |
| | | /// 是否是管理员权限(变更了,成员的时候,这个也为ture。为什么会声明这样变量,因为有些接口必须使用原来的Token) |
| | | /// </summary> |
| | | public static bool IsAdministrator { |
| | | get { |
| | | if (UserConfig.Instance == null || UserConfig.Instance.CurrentRegion == null) { |
| | | return false; |
| | | } else { |
| | | return UserConfig.Instance.CurrentRegion.IsOthreShare; |
| | | } |
| | | } |
| | | } |
| | | |
| | | ///// <summary> |
| | | ///// 是否账号在其它设备登录了 |
| | | ///// </summary> |
| | | //public static bool IsOtherDevicesLogin = false; |
| | | |
| | | //#if __IOS__ |
| | | // public static readonly string PhoneType = "iOS"; |
| | | //#else |
| | | // public static readonly string PhoneType = "Android"; |
| | | //#endif |
| | | |
| | | public static string SoftPadTitle = "Crabtree"; |
| | | public static string SoftTitle = "Crabtree"; |
| | | public static int SoftSmsType = 0; |
| | | public static string RequestSource = "1"; //0:Zigbee、1:HdlOn、2:GraphicsMonitor、3:BusproSoft、4:AgentWeb |
| | | |
| | | public static string LogoString = "";//"Logo/Logo.png"; |
| | | public static string WiFiStatus = "CrabtreeAdd/WiFi.png";//"CrabtreeAdd/WiFi.png"; |
| | | public static string RegisterLogoString = "Logo/Register_logo.png"; |
| | | public static string SeviceIP = "52.172.25.151"; |
| | | //public static string RequestHttpsHost = "https://homeautomation.havells.com"; |
| | | public static string RequestHttpsHost = "https://developer.hdlcontrol.com"; |
| | | public static string RequestHttpsHost = "https://smarth-api.havells.com/HangZhouHdlCloudApi"; |
| | | //public static string RequestHttpsHost = "https://global.hdlcontrol.com/HangZhouHdlCloudApi"; |
| | | |
| | | public static string RequestHttpsHostMqtt = "https://smarth-api.havells.com"; |
| | | |
| | | //public static string RequestHttpsHost = "https://developer.hdlcontrol.com"; |
| | | |
| | | |
| | | public static double PixelScale { |
| | |
| | | } else { |
| | | Phone.UserHomePage.Init (); |
| | | #if __IOS__ |
| | | //Loading.LodingBackgroundColor = 0x10777777; |
| | | //Loading.LodingBackgroundColor = 0x10777777; |
| | | |
| | | #else |
| | | Loading.LodingBackgroundColor = SkinStyle.Current.MainColor; |
| | | //Loading.LodingBackgroundColor = SkinStyle.Current.MainColor; |
| | | #endif |
| | | Loading.TextColor = 0xFFA9A9A9; |
| | | Loading.LodingBackgroundColor = SkinStyle.Current.MainColor; |
| | | } |
| | | } catch (Exception ex) { |
| | | //new Alert ("Eorr", ex.Message, "Close").Show(); |
| | |
| | | /// <param name="methodType">请求方法.</param> |
| | | /// <param name="dataStringJson">请求的参数.</param> |
| | | /// <param name="needHeaders">是否需要包头.</param> |
| | | public static ResponsePack RequestHttps (string methodType, string dataStringJson, bool needHeaders = true, bool needTip = true,string urlHead = "") |
| | | public static ResponsePack RequestHttps (string methodType, string dataStringJson, string urlHead = "") |
| | | { |
| | | //2020-01-08 屏蔽 |
| | | //if (!SystemRemote.checeInternet ()) { |
| | | // Application.RunOnMainThread (() => { |
| | | // MainPage.WiFiStatus = "CrabtreeAdd/WiFiUnlink.png"; |
| | | // MainPage.Loading.Hide (); |
| | | // }); |
| | | //} |
| | | //new System.Threading.Thread (() => { |
| | | // requesTime = DateTime.Now; |
| | | // isShowLoading = true; |
| | | // while (isShowLoading) { |
| | | // System.Threading.Thread.Sleep (1000); |
| | | // if (requesTime == DateTime.MinValue) { |
| | | // break; |
| | | // } |
| | | // if (requesTime.AddSeconds (30) < DateTime.Now) { |
| | | // Application.RunOnMainThread (Loading.Hide); |
| | | // isShowLoading = false; |
| | | // } |
| | | // } |
| | | //}) { IsBackground = true }.Start (); |
| | | |
| | | ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, error) => { |
| | | return true; |
| | | }; |
| | | |
| | | #region |
| | | var webClient = new WebClient (); |
| | | if (urlHead == "") { |
| | | urlHead = RequestHttpsHost; |
| | | } |
| | | //else { |
| | | // webClient.Headers.Add ("CONTENT-TYPE", "application/json"); |
| | | //} |
| | | webClient.Headers.Add ("CONTENT-TYPE", "application/json"); |
| | | string url = urlHead + methodType; |
| | | if (LoginUser != null) {/* 如果不需要验证Token可以不用传入 */ |
| | | webClient.Headers.Add ("Authorization", LoginUser.LoginTokenString); |
| | | } |
| | | byte [] bytes = null; |
| | | try { |
| | | bytes = webClient.UploadData (url, Encoding.UTF8.GetBytes (dataStringJson)); |
| | | } catch (System.Net.WebException webEx) { |
| | | Application.RunOnMainThread (() => { |
| | | MainPage.Loading.Hide (); |
| | | //new Alert ( "","Network abnormal, unable to connect to the Internet", Language.StringByID (R.MyInternationalizationString.Close)).Show (); |
| | | }); |
| | | return new ResponsePack () { StateCode = ErrorCode.NetworkError }; |
| | | } |
| | | string responseString = Encoding.UTF8.GetString (bytes); |
| | | //var DD = Newtonsoft.Json.Linq.JObject .Parse("GFGFDG");DD["ResponseData"]["PageData"] |
| | | |
| | | //var request = (HttpWebRequest)WebRequest.Create (url); |
| | | //var data = Encoding.UTF8.GetBytes (dataStringJson); |
| | | //request.Method = "POST"; |
| | | //request.ContentLength = data.Length; |
| | | //if (needHeaders) {/* 如果不需要验证Token可以不用传入 */ |
| | | // request.Headers.Set (HttpRequestHeader.Authorization, LoginUser.LoginTokenString); |
| | | //} |
| | | //using (var stream = request.GetRequestStream ()) { |
| | | // stream.Write (data, 0, data.Length); |
| | | //} |
| | | //var response = (HttpWebResponse)request.GetResponse (); |
| | | //var responseString = new StreamReader (response.GetResponseStream ()).ReadToEnd (); |
| | | |
| | | try { |
| | | return Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePack> (responseString); |
| | | } catch (Exception ex) { |
| | | Utlis.WriteLine (ex.ToString ()); |
| | | return new ResponsePack () { StateCode = "Data exception" }; |
| | | } |
| | | #endregion |
| | | |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 请求服务器方法 |
| | | /// </summary> |
| | | /// <returns>The https.</returns> |
| | | /// <param name="methodType">请求方法.</param> |
| | | /// <param name="dataStringJson">请求的参数.</param> |
| | | /// <param name="needHeaders">是否需要包头.</param> |
| | | public static byte [] RequestHttpsReturnByte (string methodType, string dataStringJson, bool needHeaders = false, string urlHead = "") |
| | | { |
| | | ResponsePack revertObj = null; |
| | | try { |
| | | if (!SystemRemote.checeInternet ()) { |
| | | Application.RunOnMainThread (() => { |
| | | MainPage.WiFiStatus = "CrabtreeAdd/WiFiUnlink.png"; |
| | | MainPage.Loading.Hide (); |
| | | }); |
| | | } |
| | | //new System.Threading.Thread (() => { |
| | | // requesTime = DateTime.Now; |
| | | // isShowLoading = true; |
| | | // while (isShowLoading) { |
| | | // System.Threading.Thread.Sleep (1000); |
| | | // if (requesTime == DateTime.MinValue) { |
| | | // break; |
| | | // } |
| | | // if (requesTime.AddSeconds (30) < DateTime.Now) { |
| | | // Application.RunOnMainThread (Loading.Hide); |
| | | // isShowLoading = false; |
| | | // } |
| | | // } |
| | | //}) { IsBackground = true }.Start (); |
| | | |
| | | |
| | | ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, error) => { |
| | | return true; |
| | | }; |
| | | |
| | | #region |
| | | var webClient = new WebClient (); |
| | | if (urlHead == "") { |
| | | urlHead = $"{RequestHttpsHost}/WebApi/"; |
| | | urlHead = "https://developer.hdlcontrol.com/api/"; |
| | | } else { |
| | | webClient.Headers.Add ("CONTENT-TYPE", "application/json"); |
| | | urlHead = RequestHttpsHost; |
| | | } |
| | | //else { |
| | | // webClient.Headers.Add ("CONTENT-TYPE", "application/json"); |
| | | //} |
| | | webClient.Headers.Add ("CONTENT-TYPE", "application/json"); |
| | | string url = urlHead + methodType; |
| | | if (needHeaders && LoginUser != null) {/* 如果不需要验证Token可以不用传入 */ |
| | | webClient.Headers.Add ("Authorization", LoginUser.LoginTokenString); |
| | |
| | | try { |
| | | bytes = webClient.UploadData (url, Encoding.UTF8.GetBytes (dataStringJson)); |
| | | } catch (System.Net.WebException webEx) { |
| | | Application.RunOnMainThread (() => { |
| | | MainPage.Loading.Hide (); |
| | | revertObj = new ResponsePack () { StateCode = "Network_Error" }; |
| | | //new Alert ( "","Network abnormal, unable to connect to the Internet", Language.StringByID (R.MyInternationalizationString.Close)).Show (); |
| | | return; |
| | | }); |
| | | return bytes; |
| | | } |
| | | string responseString = null; |
| | | if (bytes != null) { |
| | | responseString = Encoding.UTF8.GetString (bytes); |
| | | } |
| | | |
| | | //var request = (HttpWebRequest)WebRequest.Create (url); |
| | | //var data = Encoding.UTF8.GetBytes (dataStringJson); |
| | | //request.Method = "POST"; |
| | | //request.ContentLength = data.Length; |
| | | //if (needHeaders) {/* 如果不需要验证Token可以不用传入 */ |
| | | // request.Headers.Set (HttpRequestHeader.Authorization, LoginUser.LoginTokenString); |
| | | //} |
| | | //using (var stream = request.GetRequestStream ()) { |
| | | // stream.Write (data, 0, data.Length); |
| | | //} |
| | | //var response = (HttpWebResponse)request.GetResponse (); |
| | | //var responseString = new StreamReader (response.GetResponseStream ()).ReadToEnd (); |
| | | |
| | | if (responseString != null) { |
| | | try { |
| | | revertObj = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePack> (responseString); |
| | | if (revertObj.StateCode != "SUCCESS") { |
| | | if (methodType == "IsExistingAccount") { |
| | | |
| | | } else if (needTip) { |
| | | Application.RunOnMainThread (() => { |
| | | new Alert ("", revertObj.ErrorInfo, Language.StringByID (R.MyInternationalizationString.Close)).Show (); |
| | | }); |
| | | } |
| | | } |
| | | } catch (Exception ex) { |
| | | Console.WriteLine (ex.ToString ()); |
| | | } |
| | | } else { |
| | | if (methodType == "Login" && needTip) { |
| | | Application.RunOnMainThread (() => { |
| | | new Alert ("", Language.StringByID (R.MyInternationalizationString.FailureToDataBase), |
| | | Language.StringByID (R.MyInternationalizationString.Close)).Show (); |
| | | }); |
| | | } else { |
| | | if (needTip) |
| | | FailureToServer (); |
| | | } |
| | | } |
| | | return revertObj; |
| | | |
| | | |
| | | return bytes; |
| | | #endregion |
| | | } catch (Exception ex) { |
| | | Console.WriteLine (ex.Message); |
| | | return revertObj; |
| | | Utlis.WriteLine (ex.Message); |
| | | return null; |
| | | } finally { |
| | | //requesTime = requesTime.AddDays(1); |
| | | } |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 连接服务器失败 |
| | | /// </summary> |
| | | public static void FailureToServer () |
| | | { |
| | | //Application.RunOnMainThread (() => { |
| | | // new Alert ("", Language.StringByID (R.MyInternationalizationString.FailureToServer), |
| | | // Language.StringByID (R.MyInternationalizationString.Close)).Show (); |
| | | //}); |
| | | ShowAlertOnMainThread (Language.StringByID (R.MyInternationalizationString.FailureToServer)); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 2020-01-11 弹窗提示 |
| | | /// </summary> |
| | | /// <param name="mes"></param> |
| | | public static void ShowAlertOnMainThread (string mes) |
| | | { |
| | | Application.RunOnMainThread (() => { |
| | | new Alert ("", Language.StringByID (R.MyInternationalizationString.FailureToServer), |
| | | Language.StringByID (R.MyInternationalizationString.Close)).Show (); |
| | | new Alert ("", mes, Language.StringByID (R.MyInternationalizationString.Close)).Show (); |
| | | }); |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 设备不在线,无响应 |
| | |
| | | MainPage.Loading.Hide (); |
| | | }); |
| | | } |
| | | |
| | | |
| | | } |
| | | } |
| | | } |