Upgraders.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Vinno.IUS.Common.Log;
  6. using Vinno.IUS.Common.Network.Leaf;
  7. using Vinno.IUS.Common.Network.Tcp;
  8. using Vinno.IUS.Common.Network.Transfer;
  9. using Vinno.IUS.Common.Utilities;
  10. using Vinno.vCloud.Common.Storage.Download;
  11. using Vinno.vCloud.FIS.CrossPlatform.Common;
  12. using Vinno.vCloud.Protocol.Infrastructures;
  13. using Vinno.vCloud.Protocol.Messages.Terminal.Remedical.TerminialLog;
  14. using Vinno.vCloud.Protocol.Messages.Upgrade;
  15. namespace Vinno.vCloud.Common.FIS.Upgraders
  16. {
  17. internal class Upgraders : IUpgraders
  18. {
  19. private const string _tokenSplitor = "1!U$";
  20. private const string _tokenSplitor2 = "0!U$";
  21. private readonly ClientLeaf _leaf;
  22. private bool _disposed;
  23. /// <summary>
  24. /// Raise the event when force upgrade.
  25. /// </summary>
  26. public event EventHandler<UpgradePatchInfo> ForceUpgraded;
  27. /// <summary>
  28. /// The constructor of the AfterSales
  29. /// </summary>
  30. /// <param name="leaf">The <see cref="ClientLeaf"/></param>
  31. public Upgraders(ClientLeaf leaf)
  32. {
  33. _leaf = leaf;
  34. _leaf.MessageArrived += OnMessageArrived;
  35. }
  36. ~Upgraders()
  37. {
  38. Dispose();
  39. }
  40. /// <summary>
  41. /// Get upgrade version.
  42. /// </summary>
  43. /// <returns>Package info</returns>
  44. public PackageInfo GetUpgradeVersion(UpgradePlatform platform, UpgradeType upgradeType)
  45. {
  46. using (var request = MessagePool.GetMessage<GetUpgradeVersionRequest>())
  47. {
  48. request.Platform = platform;
  49. request.Type = upgradeType;
  50. var result = _leaf.Send(request);
  51. var versionResult = GetUpgradeVersionResult.Convert(result);
  52. if (versionResult != null)
  53. {
  54. var upgradeServerUrl = versionResult.UpgradeUrl;
  55. var version = versionResult.Version;
  56. var fileMd5 = versionResult.FileMD5.GetBytes();
  57. return new PackageInfo(version, upgradeServerUrl, fileMd5);
  58. }
  59. }
  60. return null;
  61. }
  62. /// <summary>
  63. /// Download client package.
  64. /// </summary>
  65. public void DownloadPackage(string url, string packagePath, UpgradePlatform platform, UpgradeType upgradeType)
  66. {
  67. var tcpCreator = new TcpCreator(url);
  68. using (var request = MessagePool.GetMessage<GetUpgradeFileDataRequest>())
  69. {
  70. request.Platform = platform;
  71. request.Type = upgradeType;
  72. var tempPath = packagePath + ".tmp";
  73. using (var stream = File.Create(tempPath))
  74. {
  75. var downloadLeaf = new ClientLeaf(new LeafIdContext(), LeafMode.None, tcpCreator);
  76. try
  77. {
  78. downloadLeaf.Send(request, sendResult =>
  79. {
  80. var dataResult = GetUpgradeFileDataResult.Convert(sendResult.Result);
  81. if (dataResult != null)
  82. {
  83. var data = dataResult.FileData.GetBytes();
  84. stream.Write(data, 0, data.Length);
  85. }
  86. });
  87. }
  88. finally
  89. {
  90. downloadLeaf.Close();
  91. }
  92. }
  93. File.Move(tempPath, packagePath);
  94. }
  95. }
  96. /// <summary>
  97. /// Download client package support the Resume breakpoint.
  98. /// </summary>
  99. public void ResumeDownloadPackage(string url, string packagePath, UpgradePlatform platform, UpgradeType upgradeType)
  100. {
  101. }
  102. /// <summary>
  103. /// Get Ultrasound Machine Latest Package Info
  104. /// </summary>
  105. /// <param name="uniqueCode"></param>
  106. /// <returns>Ultrasound Machine Package Info</returns>
  107. public UpgradePackageInfo GetUltrasoundMachineLatestPackageInfo(string uniqueCode)
  108. {
  109. using (var queryLatestPackageRequest = MessagePool.GetMessage<QueryLatestPackageRequest>())
  110. {
  111. queryLatestPackageRequest.UniquedCode = uniqueCode;
  112. var message = _leaf.Send(queryLatestPackageRequest);
  113. if (message == null)
  114. {
  115. return null;
  116. }
  117. var result = GetPackageResult.Convert(message);
  118. if (result != null)
  119. {
  120. var createUserName = result.CreateUserName;
  121. var description = result.Description;
  122. var fileName = result.FileName;
  123. var fileSize = result.FileSize;
  124. var fileToken = result.FileToken;
  125. var updateTime = result.UpdateTime;
  126. var version = result.Version;
  127. return new UpgradePackageInfo
  128. {
  129. PackageName = fileName,
  130. Version = version,
  131. UniqueCode = uniqueCode,
  132. Description = description,
  133. PackageToken = fileToken,
  134. PackageSize = fileSize,
  135. UploadUserName = createUserName,
  136. UpdateTime = updateTime,
  137. };
  138. }
  139. }
  140. return null;
  141. }
  142. /// <summary>
  143. /// Download the Latest Package of Ultrasound Machine from Storage Server Async.
  144. /// </summary>
  145. /// <param name="fileToken">File Token</param>
  146. /// <param name="packagePath">The Path to place the package</param>
  147. /// <param name="progress">Download Progress</param>
  148. /// <param name="cancelTokenSource">Cancel Token Source</param>
  149. public void DownloadPackage(string url, string packagePath, Action<double> progress = null, CancellationTokenSource cancelTokenSource = null)
  150. {
  151. var fileToken = url;
  152. if (!fileToken.StartsWith(_tokenSplitor) && !fileToken.StartsWith(_tokenSplitor2))
  153. {
  154. fileToken = _tokenSplitor + fileToken;
  155. }
  156. DownloadHelper.GetFile(fileToken, packagePath, progress, cancelTokenSource, true);
  157. }
  158. /// <summary>
  159. /// Download the Latest Package of Ultrasound Machine from Storage Server Async.
  160. /// </summary>
  161. /// <param name="fileToken">File Token</param>
  162. /// <param name="packagePath">The Path to place the package</param>
  163. /// <param name="progress">Download Progress</param>
  164. /// <param name="cancelTokenSource">Cancel Token Source</param>
  165. /// <returns></returns>
  166. public async Task DownloadPackageAsync(string url, string packagePath, Action<double> progress = null, CancellationTokenSource cancelTokenSource = null)
  167. {
  168. var fileToken = url;
  169. if (!fileToken.StartsWith(_tokenSplitor) && !fileToken.StartsWith(_tokenSplitor2))
  170. {
  171. fileToken = _tokenSplitor + fileToken;
  172. }
  173. await DownloadHelper.GetFileAsync(fileToken, packagePath, progress, cancelTokenSource);
  174. }
  175. /// <summary>
  176. /// Get Latest FIS Patch
  177. /// </summary>
  178. /// <param name="patchType">Device Patch Type</param>
  179. /// <param name="osType">Client OS Type</param>
  180. /// <returns>Latest FIS Patch Info</returns>
  181. public UpgradePatchInfo GetLatestFISPatch(DevicePatchType patchType, ClientOSType osType)
  182. {
  183. using (var request = MessagePool.GetMessage<GetPatchFISRequest>())
  184. {
  185. request.PatchType = patchType;
  186. request.FitPlatform = osType;
  187. request.IsSonopost = CommonParameter.Instance.IsSonopost;
  188. var message = _leaf.Send(request);
  189. var result = GetPatchFISResult.Convert(message);
  190. if (result != null)
  191. {
  192. var returnPatchType = result.PatchType;
  193. var version = result.Version;
  194. var url = result.FileUrl;
  195. var description = result.Description;
  196. return new UpgradePatchInfo(returnPatchType, version, url, description);
  197. }
  198. }
  199. return null;
  200. }
  201. private void DoDispose()
  202. {
  203. if (!_disposed)
  204. {
  205. _leaf.MessageArrived -= OnMessageArrived;
  206. _disposed = true;
  207. }
  208. }
  209. private void OnMessageArrived(object sender, Message e)
  210. {
  211. var modifyFISNotification = ModifyFISNotification.Convert(e);
  212. if (modifyFISNotification != null)
  213. {
  214. HandleModifyFISNotification(modifyFISNotification);
  215. }
  216. }
  217. private void HandleModifyFISNotification(ModifyFISNotification modifyFISNotification)
  218. {
  219. ForceUpgraded?.Invoke(null, new UpgradePatchInfo(modifyFISNotification.PatchType, modifyFISNotification.Version, modifyFISNotification.FileUrl, modifyFISNotification.Description));
  220. }
  221. /// <summary>
  222. /// Is Has New Package
  223. /// </summary>
  224. /// <param name="platform"></param>
  225. /// <param name="upgradeType"></param>
  226. /// <param name="version"></param>
  227. /// <returns></returns>
  228. public UpgradeInfo IsHasNewPackage(UpgradePlatform platform, UpgradeType upgradeType, string version)
  229. {
  230. try
  231. {
  232. using (var request = new GetUpgradeInfoRequest())
  233. {
  234. request.Platform = platform;
  235. request.Type = upgradeType;
  236. request.CurrentVersion = version;
  237. var currentVersion = new Version("1.0.0.0");
  238. Version.TryParse(version, out currentVersion);
  239. var result = _leaf.Send(request);
  240. var upgradeInfoResult = GetUpgradeInfoResult.Convert(result);
  241. if (upgradeInfoResult != null)
  242. {
  243. var latestVersion = new Version("1.0.0.0");
  244. Version.TryParse(upgradeInfoResult.NewVersion, out latestVersion);
  245. Logger.WriteLineInfo($"The latest version is {latestVersion} in server");
  246. if (upgradeInfoResult.UpgadeType != UpgradeTypeEnum.NoUpgrade && latestVersion > currentVersion)
  247. {
  248. return new UpgradeInfo
  249. {
  250. UpgradeUrl = upgradeInfoResult.UpgradeUrl,
  251. StorageType = upgradeInfoResult.StorageType,
  252. UpgradeType = upgradeType,
  253. UpgradePlatform = platform,
  254. };
  255. }
  256. }
  257. }
  258. }
  259. catch (Exception ex)
  260. {
  261. Logger.WriteLineError($"DownloadUFilePackage Error:{ex}");
  262. }
  263. return new UpgradeInfo
  264. {
  265. UpgradeUrl = "",
  266. UpgradeType = upgradeType,
  267. UpgradePlatform = platform,
  268. };
  269. }
  270. /// <summary>
  271. /// <summary>
  272. /// Download Package
  273. /// </summary>
  274. /// <param name="packagePath"></param>
  275. /// <param name="progress"></param>
  276. /// <param name="cancelTokenSource"></param>
  277. public void DownloadPackage(string packagePath, UpgradeInfo upgradeInfo, Action<double> progress = null, CancellationTokenSource cancelTokenSource = null)
  278. {
  279. try
  280. {
  281. if (upgradeInfo.StorageType == StorageType.ObjectStorage)
  282. {
  283. DownloadHelper.GetFile(upgradeInfo.UpgradeUrl, packagePath, progress, cancelTokenSource, true); //下载升级包
  284. }
  285. else
  286. {
  287. var tcpCreatorDownLoad = new TcpCreator(upgradeInfo.UpgradeUrl);
  288. var downLoadLeaf = new ClientLeaf(new LeafIdContext(), LeafMode.Single, tcpCreatorDownLoad);
  289. var packageFileExtension = Path.GetExtension(packagePath);
  290. var cacheFilePath = Path.ChangeExtension(packagePath, "tmp");
  291. FileHelper.DeleteFile(packagePath);
  292. FileHelper.DeleteFile(cacheFilePath);
  293. var finished = false;
  294. using (var stream = File.Open(cacheFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)) //打开文件
  295. {
  296. while (!finished)
  297. {
  298. if (cancelTokenSource != null && cancelTokenSource.IsCancellationRequested)
  299. {
  300. FileHelper.DeleteFile(cacheFilePath);
  301. return;
  302. }
  303. var position = (int)stream.Length;
  304. using (var requestDownLoad = MessagePool.GetMessage<BreakpointDownloadUpgradeFileDataRequest>())
  305. {
  306. requestDownLoad.Platform = upgradeInfo.UpgradePlatform;
  307. requestDownLoad.Type = upgradeInfo.UpgradeType;
  308. requestDownLoad.FilePosition = position;
  309. var token = downLoadLeaf.ApplyToken();
  310. var downLoadResult = downLoadLeaf.Send(token, requestDownLoad);
  311. var downLoadFileResult = BreakpointDownloadUpgradeFileDataResult.Convert(downLoadResult);
  312. if (downLoadResult != null)
  313. {
  314. downLoadFileResult.FileData.Save(stream);
  315. if (stream.Length >= downLoadFileResult.FileSize)
  316. {
  317. progress?.Invoke(1.0);
  318. finished = true;
  319. }
  320. else
  321. {
  322. progress?.Invoke((double)stream.Length / downLoadFileResult.FileSize);
  323. }
  324. }
  325. }
  326. }
  327. }
  328. File.Move(cacheFilePath, packagePath);
  329. }
  330. }
  331. catch (Exception ex)
  332. {
  333. Logger.WriteLineError($"DownloadUFilePackage Error:{ex}");
  334. }
  335. }
  336. public void Dispose()
  337. {
  338. DoDispose();
  339. GC.SuppressFinalize(this);
  340. }
  341. }
  342. }