added OWEN Web Api Controller to return list of AIS targets

This commit is contained in:
Daniel Schick 2022-10-14 10:51:23 +02:00
parent bc6fcb50c4
commit 9b75a31469
10 changed files with 152 additions and 16 deletions

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Web.Http;
namespace bsmd.AIS2Service
{
public class AISController : ApiController
{
[HttpGet]
public IEnumerable<AIS_Target> Get()
{
List<AIS_Target> list = new List<AIS_Target>(AISManager.SitRep.Values);
return list;
}
}
}

View File

@ -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<IAISThread> _tasks = new List<IAISThread>();
private static readonly ConcurrentQueue<string> _inputLines = new ConcurrentQueue<string>();
private static readonly ConcurrentQueue<AISClass> _decodedClasses = new ConcurrentQueue<AISClass>();
@ -19,6 +22,11 @@ namespace bsmd.AIS2Service
private static readonly ConcurrentQueue<AIS_Target> _dbSaveTargets = new ConcurrentQueue<AIS_Target>();
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<StartupWebAPI>(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<int, AIS_Target> 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
}
}

View File

@ -200,6 +200,7 @@ namespace bsmd.AIS2Service
catch (Exception ex)
{
_log.ErrorFormat("Something bad has happened: {0}", ex.Message);
if(!_stopFlag)
this.FatalErrorOccurred?.Invoke(this, new EventArgs());
}
}
@ -266,10 +267,10 @@ namespace bsmd.AIS2Service
public void Stop()
{
if (_thread == null) return;
_connection.Close();
_stopFlag = true;
_thread.Join();
_thread = null;
_connection.Close();
}
#endregion

View File

@ -55,6 +55,12 @@
<setting name="PosReportDBCleanupDays" serializeAs="String">
<value>7</value>
</setting>
<setting name="EnableRestAPIEndpoint" serializeAs="String">
<value>True</value>
</setting>
<setting name="RestAPIBaseAddress" serializeAs="String">
<value>http://localhost:9050</value>
</setting>
</bsmd.AIS2Service.Properties.Settings>
</applicationSettings>
</configuration>

View File

@ -17,7 +17,7 @@ namespace bsmd.AIS2Service
{
AISManager.Start();
// TODO wait some
Thread.Sleep(60000);
Thread.Sleep(120000);
// Test finish..
AISManager.Stop();
}

View File

@ -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"]));
}
}
}
}

View File

@ -23,5 +23,11 @@
<Setting Name="PosReportDBCleanupDays" Type="System.Int32" Scope="Application">
<Value Profile="(Default)">7</Value>
</Setting>
<Setting Name="EnableRestAPIEndpoint" Type="System.Boolean" Scope="Application">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="RestAPIBaseAddress" Type="System.String" Scope="Application">
<Value Profile="(Default)">http://localhost:9050</Value>
</Setting>
</Settings>
</SettingsFile>

View File

@ -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);
}
}
}

View File

@ -38,13 +38,37 @@
<Reference Include="log4net, Version=2.0.15.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
<HintPath>packages\log4net.2.0.15\lib\net45\log4net.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Owin, Version=2.0.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Owin.2.0.2\lib\net45\Microsoft.Owin.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Owin.Host.HttpListener, Version=2.0.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Owin.Host.HttpListener.2.0.2\lib\net45\Microsoft.Owin.Host.HttpListener.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Owin.Hosting, Version=2.0.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Owin.Hosting.2.0.2\lib\net45\Microsoft.Owin.Hosting.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0ebd12fd5e55cc5, processorArchitecture=MSIL">
<HintPath>packages\Owin.1.0\lib\net40\Owin.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Data.SQLite, Version=1.0.116.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
<HintPath>packages\Stub.System.Data.SQLite.Core.NetFramework.1.0.116.0\lib\net46\System.Data.SQLite.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http.Formatting, Version=5.2.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.AspNet.WebApi.Client.5.2.9\lib\net45\System.Net.Http.Formatting.dll</HintPath>
</Reference>
<Reference Include="System.Web" />
<Reference Include="System.Web.Http, Version=5.2.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.AspNet.WebApi.Core.5.2.9\lib\net45\System.Web.Http.dll</HintPath>
</Reference>
<Reference Include="System.Web.Http.Owin, Version=5.2.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.AspNet.WebApi.Owin.5.2.9\lib\net45\System.Web.Http.Owin.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
@ -61,6 +85,7 @@
<DependentUpon>AIS2_Service.cs</DependentUpon>
</Compile>
<Compile Include="AISClass.cs" />
<Compile Include="AISController.cs" />
<Compile Include="AISDecoder.cs" />
<Compile Include="AISManager.cs" />
<Compile Include="AIS_BaseStationReport.cs" />
@ -85,6 +110,7 @@
</Compile>
<Compile Include="SerialTCPReader.cs" />
<Compile Include="SitRep.cs" />
<Compile Include="StartupWebAPI.cs" />
</ItemGroup>
<ItemGroup>
<None Include="..\SQL\ais_initial.db">

View File

@ -1,6 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="log4net" version="2.0.15" targetFramework="net48" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.9" targetFramework="net48" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.9" targetFramework="net48" />
<package id="Microsoft.AspNet.WebApi.Owin" version="5.2.9" targetFramework="net48" />
<package id="Microsoft.AspNet.WebApi.OwinSelfHost" version="5.2.9" targetFramework="net48" />
<package id="Microsoft.Owin" version="2.0.2" targetFramework="net48" />
<package id="Microsoft.Owin.Host.HttpListener" version="2.0.2" targetFramework="net48" />
<package id="Microsoft.Owin.Hosting" version="2.0.2" targetFramework="net48" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net48" />
<package id="Owin" version="1.0" targetFramework="net48" />
<package id="Stub.System.Data.SQLite.Core.NetFramework" version="1.0.116.0" targetFramework="net48" />
<package id="System.Data.SQLite.Core" version="1.0.116.0" targetFramework="net48" />
</packages>