using AIK.Common.Enum; using AIK.Common.Event; using AIK.Common.Interface; using AIK.Common.Language; using AIK.Common.Models; using AIK.Common.SysCommon; using AIK.Common.WebHelper; using AIK.Models.ApiModels.Version; using AIK.Models.HidModels; using AIK.Models.SystemSettings; using AIK.Service.Extensions; using AIK.Service.IService; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; using System.Windows.Media.Animation; namespace AIK.ViewModels.UpdateCenter { public partial class UpdateCenterViewModel : ObservableObject, ILoadAware { private readonly ISettingsService _settingsService; private readonly IConfigurationService _configurationService; private readonly IDataCacheService _dataCacheService; private readonly IModalDialogService _modalDialogService; private readonly IAikDataService _aikDataService; private readonly IFileServiceFactory _fileServiceFactory; private readonly IFileCacheService _fileCacheService; private readonly IWindowService _windowService; private readonly IHidCommunicationService _hidCommunicationService; private readonly IUpdateService _updateService; [ObservableProperty] private bool _isK3ToolUpdateAvailable = false; [ObservableProperty] private AppVersionInfo _k3UpdateInfo; [ObservableProperty] private UpdateProgressDto _appUpdateInfo; [ObservableProperty] private string codeVersion = "----"; [ObservableProperty] private HidDeviceStatus _hidDeviceStatus = new HidDeviceStatus(); public Dictionary binKeyByteDic = new Dictionary(); public UpdateCenterViewModel(ISettingsService settingsService, IConfigurationService configurationService, IDataCacheService dataCacheService, IModalDialogService modalDialogService, IAikDataService aikDataService, IFileServiceFactory fileServiceFactory, IWindowService windowService, IHidCommunicationService hidCommunicationService, IUpdateService updateService) { _settingsService = settingsService; _configurationService = configurationService; _dataCacheService = dataCacheService; _modalDialogService = modalDialogService; _aikDataService = aikDataService; _fileServiceFactory = fileServiceFactory; _fileCacheService = _fileServiceFactory.GetService(FileServiceType.BIN); _windowService = windowService; _hidCommunicationService = hidCommunicationService; _hidCommunicationService.ConnectionStateChanged += OnConnectionStateChanged; _updateService = updateService; } public async Task OnLoadedAsync() { int codeNumber = 0; if (codeVersion.Contains('v')) { codeNumber = int.Parse(codeVersion.Replace("v", "").Replace(".", "")); } else { codeVersion = "----"; } AppUpdateInfo = await _updateService.CheckForUpdateAsync(); K3UpdateInfo = await _aikDataService.GetAppVersionInfo(WebApiAddress.K3ToolType); if (K3UpdateInfo != null) { await LoadAppVersionFile(); int newcodeNumber = int.Parse(K3UpdateInfo.VersionName.Replace("v", "").Replace(".", "")); if (codeNumber < newcodeNumber) { IsK3ToolUpdateAvailable = true; return; } } else { K3UpdateInfo = new AppVersionInfo(); K3UpdateInfo.VersionName = "----"; } IsK3ToolUpdateAvailable = false; } private async Task LoadAppVersionFile() { var filebin = await _fileCacheService.GetFileByteAsync(K3UpdateInfo.FileUrl); if (filebin != null && filebin.Length > 0) { if (K3UpdateInfo.FileUrl.Contains("http")) { var urifileName = UrlHelper.GetEncodedFileNameFromUrl(K3UpdateInfo.FileUrl); var realfileName = Uri.UnescapeDataString(urifileName); binKeyByteDic.Add(realfileName, filebin); } } } #region 页面方法 [RelayCommand] public void UpdateK3Tool() { //烧录工具更新 if (_hidDeviceStatus.DeviceStatus == false) { _modalDialogService.ShowDialogAsync(AiKLanguage.LanguageKey.GetValueOrDefault("DeviceDisconnected"), AiKLanguage.LanguageKey.GetValueOrDefault("K3ToolConnectTips"), MessageTypeEnum.Warning, true, false, 3000); return; } else { var keyname = binKeyByteDic.Keys.FirstOrDefault(); if (binKeyByteDic.TryGetValue(keyname, out byte[] filebyte)) { _modalDialogService.ShowProgressAsync( async (progress, ct) => { return await _hidCommunicationService.SendFileProcessAsync( keyname, filebyte, HidOperationTypeEnum.DeviceUpdate, progress, ct); }); } } } [RelayCommand] public void GetCurrentDeviceVersion() { _ = _hidCommunicationService.GetDeviceVersion(HidOperationTypeEnum.DeviceVersion); } [RelayCommand] public void UpdateApp() { if (AppUpdateInfo == null || !AppUpdateInfo.IsAvailable) { _modalDialogService.ShowTipsDialogAsync(AiKLanguage.LanguageKey.GetValueOrDefault("UpToDate")); return; } //_updateService.StartUpdateProcess();//旧版更新 _updateService.CheckForUpdate(); } #endregion #region 连接状态事件处理 private void OnConnectionStateChanged(object? sender, ConnectionStateChangedEventArgs e) { HidDeviceStatus.DeviceStatus = e.IsConnected; IsK3ToolUpdateAvailable = e.IsConnected; if (e.IsConnected) { HidDeviceStatus.DeviceStatusText = AiKLanguage.LanguageKey.GetValueOrDefault("Connected"); HidDeviceStatus.DeviceStatusColor = "#28a745"; HidDeviceStatus.DeviceStatusIcon = "\uE62a"; } else { HidDeviceStatus.DeviceStatusText = AiKLanguage.LanguageKey.GetValueOrDefault("Disconnected"); ; HidDeviceStatus.DeviceStatusColor = "#FFF44336"; HidDeviceStatus.DeviceStatusIcon = "\uE6f9"; } } #endregion #region 获取bin文件更新 private void LoadBinFileForUpdate() { //TODO 获取bin文件用于更新 } #endregion } }