EvHttpClient.cs 11 KB

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