EvHttpClient.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Specialized;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Net.Http;
  9. using System.Net.Http.Headers;
  10. using System.Text;
  11. using System.Text.RegularExpressions;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. namespace AwInitilizer.Assist
  15. {
  16. public class EvHttpClientResult
  17. {
  18. public bool IsSuccess { get; set; }
  19. public string Msg { get; set; }
  20. }
  21. public class EvAuthMsg
  22. {
  23. public string result { get; set; }
  24. public string message { get; set; }
  25. }
  26. public static class EvHttpClient
  27. {
  28. private static string account = "admin";
  29. private static string pass = "1231231238";
  30. //internal static string ServerIpAddress = "192.168.1.10";
  31. internal static string ServerUrl = "https://192.168.1.10";
  32. //internal static string ServerIpAddress = "172.18.13.84";
  33. //internal static string ServerUrl = "https://172.18.13.84";
  34. //updated EV
  35. //internal static string ServerIpAddress = "192.168.80.197";
  36. //internal static string ServerUrl = "https://192.168.80.197";
  37. //original EV
  38. //internal static string ServerIpAddress = "192.168.80.199";
  39. //internal static string ServerUrl = "https://192.168.80.199";
  40. internal static void ResetServerIpAddress(string ipAddress = null)
  41. {
  42. if (string.IsNullOrEmpty(ipAddress))
  43. {
  44. ServerUrl = "https://192.168.1.10";
  45. return;
  46. }
  47. if (!ipAddress.StartsWith("http"))
  48. {
  49. ipAddress = $"https://{ipAddress}";
  50. }
  51. ServerUrl = ipAddress;
  52. return;
  53. }
  54. internal static Task<EvHttpClientResult> GetQueryActionOpt1String()
  55. {
  56. string api = "get_query_action.php";
  57. Dictionary<string, string> param = new Dictionary<string, string>() {
  58. {"opt","1"}
  59. };
  60. return CallBase(api, param);
  61. }
  62. internal static Task<EvHttpClientResult> GetQueryActionOpt2String()
  63. {
  64. string api = "get_query_action.php";
  65. Dictionary<string, string> param = new Dictionary<string, string>() {
  66. {"opt","2"}
  67. };
  68. return CallBase(api, param);
  69. }
  70. internal static Task<EvHttpClientResult> GetQueryActionOpt3String()
  71. {
  72. string api = "get_query_action.php";
  73. Dictionary<string, string> param = new Dictionary<string, string>() {
  74. {"opt","3"}
  75. };
  76. return CallBase(api, param);
  77. }
  78. internal static Task<EvHttpClientResult> GetButtonStatusString()
  79. {
  80. string api = "get_button_action.php";
  81. Dictionary<string, string> param = new Dictionary<string, string>()
  82. {
  83. };
  84. return CallBase(api, param);
  85. }
  86. internal static Task<EvHttpClientResult> GetFactorySetResultString()
  87. {
  88. string api = "set_system_action.php";
  89. Dictionary<string, string> param = new Dictionary<string, string>();
  90. param.Add("SystemId", "");
  91. param.Add("SystemDateTime", "");
  92. param.Add("PhaseLossPolicy", "");
  93. param.Add("FactoryConfiguration", "1");
  94. param.Add("AuthorisationMode", "");
  95. param.Add("isAPP", "");
  96. param.Add("isQRCode", "");
  97. param.Add("isRFID", "");
  98. param.Add("QRCodeMadeMode", "");
  99. param.Add("QRCodeContent", "");
  100. param.Add("Intensity", "");
  101. param.Add("RfidCardNumEndian", "");
  102. param.Add("PsuAcInputType", "");
  103. return CallBase(api, param);
  104. }
  105. internal static Task<EvHttpClientResult> GetUploadfirmwareResultString(List<string> fileNames)
  106. {
  107. string api = "upgrade_iso_action.php";
  108. Dictionary<string, string> param = new Dictionary<string, string>(){
  109. {"fw_tag","iso"}
  110. };
  111. return CallBase(api, param, firmwareNames: fileNames);
  112. }
  113. internal static Task<EvHttpClientResult> GetSignalUpdateFirmwareResultString()
  114. {
  115. string api = "upgrade_iso_action.php";
  116. Dictionary<string, string> param = new Dictionary<string, string>(){
  117. {"fw_tag","iso"},
  118. };
  119. var dummyFileCotent = new ByteArrayContent(new byte[0]);
  120. dummyFileCotent.Headers.ContentDisposition
  121. = new ContentDispositionHeaderValue("attachment")
  122. {
  123. FileName = "temp.txt",
  124. Size = 0,
  125. CreationDate = DateTime.Now,
  126. ModificationDate = DateTime.Now,
  127. ReadDate = DateTime.Now,
  128. Name = "files[]"
  129. };
  130. return CallBase(api, param, customContents: new List<HttpContent>() { dummyFileCotent });
  131. }
  132. private static async Task<EvHttpClientResult> CallBase(
  133. string api,
  134. Dictionary<string, string> param,
  135. List<string> firmwareNames = null,
  136. List<HttpContent> customContents = null)
  137. {
  138. try
  139. {
  140. var url = string.Format("{0}/{1}", ServerUrl, api);
  141. EvHttpClientLogger.Instance.Log(url);
  142. Dictionary<string, string> pams = new Dictionary<string, string>
  143. {
  144. { "account", account },
  145. { "password", pass }
  146. };
  147. foreach (var pam in param)
  148. {
  149. pams.Add(pam.Key, pam.Value);
  150. }
  151. var formContent = new MultipartFormDataContent();
  152. foreach (var pam in pams)
  153. {
  154. formContent.Add(new StringContent(pam.Value), pam.Key);
  155. }
  156. if (firmwareNames != null)
  157. {
  158. if (firmwareNames.Count == 1)
  159. {
  160. var fileName = firmwareNames[0];
  161. formContent.Add(new ByteArrayContent(System.IO.File.ReadAllBytes(fileName)), "file");
  162. }
  163. else
  164. {
  165. foreach (var fileName in firmwareNames)
  166. {
  167. formContent.Add(new ByteArrayContent(System.IO.File.ReadAllBytes(fileName)), "file[]");
  168. }
  169. }
  170. }
  171. if (customContents != null)
  172. {
  173. foreach (var content in customContents)
  174. {
  175. formContent.Add(content);
  176. }
  177. }
  178. HttpResponseMessage postResult;
  179. string result = null;
  180. var handler = new HttpClientHandler();
  181. handler.ClientCertificateOptions = ClientCertificateOption.Manual;
  182. handler.ServerCertificateCustomValidationCallback =
  183. (httpRequestMessage, cert, cetChain, policyErrors) =>
  184. {
  185. return true;
  186. };
  187. using (HttpClient evClient = new HttpClient(handler))
  188. {
  189. evClient.Timeout = TimeSpan.FromSeconds(5);
  190. try
  191. {
  192. EvHttpClientLogger.Instance.Log("post with new version");
  193. postResult = await evClient.PostAsync(url, formContent);
  194. EvHttpClientLogger.Instance.Log(postResult.StatusCode.ToString());
  195. //MessageBox.Show("Rest Result:" + postResult.StatusCode);
  196. if (postResult == null || !postResult.IsSuccessStatusCode)
  197. {
  198. throw new Exception("Post fail");
  199. }
  200. result = await postResult.Content.ReadAsStringAsync();
  201. EvHttpClientLogger.Instance.Log(result);
  202. //MessageBox.Show("Rest Result:" + result);
  203. if (result.Contains("File is uploaded, please wait a moment to upgrade"))
  204. {
  205. return new EvHttpClientResult()
  206. {
  207. IsSuccess = true,
  208. Msg = result,
  209. };
  210. }
  211. var check = JsonConvert.DeserializeObject<EvAuthMsg>(result);
  212. if (check != null &&
  213. check.result != null &&
  214. check.result.ToLower() == "fail")
  215. {
  216. return new EvHttpClientResult()
  217. {
  218. IsSuccess = false,
  219. Msg = result,
  220. };
  221. }
  222. return new EvHttpClientResult()
  223. {
  224. IsSuccess = true,
  225. Msg = result,
  226. };
  227. }
  228. catch
  229. {
  230. //post fail
  231. }
  232. }
  233. using (WebClientTimeout webClient = new WebClientTimeout())
  234. {
  235. EvHttpClientLogger.Instance.Log("post with old version");
  236. NameValueCollection parameters = new NameValueCollection();
  237. foreach (var inpam in param)
  238. {
  239. parameters.Add(inpam.Key, inpam.Value);
  240. }
  241. webClient.QueryString = parameters;
  242. using (Stream stream = webClient.OpenRead(url))
  243. // 使用 StreamReader 讀取 stream 內的字元
  244. using (StreamReader reader = new StreamReader(stream))
  245. {
  246. // 將 StreamReader 所讀到的字元轉為 string
  247. result = await reader.ReadToEndAsync();
  248. }
  249. EvHttpClientLogger.Instance.Log(result);
  250. var check = JsonConvert.DeserializeObject<EvAuthMsg>(result);
  251. if (check != null &&
  252. check.result != null &&
  253. check.result.ToLower() == "fail")
  254. {
  255. return new EvHttpClientResult()
  256. {
  257. IsSuccess = false,
  258. Msg = result,
  259. };
  260. }
  261. }
  262. return new EvHttpClientResult() { IsSuccess = true, Msg = result };
  263. }
  264. catch (Exception e)
  265. {
  266. return new EvHttpClientResult() { IsSuccess = false, Msg = e.Message }; ;
  267. }
  268. }
  269. }
  270. }