diff --git a/Handlers/MPDHandler.cs b/Handlers/MPDHandler.cs index 676cf81..52878c5 100644 --- a/Handlers/MPDHandler.cs +++ b/Handlers/MPDHandler.cs @@ -20,6 +20,17 @@ using MpcNET.Types; namespace unison { + public class Statistics + { + public int Songs { get; set; } + public int Albums { get; set; } + public int Artists { get; set; } + public string TotalPlaytime { get; set; } + public string Uptime { get; set; } + public string TotalTimePlayed { get; set; } + public string DatabaseUpdate { get; set; } + } + public class MPDHandler { private bool _connected; @@ -36,6 +47,7 @@ namespace unison private MpdStatus _currentStatus; private IMpdFile _currentSong; private BitmapFrame _cover; + public Statistics _stats; private readonly System.Timers.Timer _elapsedTimer; private DispatcherTimer _retryTimer; @@ -58,6 +70,8 @@ namespace unison Initialize(null, null); + _stats = new Statistics(); + _retryTimer = new DispatcherTimer(); _retryTimer.Interval = TimeSpan.FromSeconds(5); _retryTimer.Tick += Initialize; @@ -417,6 +431,7 @@ namespace unison public MpdStatus GetStatus() => _currentStatus; public BitmapFrame GetCover() => _cover; public string GetVersion() => _version; + public Statistics GetStats() => _stats; public double GetCurrentTime() => _currentTime; public bool IsConnected() => _connected; @@ -479,5 +494,25 @@ namespace unison CommandList commandList = new CommandList(new IMpcCommand[] { new ClearCommand(), new AddCommand(Uri), new PlayCommand(0) }); SendCommand(commandList); } + + public async void QueryStats() + { + Dictionary response = await SafelySendCommandAsync(new StatsCommand()); + + _stats.Songs = int.Parse(response["songs"]); + _stats.Albums = int.Parse(response["albums"]); + _stats.Artists = int.Parse(response["artists"]); + + TimeSpan time; + time = TimeSpan.FromSeconds(int.Parse(response["uptime"])); + _stats.Uptime = time.ToString(@"dd\:hh\:mm\:ss"); + time = TimeSpan.FromSeconds(int.Parse(response["db_playtime"])); + _stats.TotalPlaytime = time.ToString(@"dd\:hh\:mm\:ss"); + time = TimeSpan.FromSeconds(int.Parse(response["playtime"])); + _stats.TotalTimePlayed = time.ToString(@"dd\:hh\:mm\:ss"); + + DateTime date = new DateTime(1970, 1, 1).AddSeconds(int.Parse(response["db_update"])).ToLocalTime(); + _stats.DatabaseUpdate = date.ToString("dd-MM-yyyy @ HH:mm"); + } } } \ No newline at end of file diff --git a/Views/MainWindow.xaml.cs b/Views/MainWindow.xaml.cs index 45449d9..af00275 100644 --- a/Views/MainWindow.xaml.cs +++ b/Views/MainWindow.xaml.cs @@ -135,6 +135,9 @@ namespace unison DefaultState(); } } + + _mpd.QueryStats(); + _settingsWin.UpdateStats(); } private void DefaultState(bool LostConnection = false) diff --git a/Views/Settings.xaml b/Views/Settings.xaml index d688dce..bc92440 100644 --- a/Views/Settings.xaml +++ b/Views/Settings.xaml @@ -46,6 +46,29 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Views/Settings.xaml.cs b/Views/Settings.xaml.cs index 2ee47bd..ac98bcd 100644 --- a/Views/Settings.xaml.cs +++ b/Views/Settings.xaml.cs @@ -88,6 +88,18 @@ namespace unison SnapcastPort.Text = (string)Application.Current.FindResource("snapcastPort"); } + public void UpdateStats() + { + MPDHandler mpd = (MPDHandler)Application.Current.Properties["mpd"]; + StatSong.Text = mpd.GetStats().Songs.ToString(); + StatAlbum.Text = mpd.GetStats().Albums.ToString(); + StatArtist.Text = mpd.GetStats().Artists.ToString(); + StatTotalPlaytime.Text = mpd.GetStats().TotalPlaytime.ToString(); + StatUptime.Text = mpd.GetStats().Uptime.ToString(); + StatTotalTimePlayed.Text = mpd.GetStats().TotalTimePlayed.ToString(); + StatDatabaseUpdate.Text = mpd.GetStats().DatabaseUpdate.ToString(); + } + public void SaveSettings() { Properties.Settings.Default.mpd_host = MpdHost.Text;