using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows; namespace AwInitilizer.Assist { public class EvHttpClientResult { public bool IsSuccess { get; set; } public string Msg { get; set; } } public class EvAuthMsg { public string result { get; set; } public object message { get; set; } } public static class EvHttpClient { internal static string account = "admin"; internal static string pass = "1231231238"; //internal static string ServerIpAddress = "192.168.1.10"; internal static string ServerUrl = "https://192.168.1.10"; //internal static string ServerIpAddress = "172.18.13.84"; //internal static string ServerUrl = "https://172.18.13.84"; //updated EV //internal static string ServerIpAddress = "192.168.80.197"; //internal static string ServerUrl = "https://192.168.80.197"; //original EV //internal static string ServerIpAddress = "192.168.80.199"; //internal static string ServerUrl = "https://192.168.80.199"; internal static void ResetServerIpAddress(string ipAddress = null) { if (string.IsNullOrEmpty(ipAddress)) { ServerUrl = "https://192.168.1.10"; return; } if (!ipAddress.StartsWith("http")) { ipAddress = $"https://{ipAddress}"; } ServerUrl = ipAddress; return; } internal static Task GetQueryActionOpt1String() { string api = "get_query_action.php"; Dictionary param = new Dictionary() { {"opt","1"} }; return CallBase(api, param); } internal static Task GetQueryActionOpt2String() { string api = "get_query_action.php"; Dictionary param = new Dictionary() { {"opt","2"} }; return CallBase(api, param); } internal static Task GetQueryActionOpt3String() { string api = "get_query_action.php"; Dictionary param = new Dictionary() { {"opt","3"} }; return CallBase(api, param); } internal static Task GetQueryActionOpt4String() { string api = "get_query_action.php"; Dictionary param = new Dictionary() { {"opt","4"} }; return CallBase(api, param); } internal static Task GetButtonStatusString() { string api = "get_button_action.php"; Dictionary param = new Dictionary() { }; return CallBase(api, param); } internal static Task GetFactorySetResultString() { string api = "set_system_action.php"; Dictionary param = new Dictionary(); param.Add("FactoryConfiguration", "1"); //param.Add("SystemId", ""); //param.Add("SystemDateTime", ""); //param.Add("PhaseLossPolicy", ""); //param.Add("FactoryConfiguration", "1"); //param.Add("AuthorisationMode", ""); //param.Add("isAPP", ""); //param.Add("isQRCode", ""); //param.Add("isRFID", ""); //param.Add("QRCodeMadeMode", ""); //param.Add("QRCodeContent", ""); //param.Add("Intensity", ""); //param.Add("RfidCardNumEndian", ""); //param.Add("PsuAcInputType", ""); return CallBase(api, param); } internal static Task GetUploadfirmwareResultString(List fileNames) { string api = "upgrade_iso_action.php"; Dictionary param = new Dictionary(){ {"fw_tag","iso"} }; var httpContentList = new List(); foreach (var fileName in fileNames) { var bytes = File.ReadAllBytes(fileName); var fileContent = new ByteArrayContent(bytes); //FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read); //var fileContent = new StreamContent(file); fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = Path.GetFileName(fileName), CreationDate = DateTime.Now, ModificationDate = DateTime.Now, ReadDate = DateTime.Now, Name = "files[]" }; httpContentList.Add(fileContent); } return CallBase(api, param, customContents: httpContentList, timeOutSeconds: 3600); //return CallBase(api, param, firmwareNames: fileNames); } internal static Task GetSignalUpdateFirmwareResultString() { string api = "upgrade_iso_action.php"; Dictionary param = new Dictionary(){ {"fw_tag","iso"}, }; var dummyFileCotent = new ByteArrayContent(new byte[0]); dummyFileCotent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "temp.txt", Size = 0, CreationDate = DateTime.Now, ModificationDate = DateTime.Now, ReadDate = DateTime.Now, Name = "files[]" }; return CallBase(api, param, customContents: new List() { dummyFileCotent }); } internal static Task SetSystemAction(string key, string value) { string api = "set_system_action.php"; Dictionary param = new Dictionary(){ {key,value}, }; return CallBase(api, param, customContents: new List() { }); } private static async Task CallBase( string api, Dictionary param, List firmwareNames = null, List customContents = null, int timeOutSeconds = 5) { try { var url = string.Format("{0}/{1}", ServerUrl, api); EvHttpClientLogger.Instance.Log(url); Dictionary pams = new Dictionary { { "account", account }, { "password", pass } }; foreach (var pam in param) { pams.Add(pam.Key, pam.Value); } var formContent = new MultipartFormDataContent(); foreach (var pam in pams) { formContent.Add(new StringContent(pam.Value), pam.Key); } if (firmwareNames != null) { foreach (var fileName in firmwareNames) { formContent.Add(new ByteArrayContent(System.IO.File.ReadAllBytes(fileName)), "file[]"); } //if (firmwareNames.Count == 1) //{ // var fileName = firmwareNames[0]; // formContent.Add(new ByteArrayContent(System.IO.File.ReadAllBytes(fileName)), "file"); //} //else //{ // foreach (var fileName in firmwareNames) // { // formContent.Add(new ByteArrayContent(System.IO.File.ReadAllBytes(fileName)), "file[]"); // } //} } if (customContents != null) { foreach (var content in customContents) { formContent.Add(content); } } HttpResponseMessage postResult; string result = null; var handler = new HttpClientHandler(); handler.ClientCertificateOptions = ClientCertificateOption.Manual; handler.ServerCertificateCustomValidationCallback = (httpRequestMessage, cert, cetChain, policyErrors) => { return true; }; using (HttpClient evClient = new HttpClient(handler)) { evClient.Timeout = TimeSpan.FromSeconds(timeOutSeconds); try { EvHttpClientLogger.Instance.Log("post with new version"); postResult = await evClient.PostAsync(url, formContent); EvHttpClientLogger.Instance.Log(postResult.StatusCode.ToString()); //MessageBox.Show("Rest Result:" + postResult.StatusCode); if (postResult == null || !postResult.IsSuccessStatusCode) { throw new Exception("Post fail"); } result = await postResult.Content.ReadAsStringAsync(); EvHttpClientLogger.Instance.Log(result); //MessageBox.Show("Rest Result:" + result); if (result.Contains("File is uploaded, please wait a moment to upgrade")) { return new EvHttpClientResult() { IsSuccess = true, Msg = result, }; } var check = JsonConvert.DeserializeObject(result); if (check != null && check.result != null && check.result.ToLower() == "fail") { return new EvHttpClientResult() { IsSuccess = false, Msg = result, }; } return new EvHttpClientResult() { IsSuccess = true, Msg = result, }; } catch (Exception e) { //post fail EvHttpClientLogger.Instance.Log(e.Message); EvHttpClientLogger.Instance.Log(e.StackTrace); } } using (WebClientTimeout webClient = new WebClientTimeout()) { EvHttpClientLogger.Instance.Log("post with old version"); NameValueCollection parameters = new NameValueCollection(); foreach (var inpam in param) { parameters.Add(inpam.Key, inpam.Value); } webClient.QueryString = parameters; using (Stream stream = webClient.OpenRead(url)) // 使用 StreamReader 讀取 stream 內的字元 using (StreamReader reader = new StreamReader(stream)) { // 將 StreamReader 所讀到的字元轉為 string result = await reader.ReadToEndAsync(); } EvHttpClientLogger.Instance.Log(result); var check = JsonConvert.DeserializeObject(result); if (check != null && check.result != null && check.result.ToLower() == "fail") { return new EvHttpClientResult() { IsSuccess = false, Msg = result, }; } } return new EvHttpClientResult() { IsSuccess = true, Msg = result }; } catch (Exception e) { return new EvHttpClientResult() { IsSuccess = false, Msg = e.Message }; ; } } } }