123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- using FISLib.AfterSales;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- using Vinno.FIS.Sonopost.Managers;
- using Vinno.FIS.Sonopost.Managers.Interfaces;
- using Vinno.IUS.Common.Log;
- namespace Vinno.FIS.Sonopost.Features.AfterSales
- {
- internal abstract class AfterSalesTask : IDisposable
- {
- private AfterSalesCommandStatus _commandStatus;
- private double _progressPercent;
- private IAfterSalesService _fisAfterSalesService;
- protected bool IsExcuting { get; set; }
- private bool _isDownloading;
- private bool _isUploading;
- /// <summary>
- /// Raised when status changed.
- /// </summary>
- public event EventHandler CommandStatusChanged;
- /// <summary>
- /// Raised when progress changed.
- /// </summary>
- public event EventHandler ProgressChanged;
- /// <summary>
- /// Gets the cancellationTokenSource for this commmand.
- /// </summary>
- public CancellationTokenSource CancellationTokenSource { get; private set; }
- /// <summary>
- /// Gets or sets the after sales command status.
- /// </summary>
- public AfterSalesCommandStatus Status
- {
- get { return _commandStatus; }
- set
- {
- if (_commandStatus != value)
- {
- _commandStatus = value;
- CommandStatusChanged?.Invoke(this, EventArgs.Empty);
- }
- }
- }
- /// <summary>
- /// When command status is progressing, the value is valid. value is 0 to 1
- /// </summary>
- public double ProgressPercent
- {
- get { return _progressPercent; }
- set
- {
- if (!Equals(_progressPercent, value))
- {
- _progressPercent = value;
- if (_progressPercent >= 1)
- {
- Status = AfterSalesCommandStatus.RequestSuccess;
- }
- ProgressChanged?.Invoke(this, EventArgs.Empty);
- }
- }
- }
- /// <summary>
- /// Gets the detail type of the task.
- /// </summary>
- public FISDetailType DetailType { get; private set; }
- /// <summary>
- /// Gets the progress status.
- /// </summary>
- public FISProgressStatus ProgressStatus { get; private set; }
- /// <summary>
- /// Gets the addition parameters
- /// </summary>
- public IEnumerable<FISAdditionParameter> FISAdditionParameters { get; private set; }
- /// <summary>
- /// Execute the commmand
- /// </summary>
- public abstract void Execute();
- public AfterSalesTask()
- {
- _fisAfterSalesService = AppManager.Instance.GetManager<IFISManager>().FISAfterSalesService;
- _fisAfterSalesService.UploadProgressChanged += OnUploadProgressChanged;
- _fisAfterSalesService.DownloadProgressChanged += OnDownloadProgressChanged;
- _fisAfterSalesService.DownloadResultRaised += OnDownloadResultRaised;
- _fisAfterSalesService.UploadResultRaised += OnUploadResultRaised;
- }
- protected abstract void OnDownloadProgressChanged(object sender, double e);
- protected abstract void OnDownloadResultRaised(object sender, int e);
- protected abstract void OnUploadProgressChanged(object sender, double e);
- protected abstract void OnUploadResultRaised(object sender, string e);
- /// <summary>
- /// Update progress info.
- /// </summary>
- /// <param name="percent">The progress percent.</param>
- /// <param name="type"></param>
- /// <param name="status"></param>
- protected void UpdateProgressInfo(FISProcessInfo processInfo)
- {
- try
- {
- if (processInfo == null)
- {
- return;
- }
- DetailType = processInfo.DetailType;
- ProgressStatus = processInfo.Status;
- switch (processInfo.Status)
- {
- case FISProgressStatus.Progressing:
- ProgressPercent = processInfo.Percent;
- break;
- case FISProgressStatus.Finished:
- ProgressPercent = 1;
- break;
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"GetLogTaskAfterSales UpdateProgressInfo Error:{ex}");
- }
- }
- /// <summary>
- /// upload file to storage server
- /// </summary>
- /// <param name="filePath">file path for upload</param>
- protected void UploadFile(string filePath)
- {
- try
- {
- if (_isUploading)
- {
- return;
- }
- _isUploading = true;
- _fisAfterSalesService.UploadFile(filePath);
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"GetLogTaskAfterSales UploadFile Error:{ex}");
- _isUploading = false;
- }
- }
- /// <summary>
- /// get file from storage server
- /// </summary>
- /// <param name="fileToken">file token</param>
- /// <param name="filePath">file path for store</param>
- protected void DownloadFile(string fileToken, string filePath)
- {
- try
- {
- if (_isDownloading)
- {
- return;
- }
- _isDownloading = true;
- var fileTokenList = fileToken.Split(';').ToList();
- if (fileTokenList.Count == 1)
- {
- _fisAfterSalesService.DownloadFile(fileTokenList[0], filePath);
- }
- else
- {
- _fisAfterSalesService.DownloadLargeFile(fileTokenList, filePath);
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"GetLogTaskAfterSales DownloadFile Error:{ex}");
- _isDownloading = false;
- }
- }
- protected void CancelUpload()
- {
- try
- {
- if (_isUploading)
- {
- _fisAfterSalesService.StopUpload();
- _isUploading = false;
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"GetLogTaskAfterSales CancelUpload Error:{ex}");
- }
- }
- protected void CancelDownload()
- {
- try
- {
- if (_isDownloading)
- {
- _fisAfterSalesService.StopDownload();
- _isDownloading = false;
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"GetLogTaskAfterSales CancelDownload Error:{ex}");
- }
- }
- /// <summary>
- /// Send Process Result To Server
- /// </summary>
- /// <param name="fisProcessResult">FIS Process Result</param>
- protected void SendProcessResultToServer(FISProcessResult fisProcessResult)
- {
- try
- {
- _fisAfterSalesService.SendProcessResultToServer(fisProcessResult);
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"GetLogTaskAfterSales SendProcessResultToServer Error:{ex}");
- }
- }
- protected void SendRemoteMaintenanceResultToServer(FISRemoteMaintenanceResult fitRemoteMaintenanceResult)
- {
- try
- {
- _fisAfterSalesService.SendRemoteMaintenanceResultToServer(fitRemoteMaintenanceResult);
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"GetLogTaskAfterSales SendRemoteMaintenanceResultToServer Error:{ex}");
- }
- }
- internal void Init()
- {
- CancellationTokenSource = new CancellationTokenSource();
- }
- /// <summary>
- /// Sets the parameters getted from server.
- /// </summary>
- /// <param name="parameters"></param>
- public void SetParameters(IEnumerable<FISAdditionParameter> parameters)
- {
- try
- {
- FISAdditionParameters = parameters;
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"GetLogTaskAfterSales SetParameter Error:{ex}");
- }
- }
- /// <summary>
- /// Cancel the operation of the command,include OnCancel
- /// </summary>
- /// <param name="fromRemoteService">From Remote Service</param>
- /// <param name="fisProcessResult"></param>
- public void Cancel(bool fromRemoteService)
- {
- try
- {
- Status = AfterSalesCommandStatus.Cancelling;
- CancellationTokenSource.Cancel();
- CancelDownload();
- CancelUpload();
- OnCancel();
- if (fromRemoteService)
- {
- Logger.WriteLineInfo($"AfterSalesTask Cancelled FromRemoteService:{fromRemoteService}");
- }
- IsExcuting = false;
- Status = AfterSalesCommandStatus.Cancelled;
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"GetLogTaskAfterSales Cancel Error:{ex}");
- }
- }
- protected abstract void OnCancel();
- public void Dispose()
- {
- _fisAfterSalesService.UploadProgressChanged -= OnUploadProgressChanged;
- _fisAfterSalesService.DownloadProgressChanged -= OnDownloadProgressChanged;
- _fisAfterSalesService.DownloadResultRaised -= OnDownloadResultRaised;
- _fisAfterSalesService.UploadResultRaised -= OnUploadResultRaised;
- if (IsExcuting || _isUploading || _isDownloading)
- {
- Cancel(false);
- }
- }
- }
- }
|