123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- 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<EvHttpClientResult> GetQueryActionOpt1String()
- {
- string api = "get_query_action.php";
- Dictionary<string, string> param = new Dictionary<string, string>() {
- {"opt","1"}
- };
- return CallBase(api, param);
- }
- internal static Task<EvHttpClientResult> GetQueryActionOpt2String()
- {
- string api = "get_query_action.php";
- Dictionary<string, string> param = new Dictionary<string, string>() {
- {"opt","2"}
- };
- return CallBase(api, param);
- }
- internal static Task<EvHttpClientResult> GetQueryActionOpt3String()
- {
- string api = "get_query_action.php";
- Dictionary<string, string> param = new Dictionary<string, string>() {
- {"opt","3"}
- };
- return CallBase(api, param);
- }
- internal static Task<EvHttpClientResult> GetQueryActionOpt4String()
- {
- string api = "get_query_action.php";
- Dictionary<string, string> param = new Dictionary<string, string>() {
- {"opt","4"}
- };
- return CallBase(api, param);
- }
- internal static Task<EvHttpClientResult> GetButtonStatusString()
- {
- string api = "get_button_action.php";
- Dictionary<string, string> param = new Dictionary<string, string>()
- {
- };
- return CallBase(api, param);
- }
- internal static Task<EvHttpClientResult> GetFactorySetResultString()
- {
- string api = "set_system_action.php";
- Dictionary<string, string> param = new Dictionary<string, string>();
- 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<EvHttpClientResult> GetUploadfirmwareResultString(List<string> fileNames)
- {
- string api = "upgrade_iso_action.php";
- Dictionary<string, string> param = new Dictionary<string, string>(){
- {"fw_tag","iso"}
- };
- var httpContentList = new List<HttpContent>();
- 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<EvHttpClientResult> GetSignalUpdateFirmwareResultString()
- {
- string api = "upgrade_iso_action.php";
- Dictionary<string, string> param = new Dictionary<string, string>(){
- {"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<HttpContent>() { dummyFileCotent });
- }
- internal static Task<EvHttpClientResult> SetSystemAction(string key, string value)
- {
- string api = "set_system_action.php";
- Dictionary<string, string> param = new Dictionary<string, string>(){
- {key,value},
- };
- return CallBase(api, param, customContents: new List<HttpContent>() { });
- }
- private static async Task<EvHttpClientResult> CallBase(
- string api,
- Dictionary<string, string> param,
- List<string> firmwareNames = null,
- List<HttpContent> customContents = null,
- int timeOutSeconds = 5)
- {
- try
- {
- var url = string.Format("{0}/{1}", ServerUrl, api);
- EvHttpClientLogger.Instance.Log(url);
- Dictionary<string, string> pams = new Dictionary<string, string>
- {
- { "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<EvAuthMsg>(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<EvAuthMsg>(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 }; ;
- }
- }
- }
- }
|