MainWindow.xaml.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using UpgradePackageUploadTool.Services;
  12. using UpgradePackageUploadTool.Settings;
  13. using Vinno.IUS.Common.Network.Leaf;
  14. using Vinno.IUS.Common.Network.Tcp;
  15. using Vinno.IUS.Common.Network.Transfer;
  16. using Vinno.vCloud.Common.Storage.Download;
  17. using Vinno.vCloud.Protocol.Infrastructures;
  18. using Vinno.vCloud.Protocol.Initializers;
  19. using Vinno.vCloud.Protocol.Messages;
  20. using Vinno.vCloud.Protocol.Messages.Upgrade;
  21. namespace UpgradePackageUploadTool
  22. {
  23. public class UltrasoundMachineUpgrader
  24. {
  25. private ClientLeaf _leaf;
  26. private readonly string _url;
  27. /// <summary>
  28. /// The constructor of the UltrasoundMachineUpgrader.
  29. /// </summary>
  30. /// <param name="url">The server url.</param>
  31. public UltrasoundMachineUpgrader(string url)
  32. {
  33. _url = url;
  34. TerminalTagsInitializer.Initialize();
  35. SystemInitializer.Initialize();
  36. }
  37. /// <summary>
  38. /// Get Ultrasound Machine Latest Package Info
  39. /// </summary>
  40. /// <param name="packageType">Package Type</param>
  41. /// <returns>Ultrasound Machine Package Info</returns>
  42. public GeneralPackInfo GetUltrasoundMachineLatestPackageInfo(string uniquedCode)
  43. {
  44. try
  45. {
  46. CreateLeaf();
  47. using (var queryLatestPackageRequest = MessagePool.GetMessage<QueryLatestPackageRequest>())
  48. {
  49. queryLatestPackageRequest.UniquedCode = uniquedCode;
  50. var message = _leaf.Send(queryLatestPackageRequest);
  51. if (message == null)
  52. {
  53. return null;
  54. }
  55. var result = GetPackageResult.Convert(message);
  56. if (result != null)
  57. {
  58. var createUserName = result.CreateUserName;
  59. var description = result.Description;
  60. var fileName = result.FileName;
  61. var fileSize = result.FileSize;
  62. var fileToken = result.FileToken;
  63. var updateTime = result.UpdateTime;
  64. var version = result.Version;
  65. return new GeneralPackInfo(createUserName, description, fileName, fileToken, fileSize, result.PackageType, updateTime, version, result.UniquedCode);
  66. }
  67. }
  68. return null;
  69. }
  70. finally
  71. {
  72. CloseLeaf();
  73. }
  74. }
  75. /// <summary>
  76. /// Download the Latest Package of Ultrasound Machine from Storage Server Async.
  77. /// </summary>
  78. /// <param name="fileToken">File Token</param>
  79. /// <param name="packagePath">The Path to place the package</param>
  80. /// <param name="progress">Download Progress</param>
  81. /// <param name="cancelTokenSource">Cancel Token Source</param>
  82. public void DownloadUltrasoundMachineLatestPackage(string fileToken, string packagePath, Action<double> progress = null, CancellationTokenSource cancelTokenSource = null)
  83. {
  84. DownloadHelper.GetFile(fileToken, packagePath, progress, cancelTokenSource, true);
  85. }
  86. /// <summary>
  87. /// Create a leaf to connect to the server.
  88. /// </summary>
  89. private void CreateLeaf()
  90. {
  91. if (_leaf != null)
  92. {
  93. _leaf.Close();
  94. }
  95. _leaf = new ClientLeaf(new LeafIdContext(), LeafMode.Dual, new TcpCreator(_url), "Terminal upgrade:");
  96. if (!_leaf.Online)
  97. {
  98. _leaf.Close();
  99. }
  100. else
  101. {
  102. _leaf.RegisterSetAccountDataMessageFunc(SetAccountDataToMessage);
  103. }
  104. }
  105. private void CloseLeaf()
  106. {
  107. _leaf?.Close();
  108. }
  109. private Message SetAccountDataToMessage(Message message)
  110. {
  111. if (message is ClientRequestMessage clientRequestMessage)
  112. {
  113. clientRequestMessage.AccountData = GetAccountDataMessage() as ClientAccountMessage;
  114. return clientRequestMessage;
  115. }
  116. return message;
  117. }
  118. private Message GetAccountDataMessage()
  119. {
  120. var accountData = MessagePool.GetMessage<ClientAccountMessage>();
  121. accountData.AccountId = "TerminalUpgradeId";
  122. accountData.AccountName = "TerminalUpgrade";
  123. accountData.Source = LoginSource.UltrasoundMachine;
  124. return accountData;
  125. }
  126. }
  127. /// <summary>
  128. /// MainWindow.xaml 的交互逻辑
  129. /// </summary>
  130. public partial class MainWindow : Window
  131. {
  132. private vCloudService _vCloudService;
  133. private bool _isConnected;
  134. private int _startTickCount;
  135. private UltrasoundMachineUpgrader _ultrasoundMachineUpgrader;
  136. private PackageType _packageType;
  137. private Version _newVersion;
  138. private Version _oldVersion;
  139. private WingServerService _wingServerService;
  140. private void ConnectStatusChanged()
  141. {
  142. if (_isConnected)
  143. {
  144. ConnectOrDisconnectServer_Button.Content = "断开连接";
  145. ServerIP_Text.IsEnabled = false;
  146. ServerPort_Text.IsEnabled = false;
  147. Account_Text.IsEnabled = false;
  148. Password_PasswordBox.IsEnabled = false;
  149. WingServer_Text.IsEnabled = false;
  150. _ultrasoundMachineUpgrader = new UltrasoundMachineUpgrader($"{ServerIP_Text.Text}:{ServerPort_Text.Text}");
  151. }
  152. else
  153. {
  154. ConnectOrDisconnectServer_Button.Content = "连接";
  155. ServerIP_Text.IsEnabled = true;
  156. ServerPort_Text.IsEnabled = true;
  157. Account_Text.IsEnabled = true;
  158. Password_PasswordBox.IsEnabled = true;
  159. WingServer_Text.IsEnabled = true;
  160. }
  161. }
  162. public MainWindow()
  163. {
  164. InitializeComponent();
  165. LoadConfig();
  166. _isConnected = false;
  167. _vCloudService = new vCloudService();
  168. _vCloudService.UploadProgressChanged += OnUploadProgressChanged;
  169. }
  170. protected override void OnClosing(CancelEventArgs e)
  171. {
  172. _vCloudService.UploadProgressChanged -= OnUploadProgressChanged;
  173. base.OnClosing(e);
  174. }
  175. private void OnUploadProgressChanged(object sender, int e)
  176. {
  177. Dispatcher.Invoke(() =>
  178. {
  179. Waiting.ProgressTextBlock.Text = $"{e}%";
  180. });
  181. }
  182. private void LoadConfig()
  183. {
  184. ServerIP_Text.Text = SettingConfig.Instance.ServerIP;
  185. ServerPort_Text.Text = SettingConfig.Instance.ServerPort.ToString();
  186. Account_Text.Text = SettingConfig.Instance.Account;
  187. Password_PasswordBox.Password = SettingConfig.Instance.Password;
  188. WingServer_Text.Text = SettingConfig.Instance.WingServer;
  189. }
  190. private void ServerIPText_TextChanged(object sender, TextChangedEventArgs e)
  191. {
  192. SettingConfig.Instance.ServerIP = ServerIP_Text.Text;
  193. SettingConfig.Instance.Save();
  194. }
  195. private void ServerPortText_TextChanged(object sender, TextChangedEventArgs e)
  196. {
  197. if (int.TryParse(ServerPort_Text.Text, out int port))
  198. {
  199. SettingConfig.Instance.ServerPort = port;
  200. SettingConfig.Instance.Save();
  201. }
  202. }
  203. private void Account_Text_TextChanged(object sender, TextChangedEventArgs e)
  204. {
  205. SettingConfig.Instance.Account = Account_Text.Text;
  206. SettingConfig.Instance.Save();
  207. }
  208. private void Password_PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
  209. {
  210. SettingConfig.Instance.Password = Password_PasswordBox.Password;
  211. SettingConfig.Instance.Save();
  212. }
  213. private void OnOpenFile(object sender, RoutedEventArgs e)
  214. {
  215. OpenFileDialog openFileDialog = new OpenFileDialog
  216. {
  217. Multiselect = false,
  218. Title = "请选择上传的安装包",
  219. Filter = "安卓安装包|*.apk"
  220. };
  221. if (openFileDialog.ShowDialog() ?? false)
  222. {
  223. var filepath = openFileDialog.FileName;
  224. FilePath_Text.Text = filepath;
  225. }
  226. }
  227. private async void OnUploadPackage(object sender, RoutedEventArgs e)
  228. {
  229. ButtonIsEnabledChange(false);
  230. try
  231. {
  232. if (_isConnected)
  233. {
  234. if (!UploadInfoCheck())
  235. {
  236. ButtonIsEnabledChange(true);
  237. return;
  238. }
  239. _startTickCount = Environment.TickCount;
  240. Waiting.StartWaiting();
  241. try
  242. {
  243. var fileName = $"{FileName_Text.Text}_{Version_Text.Text}.apk";
  244. var packInfo = await _wingServerService.UploadPackageAsync(FilePath_Text.Text, Version_Text.Text, fileName, Description_Text.Text, FileName_Text.Text);
  245. if (packInfo != null)
  246. {
  247. ShowPackInfo(packInfo);
  248. var endTickCount = Environment.TickCount;
  249. var elapsedTime = Math.Round((endTickCount - _startTickCount) / 1000d, 2);
  250. MessageBox.Show($"Upload Package Successful! Elapsed Time is {elapsedTime} s.");
  251. }
  252. else
  253. {
  254. ShowInfo_DataGrid.Visibility = Visibility.Collapsed;
  255. MessageBox.Show("Upload Package Failed!");
  256. }
  257. }
  258. catch (Exception ex)
  259. {
  260. MessageBox.Show(ex.Message);
  261. }
  262. }
  263. else
  264. {
  265. MessageBox.Show("Please Login First!");
  266. }
  267. }
  268. catch (Exception ex)
  269. {
  270. MessageBox.Show($"Upload Package Failed! Fail Message: {ex.Message}");
  271. }
  272. Waiting.StopWaiting();
  273. ButtonIsEnabledChange(true);
  274. }
  275. private void ShowPackInfo(GeneralPackInfo packInfo)
  276. {
  277. List<GeneralPackInfo> packInfos = new List<GeneralPackInfo> { packInfo };
  278. ShowInfo_DataGrid.ItemsSource = packInfos;
  279. ShowInfo_DataGrid.Visibility = Visibility.Visible;
  280. }
  281. private void ButtonIsEnabledChange(bool isEnabled)
  282. {
  283. UploadPackage_Button.IsEnabled = isEnabled;
  284. ConnectOrDisconnectServer_Button.IsEnabled = isEnabled;
  285. OpenFile_Button.IsEnabled = isEnabled;
  286. Download_Button.IsEnabled = isEnabled;
  287. }
  288. private bool UploadInfoCheck()
  289. {
  290. if (string.IsNullOrEmpty(FilePath_Text.Text))
  291. {
  292. MessageBox.Show($"The package path is empty,please choose the package.");
  293. return false;
  294. }
  295. if (string.IsNullOrEmpty(Version_Text.Text))
  296. {
  297. MessageBox.Show($"The version of package is empty, please input the version of package!");
  298. return false;
  299. }
  300. if (!Version.TryParse(Version_Text.Text, out _newVersion))
  301. {
  302. MessageBox.Show("Invalid version format!");
  303. return false;
  304. }
  305. if (_oldVersion != null && _newVersion < _oldVersion)
  306. {
  307. MessageBox.Show("Please make sure the new version is greater than the current one!");
  308. return false;
  309. }
  310. if (string.IsNullOrEmpty(FileName_Text.Text))
  311. {
  312. MessageBox.Show($"The package name is empty,please input the package name");
  313. return false;
  314. }
  315. return true;
  316. }
  317. private async void OnConnectOrDisconnectServer(object sender, RoutedEventArgs e)
  318. {
  319. Waiting.StartWaiting();
  320. _oldVersion = null;
  321. ConnectOrDisconnectServer_Button.IsEnabled = false;
  322. try
  323. {
  324. if (!_isConnected)
  325. {
  326. if (!ValidateInput())
  327. {
  328. return;
  329. }
  330. _wingServerService = new WingServerService(SettingConfig.Instance.WingServer);
  331. var result = await _wingServerService.LoginAsync(SettingConfig.Instance.Account, SettingConfig.Instance.Password);
  332. if (!result.Success)
  333. {
  334. _isConnected = false;
  335. ConnectStatusChanged();
  336. MessageBox.Show(result.ErrMessage);
  337. }
  338. else
  339. {
  340. _isConnected = true;
  341. ConnectStatusChanged();
  342. }
  343. }
  344. else
  345. {
  346. var result = await _wingServerService.LoginOutAsync();
  347. if (result.Success)
  348. {
  349. _isConnected = false;
  350. ConnectStatusChanged();
  351. ShowInfo_DataGrid.Visibility = Visibility.Collapsed;
  352. }
  353. else
  354. {
  355. _isConnected = true;
  356. ConnectStatusChanged();
  357. MessageBox.Show("Loginout Failed.");
  358. }
  359. }
  360. }
  361. catch (Exception ex)
  362. {
  363. MessageBox.Show($"Operation Fail:{ex.Message}");
  364. }
  365. finally
  366. {
  367. Waiting.StopWaiting();
  368. ConnectOrDisconnectServer_Button.IsEnabled = true;
  369. }
  370. }
  371. private bool ValidateInput()
  372. {
  373. //if (!ValidateServerAddress(ServerIP_Text.Text))
  374. //{
  375. // MessageBox.Show("Invalid server address");
  376. // return false;
  377. //}
  378. //if (!IsPort(ServerPort_Text.Text))
  379. //{
  380. // MessageBox.Show("Invalid port");
  381. // return false;
  382. //}
  383. if (string.IsNullOrEmpty(WingServer_Text.Text))
  384. {
  385. MessageBox.Show("Server Url is empty!");
  386. return false;
  387. }
  388. if (string.IsNullOrEmpty(Account_Text.Text))
  389. {
  390. MessageBox.Show("Account is empty!");
  391. return false;
  392. }
  393. if (string.IsNullOrEmpty(Password_PasswordBox.Password))
  394. {
  395. MessageBox.Show("Password is empty!");
  396. return false;
  397. }
  398. return true;
  399. }
  400. private bool IsPort(string port)
  401. {
  402. if (int.TryParse(port, out int portnumber))
  403. {
  404. if (portnumber > 0 && portnumber < 65535)
  405. {
  406. return true;
  407. }
  408. return false;
  409. }
  410. return false;
  411. }
  412. private bool ValidateServerAddress(string serverip)
  413. {
  414. if (string.IsNullOrEmpty(serverip))
  415. {
  416. return false;
  417. }
  418. if (IPAddress.TryParse(serverip, out IPAddress ip))
  419. {
  420. return true;
  421. }
  422. else
  423. {
  424. try
  425. {
  426. ip = Dns.GetHostAddresses(serverip).Where(x => x.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).First();
  427. if (ip != null)
  428. {
  429. return true;
  430. }
  431. else
  432. {
  433. return false;
  434. }
  435. }
  436. catch
  437. {
  438. return false;
  439. }
  440. }
  441. }
  442. private async void OnDownloadPackage(object sender, RoutedEventArgs e)
  443. {
  444. if (string.IsNullOrEmpty(FileName_Text.Text))
  445. {
  446. return;
  447. }
  448. if (!_isConnected)
  449. {
  450. MessageBox.Show("Please Login First!");
  451. return;
  452. }
  453. ButtonIsEnabledChange(false);
  454. try
  455. {
  456. var package = await _wingServerService.GetPackageInfoAsync(FileName_Text.Text);
  457. if (package != null)
  458. {
  459. var localPatch = $"D://{package.FileName}";
  460. _ultrasoundMachineUpgrader.DownloadUltrasoundMachineLatestPackage($"1!U${package.FileToken}", localPatch);
  461. MessageBox.Show($"Download success, path:{localPatch}");
  462. }
  463. else
  464. {
  465. MessageBox.Show($"Package does not exist!");
  466. }
  467. }
  468. catch (Exception ex)
  469. {
  470. MessageBox.Show($"Download Fail:{ex.Message}");
  471. }
  472. finally
  473. {
  474. ButtonIsEnabledChange(true);
  475. }
  476. }
  477. private async void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  478. {
  479. _oldVersion = null;
  480. SettingConfig.Instance.PackageType = _packageType;
  481. SettingConfig.Instance.Save();
  482. ShowInfo_DataGrid.ItemsSource = null;
  483. if (_vCloudService != null && _isConnected)
  484. {
  485. try
  486. {
  487. Waiting.StartWaiting();
  488. var packInfo = await _vCloudService.GetLatestPackageAsync(FileName_Text.Text);
  489. if (packInfo != null)
  490. {
  491. _oldVersion = new Version(packInfo.Version);
  492. ShowPackInfo(packInfo);
  493. }
  494. }
  495. finally
  496. {
  497. Waiting.StopWaiting();
  498. }
  499. }
  500. }
  501. private async void OnGetLatestPackage(object sender, RoutedEventArgs e)
  502. {
  503. try
  504. {
  505. if (string.IsNullOrEmpty(FileName_Text.Text))
  506. {
  507. return;
  508. }
  509. if (_isConnected)
  510. {
  511. var package = await _wingServerService.GetPackageInfoAsync(FileName_Text.Text);
  512. if (package != null)
  513. {
  514. _oldVersion = new Version(package.Version);
  515. ShowPackInfo(package);
  516. }
  517. else
  518. {
  519. MessageBox.Show($"Package does not exist!");
  520. }
  521. }
  522. else
  523. {
  524. MessageBox.Show("Please Login First!");
  525. }
  526. }
  527. catch (Exception ex)
  528. {
  529. MessageBox.Show(ex.Message);
  530. }
  531. }
  532. private void WingServer_Text_TextChanged(object sender, TextChangedEventArgs e)
  533. {
  534. SettingConfig.Instance.WingServer = WingServer_Text.Text;
  535. SettingConfig.Instance.Save();
  536. }
  537. }
  538. }