123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- using FISLib.AfterSales;
- using System;
- using System.IO;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using Vinno.FIS.Sonopost.Common;
- using Vinno.FIS.Sonopost.Features.Oled;
- using Vinno.FIS.Sonopost.Helpers;
- using Vinno.FIS.Sonopost.Managers;
- using Vinno.FIS.Sonopost.Managers.Interfaces;
- using Vinno.IUS.Common.Log;
- using Vinno.IUS.Common.Utilities;
- using DirectoryHelper = Vinno.FIS.Sonopost.Helpers.DirectoryHelper;
- using FileHelper = Vinno.FIS.Sonopost.Helpers.FileHelper;
- namespace Vinno.FIS.Sonopost.Features.AfterSales
- {
- internal class PushPatchAfterSalesTask : AfterSalesTask
- {
- private string _downloadPatchName;
- private string _downloadFileToken;
- private string _targetId;
- private string _remotePatchPath;
- private const double _downloadSuccess = 0.8;
- private double _downloadProcess;
- private string _tempLoadFile;
- private string _patchFile;
- private FISMasterInfo _masterInfo;
- public PushPatchAfterSalesTask(FISMasterInfo masterInfo)
- {
- Status = AfterSalesCommandStatus.Progressing;
- _masterInfo = masterInfo;
- }
- protected override void OnDownloadProgressChanged(object sender, double downloadProgress)
- {
- var finalProcess = downloadProgress * _downloadSuccess;
- SetPushPatchProcess(finalProcess);
- }
- private void SetPushPatchProcess(double process)
- {
- try
- {
- _downloadProcess = process;
- FISProcessInfo processinfo;
- if (_downloadProcess >= 1)
- {
- processinfo = new FISProcessInfo(_downloadProcess, FISProgressStatus.Progressing, FISDetailType.Success);
- UpdateProgressInfo(processinfo);
- SendProcessResultToServer(new FISProcessResult(_targetId, FISTaskType.PushPatch, FISTaskStatus.Finished, FISDetailType.PushPatchSuccess, _downloadProcess, FISProcessSource.FromTerminal, string.Empty));
- }
- else if (_downloadProcess < 0.8)
- {
- processinfo = new FISProcessInfo(_downloadProcess, FISProgressStatus.Progressing, FISDetailType.DownLoadPacth);
- UpdateProgressInfo(processinfo);
- SendProcessResultToServer(new FISProcessResult(_targetId, FISTaskType.PushPatch, FISTaskStatus.Progressing, FISDetailType.DownLoadPacth, _downloadProcess, FISProcessSource.FromTerminal, string.Empty));
- }
- else
- {
- processinfo = new FISProcessInfo(_downloadProcess, FISProgressStatus.Progressing, FISDetailType.ExtractPatchFile);
- UpdateProgressInfo(processinfo);
- SendProcessResultToServer(new FISProcessResult(_targetId, FISTaskType.PushPatch, FISTaskStatus.Progressing, FISDetailType.ExtractPatchFile, _downloadProcess, FISProcessSource.FromTerminal, string.Empty));
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"Send download patch process error{ex}");
- }
- }
- protected override void OnDownloadResultRaised(object sender, int e)
- {
- try
- {
- if (e == 1)
- {
- SetPushPatchProcess(_downloadSuccess);
- MoveFile(_tempLoadFile, _patchFile);
- SetPushPatchProcess(0.85);
- SetPushPatchProcess(0.95);
- SetPushPatchProcess(1);
- try
- {
- ForceDisconnect();
- AppManager.Instance.GetManager<ILoginManager>().Logoff();
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"AfterSalesManager PatchTask LogOff Error:{ex}");
- }
- finally
- {
- AppManager.Instance.GetManager<IOledManager>().ShowStatus(OledMessage.Upgrading);
- UpgradeHelper.StartUpgrade(_patchFile, true);
- }
- }
- else
- {
- if (CancellationTokenSource.IsCancellationRequested)
- {
- DeleteTempPatchFile();
- return;
- }
- try
- {
- SendProcessResultToServer(new FISProcessResult(_targetId, FISTaskType.PushPatch, FISTaskStatus.Failed, FISDetailType.PushPatchError, 0, FISProcessSource.FromTerminal, string.Empty));
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"Send progress error happens:{ex}");
- }
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"PushPatchAfterSalesTask OnDownloadResultRaised Error:{ex}");
- }
- finally
- {
- CancelDownload();
- IsExcuting = false;
- Logger.WriteLineDebug($"Delete download patch file:{_patchFile}");
- FileHelper.DeleteFile(_patchFile);
- FileHelper.DeleteFile(_tempLoadFile);
- }
- }
- private void MoveFile(string sourceFile, string destFile)
- {
- try
- {
- if (File.Exists(sourceFile))
- {
- File.Move(sourceFile, destFile);
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"Move file:{sourceFile} to {destFile} eror:{ex}");
- }
- }
- private void DeleteTempPatchFile()
- {
- var oldTempPatchs = Directory.GetFiles(_remotePatchPath, "*.tmp");
- if (oldTempPatchs.Length > 0)
- {
- foreach (var tempPatchFile in oldTempPatchs)
- {
- FileHelper.DeleteFile(tempPatchFile);
- }
- }
- }
- public override void Execute()
- {
- try
- {
- if (IsExcuting)
- {
- return;
- }
- IsExcuting = true;
- if (FISAdditionParameters == null)
- {
- Logger.WriteLineError($"GetLogTask FISAdditionParameters is null");
- SendProcessResultToServer(new FISProcessResult(_masterInfo?.UserId, FISTaskType.GetLog, FISTaskStatus.Failed, FISDetailType.GetLogDeny, 0.01, FISProcessSource.FromTerminal, string.Empty));
- return;
- }
- InitParameters();
- var task = new Task(DownPacth, CancellationTokenSource.Token);
- task.Start();
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"PushPatchAfterSalesTask Execute Error:{ex}");
- IsExcuting = false;
- }
- }
- private void InitParameters()
- {
- var parameterFileToken = FISAdditionParameters.FirstOrDefault(x => x.Name == "FileToken");
- _downloadFileToken = parameterFileToken.Value;
- var parameterPatchName = FISAdditionParameters.FirstOrDefault(x => x.Name == "PatchName");
- _downloadPatchName = parameterPatchName.Value;
- _targetId = _masterInfo.UserId;
- _remotePatchPath = Path.Combine(SonopostConstants.DataFolder, "RemoteUpgrade");
- }
- private void DownPacth()
- {
- var id = IdHelper.Generate<string>();
- _tempLoadFile = Path.Combine(_remotePatchPath, id + ".tmp");
- _patchFile = Path.Combine(_remotePatchPath, _downloadPatchName + ".zip");
- try
- {
- _downloadProcess = 0;
- DirectoryHelper.CreateDirectory(_remotePatchPath);
- DownloadFile(_downloadFileToken, _tempLoadFile);
- }
- catch
- {
- if (CancellationTokenSource.IsCancellationRequested)
- {
- DeleteTempPatchFile();
- return;
- }
- try
- {
- SendProcessResultToServer(new FISProcessResult(_targetId, FISTaskType.PushPatch, FISTaskStatus.Failed, FISDetailType.PushPatchError, 0, FISProcessSource.FromTerminal, string.Empty));
- }
- catch (Exception e)
- {
- Logger.WriteLineError($"Send progress error happens:{e}");
- }
- IsExcuting = false;
- throw;
- }
- finally
- {
- Logger.WriteLineDebug($"Delete download patch file:{_patchFile}");
- FileHelper.DeleteFile(_patchFile);
- FileHelper.DeleteFile(_tempLoadFile);
- }
- }
- protected override void OnUploadProgressChanged(object sender, double uploadProgress)
- {
- }
- protected override void OnUploadResultRaised(object sender, string fileToken)
- {
- }
- protected override void OnCancel()
- {
- try
- {
- var process = new FISProcessResult(_targetId, FISTaskType.PushPatch, FISTaskStatus.Failed, FISDetailType.PushPatchCancel,
- 0, FISProcessSource.FromTerminal, string.Empty);
- SendProcessResultToServer(process);
- CancellationTokenSource.Cancel();
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"PushPatchTask OnCancel error: {ex}");
- }
- }
- }
- }
|