PushPatchAfterSalesTask.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. try
  81. {
  82. ForceDisconnect();
  83. AppManager.Instance.GetManager<ILoginManager>().Logoff();
  84. }
  85. catch (Exception ex)
  86. {
  87. Logger.WriteLineError($"AfterSalesManager PatchTask LogOff Error:{ex}");
  88. }
  89. finally
  90. {
  91. AppManager.Instance.GetManager<IOledManager>().ShowStatus(OledMessage.Upgrading);
  92. UpgradeHelper.StartUpgrade(_patchFile, true);
  93. }
  94. }
  95. else
  96. {
  97. if (CancellationTokenSource.IsCancellationRequested)
  98. {
  99. DeleteTempPatchFile();
  100. return;
  101. }
  102. try
  103. {
  104. SendProcessResultToServer(new FISProcessResult(_targetId, FISTaskType.PushPatch, FISTaskStatus.Failed, FISDetailType.PushPatchError, 0, FISProcessSource.FromTerminal, string.Empty));
  105. }
  106. catch (Exception ex)
  107. {
  108. Logger.WriteLineError($"Send progress error happens:{ex}");
  109. }
  110. }
  111. }
  112. catch (Exception ex)
  113. {
  114. Logger.WriteLineError($"PushPatchAfterSalesTask OnDownloadResultRaised Error:{ex}");
  115. }
  116. finally
  117. {
  118. CancelDownload();
  119. IsExcuting = false;
  120. Logger.WriteLineDebug($"Delete download patch file:{_patchFile}");
  121. FileHelper.DeleteFile(_patchFile);
  122. FileHelper.DeleteFile(_tempLoadFile);
  123. }
  124. }
  125. private void MoveFile(string sourceFile, string destFile)
  126. {
  127. try
  128. {
  129. if (File.Exists(sourceFile))
  130. {
  131. File.Move(sourceFile, destFile);
  132. }
  133. }
  134. catch (Exception ex)
  135. {
  136. Logger.WriteLineError($"Move file:{sourceFile} to {destFile} eror:{ex}");
  137. }
  138. }
  139. private void DeleteTempPatchFile()
  140. {
  141. var oldTempPatchs = Directory.GetFiles(_remotePatchPath, "*.tmp");
  142. if (oldTempPatchs.Length > 0)
  143. {
  144. foreach (var tempPatchFile in oldTempPatchs)
  145. {
  146. FileHelper.DeleteFile(tempPatchFile);
  147. }
  148. }
  149. }
  150. public override void Execute()
  151. {
  152. try
  153. {
  154. if (IsExcuting)
  155. {
  156. return;
  157. }
  158. IsExcuting = true;
  159. if (FISAdditionParameters == null)
  160. {
  161. Logger.WriteLineError($"GetLogTask FISAdditionParameters is null");
  162. SendProcessResultToServer(new FISProcessResult(_masterInfo?.UserId, FISTaskType.GetLog, FISTaskStatus.Failed, FISDetailType.GetLogDeny, 0.01, FISProcessSource.FromTerminal, string.Empty));
  163. return;
  164. }
  165. InitParameters();
  166. var task = new Task(DownPacth, CancellationTokenSource.Token);
  167. task.Start();
  168. }
  169. catch (Exception ex)
  170. {
  171. Logger.WriteLineError($"PushPatchAfterSalesTask Execute Error:{ex}");
  172. IsExcuting = false;
  173. }
  174. }
  175. private void InitParameters()
  176. {
  177. var parameterFileToken = FISAdditionParameters.FirstOrDefault(x => x.Name == "FileToken");
  178. _downloadFileToken = parameterFileToken.Value;
  179. var parameterPatchName = FISAdditionParameters.FirstOrDefault(x => x.Name == "PatchName");
  180. _downloadPatchName = parameterPatchName.Value;
  181. _targetId = _masterInfo.UserId;
  182. _remotePatchPath = Path.Combine(SonopostConstants.DataFolder, "RemoteUpgrade");
  183. }
  184. private void DownPacth()
  185. {
  186. var id = IdHelper.Generate<string>();
  187. _tempLoadFile = Path.Combine(_remotePatchPath, id + ".tmp");
  188. _patchFile = Path.Combine(_remotePatchPath, _downloadPatchName + ".zip");
  189. try
  190. {
  191. _downloadProcess = 0;
  192. DirectoryHelper.CreateDirectory(_remotePatchPath);
  193. DownloadFile(_downloadFileToken, _tempLoadFile);
  194. }
  195. catch
  196. {
  197. if (CancellationTokenSource.IsCancellationRequested)
  198. {
  199. DeleteTempPatchFile();
  200. return;
  201. }
  202. try
  203. {
  204. SendProcessResultToServer(new FISProcessResult(_targetId, FISTaskType.PushPatch, FISTaskStatus.Failed, FISDetailType.PushPatchError, 0, FISProcessSource.FromTerminal, string.Empty));
  205. }
  206. catch (Exception e)
  207. {
  208. Logger.WriteLineError($"Send progress error happens:{e}");
  209. }
  210. IsExcuting = false;
  211. throw;
  212. }
  213. finally
  214. {
  215. Logger.WriteLineDebug($"Delete download patch file:{_patchFile}");
  216. FileHelper.DeleteFile(_patchFile);
  217. FileHelper.DeleteFile(_tempLoadFile);
  218. }
  219. }
  220. protected override void OnUploadProgressChanged(object sender, double uploadProgress)
  221. {
  222. }
  223. protected override void OnUploadResultRaised(object sender, string fileToken)
  224. {
  225. }
  226. protected override void OnCancel()
  227. {
  228. try
  229. {
  230. var process = new FISProcessResult(_targetId, FISTaskType.PushPatch, FISTaskStatus.Failed, FISDetailType.PushPatchCancel,
  231. 0, FISProcessSource.FromTerminal, string.Empty);
  232. SendProcessResultToServer(process);
  233. CancellationTokenSource.Cancel();
  234. }
  235. catch (Exception ex)
  236. {
  237. Logger.WriteLineError($"PushPatchTask OnCancel error: {ex}");
  238. }
  239. }
  240. }
  241. }