FirmwareFtpUploadProcedure.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using FluentFTP;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. namespace AwInitilizer.Procedure.FirmwareBundleUpload
  10. {
  11. public class FirmwareFtpUploadProcedure : ProcedureBase
  12. {
  13. public ErrorType Error { get; set; } = ErrorType.None;
  14. private ProcedureLog.LogWriter<FirmwareFtpUploadProcedure, LogEvent> LogWriter;
  15. private readonly static Dictionary<LogEvent, string> ReportDict = new Dictionary<LogEvent, string>()
  16. {
  17. { LogEvent.UploadStartWait, "UploadStartWait" },
  18. { LogEvent.FirmwareUpload, "FirmwareUpload" },
  19. };
  20. private readonly static Dictionary<LogEvent, string> LogDict = new Dictionary<LogEvent, string>()
  21. {
  22. { LogEvent.UploadStartWait, "Wait restart {0}" },
  23. { LogEvent.FirmwareUpload, "Firmware Upload {0}" },
  24. };
  25. public FirmwareFtpUploadProcedure() : base()
  26. {
  27. Name = string.Format("Firmware Upload");
  28. Content = string.Format("Upload all Firemware");
  29. LogWriter = new ProcedureLog.LogWriter<FirmwareFtpUploadProcedure, LogEvent>(this)
  30. {
  31. ReportPair = ReportDict,
  32. LogPair = LogDict
  33. };
  34. }
  35. internal override async Task<bool> Run()
  36. {
  37. bool response = false;
  38. int pollingCnt = 0;
  39. for (pollingCnt = 0; pollingCnt < 56; pollingCnt++)
  40. {
  41. await Task.Delay(TimeSpan.FromSeconds(15));
  42. response = await CheckGetQuertyAction();
  43. if (response)
  44. break;
  45. }
  46. LogWriter.Log(string.Format("EVSE connet elapsed minute(s) : {0}, Expect:<14", pollingCnt * 0.25));
  47. //timeout
  48. if (pollingCnt >= 56)
  49. {
  50. LogWriter.Report(LogEvent.UploadStartWait, "fail", isError: true);
  51. Error = ErrorType.StartWaitTimeout;
  52. return false;
  53. }
  54. LogWriter.Report(LogEvent.UploadStartWait, "success");
  55. //upload firmware
  56. bool result = false;
  57. for (int tryCnt = 0; tryCnt < 10; tryCnt++)
  58. {
  59. result = await UploadWithFtp();
  60. if (result)
  61. break;
  62. else
  63. await Task.Delay(1000);
  64. }
  65. if (!result)
  66. {
  67. Error = ErrorType.UploadFailed;
  68. return false;
  69. }
  70. //signal Update start
  71. var signalUpdateResult = await SignalUpdate();
  72. return result;
  73. }
  74. private async Task<bool> UploadWithFtp()
  75. {
  76. var updateList = UpdateData.FirmwareUpdateModels;
  77. //FtpClient client = new FtpClient(ServerIpAddress, "root", "y42j/4cj84");
  78. FtpClient client = new FtpClient(ServerIpAddress, "vern", "delta");
  79. try
  80. {
  81. var profile = await client.AutoConnectAsync();
  82. if (profile == null)
  83. {
  84. throw new Exception("profile null");
  85. }
  86. }
  87. catch
  88. {
  89. LogWriter.Log($"Ftp Connect failed");
  90. LogWriter.Report(LogEvent.FirmwareUpload, "fail", isError: true);
  91. return false;
  92. }
  93. for (int modelIndex = 0; modelIndex < updateList.Count; modelIndex++) //foreach (var model in updateList)
  94. {
  95. var model = updateList[modelIndex];
  96. var firmwareFilePath = Path.Combine(Directory.GetCurrentDirectory(), model.FirmwareFileName);
  97. var remoteFilePath = string.Format("/mnt/{0}", Path.GetFileName(model.FirmwareFileName));
  98. var status = await client.UploadFileAsync(model.FirmwareFileName, remoteFilePath);
  99. if(status != FtpStatus.Success)
  100. {
  101. LogWriter.Log($"Firmware {modelIndex} upload failed");
  102. LogWriter.Report(LogEvent.FirmwareUpload, "fail", isError: true);
  103. return false;
  104. }
  105. }
  106. await client.DisconnectAsync();
  107. return true;
  108. }
  109. private async Task<bool> SignalUpdate()
  110. {
  111. var updateList = UpdateData.FirmwareUpdateModels;
  112. var restClient = new RestSharp.RestClient($"https://{ServerIpAddress}");
  113. restClient.ConfigureWebRequest((r) => { r.KeepAlive = true; });
  114. var request = new RestSharp.RestRequest("upgrade_iso_action.php", RestSharp.Method.POST);
  115. request.AlwaysMultipartFormData = true;
  116. request.AddHeader("Content-Type", "multipart/form-data");
  117. request.AddParameter("fw_tag", "iso");
  118. //request.AddFile("files[]", new byte[0] , "dummy");
  119. RestSharp.IRestResponse response = await restClient.ExecuteAsync(request);
  120. if (response.IsSuccessful)
  121. {
  122. return true;
  123. }
  124. else
  125. {
  126. if (response.ErrorException != null)
  127. {
  128. LogWriter.Log(response.ErrorException.Message, isError: true, isDebugLog: true);
  129. if (response.ErrorException.InnerException != null)
  130. {
  131. LogWriter.Log(response.ErrorException.InnerException.Message, isError: true, isDebugLog: true);
  132. }
  133. LogWriter.Log(response.StatusCode.ToString(), isError: true, isDebugLog: true);
  134. }
  135. return false;
  136. }
  137. }
  138. }
  139. }