From 9b75a314699007324d7c99551d25ac2c14c6290b Mon Sep 17 00:00:00 2001 From: Daniel Schick Date: Fri, 14 Oct 2022 10:51:23 +0200 Subject: [PATCH] added OWEN Web Api Controller to return list of AIS targets --- AIS/bsmd.AIS2Service/AISController.cs | 16 +++++ AIS/bsmd.AIS2Service/AISManager.cs | 59 +++++++++++++++---- AIS/bsmd.AIS2Service/AIS_SQLiteStorage.cs | 7 ++- AIS/bsmd.AIS2Service/App.config | 6 ++ AIS/bsmd.AIS2Service/Program.cs | 2 +- .../Properties/Settings.Designer.cs | 18 ++++++ .../Properties/Settings.settings | 6 ++ AIS/bsmd.AIS2Service/StartupWebAPI.cs | 19 ++++++ AIS/bsmd.AIS2Service/bsmd.AIS2Service.csproj | 26 ++++++++ AIS/bsmd.AIS2Service/packages.config | 9 +++ 10 files changed, 152 insertions(+), 16 deletions(-) create mode 100644 AIS/bsmd.AIS2Service/AISController.cs create mode 100644 AIS/bsmd.AIS2Service/StartupWebAPI.cs diff --git a/AIS/bsmd.AIS2Service/AISController.cs b/AIS/bsmd.AIS2Service/AISController.cs new file mode 100644 index 00000000..f529e4f1 --- /dev/null +++ b/AIS/bsmd.AIS2Service/AISController.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Web.Http; + +namespace bsmd.AIS2Service +{ + public class AISController : ApiController + { + [HttpGet] + public IEnumerable Get() + { + List list = new List(AISManager.SitRep.Values); + return list; + } + } +} diff --git a/AIS/bsmd.AIS2Service/AISManager.cs b/AIS/bsmd.AIS2Service/AISManager.cs index e11c3d3c..821fc8de 100644 --- a/AIS/bsmd.AIS2Service/AISManager.cs +++ b/AIS/bsmd.AIS2Service/AISManager.cs @@ -2,15 +2,18 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading; -using System.Threading.Tasks; + +using Microsoft.Owin.Hosting; +using System.Net.Http; namespace bsmd.AIS2Service { internal static class AISManager { + + #region fields + private static readonly List _tasks = new List(); private static readonly ConcurrentQueue _inputLines = new ConcurrentQueue(); private static readonly ConcurrentQueue _decodedClasses = new ConcurrentQueue(); @@ -19,6 +22,11 @@ namespace bsmd.AIS2Service private static readonly ConcurrentQueue _dbSaveTargets = new ConcurrentQueue(); private static Timer _staleTargetTimer; // cleanup sitrep private static Timer _stalePosReportTimer; // clean db + private static IDisposable _restAPISelfHost = null; + + #endregion + + #region public methods public static async void Start() { @@ -45,8 +53,43 @@ namespace bsmd.AIS2Service // init timer tasks _staleTargetTimer = new Timer(StaleTargetTimerCheck, null, 0, 60000); // check every minute, start immediately _stalePosReportTimer = new Timer(StalePosReportCheck, sqliteStorage, 0, 60000 * 10); // every ten minutes, + + // if required start self-hosted owin endpoint + if(Properties.Settings.Default.EnableRestAPIEndpoint) + { + _restAPISelfHost = WebApp.Start(url: Properties.Settings.Default.RestAPIBaseAddress); + } + } + public static void Stop() + { + foreach (var task in _tasks) + { + task.Stop(); + _log.InfoFormat("{0} stopped", task.Name); + } + + if (Properties.Settings.Default.EnableRestAPIEndpoint && (_restAPISelfHost != null)) + _restAPISelfHost.Dispose(); + + _staleTargetTimer.Dispose(); + _stalePosReportTimer.Dispose(); + } + + #endregion + + #region Properties + + public static ConcurrentDictionary SitRep + { + get { return _sitRepList; } + } + + #endregion + + #region private methods / timer callbacks + private static void Task_FatalErrorOccurred(object sender, EventArgs e) { throw new NotImplementedException("TBD: shutdown the whole operation?"); @@ -78,15 +121,7 @@ namespace bsmd.AIS2Service } } - public static void Stop() - { - foreach (var task in _tasks) - { - task.Stop(); - _log.InfoFormat("{0} stopped", task.Name); - } - - } + #endregion } } diff --git a/AIS/bsmd.AIS2Service/AIS_SQLiteStorage.cs b/AIS/bsmd.AIS2Service/AIS_SQLiteStorage.cs index 1e3123f5..707f8805 100644 --- a/AIS/bsmd.AIS2Service/AIS_SQLiteStorage.cs +++ b/AIS/bsmd.AIS2Service/AIS_SQLiteStorage.cs @@ -200,7 +200,8 @@ namespace bsmd.AIS2Service catch (Exception ex) { _log.ErrorFormat("Something bad has happened: {0}", ex.Message); - this.FatalErrorOccurred?.Invoke(this, new EventArgs()); + if(!_stopFlag) + this.FatalErrorOccurred?.Invoke(this, new EventArgs()); } } @@ -265,11 +266,11 @@ namespace bsmd.AIS2Service public void Stop() { - if (_thread == null) return; - _connection.Close(); + if (_thread == null) return; _stopFlag = true; _thread.Join(); _thread = null; + _connection.Close(); } #endregion diff --git a/AIS/bsmd.AIS2Service/App.config b/AIS/bsmd.AIS2Service/App.config index 9560d53c..ebffd0bc 100644 --- a/AIS/bsmd.AIS2Service/App.config +++ b/AIS/bsmd.AIS2Service/App.config @@ -55,6 +55,12 @@ 7 + + True + + + http://localhost:9050 + \ No newline at end of file diff --git a/AIS/bsmd.AIS2Service/Program.cs b/AIS/bsmd.AIS2Service/Program.cs index a518a447..33bbc1ac 100644 --- a/AIS/bsmd.AIS2Service/Program.cs +++ b/AIS/bsmd.AIS2Service/Program.cs @@ -17,7 +17,7 @@ namespace bsmd.AIS2Service { AISManager.Start(); // TODO wait some - Thread.Sleep(60000); + Thread.Sleep(120000); // Test finish.. AISManager.Stop(); } diff --git a/AIS/bsmd.AIS2Service/Properties/Settings.Designer.cs b/AIS/bsmd.AIS2Service/Properties/Settings.Designer.cs index 2dd9f04e..ae8c55a7 100644 --- a/AIS/bsmd.AIS2Service/Properties/Settings.Designer.cs +++ b/AIS/bsmd.AIS2Service/Properties/Settings.Designer.cs @@ -85,5 +85,23 @@ namespace bsmd.AIS2Service.Properties { return ((int)(this["PosReportDBCleanupDays"])); } } + + [global::System.Configuration.ApplicationScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("True")] + public bool EnableRestAPIEndpoint { + get { + return ((bool)(this["EnableRestAPIEndpoint"])); + } + } + + [global::System.Configuration.ApplicationScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("http://localhost:9050")] + public string RestAPIBaseAddress { + get { + return ((string)(this["RestAPIBaseAddress"])); + } + } } } diff --git a/AIS/bsmd.AIS2Service/Properties/Settings.settings b/AIS/bsmd.AIS2Service/Properties/Settings.settings index 5068b48c..4d561b1d 100644 --- a/AIS/bsmd.AIS2Service/Properties/Settings.settings +++ b/AIS/bsmd.AIS2Service/Properties/Settings.settings @@ -23,5 +23,11 @@ 7 + + True + + + http://localhost:9050 + \ No newline at end of file diff --git a/AIS/bsmd.AIS2Service/StartupWebAPI.cs b/AIS/bsmd.AIS2Service/StartupWebAPI.cs new file mode 100644 index 00000000..5341523d --- /dev/null +++ b/AIS/bsmd.AIS2Service/StartupWebAPI.cs @@ -0,0 +1,19 @@ +using System.Web.Http; +using Owin; + +namespace bsmd.AIS2Service +{ + public class StartupWebAPI + { + public void Configuration(IAppBuilder appBuilder) + { + HttpConfiguration config = new HttpConfiguration(); + config.Routes.MapHttpRoute( + name: "AISList", + routeTemplate: "api/{Controller}", + defaults: new { id = RouteParameter.Optional, Controller = "AIS"} + ); + appBuilder.UseWebApi(config); + } + } +} diff --git a/AIS/bsmd.AIS2Service/bsmd.AIS2Service.csproj b/AIS/bsmd.AIS2Service/bsmd.AIS2Service.csproj index 00527177..ec6f5bdf 100644 --- a/AIS/bsmd.AIS2Service/bsmd.AIS2Service.csproj +++ b/AIS/bsmd.AIS2Service/bsmd.AIS2Service.csproj @@ -38,13 +38,37 @@ packages\log4net.2.0.15\lib\net45\log4net.dll + + packages\Microsoft.Owin.2.0.2\lib\net45\Microsoft.Owin.dll + + + packages\Microsoft.Owin.Host.HttpListener.2.0.2\lib\net45\Microsoft.Owin.Host.HttpListener.dll + + + packages\Microsoft.Owin.Hosting.2.0.2\lib\net45\Microsoft.Owin.Hosting.dll + + + packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll + + + packages\Owin.1.0\lib\net40\Owin.dll + packages\Stub.System.Data.SQLite.Core.NetFramework.1.0.116.0\lib\net46\System.Data.SQLite.dll + + packages\Microsoft.AspNet.WebApi.Client.5.2.9\lib\net45\System.Net.Http.Formatting.dll + + + packages\Microsoft.AspNet.WebApi.Core.5.2.9\lib\net45\System.Web.Http.dll + + + packages\Microsoft.AspNet.WebApi.Owin.5.2.9\lib\net45\System.Web.Http.Owin.dll + @@ -61,6 +85,7 @@ AIS2_Service.cs + @@ -85,6 +110,7 @@ + diff --git a/AIS/bsmd.AIS2Service/packages.config b/AIS/bsmd.AIS2Service/packages.config index ee2c1118..8edf1530 100644 --- a/AIS/bsmd.AIS2Service/packages.config +++ b/AIS/bsmd.AIS2Service/packages.config @@ -1,6 +1,15 @@  + + + + + + + + + \ No newline at end of file