diff --git a/RaceLapTimer/Interfaces/INotifierProviderFactory.cs b/RaceLapTimer/Interfaces/INotifierProviderFactory.cs index e43b286..1f5361e 100644 --- a/RaceLapTimer/Interfaces/INotifierProviderFactory.cs +++ b/RaceLapTimer/Interfaces/INotifierProviderFactory.cs @@ -2,6 +2,6 @@ { public interface INotifierProviderFactory { - INotifierProvider CreateProvider(); + INotifierProvider GetProvider(); } } diff --git a/RaceLapTimer/Interfaces/ISystemControl.cs b/RaceLapTimer/Interfaces/ISystemControl.cs new file mode 100644 index 0000000..6ec0b46 --- /dev/null +++ b/RaceLapTimer/Interfaces/ISystemControl.cs @@ -0,0 +1,7 @@ +namespace Interfaces +{ + public interface ISystemControl + { + void Shutdown(); + } +} \ No newline at end of file diff --git a/RaceLapTimer/Interfaces/ISystemControlManager.cs b/RaceLapTimer/Interfaces/ISystemControlManager.cs new file mode 100644 index 0000000..92244c3 --- /dev/null +++ b/RaceLapTimer/Interfaces/ISystemControlManager.cs @@ -0,0 +1,10 @@ +using System.Collections.Generic; + +namespace Interfaces +{ + public interface ISystemControlManager + { + List GetAvailableProviders(); + void Shutdown(); + } +} \ No newline at end of file diff --git a/RaceLapTimer/Interfaces/ISystemControlProviderFactory.cs b/RaceLapTimer/Interfaces/ISystemControlProviderFactory.cs new file mode 100644 index 0000000..5d38bb5 --- /dev/null +++ b/RaceLapTimer/Interfaces/ISystemControlProviderFactory.cs @@ -0,0 +1,7 @@ +namespace Interfaces +{ + public interface ISystemControlProviderFactory + { + ISystemControl GetProvider(); + } +} diff --git a/RaceLapTimer/Interfaces/ITransponderUtilities.cs b/RaceLapTimer/Interfaces/ITransponderUtilities.cs new file mode 100644 index 0000000..16c7d39 --- /dev/null +++ b/RaceLapTimer/Interfaces/ITransponderUtilities.cs @@ -0,0 +1,7 @@ +namespace Interfaces +{ + public interface ITransponderUtilities + { + int GetLastScannedIdAsync(); + } +} diff --git a/RaceLapTimer/Interfaces/ITransponderUtilityProviderFactory.cs b/RaceLapTimer/Interfaces/ITransponderUtilityProviderFactory.cs new file mode 100644 index 0000000..c81b54a --- /dev/null +++ b/RaceLapTimer/Interfaces/ITransponderUtilityProviderFactory.cs @@ -0,0 +1,7 @@ +namespace Interfaces +{ + public interface ITransponderUtilityProviderFactory + { + ITransponderUtilities GetProvider(); + } +} diff --git a/RaceLapTimer/Interfaces/Interfaces.csproj b/RaceLapTimer/Interfaces/Interfaces.csproj index 4c5685c..dd8532a 100644 --- a/RaceLapTimer/Interfaces/Interfaces.csproj +++ b/RaceLapTimer/Interfaces/Interfaces.csproj @@ -50,6 +50,11 @@ + + + + + diff --git a/RaceLapTimer/IrDaemonNotifier/IrDaemonController.cs b/RaceLapTimer/IrDaemonNotifier/IrDaemonController.cs new file mode 100644 index 0000000..9bd432e --- /dev/null +++ b/RaceLapTimer/IrDaemonNotifier/IrDaemonController.cs @@ -0,0 +1,21 @@ +using System.Diagnostics; +using Interfaces; + +namespace IrDaemonNotifier +{ + public class IrDaemonController:ISystemControl + { + public void Shutdown() + { + Debug.WriteLine("test"); + } + } + + public class IrDaemonControllerProviderFactory : ISystemControlProviderFactory + { + public ISystemControl GetProvider() + { + return new IrDaemonController(); + } + } +} diff --git a/RaceLapTimer/IrDaemonNotifier/IrDaemonNotifier.csproj b/RaceLapTimer/IrDaemonNotifier/IrDaemonNotifier.csproj index 687c386..8033198 100644 --- a/RaceLapTimer/IrDaemonNotifier/IrDaemonNotifier.csproj +++ b/RaceLapTimer/IrDaemonNotifier/IrDaemonNotifier.csproj @@ -40,6 +40,7 @@ + diff --git a/RaceLapTimer/IrDaemonNotifier/IrDaemonNotifierProviderFactory.cs b/RaceLapTimer/IrDaemonNotifier/IrDaemonNotifierProviderFactory.cs index da2dce9..61e7a8f 100644 --- a/RaceLapTimer/IrDaemonNotifier/IrDaemonNotifierProviderFactory.cs +++ b/RaceLapTimer/IrDaemonNotifier/IrDaemonNotifierProviderFactory.cs @@ -15,7 +15,7 @@ namespace IrDaemonNotifier { _logger = logger; } - public INotifierProvider CreateProvider() + public INotifierProvider GetProvider() { return new IrDaemonNotifier(_logger); } diff --git a/RaceLapTimer/RaceLapTimer/AuthenticationBootstrapper.cs b/RaceLapTimer/RaceLapTimer/AuthenticationBootstrapper.cs index 70e12a1..ab0bc78 100644 --- a/RaceLapTimer/RaceLapTimer/AuthenticationBootstrapper.cs +++ b/RaceLapTimer/RaceLapTimer/AuthenticationBootstrapper.cs @@ -1,5 +1,4 @@ -using System.Diagnostics; -using System.IO; +using System.IO; using Interfaces; using Nancy; using Nancy.Authentication.Forms; @@ -7,11 +6,10 @@ using Nancy.Bootstrapper; using Nancy.Bootstrappers.Ninject; using Nancy.Conventions; using Nancy.Diagnostics; -using Nancy.TinyIoc; using Ninject; -using RaceLapTimer.ApiControllers; using RaceLapTimer.Extensions; using RaceLapTimer.Extensions.Notifications; +using RaceLapTimer.Extensions.SystemControl; namespace RaceLapTimer { @@ -39,10 +37,10 @@ namespace RaceLapTimer container.Load(cfgFilePath); container.Bind().To().InSingletonScope(); - + container.Bind().To().InSingletonScope(); - //var nM = container.Get(); - //nM.NotifyRaceStarted(new NotificationEventArgs()); + var sM = container.Get(); + sM.Shutdown(); } protected override void ConfigureRequestContainer(IKernel container, NancyContext context) diff --git a/RaceLapTimer/RaceLapTimer/Extensions/Notifications/ContainerHelper.cs b/RaceLapTimer/RaceLapTimer/Extensions/ContainerHelper.cs similarity index 90% rename from RaceLapTimer/RaceLapTimer/Extensions/Notifications/ContainerHelper.cs rename to RaceLapTimer/RaceLapTimer/Extensions/ContainerHelper.cs index 189a03a..6784c5a 100644 --- a/RaceLapTimer/RaceLapTimer/Extensions/Notifications/ContainerHelper.cs +++ b/RaceLapTimer/RaceLapTimer/Extensions/ContainerHelper.cs @@ -3,11 +3,11 @@ using System.Collections.Generic; using Interfaces; using Ninject; -namespace RaceLapTimer.Extensions.Notifications +namespace RaceLapTimer.Extensions { class ContainerHelper : IContainerHelper { - private IKernel _container; + private readonly IKernel _container; public ContainerHelper(IKernel container) { _container = container; diff --git a/RaceLapTimer/RaceLapTimer/Extensions/Notifications/NotificationManager.cs b/RaceLapTimer/RaceLapTimer/Extensions/Notifications/NotificationManager.cs index c7330bb..050fa77 100644 --- a/RaceLapTimer/RaceLapTimer/Extensions/Notifications/NotificationManager.cs +++ b/RaceLapTimer/RaceLapTimer/Extensions/Notifications/NotificationManager.cs @@ -1,9 +1,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using System.IO; using System.Linq; -using System.Reflection; using System.Threading.Tasks; using Interfaces; @@ -18,13 +16,13 @@ namespace RaceLapTimer.Extensions.Notifications RegisterProviders(locator); } - private async void RegisterProviders(IPluginLocator locator) + private void RegisterProviders(IPluginLocator locator) { - var providerFactories = await locator.LocateAsync(); + var providerFactories = locator.Locate(); _providers = new List(); foreach (var factory in providerFactories) { - _providers.Add(factory.CreateProvider()); + _providers.Add(factory.GetProvider()); } } diff --git a/RaceLapTimer/RaceLapTimer/Extensions/PluginLocator.cs b/RaceLapTimer/RaceLapTimer/Extensions/PluginLocator.cs index a683e1a..c4ac91c 100644 --- a/RaceLapTimer/RaceLapTimer/Extensions/PluginLocator.cs +++ b/RaceLapTimer/RaceLapTimer/Extensions/PluginLocator.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; @@ -13,11 +14,13 @@ namespace RaceLapTimer.Extensions { private readonly IContainerHelper _helper; private readonly IPluginPathProvider _pluginPath; + private readonly ILoggerService _logger; - public PluginLocator(IContainerHelper helper, IPluginPathProvider pluginPath) + public PluginLocator(IContainerHelper helper, IPluginPathProvider pluginPath, ILoggerService logger) { _helper = helper; _pluginPath = pluginPath; + _logger = logger; } public async Task> LocateAsync() @@ -27,48 +30,71 @@ namespace RaceLapTimer.Extensions public List Locate() { - if (!Directory.Exists(_pluginPath.GetPluginPath)) return new List(); - var files = Directory.GetFiles(_pluginPath.GetPluginPath); - var discoveryList = new List(); - - foreach (var file in files) + try { - var ext = Path.GetExtension(file); - if (!IsAssembly(ext)) continue; + if (!Directory.Exists(_pluginPath.GetPluginPath)) return new List(); + var files = Directory.GetFiles(_pluginPath.GetPluginPath); + var discoveryList = new List(); - var assembly = Assembly.ReflectionOnlyLoadFrom(file); - var pluginTypes = (from t in assembly.GetExportedTypes() - where t.GetInterfaces().Any(x => x.Name == nameof(INotifierProviderFactory)) - select t).ToList(); - if (pluginTypes.Any()) + foreach (var file in files) { - discoveryList.Add(new DiscoveredPlugin + var ext = Path.GetExtension(file); + if (!IsAssembly(ext)) continue; + + var assembly = Assembly.ReflectionOnlyLoadFrom(file); + var pluginTypes = (from t in assembly.GetExportedTypes() + where t.GetInterfaces().Any(x => x.Name == typeof(T).Name) + select t).ToList(); + if (pluginTypes.Any()) { - FilePath = file, - PluginType = pluginTypes.First(), - TypeToBindTo = typeof(INotifierProviderFactory), - UniqueName = Path.GetFileNameWithoutExtension(file) - }); + discoveryList.Add(new DiscoveredPlugin + { + FilePath = file, + PluginType = pluginTypes.First(), + TypeToBindTo = typeof(T), + UniqueName = Path.GetFileNameWithoutExtension(file) + }); + } } - } - foreach (var plugin in discoveryList) + foreach (var plugin in discoveryList) + { + var assy = AssemblyIsLoaded(plugin.FilePath) + ? GetAssemblyFromAppDomain(plugin.FilePath) + : Assembly.LoadFrom(plugin.FilePath); + + //register our type + var pluginTypes = + (from t in assy.GetExportedTypes() + where t.GetInterfaces().Any(x => x.Name == plugin.TypeToBindTo.Name) + select t).ToList(); + + if (!pluginTypes.Any()) continue; + + var pg = pluginTypes.First(); + _helper.RegisterType(plugin.TypeToBindTo, pg, plugin.UniqueName); + } + + return _helper.GetAll().ToList(); + } + catch (Exception ex) { - //load into the appDomain.. - var assy = Assembly.LoadFrom(plugin.FilePath); - //register our type - var pluginTypes = - (from t in assy.GetExportedTypes() - where t.GetInterfaces().Any(x => x.Name == plugin.TypeToBindTo.Name) - select t).ToList(); - - if (!pluginTypes.Any()) continue; - - var pg = pluginTypes.First(); - _helper.RegisterType(plugin.TypeToBindTo, pg, plugin.UniqueName); + _logger.Error(ex); + return new List(); } + } - return _helper.GetAll().ToList(); + private Assembly GetAssemblyFromAppDomain(string pluginFilePath) + { + var name = Path.GetFileName(pluginFilePath); + return AppDomain.CurrentDomain.GetAssemblies().First(x => x.ManifestModule.Name == name); + } + + private bool AssemblyIsLoaded(string pluginFilePath) + { + var name = Path.GetFileName(pluginFilePath); + var d = AppDomain.CurrentDomain.GetAssemblies().Where(x => x.ManifestModule.Name == name); + return d.Any(); } private bool IsAssembly(string extension) @@ -78,11 +104,7 @@ namespace RaceLapTimer.Extensions "dll", "exe" }; var ext = extension.Remove(0, 1); - if (validAssemblyExts.Contains(ext)) - { - return true; - } - return false; + return validAssemblyExts.Contains(ext); } private class DiscoveredPlugin diff --git a/RaceLapTimer/RaceLapTimer/Extensions/SystemControl/SystemControlManager.cs b/RaceLapTimer/RaceLapTimer/Extensions/SystemControl/SystemControlManager.cs new file mode 100644 index 0000000..59e2b57 --- /dev/null +++ b/RaceLapTimer/RaceLapTimer/Extensions/SystemControl/SystemControlManager.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Threading.Tasks; +using Interfaces; + +namespace RaceLapTimer.Extensions.SystemControl +{ + class SystemControlManager : ISystemControlManager + { + private List _providers; + private readonly ILoggerService _logger; + + public SystemControlManager(IPluginLocator locator, ILoggerService logger) + { + RegisterProviders(locator); + _logger = logger; + } + + private void RegisterProviders(IPluginLocator locator) + { + try + { + var providerFactories = locator.Locate(); + _providers = new List(); + foreach (var factory in providerFactories) + { + _providers.Add(factory.GetProvider()); + } + } + catch (Exception ex) + { + _logger.Error(ex); + } + } + + public List GetAvailableProviders() + { + return _providers.Select(x=>x.GetType().ToString()).ToList(); + } + + public void Shutdown() + { + foreach (var provider in _providers) + { + Task.Factory.StartNew(() => provider.Shutdown()); + } + } + } +} diff --git a/RaceLapTimer/RaceLapTimer/RaceLapTimer.csproj b/RaceLapTimer/RaceLapTimer/RaceLapTimer.csproj index 27fe674..af757ba 100644 --- a/RaceLapTimer/RaceLapTimer/RaceLapTimer.csproj +++ b/RaceLapTimer/RaceLapTimer/RaceLapTimer.csproj @@ -115,6 +115,7 @@ + @@ -126,7 +127,7 @@ - + diff --git a/RaceLapTimer/UdpNotifier/UdpNotifierProviderFactory.cs b/RaceLapTimer/UdpNotifier/UdpNotifierProviderFactory.cs index 2eed2c5..c912a27 100644 --- a/RaceLapTimer/UdpNotifier/UdpNotifierProviderFactory.cs +++ b/RaceLapTimer/UdpNotifier/UdpNotifierProviderFactory.cs @@ -15,7 +15,7 @@ namespace UdpNotifier _logger = logger; } - public INotifierProvider CreateProvider() + public INotifierProvider GetProvider() { return new UdpNotifierProvider(_logger); }