EvHttpClient.cs 9.8 KB

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