DownloadImageModel.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.IO;
  3. using System.Text.Json;
  4. using System.Text.RegularExpressions;
  5. using System.Windows.Threading;
  6. using Vinno.IUS.Common.Log;
  7. using Vinno.IUS.Common.Network.Transfer;
  8. using Vinno.vCloud.Protocol.Initializers;
  9. namespace DownloadImagesTool
  10. {
  11. public class DownloadImageModel : ViewModel
  12. {
  13. private DownloadImageWorker downloadImageWorker = new DownloadImageWorker();
  14. private string _queueCount;
  15. public string QueueCount
  16. {
  17. get { return _queueCount; }
  18. set
  19. {
  20. if (_queueCount != value)
  21. {
  22. _queueCount = value;
  23. OnPropertyChanged(() => QueueCount);
  24. }
  25. }
  26. }
  27. private string _logs;
  28. public string Logs
  29. {
  30. get { return _logs; }
  31. set
  32. {
  33. if (_logs != value)
  34. {
  35. _logs = value;
  36. OnPropertyChanged(() => Logs);
  37. }
  38. }
  39. }
  40. private string _serverAddress;
  41. public string ServerAddress
  42. {
  43. get { return _serverAddress; }
  44. set
  45. {
  46. if (_serverAddress != value)
  47. {
  48. _serverAddress = value;
  49. OnPropertyChanged(() => ServerAddress);
  50. }
  51. }
  52. }
  53. private string _userNames;
  54. public string UserNames
  55. {
  56. get { return _userNames; }
  57. set
  58. {
  59. if (_userNames != value)
  60. {
  61. _userNames = value;
  62. OnPropertyChanged(() => UserNames);
  63. }
  64. }
  65. }
  66. public Command DownloadImageCommand { get; }
  67. public DownloadImageModel()
  68. {
  69. TerminalTagsInitializer.Initialize();
  70. ClientTagsInitializer.Initialize();
  71. SystemInitializer.Initialize();
  72. var configStr = Regex.Replace(File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "setting.conf")), "[^\\S]*", "");
  73. dynamic config = JsonHelper.DeserializerJson<dynamic>(configStr, new JsonSerializerOptions { Converters = { new DynamicJsonConverter() } });
  74. UserNames = config.userNames;
  75. ServerAddress = config.serverHost;
  76. var logEngine = new LogEngineImplement((s) =>
  77. {
  78. Console.WriteLine(s.Content);
  79. });
  80. Logger.RegisterEngine(logEngine);
  81. DownloadImageCommand = new ButtonCommand(OnDownloadImageCommand);
  82. }
  83. private async void OnDownloadImageCommand(object obj)
  84. {
  85. Logs += "开始下载请稍等...\r\n";
  86. var userNames = UserNames.Split(',', StringSplitOptions.RemoveEmptyEntries);
  87. if (userNames != null && userNames.Length > 0)
  88. {
  89. await downloadImageWorker.StartDownload(ServerAddress, log => {
  90. Dispatcher.CurrentDispatcher.Invoke(()=>Logs += $"{log}\r\n");
  91. }, setQueueCount => {
  92. Dispatcher.CurrentDispatcher.Invoke(() => QueueCount = $"{setQueueCount}");
  93. }, _userNames);
  94. }
  95. }
  96. }
  97. }