PushPatchAfterSalesTask.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using FISLib.AfterSales;
  2. using System;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Vinno.FIS.Sonopost.Common;
  8. using Vinno.FIS.Sonopost.Features.Oled;
  9. using Vinno.FIS.Sonopost.Helpers;
  10. using Vinno.FIS.Sonopost.Managers;
  11. using Vinno.FIS.Sonopost.Managers.Interfaces;
  12. using Vinno.IUS.Common.Log;
  13. using Vinno.IUS.Common.Utilities;
  14. using DirectoryHelper = Vinno.FIS.Sonopost.Helpers.DirectoryHelper;
  15. using FileHelper = Vinno.FIS.Sonopost.Helpers.FileHelper;
  16. namespace Vinno.FIS.Sonopost.Features.AfterSales
  17. {
  18. internal class PushPatchAfterSalesTask : AfterSalesTask
  19. {
  20. private string _downloadPatchName;
  21. private string _downloadFileToken;
  22. private string _targetId;
  23. private string _remotePatchPath;
  24. private const double _downloadSuccess = 0.8;
  25. private double _downloadProcess;
  26. private string _tempLoadFile;
  27. private string _patchFile;
  28. private FISMasterInfo _masterInfo;
  29. public PushPatchAfterSalesTask(FISMasterInfo masterInfo)
  30. {
  31. Status = AfterSalesCommandStatus.Progressing;
  32. _masterInfo = masterInfo;
  33. }
  34. protected override void OnDownloadProgressChanged(object sender, double downloadProgress)
  35. {
  36. var finalProcess = downloadProgress * _downloadSuccess;
  37. SetPushPatchProcess(finalProcess);
  38. }
  39. private void SetPushPatchProcess(double process)
  40. {
  41. try
  42. {
  43. _downloadProcess = process;
  44. FISProcessInfo processinfo;
  45. if (_downloadProcess >= 1)
  46. {
  47. processinfo = new FISProcessInfo(_downloadProcess, FISProgressStatus.Progressing, FISDetailType.Success);
  48. UpdateProgressInfo(processinfo);
  49. SendProcessResultToServer(new FISProcessResult(_targetId, FISTaskType.PushPatch, FISTaskStatus.Finished, FISDetailType.PushPatchSuccess, _downloadProcess, FISProcessSource.FromTerminal, string.Empty));
  50. }
  51. else if (_downloadProcess < 0.8)
  52. {
  53. processinfo = new FISProcessInfo(_downloadProcess, FISProgressStatus.Progressing, FISDetailType.DownLoadPacth);
  54. UpdateProgressInfo(processinfo);
  55. SendProcessResultToServer(new FISProcessResult(_targetId, FISTaskType.PushPatch, FISTaskStatus.Progressing, FISDetailType.DownLoadPacth, _downloadProcess, FISProcessSource.FromTerminal, string.Empty));
  56. }
  57. else
  58. {
  59. processinfo = new FISProcessInfo(_downloadProcess, FISProgressStatus.Progressing, FISDetailType.ExtractPatchFile);
  60. UpdateProgressInfo(processinfo);
  61. SendProcessResultToServer(new FISProcessResult(_targetId, FISTaskType.PushPatch, FISTaskStatus.Progressing, FISDetailType.ExtractPatchFile, _downloadProcess, FISProcessSource.FromTerminal, string.Empty));
  62. }
  63. }
  64. catch (Exception ex)
  65. {
  66. Logger.WriteLineError($"Send download patch process error{ex}");
  67. }
  68. }
  69. protected override void OnDownloadResultRaised(object sender, int e)
  70. {
  71. try
  72. {
  73. if (e == 1)
  74. {
  75. SetPushPatchProcess(_downloadSuccess);
  76. MoveFile(_tempLoadFile, _patchFile);
  77. SetPushPatchProcess(0.85);
  78. SetPushPatchProcess(0.95);
  79. SetPushPatchProcess(1);
  80. AppManager.Instance.GetManager<IOledManager>().ShowStatus(OledMessage.Upgrading);
  81. UpgradeHelper.StartUpgrade(_patchFile, true);
  82. }
  83. else
  84. {
  85. if (CancellationTokenSource.IsCancellationRequested)
  86. {
  87. DeleteTempPatchFile();
  88. return;
  89. }
  90. try
  91. {
  92. SendProcessResultToServer(new FISProcessResult(_targetId, FISTaskType.PushPatch, FISTaskStatus.Failed, FISDetailType.PushPatchError, 0, FISProcessSource.FromTerminal, string.Empty));
  93. }
  94. catch (Exception ex)
  95. {
  96. Logger.WriteLineError($"Send progress error happens:{ex}");
  97. }
  98. }
  99. }
  100. catch (Exception ex)
  101. {
  102. Logger.WriteLineError($"PushPatchAfterSalesTask OnDownloadResultRaised Error:{ex}");
  103. }
  104. finally
  105. {
  106. CancelDownload();
  107. IsExcuting = false;
  108. Logger.WriteLineDebug($"Delete download patch file:{_patchFile}");
  109. FileHelper.DeleteFile(_patchFile);
  110. FileHelper.DeleteFile(_tempLoadFile);
  111. }
  112. }
  113. private void MoveFile(string sourceFile, string destFile)
  114. {
  115. try
  116. {
  117. if (File.Exists(sourceFile))
  118. {
  119. File.Move(sourceFile, destFile);
  120. }
  121. }
  122. catch (Exception ex)
  123. {
  124. Logger.WriteLineError($"Move file:{sourceFile} to {destFile} eror:{ex}");
  125. }
  126. }
  127. private void DeleteTempPatchFile()
  128. {
  129. var oldTempPatchs = Directory.GetFiles(_remotePatchPath, "*.tmp");
  130. if (oldTempPatchs.Length > 0)
  131. {
  132. foreach (var tempPatchFile in oldTempPatchs)
  133. {
  134. FileHelper.DeleteFile(tempPatchFile);
  135. }
  136. }
  137. }
  138. public override void Execute()
  139. {
  140. try
  141. {
  142. if (IsExcuting)
  143. {
  144. return;
  145. }
  146. IsExcuting = true;
  147. if (FISAdditionParameters == null)
  148. {
  149. Logger.WriteLineError($"GetLogTask FISAdditionParameters is null");
  150. SendProcessResultToServer(new FISProcessResult(_masterInfo?.UserId, FISTaskType.GetLog, FISTaskStatus.Failed, FISDetailType.GetLogDeny, 0.01, FISProcessSource.FromTerminal, string.Empty));
  151. return;
  152. }
  153. InitParameters();
  154. var task = new Task(DownPacth, CancellationTokenSource.Token);
  155. task.Start();
  156. }
  157. catch (Exception ex)
  158. {
  159. Logger.WriteLineError($"PushPatchAfterSalesTask Execute Error:{ex}");
  160. IsExcuting = false;
  161. }
  162. }
  163. private void InitParameters()
  164. {
  165. var parameterFileToken = FISAdditionParameters.FirstOrDefault(x => x.Name == "FileToken");
  166. _downloadFileToken = parameterFileToken.Value;
  167. var parameterPatchName = FISAdditionParameters.FirstOrDefault(x => x.Name == "PatchName");
  168. _downloadPatchName = parameterPatchName.Value;
  169. _targetId = _masterInfo.UserId;
  170. _remotePatchPath = Path.Combine(SonopostConstants.DataFolder, "RemoteUpgrade");
  171. }
  172. private void DownPacth()
  173. {
  174. var id = IdHelper.Generate<string>();
  175. _tempLoadFile = Path.Combine(_remotePatchPath, id + ".tmp");
  176. _patchFile = Path.Combine(_remotePatchPath, _downloadPatchName + ".zip");
  177. try
  178. {
  179. _downloadProcess = 0;
  180. DirectoryHelper.CreateDirectory(_remotePatchPath);
  181. DownloadFile(_downloadFileToken, _tempLoadFile);
  182. }
  183. catch
  184. {
  185. if (CancellationTokenSource.IsCancellationRequested)
  186. {
  187. DeleteTempPatchFile();
  188. return;
  189. }
  190. try
  191. {
  192. SendProcessResultToServer(new FISProcessResult(_targetId, FISTaskType.PushPatch, FISTaskStatus.Failed, FISDetailType.PushPatchError, 0, FISProcessSource.FromTerminal, string.Empty));
  193. }
  194. catch (Exception e)
  195. {
  196. Logger.WriteLineError($"Send progress error happens:{e}");
  197. }
  198. IsExcuting = false;
  199. throw;
  200. }
  201. finally
  202. {
  203. Logger.WriteLineDebug($"Delete download patch file:{_patchFile}");
  204. FileHelper.DeleteFile(_patchFile);
  205. FileHelper.DeleteFile(_tempLoadFile);
  206. }
  207. }
  208. protected override void OnUploadProgressChanged(object sender, double uploadProgress)
  209. {
  210. }
  211. protected override void OnUploadResultRaised(object sender, string fileToken)
  212. {
  213. }
  214. protected override void OnCancel()
  215. {
  216. try
  217. {
  218. var process = new FISProcessResult(_targetId, FISTaskType.PushPatch, FISTaskStatus.Failed, FISDetailType.PushPatchCancel,
  219. 0, FISProcessSource.FromTerminal, string.Empty);
  220. SendProcessResultToServer(process);
  221. CancellationTokenSource.Cancel();
  222. }
  223. catch (Exception ex)
  224. {
  225. Logger.WriteLineError($"PushPatchTask OnCancel error: {ex}");
  226. }
  227. }
  228. }
  229. }