AfterSalesTask.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. using FISLib.AfterSales;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading;
  6. using Vinno.FIS.Sonopost.Managers;
  7. using Vinno.FIS.Sonopost.Managers.Interfaces;
  8. using Vinno.IUS.Common.Log;
  9. namespace Vinno.FIS.Sonopost.Features.AfterSales
  10. {
  11. internal abstract class AfterSalesTask : IDisposable
  12. {
  13. private AfterSalesCommandStatus _commandStatus;
  14. private double _progressPercent;
  15. private IAfterSalesService _fisAfterSalesService;
  16. protected bool IsExcuting { get; set; }
  17. private bool _isDownloading;
  18. private bool _isUploading;
  19. /// <summary>
  20. /// Raised when status changed.
  21. /// </summary>
  22. public event EventHandler CommandStatusChanged;
  23. /// <summary>
  24. /// Raised when progress changed.
  25. /// </summary>
  26. public event EventHandler ProgressChanged;
  27. /// <summary>
  28. /// Gets the cancellationTokenSource for this commmand.
  29. /// </summary>
  30. public CancellationTokenSource CancellationTokenSource { get; private set; }
  31. /// <summary>
  32. /// Gets or sets the after sales command status.
  33. /// </summary>
  34. public AfterSalesCommandStatus Status
  35. {
  36. get { return _commandStatus; }
  37. set
  38. {
  39. if (_commandStatus != value)
  40. {
  41. _commandStatus = value;
  42. CommandStatusChanged?.Invoke(this, EventArgs.Empty);
  43. }
  44. }
  45. }
  46. /// <summary>
  47. /// When command status is progressing, the value is valid. value is 0 to 1
  48. /// </summary>
  49. public double ProgressPercent
  50. {
  51. get { return _progressPercent; }
  52. set
  53. {
  54. if (!Equals(_progressPercent, value))
  55. {
  56. _progressPercent = value;
  57. if (_progressPercent >= 1)
  58. {
  59. Status = AfterSalesCommandStatus.RequestSuccess;
  60. }
  61. ProgressChanged?.Invoke(this, EventArgs.Empty);
  62. }
  63. }
  64. }
  65. /// <summary>
  66. /// Gets the detail type of the task.
  67. /// </summary>
  68. public FISDetailType DetailType { get; private set; }
  69. /// <summary>
  70. /// Gets the progress status.
  71. /// </summary>
  72. public FISProgressStatus ProgressStatus { get; private set; }
  73. /// <summary>
  74. /// Gets the addition parameters
  75. /// </summary>
  76. public IEnumerable<FISAdditionParameter> FISAdditionParameters { get; private set; }
  77. /// <summary>
  78. /// Execute the commmand
  79. /// </summary>
  80. public abstract void Execute();
  81. public AfterSalesTask()
  82. {
  83. _fisAfterSalesService = AppManager.Instance.GetManager<IFISManager>().FISAfterSalesService;
  84. _fisAfterSalesService.UploadProgressChanged += OnUploadProgressChanged;
  85. _fisAfterSalesService.DownloadProgressChanged += OnDownloadProgressChanged;
  86. _fisAfterSalesService.DownloadResultRaised += OnDownloadResultRaised;
  87. _fisAfterSalesService.UploadResultRaised += OnUploadResultRaised;
  88. }
  89. protected abstract void OnDownloadProgressChanged(object sender, double e);
  90. protected abstract void OnDownloadResultRaised(object sender, int e);
  91. protected abstract void OnUploadProgressChanged(object sender, double e);
  92. protected abstract void OnUploadResultRaised(object sender, string e);
  93. /// <summary>
  94. /// Update progress info.
  95. /// </summary>
  96. /// <param name="percent">The progress percent.</param>
  97. /// <param name="type"></param>
  98. /// <param name="status"></param>
  99. protected void UpdateProgressInfo(FISProcessInfo processInfo)
  100. {
  101. try
  102. {
  103. if (processInfo == null)
  104. {
  105. return;
  106. }
  107. DetailType = processInfo.DetailType;
  108. ProgressStatus = processInfo.Status;
  109. switch (processInfo.Status)
  110. {
  111. case FISProgressStatus.Progressing:
  112. ProgressPercent = processInfo.Percent;
  113. break;
  114. case FISProgressStatus.Finished:
  115. ProgressPercent = 1;
  116. break;
  117. }
  118. }
  119. catch (Exception ex)
  120. {
  121. Logger.WriteLineError($"GetLogTaskAfterSales UpdateProgressInfo Error:{ex}");
  122. }
  123. }
  124. /// <summary>
  125. /// upload file to storage server
  126. /// </summary>
  127. /// <param name="filePath">file path for upload</param>
  128. protected void UploadFile(string filePath)
  129. {
  130. try
  131. {
  132. if (_isUploading)
  133. {
  134. return;
  135. }
  136. _isUploading = true;
  137. _fisAfterSalesService.UploadFile(filePath);
  138. }
  139. catch (Exception ex)
  140. {
  141. Logger.WriteLineError($"GetLogTaskAfterSales UploadFile Error:{ex}");
  142. _isUploading = false;
  143. }
  144. }
  145. /// <summary>
  146. /// get file from storage server
  147. /// </summary>
  148. /// <param name="fileToken">file token</param>
  149. /// <param name="filePath">file path for store</param>
  150. protected void DownloadFile(string fileToken, string filePath)
  151. {
  152. try
  153. {
  154. if (_isDownloading)
  155. {
  156. return;
  157. }
  158. _isDownloading = true;
  159. var fileTokenList = fileToken.Split(';').ToList();
  160. if (fileTokenList.Count == 1)
  161. {
  162. _fisAfterSalesService.DownloadFile(fileTokenList[0], filePath);
  163. }
  164. else
  165. {
  166. _fisAfterSalesService.DownloadLargeFile(fileTokenList, filePath);
  167. }
  168. }
  169. catch (Exception ex)
  170. {
  171. Logger.WriteLineError($"GetLogTaskAfterSales DownloadFile Error:{ex}");
  172. _isDownloading = false;
  173. }
  174. }
  175. protected void CancelUpload()
  176. {
  177. try
  178. {
  179. if (_isUploading)
  180. {
  181. _fisAfterSalesService.StopUpload();
  182. _isUploading = false;
  183. }
  184. }
  185. catch (Exception ex)
  186. {
  187. Logger.WriteLineError($"GetLogTaskAfterSales CancelUpload Error:{ex}");
  188. }
  189. }
  190. protected void CancelDownload()
  191. {
  192. try
  193. {
  194. if (_isDownloading)
  195. {
  196. _fisAfterSalesService.StopDownload();
  197. _isDownloading = false;
  198. }
  199. }
  200. catch (Exception ex)
  201. {
  202. Logger.WriteLineError($"GetLogTaskAfterSales CancelDownload Error:{ex}");
  203. }
  204. }
  205. /// <summary>
  206. /// Send Process Result To Server
  207. /// </summary>
  208. /// <param name="fisProcessResult">FIS Process Result</param>
  209. protected void SendProcessResultToServer(FISProcessResult fisProcessResult)
  210. {
  211. try
  212. {
  213. _fisAfterSalesService.SendProcessResultToServer(fisProcessResult);
  214. }
  215. catch (Exception ex)
  216. {
  217. Logger.WriteLineError($"GetLogTaskAfterSales SendProcessResultToServer Error:{ex}");
  218. }
  219. }
  220. protected void SendRemoteMaintenanceResultToServer(FISRemoteMaintenanceResult fitRemoteMaintenanceResult)
  221. {
  222. try
  223. {
  224. _fisAfterSalesService.SendRemoteMaintenanceResultToServer(fitRemoteMaintenanceResult);
  225. }
  226. catch (Exception ex)
  227. {
  228. Logger.WriteLineError($"GetLogTaskAfterSales SendRemoteMaintenanceResultToServer Error:{ex}");
  229. }
  230. }
  231. internal void Init()
  232. {
  233. CancellationTokenSource = new CancellationTokenSource();
  234. }
  235. /// <summary>
  236. /// Sets the parameters getted from server.
  237. /// </summary>
  238. /// <param name="parameters"></param>
  239. public void SetParameters(IEnumerable<FISAdditionParameter> parameters)
  240. {
  241. try
  242. {
  243. FISAdditionParameters = parameters;
  244. }
  245. catch (Exception ex)
  246. {
  247. Logger.WriteLineError($"GetLogTaskAfterSales SetParameter Error:{ex}");
  248. }
  249. }
  250. /// <summary>
  251. /// Cancel the operation of the command,include OnCancel
  252. /// </summary>
  253. /// <param name="fromRemoteService">From Remote Service</param>
  254. /// <param name="fisProcessResult"></param>
  255. public void Cancel(bool fromRemoteService)
  256. {
  257. try
  258. {
  259. Status = AfterSalesCommandStatus.Cancelling;
  260. CancellationTokenSource.Cancel();
  261. CancelDownload();
  262. CancelUpload();
  263. OnCancel();
  264. if (fromRemoteService)
  265. {
  266. Logger.WriteLineInfo($"AfterSalesTask Cancelled FromRemoteService:{fromRemoteService}");
  267. }
  268. IsExcuting = false;
  269. Status = AfterSalesCommandStatus.Cancelled;
  270. }
  271. catch (Exception ex)
  272. {
  273. Logger.WriteLineError($"GetLogTaskAfterSales Cancel Error:{ex}");
  274. }
  275. }
  276. protected abstract void OnCancel();
  277. public void Dispose()
  278. {
  279. _fisAfterSalesService.UploadProgressChanged -= OnUploadProgressChanged;
  280. _fisAfterSalesService.DownloadProgressChanged -= OnDownloadProgressChanged;
  281. _fisAfterSalesService.DownloadResultRaised -= OnDownloadResultRaised;
  282. _fisAfterSalesService.UploadResultRaised -= OnUploadResultRaised;
  283. if (IsExcuting || _isUploading || _isDownloading)
  284. {
  285. Cancel(false);
  286. }
  287. }
  288. }
  289. }