From c3cb978e1b891149044331becc11444fcbc97685 Mon Sep 17 00:00:00 2001 From: Alex Dresko Date: Mon, 3 Sep 2018 18:49:11 -0400 Subject: [PATCH] Enhancements via @zimmer62 https://github.com/zimmer62/HSPI/commit/356e0ebca5321689d9288b26de5c0e5a15529a87 * Non-blocking connect support * Made some protected properties public --- HSPI/Connector.cs | 29 +++++++++++++++++------------ HSPI/HSPIBase.cs | 12 ++++++++---- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/HSPI/Connector.cs b/HSPI/Connector.cs index 1cbf1d6..f461440 100644 --- a/HSPI/Connector.cs +++ b/HSPI/Connector.cs @@ -11,20 +11,20 @@ public static class Connector Justification = "The function wouldn't do anything without a plugin.")] [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "I don't know what kinds of exceptions it _could_ throw.")] - public static void Connect(string[] args) where TPlugin : HspiBase, new() + public static TPlugin Connect(string[] args, bool pollForShutdown = true) where TPlugin : HspiBase, new() { + // create an instance of our plugin. + var myPlugin = new TPlugin(); + Console.WriteLine(myPlugin.Name); + + if (Environment.UserInteractive) + { + Console.Title = myPlugin.Name; + } + Parser.Default.ParseArguments(args) .WithParsed(options => { - // create an instance of our plugin. - var myPlugin = new TPlugin(); - Console.WriteLine(myPlugin.Name); - - if (Environment.UserInteractive) - { - Console.Title = myPlugin.Name; - } - // Get our plugin to connect to HomeSeer Console.WriteLine($"\nConnecting to HomeSeer at {options.Server}:{options.Port} ..."); try @@ -43,7 +43,7 @@ public static class Connector // let the plugin do it's thing, wait until it shuts down or the connection to HomeSeer fails. try { - while (true) + while (pollForShutdown) { // do nothing for a bit Thread.Sleep(200); @@ -68,8 +68,13 @@ public static class Connector Console.WriteLine($"Unhandled exception from Plugin: {ex.Message}"); } + if (pollForShutdown) + { + Environment.Exit(0); + } }); - Environment.Exit(0); + + return myPlugin; } } } \ No newline at end of file diff --git a/HSPI/HSPIBase.cs b/HSPI/HSPIBase.cs index eedde05..dab92ea 100644 --- a/HSPI/HSPIBase.cs +++ b/HSPI/HSPIBase.cs @@ -13,15 +13,19 @@ namespace Hspi // ReSharper disable once InconsistentNaming public abstract class HspiBase : IPlugInAPI { - protected IAppCallbackAPI Callback { get; set; } + // ReSharper disable once MemberCanBePrivate.Global + public IAppCallbackAPI Callback { get; set; } - protected IScsServiceClient CallbackClient { get; set; } + // ReSharper disable once MemberCanBePrivate.Global + public IScsServiceClient CallbackClient { get; set; } - protected IHSApplication HS { get; set; } + // ReSharper disable once MemberCanBeProtected.Global + public IHSApplication HS { get; set; } [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Hs", Justification = "R# doesn't like MS's standard.")] - protected IScsServiceClient HsClient { get; set; } + // ReSharper disable once MemberCanBePrivate.Global + public IScsServiceClient HsClient { get; set; } public bool Shutdown { get; set; }