FirmwareFtpUploadProcedure.cs 5.4 KB

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