diff --git a/Selector.CLI/Options.cs b/Selector.CLI/Options.cs new file mode 100644 index 0000000..441c200 --- /dev/null +++ b/Selector.CLI/Options.cs @@ -0,0 +1,10 @@ + +namespace Selector.CLI +{ + class RootOptions + { + public const string Key = "Selector"; + + public int Number { get; set; } = 2; + } +} diff --git a/Selector.CLI/Program.cs b/Selector.CLI/Program.cs index d2cd32e..40b4efd 100644 --- a/Selector.CLI/Program.cs +++ b/Selector.CLI/Program.cs @@ -4,6 +4,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Configuration; +using NLog.Extensions.Logging; namespace Selector.CLI { @@ -18,17 +19,24 @@ namespace Selector.CLI static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureServices((context, services) => { + + // CONFIG + services.Configure(options => + context.Configuration.GetSection(RootOptions.Key).Bind(options) + ); + + // SERVICES services.AddTransient(); services.AddTransient(); + services.AddTransient(); + + // HOSTED SERVICES services.AddHostedService(); }) - .ConfigureLogging(builder => { - builder - .AddSimpleConsole(options => { - options.IncludeScopes = true; - options.SingleLine = true; - options.TimestampFormat = "yyyy-mm-dd hh:mm:ss "; - }); + .ConfigureLogging((context, builder) => { + builder.ClearProviders(); + builder.SetMinimumLevel(LogLevel.Trace); + builder.AddNLog(context.Configuration); }); } } diff --git a/Selector.CLI/Selector.CLI.csproj b/Selector.CLI/Selector.CLI.csproj index 9f25b36..a288243 100644 --- a/Selector.CLI/Selector.CLI.csproj +++ b/Selector.CLI/Selector.CLI.csproj @@ -12,6 +12,8 @@ + + @@ -23,6 +25,9 @@ PreserveNewest + + PreserveNewest + diff --git a/Selector.CLI/SelectorOptions.cs b/Selector.CLI/SelectorOptions.cs deleted file mode 100644 index 072fd23..0000000 --- a/Selector.CLI/SelectorOptions.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Selector.CLI -{ - class SelectorOptions - { - public const string Options = "Selector"; - - public int Number { get; set; } - } -} diff --git a/Selector.CLI/WatcherService.cs b/Selector.CLI/WatcherService.cs index 1241afa..ffd4be3 100644 --- a/Selector.CLI/WatcherService.cs +++ b/Selector.CLI/WatcherService.cs @@ -7,35 +7,25 @@ using System.Threading.Tasks; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; namespace Selector.CLI { class WatcherService : IHostedService { private readonly ILogger Logger; - private readonly IConfiguration Config; + private readonly RootOptions Config; - public WatcherService(ILogger logger, IConfiguration config) + public WatcherService(ILogger logger, IOptions config) { Logger = logger; - Config = config; + Config = config.Value; } public Task StartAsync(CancellationToken cancellationToken) { Logger.LogInformation("Starting up"); - foreach ((var key, var pair) in Config.AsEnumerable()) - //foreach ((var key, var pair) in Config.GetSection("Selector").AsEnumerable()) - { - Logger.LogInformation($"{key} => {pair}"); - } - - using(Logger.BeginScope("A New Scope!")) - { - Logger.LogError("From the scope!"); - } - return Task.CompletedTask; } diff --git a/Selector.CLI/appsettings.json b/Selector.CLI/appsettings.json index 4314c3f..a228a47 100644 --- a/Selector.CLI/appsettings.json +++ b/Selector.CLI/appsettings.json @@ -1,20 +1,11 @@ { - "string config": "asdf", - "Selector": { - "Number": 4 - }, + //"Selector": { + // "Number2": 4, + // "Number": 4 + //}, "Logging": { "LogLevel": { - "Default": "Information", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" - }, - "Console": { - "LogLevel": { - "Default": "Information", - "Microsoft": "Warning", - //"Microsoft.Hosting.Lifetime": "Information" - } + "Default": "Information" } } } \ No newline at end of file diff --git a/Selector.CLI/nlog.config b/Selector.CLI/nlog.config new file mode 100644 index 0000000..808429a --- /dev/null +++ b/Selector.CLI/nlog.config @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Selector/Helpers/SpotifyExtensions.cs b/Selector/Helpers/SpotifyExtensions.cs new file mode 100644 index 0000000..77750aa --- /dev/null +++ b/Selector/Helpers/SpotifyExtensions.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Linq; + +using SpotifyAPI.Web; + +namespace Selector.Helpers +{ + public static class SpotifyExtensions + { + public static string ToString(this FullTrack track) => $"{track.Name} - {track.Album.Name} - {track.Artists}"; + public static string ToString(this SimpleAlbum album) => $"{album.Name} - {album.Artists}"; + public static string ToString(this SimpleArtist artist) => $"{artist.Name}"; + + public static string ToString(this FullEpisode ep) => $"{ep.Name} - {ep.Show}"; + public static string ToString(this SimpleShow show) => $"{show.Name} - {show.Publisher}"; + + public static string ToString(this CurrentlyPlayingContext context) => $"{context.IsPlaying}, {context.Item}, {context.Device}"; + public static string ToString(this Device device) => $"{device.Id}: {device.Name} {device.VolumePercent}%"; + + public static string ToString(this IEnumerable artists) => string.Join("/", artists.Select(a => a.Name)); + } +} diff --git a/Selector/Selector.csproj b/Selector/Selector.csproj index 3b81dbd..0b7168e 100644 --- a/Selector/Selector.csproj +++ b/Selector/Selector.csproj @@ -7,6 +7,7 @@ + diff --git a/Selector/Watcher/PlayerWatcher.cs b/Selector/Watcher/PlayerWatcher.cs index 5a93376..52992a0 100644 --- a/Selector/Watcher/PlayerWatcher.cs +++ b/Selector/Watcher/PlayerWatcher.cs @@ -4,10 +4,14 @@ using System.Threading; using System.Threading.Tasks; using SpotifyAPI.Web; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; + namespace Selector { public class PlayerWatcher: BaseWatcher, IPlayerWatcher { + private readonly ILogger Logger; private readonly IPlayerClient spotifyClient; private readonly IEqual eq; @@ -26,10 +30,12 @@ namespace Selector public PlayerWatcher(IPlayerClient spotifyClient, IEqual equalityChecker, + ILogger logger = null, int pollPeriod = 3000) { this.spotifyClient = spotifyClient; eq = equalityChecker; + Logger = logger ?? NullLogger.Instance; PollPeriod = pollPeriod; } @@ -64,12 +70,14 @@ namespace Selector if(previous is null && (Live.Item is FullTrack || Live.Item is FullEpisode)) { + Logger.LogDebug($"Playback started: {Live}"); OnPlayingChange(ListeningChangeEventArgs.From(previous, Live)); } // STOPPED PLAYBACK else if((previous.Item is FullTrack || previous.Item is FullEpisode) && Live is null) { + Logger.LogDebug($"Playback stopped: {previous}"); OnPlayingChange(ListeningChangeEventArgs.From(previous, Live)); } // CONTINUING PLAYBACK @@ -79,16 +87,18 @@ namespace Selector if(previous.Item is FullTrack previousTrack && Live.Item is FullTrack currentTrack) { - if(!eq.IsEqual(previousTrack, currentTrack)) { + Logger.LogDebug($"Track changed: {previousTrack} -> {currentTrack}"); OnItemChange(ListeningChangeEventArgs.From(previous, Live)); } if(!eq.IsEqual(previousTrack.Album, currentTrack.Album)) { + Logger.LogDebug($"Album changed: {previousTrack.Album} -> {currentTrack.Album}"); OnAlbumChange(ListeningChangeEventArgs.From(previous, Live)); } if(!eq.IsEqual(previousTrack.Artists[0], currentTrack.Artists[0])) { + Logger.LogDebug($"Artist changed: {previousTrack.Artists[0]} -> {currentTrack.Artists[0]}"); OnArtistChange(ListeningChangeEventArgs.From(previous, Live)); } } @@ -96,6 +106,7 @@ namespace Selector else if((previous.Item is FullTrack && Live.Item is FullEpisode) || (previous.Item is FullEpisode && Live.Item is FullTrack)) { + Logger.LogDebug($"Media type changed: {previous.Item}, {previous.Item}"); OnContentChange(ListeningChangeEventArgs.From(previous, Live)); OnItemChange(ListeningChangeEventArgs.From(previous, Live)); } @@ -104,6 +115,7 @@ namespace Selector && Live.Item is FullEpisode currentEp) { if(!eq.IsEqual(previousEp, currentEp)) { + Logger.LogDebug($"Podcast changed: {previousEp} -> {currentEp}"); OnItemChange(ListeningChangeEventArgs.From(previous, Live)); } } @@ -113,21 +125,25 @@ namespace Selector // CONTEXT if(!eq.IsEqual(previous.Context, Live.Context)) { + Logger.LogDebug($"Context changed: {previous.Context} -> {Live.Context}"); OnContextChange(ListeningChangeEventArgs.From(previous, Live)); } // DEVICE if(!eq.IsEqual(previous?.Device, Live?.Device)) { + Logger.LogDebug($"Device changed: {previous?.Device} -> {Live?.Device}"); OnDeviceChange(ListeningChangeEventArgs.From(previous, Live)); } // IS PLAYING if(previous.IsPlaying != Live.IsPlaying) { + Logger.LogDebug($"Playing state changed: {previous.IsPlaying} -> {Live.IsPlaying}"); OnPlayingChange(ListeningChangeEventArgs.From(previous, Live)); } // VOLUME if(previous.Device.VolumePercent != Live.Device.VolumePercent) { + Logger.LogDebug($"Volume changed: {previous.Device.VolumePercent}% -> {Live.Device.VolumePercent}%"); OnVolumeChange(ListeningChangeEventArgs.From(previous, Live)); } } diff --git a/Selector/Watcher/WatcherCollection/WatcherCollection.cs b/Selector/Watcher/WatcherCollection/WatcherCollection.cs index 9388a5a..b43da4d 100644 --- a/Selector/Watcher/WatcherCollection/WatcherCollection.cs +++ b/Selector/Watcher/WatcherCollection/WatcherCollection.cs @@ -1,3 +1,5 @@ +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using System; using System.Collections; using System.Collections.Generic; @@ -10,12 +12,13 @@ namespace Selector { public class WatcherCollection: IWatcherCollection, IDisposable, IEnumerable { + private readonly ILogger Logger; public bool IsRunning { get; private set; } = true; private List Watchers { get; set; } = new(); - public WatcherCollection() + public WatcherCollection(ILogger logger = null) { - + Logger = logger ?? NullLogger.Instance; } public int Count => Watchers.Count; @@ -42,6 +45,7 @@ namespace Selector public void Start() { + Logger.LogDebug($"Starting {Count} watchers"); foreach(var watcher in Watchers) { watcher.Start(); @@ -51,7 +55,8 @@ namespace Selector public void Stop() { - foreach(var watcher in Watchers) + Logger.LogDebug($"Stopping {Count} watchers"); + foreach (var watcher in Watchers) { watcher.Stop(); }