UpgradersV2.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using JsonRpcLite.Rpc;
  2. using System;
  3. using System.Threading;
  4. using Vinno.IUS.Common.Log;
  5. using Vinno.IUS.Common.Network.Leaf;
  6. using Vinno.vCloud.Common.FIS.Helper;
  7. using Vinno.vCloud.Common.Storage.Download;
  8. using Vinno.vCloud.Protocol.Messages.Upgrade;
  9. using WingInterfaceLibrary.Enum;
  10. using WingInterfaceLibrary.Interface;
  11. using GetUpgradeInfoRequest = WingInterfaceLibrary.Request.Upgrade.GetUpgradeInfoRequest;
  12. using UpgradeTypeEnum = WingInterfaceLibrary.Enum.UpgradeTypeEnum;
  13. namespace Vinno.vCloud.Common.FIS.Upgraders
  14. {
  15. internal class UpgradersV2 : IUpgradersV2
  16. {
  17. private bool _disposed;
  18. private readonly JsonRpcClient _client;
  19. private readonly IUpgradeService _upgradeService;
  20. /// <summary>
  21. /// Raise the event when force upgrade.
  22. /// </summary>
  23. public event EventHandler<UpgradePatchInfo> ForceUpgraded;
  24. /// <summary>
  25. /// The constructor of the AfterSales
  26. /// </summary>
  27. /// <param name="leaf">The <see cref="ClientLeaf"/></param>
  28. public UpgradersV2(JsonRpcClient client)
  29. {
  30. _client = client;
  31. _upgradeService = _client.CreateProxy<IUpgradeService>();
  32. }
  33. ~UpgradersV2()
  34. {
  35. Dispose();
  36. }
  37. private void DoDispose()
  38. {
  39. if (!_disposed)
  40. {
  41. _disposed = true;
  42. }
  43. }
  44. /// <summary>
  45. /// Is Has New Package
  46. /// </summary>
  47. /// <param name="platform"></param>
  48. /// <param name="upgradeType"></param>
  49. /// <param name="version"></param>
  50. /// <returns></returns>
  51. public UpgradeInfo IsHasNewPackage(UpgradePlatform platform, UpgradeType upgradeType, string version)
  52. {
  53. try
  54. {
  55. UpgradePlatformEnum upgradePlatformEnum;
  56. UpgradeSourceTypeEnum upgradeSourceTypeEnum;
  57. if (platform == UpgradePlatform.PC)
  58. {
  59. upgradePlatformEnum = UpgradePlatformEnum.PC;
  60. }
  61. else if (platform == UpgradePlatform.Android)
  62. {
  63. upgradePlatformEnum = UpgradePlatformEnum.Android;
  64. }
  65. else if (platform == UpgradePlatform.Mac)
  66. {
  67. upgradePlatformEnum = UpgradePlatformEnum.Mac;
  68. }
  69. else if (platform == UpgradePlatform.IOS)
  70. {
  71. upgradePlatformEnum = UpgradePlatformEnum.IOS;
  72. }
  73. else
  74. {
  75. Logger.WriteLineError($"IsHasNewPackage Error:Unsupport Platform:{platform}");
  76. return new UpgradeInfo
  77. {
  78. UpgradeUrl = "",
  79. UpgradeType = upgradeType,
  80. UpgradePlatform = platform,
  81. };
  82. }
  83. if (upgradeType == UpgradeType.Client)
  84. {
  85. upgradeSourceTypeEnum = UpgradeSourceTypeEnum.Client;
  86. }
  87. else if (upgradeType == UpgradeType.Agent || upgradeType == UpgradeType.SONOPOST)
  88. {
  89. upgradeSourceTypeEnum = UpgradeSourceTypeEnum.SONOPOST;
  90. }
  91. else
  92. {
  93. Logger.WriteLineError($"IsHasNewPackage Error:Unsupport UpgradeType:{upgradeType}");
  94. return new UpgradeInfo
  95. {
  96. UpgradeUrl = "",
  97. UpgradeType = upgradeType,
  98. UpgradePlatform = platform,
  99. };
  100. }
  101. GetUpgradeInfoRequest getUpgradeInfoRequest = new GetUpgradeInfoRequest
  102. {
  103. CurrentVersion = version,
  104. Platform = upgradePlatformEnum,
  105. SourceType = upgradeSourceTypeEnum,
  106. Language = "zh-CN",
  107. };
  108. var upgradeInfoResult = JsonRpcHelper.GetUpgradeInfo(_upgradeService, getUpgradeInfoRequest);
  109. if (upgradeInfoResult == null)
  110. {
  111. Logger.WriteLineError($"IsHasNewPackage GetUpgradeInfo Error:result is null");
  112. return new UpgradeInfo
  113. {
  114. UpgradeUrl = "",
  115. UpgradeType = upgradeType,
  116. UpgradePlatform = platform,
  117. };
  118. }
  119. var currentVersion = new Version("1.0.0.0");
  120. Version.TryParse(version, out currentVersion);
  121. var latestVersion = new Version("1.0.0.0");
  122. Version.TryParse(upgradeInfoResult.NewVersion, out latestVersion);
  123. Logger.WriteLineWarn($"The Latest Version is {latestVersion} in server");
  124. if (upgradeInfoResult.UpgradeType != UpgradeTypeEnum.NoUpgrade && latestVersion > currentVersion)
  125. {
  126. return new UpgradeInfo
  127. {
  128. UpgradeUrl = upgradeInfoResult.UpgradeCDNUrl ?? upgradeInfoResult.UpgradeSourceUrl,
  129. UpgradeType = upgradeType,
  130. UpgradePlatform = platform,
  131. };
  132. }
  133. }
  134. catch (Exception ex)
  135. {
  136. Logger.WriteLineError($"IsHasNewPackage Error:{ex}");
  137. }
  138. return new UpgradeInfo
  139. {
  140. UpgradeUrl = "",
  141. UpgradeType = upgradeType,
  142. UpgradePlatform = platform,
  143. };
  144. }
  145. /// <summary>
  146. /// <summary>
  147. /// Download Package
  148. /// </summary>
  149. /// <param name="packagePath"></param>
  150. /// <param name="progress"></param>
  151. /// <param name="cancelTokenSource"></param>
  152. public void DownloadPackage(string packagePath, UpgradeInfo upgradeInfo, Action<double> progress = null, CancellationTokenSource cancelTokenSource = null)
  153. {
  154. var fileToken = "1!U$" + upgradeInfo.UpgradeUrl;
  155. DownloadHelper.GetFile(fileToken, packagePath, progress, cancelTokenSource, true); //下载升级包
  156. }
  157. public void Dispose()
  158. {
  159. DoDispose();
  160. GC.SuppressFinalize(this);
  161. }
  162. }
  163. }