EvHttpClient.cs 13 KB

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