FirmwareFtpUploadProcedure.cs 5.4 KB

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