123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- using JsonRpcLite.Rpc;
- using System;
- using System.Threading;
- using Vinno.IUS.Common.Log;
- using Vinno.IUS.Common.Network.Leaf;
- using Vinno.vCloud.Common.FIS.Helper;
- using Vinno.vCloud.Common.Storage.Download;
- using Vinno.vCloud.Protocol.Messages.Upgrade;
- using WingInterfaceLibrary.Enum;
- using WingInterfaceLibrary.Interface;
- using GetUpgradeInfoRequest = WingInterfaceLibrary.Request.Upgrade.GetUpgradeInfoRequest;
- using UpgradeTypeEnum = WingInterfaceLibrary.Enum.UpgradeTypeEnum;
- namespace Vinno.vCloud.Common.FIS.Upgraders
- {
- internal class UpgradersV2 : IUpgradersV2
- {
- private bool _disposed;
- private readonly JsonRpcClient _client;
- private readonly IUpgradeService _upgradeService;
- /// <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 UpgradersV2(JsonRpcClient client)
- {
- _client = client;
- _upgradeService = _client.CreateProxy<IUpgradeService>();
- }
- ~UpgradersV2()
- {
- Dispose();
- }
- private void DoDispose()
- {
- if (!_disposed)
- {
- _disposed = true;
- }
- }
- /// <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
- {
- UpgradePlatformEnum upgradePlatformEnum;
- UpgradeSourceTypeEnum upgradeSourceTypeEnum;
- if (platform == UpgradePlatform.PC)
- {
- upgradePlatformEnum = UpgradePlatformEnum.PC;
- }
- else if (platform == UpgradePlatform.Android)
- {
- upgradePlatformEnum = UpgradePlatformEnum.Android;
- }
- else if (platform == UpgradePlatform.Mac)
- {
- upgradePlatformEnum = UpgradePlatformEnum.Mac;
- }
- else if (platform == UpgradePlatform.IOS)
- {
- upgradePlatformEnum = UpgradePlatformEnum.IOS;
- }
- else
- {
- Logger.WriteLineError($"IsHasNewPackage Error:Unsupport Platform:{platform}");
- return new UpgradeInfo
- {
- UpgradeUrl = "",
- UpgradeType = upgradeType,
- UpgradePlatform = platform,
- };
- }
- if (upgradeType == UpgradeType.Client)
- {
- upgradeSourceTypeEnum = UpgradeSourceTypeEnum.Client;
- }
- else if (upgradeType == UpgradeType.Agent || upgradeType == UpgradeType.SONOPOST)
- {
- upgradeSourceTypeEnum = UpgradeSourceTypeEnum.SONOPOST;
- }
- else
- {
- Logger.WriteLineError($"IsHasNewPackage Error:Unsupport UpgradeType:{upgradeType}");
- return new UpgradeInfo
- {
- UpgradeUrl = "",
- UpgradeType = upgradeType,
- UpgradePlatform = platform,
- };
- }
- GetUpgradeInfoRequest getUpgradeInfoRequest = new GetUpgradeInfoRequest
- {
- CurrentVersion = version,
- Platform = upgradePlatformEnum,
- SourceType = upgradeSourceTypeEnum,
- Language = "zh-CN",
- };
- var upgradeInfoResult = JsonRpcHelper.GetUpgradeInfo(_upgradeService, getUpgradeInfoRequest);
- if (upgradeInfoResult == null)
- {
- Logger.WriteLineError($"IsHasNewPackage GetUpgradeInfo Error:result is null");
- return new UpgradeInfo
- {
- UpgradeUrl = "",
- UpgradeType = upgradeType,
- UpgradePlatform = platform,
- };
- }
- var currentVersion = new Version("1.0.0.0");
- Version.TryParse(version, out currentVersion);
- var latestVersion = new Version("1.0.0.0");
- Version.TryParse(upgradeInfoResult.NewVersion, out latestVersion);
- Logger.WriteLineWarn($"The Latest Version is {latestVersion} in server");
- if (upgradeInfoResult.UpgradeType != UpgradeTypeEnum.NoUpgrade && latestVersion > currentVersion)
- {
- return new UpgradeInfo
- {
- UpgradeUrl = upgradeInfoResult.UpgradeCDNUrl ?? upgradeInfoResult.UpgradeSourceUrl,
- UpgradeType = upgradeType,
- UpgradePlatform = platform,
- };
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"IsHasNewPackage 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)
- {
- var fileToken = "1!U$" + upgradeInfo.UpgradeUrl;
- DownloadHelper.GetFile(fileToken, packagePath, progress, cancelTokenSource, true); //下载升级包
- }
- public void Dispose()
- {
- DoDispose();
- GC.SuppressFinalize(this);
- }
- }
- }
|