123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- using System;
- using System.IO;
- using System.Threading;
- using System.Threading.Tasks;
- using Vinno.IUS.Common.Log;
- using Vinno.IUS.Common.Network.Leaf;
- using Vinno.IUS.Common.Network.Tcp;
- using Vinno.IUS.Common.Network.Transfer;
- using Vinno.IUS.Common.Utilities;
- using Vinno.vCloud.Common.Storage.Download;
- using Vinno.vCloud.FIS.CrossPlatform.Common;
- using Vinno.vCloud.Protocol.Infrastructures;
- using Vinno.vCloud.Protocol.Messages.Terminal.Remedical.TerminialLog;
- using Vinno.vCloud.Protocol.Messages.Upgrade;
- namespace Vinno.vCloud.Common.FIS.Upgraders
- {
- internal class Upgraders : IUpgraders
- {
- private const string _tokenSplitor = "1!U$";
- private const string _tokenSplitor2 = "0!U$";
- private readonly ClientLeaf _leaf;
- private bool _disposed;
- /// <summary>
- /// Raise the event when force upgrade.
- /// </summary>
- public event EventHandler<UpgradePatchInfo> ForceUpgraded;
- /// <summary>
- /// The constructor of the AfterSales
- /// </summary>
- /// <param name="leaf">The <see cref="ClientLeaf"/></param>
- public Upgraders(ClientLeaf leaf)
- {
- _leaf = leaf;
- _leaf.MessageArrived += OnMessageArrived;
- }
- ~Upgraders()
- {
- Dispose();
- }
- /// <summary>
- /// Get upgrade version.
- /// </summary>
- /// <returns>Package info</returns>
- public PackageInfo GetUpgradeVersion(UpgradePlatform platform, UpgradeType upgradeType)
- {
- using (var request = MessagePool.GetMessage<GetUpgradeVersionRequest>())
- {
- request.Platform = platform;
- request.Type = upgradeType;
- var result = _leaf.Send(request);
- var versionResult = GetUpgradeVersionResult.Convert(result);
- if (versionResult != null)
- {
- var upgradeServerUrl = versionResult.UpgradeUrl;
- var version = versionResult.Version;
- var fileMd5 = versionResult.FileMD5.GetBytes();
- return new PackageInfo(version, upgradeServerUrl, fileMd5);
- }
- }
- return null;
- }
- /// <summary>
- /// Download client package.
- /// </summary>
- public void DownloadPackage(string url, string packagePath, UpgradePlatform platform, UpgradeType upgradeType)
- {
- var tcpCreator = new TcpCreator(url);
- using (var request = MessagePool.GetMessage<GetUpgradeFileDataRequest>())
- {
- request.Platform = platform;
- request.Type = upgradeType;
- var tempPath = packagePath + ".tmp";
- using (var stream = File.Create(tempPath))
- {
- var downloadLeaf = new ClientLeaf(new LeafIdContext(), LeafMode.None, tcpCreator);
- try
- {
- downloadLeaf.Send(request, sendResult =>
- {
- var dataResult = GetUpgradeFileDataResult.Convert(sendResult.Result);
- if (dataResult != null)
- {
- var data = dataResult.FileData.GetBytes();
- stream.Write(data, 0, data.Length);
- }
- });
- }
- finally
- {
- downloadLeaf.Close();
- }
- }
- File.Move(tempPath, packagePath);
- }
- }
- /// <summary>
- /// Download client package support the Resume breakpoint.
- /// </summary>
- public void ResumeDownloadPackage(string url, string packagePath, UpgradePlatform platform, UpgradeType upgradeType)
- {
- }
- /// <summary>
- /// Get Ultrasound Machine Latest Package Info
- /// </summary>
- /// <param name="uniqueCode"></param>
- /// <returns>Ultrasound Machine Package Info</returns>
- public UpgradePackageInfo GetUltrasoundMachineLatestPackageInfo(string uniqueCode)
- {
- using (var queryLatestPackageRequest = MessagePool.GetMessage<QueryLatestPackageRequest>())
- {
- queryLatestPackageRequest.UniquedCode = uniqueCode;
- var message = _leaf.Send(queryLatestPackageRequest);
- if (message == null)
- {
- return null;
- }
- var result = GetPackageResult.Convert(message);
- if (result != null)
- {
- var createUserName = result.CreateUserName;
- var description = result.Description;
- var fileName = result.FileName;
- var fileSize = result.FileSize;
- var fileToken = result.FileToken;
- var updateTime = result.UpdateTime;
- var version = result.Version;
- return new UpgradePackageInfo
- {
- PackageName = fileName,
- Version = version,
- UniqueCode = uniqueCode,
- Description = description,
- PackageToken = fileToken,
- PackageSize = fileSize,
- UploadUserName = createUserName,
- UpdateTime = updateTime,
- };
- }
- }
- return null;
- }
- /// <summary>
- /// Download the Latest Package of Ultrasound Machine from Storage Server Async.
- /// </summary>
- /// <param name="fileToken">File Token</param>
- /// <param name="packagePath">The Path to place the package</param>
- /// <param name="progress">Download Progress</param>
- /// <param name="cancelTokenSource">Cancel Token Source</param>
- public void DownloadPackage(string url, string packagePath, Action<double> progress = null, CancellationTokenSource cancelTokenSource = null)
- {
- var fileToken = url;
- if (!fileToken.StartsWith(_tokenSplitor) && !fileToken.StartsWith(_tokenSplitor2))
- {
- fileToken = _tokenSplitor + fileToken;
- }
- DownloadHelper.GetFile(fileToken, packagePath, progress, cancelTokenSource, true);
- }
- /// <summary>
- /// Download the Latest Package of Ultrasound Machine from Storage Server Async.
- /// </summary>
- /// <param name="fileToken">File Token</param>
- /// <param name="packagePath">The Path to place the package</param>
- /// <param name="progress">Download Progress</param>
- /// <param name="cancelTokenSource">Cancel Token Source</param>
- /// <returns></returns>
- public async Task DownloadPackageAsync(string url, string packagePath, Action<double> progress = null, CancellationTokenSource cancelTokenSource = null)
- {
- var fileToken = url;
- if (!fileToken.StartsWith(_tokenSplitor) && !fileToken.StartsWith(_tokenSplitor2))
- {
- fileToken = _tokenSplitor + fileToken;
- }
- await DownloadHelper.GetFileAsync(fileToken, packagePath, progress, cancelTokenSource);
- }
- /// <summary>
- /// Get Latest FIS Patch
- /// </summary>
- /// <param name="patchType">Device Patch Type</param>
- /// <param name="osType">Client OS Type</param>
- /// <returns>Latest FIS Patch Info</returns>
- public UpgradePatchInfo GetLatestFISPatch(DevicePatchType patchType, ClientOSType osType)
- {
- using (var request = MessagePool.GetMessage<GetPatchFISRequest>())
- {
- request.PatchType = patchType;
- request.FitPlatform = osType;
- request.IsSonopost = CommonParameter.Instance.IsSonopost;
- var message = _leaf.Send(request);
- var result = GetPatchFISResult.Convert(message);
- if (result != null)
- {
- var returnPatchType = result.PatchType;
- var version = result.Version;
- var url = result.FileUrl;
- var description = result.Description;
- return new UpgradePatchInfo(returnPatchType, version, url, description);
- }
- }
- return null;
- }
- private void DoDispose()
- {
- if (!_disposed)
- {
- _leaf.MessageArrived -= OnMessageArrived;
- _disposed = true;
- }
- }
- private void OnMessageArrived(object sender, Message e)
- {
- var modifyFISNotification = ModifyFISNotification.Convert(e);
- if (modifyFISNotification != null)
- {
- HandleModifyFISNotification(modifyFISNotification);
- }
- }
- private void HandleModifyFISNotification(ModifyFISNotification modifyFISNotification)
- {
- ForceUpgraded?.Invoke(null, new UpgradePatchInfo(modifyFISNotification.PatchType, modifyFISNotification.Version, modifyFISNotification.FileUrl, modifyFISNotification.Description));
- }
- /// <summary>
- /// Is Has New Package
- /// </summary>
- /// <param name="platform"></param>
- /// <param name="upgradeType"></param>
- /// <param name="version"></param>
- /// <returns></returns>
- public UpgradeInfo IsHasNewPackage(UpgradePlatform platform, UpgradeType upgradeType, string version)
- {
- try
- {
- using (var request = new GetUpgradeInfoRequest())
- {
- request.Platform = platform;
- request.Type = upgradeType;
- request.CurrentVersion = version;
- var currentVersion = new Version("1.0.0.0");
- Version.TryParse(version, out currentVersion);
- var result = _leaf.Send(request);
- var upgradeInfoResult = GetUpgradeInfoResult.Convert(result);
- if (upgradeInfoResult != null)
- {
- var latestVersion = new Version("1.0.0.0");
- Version.TryParse(upgradeInfoResult.NewVersion, out latestVersion);
- Logger.WriteLineInfo($"The latest version is {latestVersion} in server");
- if (upgradeInfoResult.UpgadeType != UpgradeTypeEnum.NoUpgrade && latestVersion > currentVersion)
- {
- return new UpgradeInfo
- {
- UpgradeUrl = upgradeInfoResult.UpgradeUrl,
- StorageType = upgradeInfoResult.StorageType,
- UpgradeType = upgradeType,
- UpgradePlatform = platform,
- };
- }
- }
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"DownloadUFilePackage Error:{ex}");
- }
- return new UpgradeInfo
- {
- UpgradeUrl = "",
- UpgradeType = upgradeType,
- UpgradePlatform = platform,
- };
- }
- /// <summary>
- /// <summary>
- /// Download Package
- /// </summary>
- /// <param name="packagePath"></param>
- /// <param name="progress"></param>
- /// <param name="cancelTokenSource"></param>
- public void DownloadPackage(string packagePath, UpgradeInfo upgradeInfo, Action<double> progress = null, CancellationTokenSource cancelTokenSource = null)
- {
- try
- {
- if (upgradeInfo.StorageType == StorageType.ObjectStorage)
- {
- DownloadHelper.GetFile(upgradeInfo.UpgradeUrl, packagePath, progress, cancelTokenSource, true); //下载升级包
- }
- else
- {
- var tcpCreatorDownLoad = new TcpCreator(upgradeInfo.UpgradeUrl);
- var downLoadLeaf = new ClientLeaf(new LeafIdContext(), LeafMode.Single, tcpCreatorDownLoad);
- var packageFileExtension = Path.GetExtension(packagePath);
- var cacheFilePath = Path.ChangeExtension(packagePath, "tmp");
- FileHelper.DeleteFile(packagePath);
- FileHelper.DeleteFile(cacheFilePath);
- var finished = false;
- using (var stream = File.Open(cacheFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)) //打开文件
- {
- while (!finished)
- {
- if (cancelTokenSource != null && cancelTokenSource.IsCancellationRequested)
- {
- FileHelper.DeleteFile(cacheFilePath);
- return;
- }
- var position = (int)stream.Length;
- using (var requestDownLoad = MessagePool.GetMessage<BreakpointDownloadUpgradeFileDataRequest>())
- {
- requestDownLoad.Platform = upgradeInfo.UpgradePlatform;
- requestDownLoad.Type = upgradeInfo.UpgradeType;
- requestDownLoad.FilePosition = position;
- var token = downLoadLeaf.ApplyToken();
- var downLoadResult = downLoadLeaf.Send(token, requestDownLoad);
- var downLoadFileResult = BreakpointDownloadUpgradeFileDataResult.Convert(downLoadResult);
- if (downLoadResult != null)
- {
- downLoadFileResult.FileData.Save(stream);
- if (stream.Length >= downLoadFileResult.FileSize)
- {
- progress?.Invoke(1.0);
- finished = true;
- }
- else
- {
- progress?.Invoke((double)stream.Length / downLoadFileResult.FileSize);
- }
- }
- }
- }
- }
- File.Move(cacheFilePath, packagePath);
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"DownloadUFilePackage Error:{ex}");
- }
- }
- public void Dispose()
- {
- DoDispose();
- GC.SuppressFinalize(this);
- }
- }
- }
|