using System; using System.IO; using System.Text.Json; using System.Text.RegularExpressions; using System.Windows.Threading; using Vinno.IUS.Common.Log; using Vinno.IUS.Common.Network.Transfer; using Vinno.vCloud.Protocol.Initializers; namespace DownloadImagesTool { public class DownloadImageModel : ViewModel { private DownloadImageWorker downloadImageWorker = new DownloadImageWorker(); private string _queueCount; public string QueueCount { get { return _queueCount; } set { if (_queueCount != value) { _queueCount = value; OnPropertyChanged(() => QueueCount); } } } private string _logs; public string Logs { get { return _logs; } set { if (_logs != value) { _logs = value; OnPropertyChanged(() => Logs); } } } private string _serverAddress; public string ServerAddress { get { return _serverAddress; } set { if (_serverAddress != value) { _serverAddress = value; OnPropertyChanged(() => ServerAddress); } } } private string _userNames; public string UserNames { get { return _userNames; } set { if (_userNames != value) { _userNames = value; OnPropertyChanged(() => UserNames); } } } public Command DownloadImageCommand { get; } public DownloadImageModel() { TerminalTagsInitializer.Initialize(); ClientTagsInitializer.Initialize(); SystemInitializer.Initialize(); var configStr = Regex.Replace(File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "setting.conf")), "[^\\S]*", ""); dynamic config = JsonHelper.DeserializerJson(configStr, new JsonSerializerOptions { Converters = { new DynamicJsonConverter() } }); UserNames = config.userNames; ServerAddress = config.serverHost; var logEngine = new LogEngineImplement((s) => { Console.WriteLine(s.Content); }); Logger.RegisterEngine(logEngine); DownloadImageCommand = new ButtonCommand(OnDownloadImageCommand); } private async void OnDownloadImageCommand(object obj) { Logs += "开始下载请稍等...\r\n"; var userNames = UserNames.Split(',', StringSplitOptions.RemoveEmptyEntries); if (userNames != null && userNames.Length > 0) { await downloadImageWorker.StartDownload(ServerAddress, log => { Dispatcher.CurrentDispatcher.Invoke(()=>Logs += $"{log}\r\n"); }, setQueueCount => { Dispatcher.CurrentDispatcher.Invoke(() => QueueCount = $"{setQueueCount}"); }, _userNames); } } } }