FirmwareUploadProcedure.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using AwInitilizer.Assist;
  2. using AwInitilizer.Model;
  3. using CsuWebApiLib;
  4. using InitializerModel;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.Specialized;
  8. using System.Globalization;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Net;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Web;
  15. namespace AwInitilizer.Procedure
  16. {
  17. public class FirmwareUploadProcedure : ProcedureBase
  18. {
  19. internal string Version;
  20. internal string fileName;
  21. internal string module;
  22. private FirmwareUpdateModel _model;
  23. public FirmwareUploadProcedure(FirmwareUpdateModel model) : base()
  24. {
  25. _model = model;
  26. Name = string.Format("Firmware {0} upload", model.Module);
  27. Content = string.Format("Update {0} Firemware", model.Module);
  28. Version = model.Version;
  29. fileName = model.FirmwareFileName;
  30. module = model.Module;
  31. }
  32. internal override async Task<bool> Run()
  33. {
  34. Logger.Print("Firmware Uploading...");
  35. var uploadResult = await Uploadfirmware(fileName);
  36. MesLogData.Add($"{module}Upload", uploadResult?"1":"0", uploadResult);
  37. return uploadResult;
  38. }
  39. internal async Task<bool> Uploadfirmware(string fileName)
  40. {
  41. try
  42. {
  43. var result = await EvApi.Uploadfirmware(fileName);
  44. InfoLog += $"get firmware update response {result.Response}\n";
  45. return result.Result;
  46. //using (var stream = File.Open(fileName, FileMode.Open))
  47. //{
  48. // UploadFileAsync(
  49. // new NameValueCollection()
  50. // {
  51. // {"fw_tag","iso" }
  52. // },
  53. // new UploadFile()
  54. // {
  55. // Name = "file",
  56. // Filename = Path.GetFileName(fileName),
  57. // Stream = stream
  58. // }
  59. // );
  60. //}
  61. //return true;
  62. //using (var stream = File.Open(fileName, FileMode.Open))
  63. //{
  64. // var response = await UploadFiles(
  65. // $"https://{ServerIpAddress}/upgrade_iso_action.php",
  66. // new List<UploadFile>() {
  67. // new UploadFile()
  68. // {
  69. // Name="file",
  70. // Filename= Path.GetFileName(fileName),
  71. // Stream = stream
  72. // }
  73. // },
  74. // new NameValueCollection() {
  75. // {"fw_tag","iso" }
  76. // }
  77. // );
  78. // var responseStr = Encoding.ASCII.GetString(response).ToLower();
  79. // InfoLog += $"get firmware update response {responseStr}\n";
  80. // if (responseStr.Contains("file is uploaded"))
  81. // return true;
  82. // return false;
  83. //}
  84. //return true;
  85. //using (WebClient webClient = new WebClient())
  86. //{
  87. // NameValueCollection parameters = new NameValueCollection();
  88. // parameters.Add("fw_tag", "iso");
  89. // webClient.QueryString = parameters;
  90. // var responseBytes = await webClient.UploadFileTaskAsync($"https://{ServerIpAddress}/upgrade_iso_action.php", fileName);
  91. // string responseString = Encoding.ASCII.GetString(responseBytes);
  92. // return true;
  93. //}
  94. }
  95. catch (Exception e)
  96. {
  97. Logger.Print("Upload Firmware Failed", isError: true);
  98. Logger.Print(e.Message + "", isError: true);
  99. InfoLog += "Upload Firmware Failed\n";
  100. InfoLog += e.Message;
  101. InfoLog += "\n";
  102. return false;
  103. }
  104. }
  105. public async Task<byte[]> UploadFiles(string address, IEnumerable<UploadFile> files, NameValueCollection values)
  106. {
  107. var request = (HttpWebRequest)HttpWebRequest.Create(address);
  108. request.Timeout = 60 * 1000;
  109. request.KeepAlive = true;
  110. request.Accept = "*/*";
  111. request.Method = "POST";
  112. request.Referer = address;
  113. request.Expect = "";
  114. var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
  115. request.ContentType = "multipart/form-data; boundary=" + boundary;
  116. boundary = "--" + boundary;
  117. using (var requestStream = request.GetRequestStream())
  118. {
  119. // Write the values
  120. foreach (string name in values.Keys)
  121. {
  122. var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
  123. requestStream.Write(buffer, 0, buffer.Length);
  124. buffer = Encoding.ASCII.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"{1}{1}", name, Environment.NewLine));
  125. requestStream.Write(buffer, 0, buffer.Length);
  126. buffer = Encoding.UTF8.GetBytes(values[name] + Environment.NewLine);
  127. requestStream.Write(buffer, 0, buffer.Length);
  128. }
  129. // Write the files
  130. foreach (var file in files)
  131. {
  132. var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
  133. requestStream.Write(buffer, 0, buffer.Length);
  134. buffer = Encoding.UTF8.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}", file.Name, file.Filename, Environment.NewLine));
  135. requestStream.Write(buffer, 0, buffer.Length);
  136. var ctype = MimeMapping.GetMimeMapping(file.Filename);
  137. buffer = Encoding.ASCII.GetBytes(string.Format("Content-Type: {0}{1}{1}", MimeMapping.GetMimeMapping(file.Filename), Environment.NewLine));
  138. requestStream.Write(buffer, 0, buffer.Length);
  139. file.Stream.CopyTo(requestStream);
  140. buffer = Encoding.ASCII.GetBytes(Environment.NewLine);
  141. requestStream.Write(buffer, 0, buffer.Length);
  142. }
  143. var boundaryBuffer = Encoding.ASCII.GetBytes(boundary + "--");
  144. requestStream.Write(boundaryBuffer, 0, boundaryBuffer.Length);
  145. }
  146. using (var response = await request.GetResponseAsync())
  147. using (var responseStream = response.GetResponseStream())
  148. using (var stream = new MemoryStream())
  149. {
  150. responseStream.CopyTo(stream);
  151. return stream.ToArray();
  152. }
  153. }
  154. }
  155. }