PushPatchAfterSalesTask.cs 9.7 KB

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