EvHttpClient.cs 11 KB

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