diff --git a/Handlers/MPDHandler.cs b/Handlers/MPDHandler.cs index 29c051a..3287f1a 100644 --- a/Handlers/MPDHandler.cs +++ b/Handlers/MPDHandler.cs @@ -36,6 +36,9 @@ namespace unison private readonly System.Timers.Timer _elapsedTimer; private DispatcherTimer _retryTimer; + bool _isUpdatingStatus = false; + bool _isUpdatingSong = false; + private event EventHandler ConnectionChanged; private event EventHandler StatusChanged; private event EventHandler SongChanged; @@ -117,6 +120,44 @@ namespace unison }); } + public void SendCommand(IMpcCommand command) + { + Task.Run(async () => + { + await SafelySendCommandAsync(command); + }); + } + + public async Task SafelySendCommandAsync(IMpcCommand command) + { + if (_commandConnection == null) + { + Trace.WriteLine("[SafelySendCommandAsync] no command connection"); + return default(T); + } + + try + { + IMpdMessage response = await _commandConnection.SendAsync(command); + if (!response.IsResponseValid) + { + var mpdError = response.Response?.Result?.MpdError; + if (mpdError != null && mpdError != "") + throw new Exception(mpdError); + else + throw new Exception($"Invalid server response: {response}."); + } + + return response.Response.Content; + } + catch (Exception e) + { + Trace.WriteLine($"Sending {command.GetType().Name} failed: {e.Message}"); + } + + return default(T); + } + private void Initialize(object sender, EventArgs e) { if (!_connected) @@ -177,6 +218,16 @@ namespace unison return connection; } + private void Disconnected() + { + _connected = false; + + _connection = null; + _commandConnection = null; + + ConnectionChanged?.Invoke(this, EventArgs.Empty); + } + private void Loop(CancellationToken token) { Task.Run(async () => @@ -205,16 +256,6 @@ namespace unison }).ConfigureAwait(false); } - private void Disconnected() - { - _connected = false; - - _connection = null; - _commandConnection = null; - - ConnectionChanged?.Invoke(this, EventArgs.Empty); - } - private async Task HandleIdleResponseAsync(string subsystems) { try @@ -224,9 +265,7 @@ namespace unison await UpdateStatusAsync(); if (subsystems.Contains("player")) - { await UpdateSongAsync(); - } } } catch (Exception e) @@ -235,12 +274,11 @@ namespace unison } } - bool _isUpdatingStatus = false; private async Task UpdateStatusAsync() { - if (_connection == null) return; - - if (_isUpdatingStatus) return; + if (_connection == null || _isUpdatingStatus) + return; + _isUpdatingStatus = true; try @@ -258,15 +296,15 @@ namespace unison { Trace.WriteLine($"Error in Idle connection thread: {e.Message}"); } + _isUpdatingStatus = false; } - bool _isUpdatingSong = false; private async Task UpdateSongAsync() { - if (_connection == null) return; + if (_connection == null || _isUpdatingSong) + return; - if (_isUpdatingSong) return; _isUpdatingSong = true; try @@ -278,57 +316,17 @@ namespace unison UpdateSong(); } else - { throw new Exception(); - } } catch (Exception e) { Trace.WriteLine($"Error in Idle connection thread: {e.Message}"); } + _isUpdatingSong = false; } - public void SendCommand(IMpcCommand command) - { - Task.Run(async () => - { - await SafelySendCommandAsync(command); - }); - } - - public async Task SafelySendCommandAsync(IMpcCommand command) - { - if (_commandConnection == null) - { - Trace.WriteLine("[SafelySendCommandAsync] no command connection"); - return default(T); - } - - try - { - IMpdMessage response = await _commandConnection.SendAsync(command); - if (!response.IsResponseValid) - { - // If we have an MpdError string, only show that as the error to avoid extra noise - var mpdError = response.Response?.Result?.MpdError; - if (mpdError != null && mpdError != "") - throw new Exception(mpdError); - else - throw new Exception($"Invalid server response: {response}."); - } - - return response.Response.Content; - } - catch (Exception e) - { - Trace.WriteLine($"Sending {command.GetType().Name} failed: {e.Message}"); - } - - return default(T); - } - - private async void GetAlbumBitmap(string path, CancellationToken token = default) + private async void GetAlbumCover(string path, CancellationToken token = default) { List data = new List(); try @@ -370,11 +368,23 @@ namespace unison UpdateCover(); } + public void UpdateStatus() + { + if (!_connected || _currentStatus == null) + return; + + _currentRandom = _currentStatus.Random; + _currentRepeat = _currentStatus.Repeat; + _currentConsume = _currentStatus.Consume; + _currentSingle = _currentStatus.Single; + _currentVolume = _currentStatus.Volume; + + StatusChanged?.Invoke(this, EventArgs.Empty); + } + public void UpdateSong() { - if (!_connected) - return; - if (_currentSong == null) + if (!_connected || _currentSong == null) return; _currentTime = _currentStatus.Elapsed.TotalSeconds; @@ -385,7 +395,7 @@ namespace unison SongChanged?.Invoke(this, EventArgs.Empty); string uri = Regex.Escape(_currentSong.Path); - GetAlbumBitmap(uri); + GetAlbumCover(uri); } public void UpdateCover() @@ -393,22 +403,6 @@ namespace unison CoverChanged?.Invoke(this, EventArgs.Empty); } - public void UpdateStatus() - { - if (!_connected) - return; - if (_currentStatus == null) - return; - - _currentRandom = _currentStatus.Random; - _currentRepeat = _currentStatus.Repeat; - _currentConsume = _currentStatus.Consume; - _currentSingle = _currentStatus.Single; - _currentVolume = _currentStatus.Volume; - - StatusChanged?.Invoke(this, EventArgs.Empty); - } - public IMpdFile GetCurrentSong() => _currentSong; public MpdStatus GetStatus() => _currentStatus; public BitmapFrame GetCover() => _cover; diff --git a/Handlers/SnapcastHandler.cs b/Handlers/SnapcastHandler.cs index 3daf429..a7b5424 100644 --- a/Handlers/SnapcastHandler.cs +++ b/Handlers/SnapcastHandler.cs @@ -46,7 +46,10 @@ namespace unison } catch (Exception err) { - MessageBox.Show($"[Snapcast error]\nInvalid path: {err.Message}\n\nCurrent path: {Properties.Settings.Default.snapcast_path}\nYou can reset it in the settings if needed.", + MessageBox.Show($"[{unison.Resources.Resources.Snapcast_Popup1}]\n" + + $"{unison.Resources.Resources.Snapcast_Popup2} {err.Message}\n\n" + + $"{unison.Resources.Resources.Snapcast_Popup3} {Properties.Settings.Default.snapcast_path}\n" + + $"{unison.Resources.Resources.Snapcast_Popup4}", "unison", MessageBoxButton.OK, MessageBoxImage.Error); return; } diff --git a/Resources/Resources.Designer.cs b/Resources/Resources.Designer.cs index b12d00e..19755e1 100644 --- a/Resources/Resources.Designer.cs +++ b/Resources/Resources.Designer.cs @@ -366,6 +366,42 @@ namespace unison.Resources { } } + /// + /// Looks up a localized string similar to Snapcast error. + /// + public static string Snapcast_Popup1 { + get { + return ResourceManager.GetString("Snapcast_Popup1", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Invalid path:. + /// + public static string Snapcast_Popup2 { + get { + return ResourceManager.GetString("Snapcast_Popup2", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Current path:. + /// + public static string Snapcast_Popup3 { + get { + return ResourceManager.GetString("Snapcast_Popup3", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to You can reset it in the settings if needed.. + /// + public static string Snapcast_Popup4 { + get { + return ResourceManager.GetString("Snapcast_Popup4", resourceCulture); + } + } + /// /// Looks up a localized string similar to Start Snapcast. /// diff --git a/Resources/Resources.fr-FR.resx b/Resources/Resources.fr-FR.resx index 5181015..f588b6f 100644 --- a/Resources/Resources.fr-FR.resx +++ b/Resources/Resources.fr-FR.resx @@ -219,6 +219,18 @@ Montrer la fenêtre + + Erreur Snapcast + + + Chemin invalide : + + + Chemin actuel : + + + Vous pouvez le réinitialiser dans la configuration. + Démarrer Snapcast diff --git a/Resources/Resources.resx b/Resources/Resources.resx index 8abc7b3..da1fcd5 100644 --- a/Resources/Resources.resx +++ b/Resources/Resources.resx @@ -219,6 +219,18 @@ Show window + + Snapcast error + + + Invalid path: + + + Current path: + + + You can reset it in the settings if needed. + Start Snapcast diff --git a/unison.csproj b/unison.csproj index 26b2a7e..2eac5c9 100644 --- a/unison.csproj +++ b/unison.csproj @@ -7,7 +7,7 @@ Resources\icon-full.ico unison.App - 0.0.1 + 1.0 Théo Marchal LICENSE