Initial_commit_SecMPS_v2
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
using IntegrationGateway.Core.Abstractions;
|
||||
using IntegrationGateway.Core.Infrastructure;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace IntegrationGateway.Host.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/gateway/alarms")]
|
||||
public class AlarmsController : ControllerBase
|
||||
{
|
||||
private readonly AdapterRegistry _registry;
|
||||
|
||||
public AlarmsController(AdapterRegistry registry) => _registry = registry;
|
||||
|
||||
[HttpGet("{adapter}")]
|
||||
public async Task<IActionResult> GetAlarms(string adapter, [FromQuery] DateTime from, [FromQuery] DateTime to,
|
||||
[FromQuery] int page = 1, [FromQuery] int size = 50)
|
||||
{
|
||||
var a = _registry.Get(adapter);
|
||||
if (a is not IHasAlarms al) return NotFound();
|
||||
return Ok(await al.GetAlarmsAsync(page, size, from, to));
|
||||
}
|
||||
|
||||
[HttpPost("{adapter}/{alarmId}/confirm")]
|
||||
public async Task<IActionResult> Confirm(string adapter, string alarmId)
|
||||
{
|
||||
var a = _registry.Get(adapter);
|
||||
if (a is not IHasAlarms al) return NotFound();
|
||||
await al.ConfirmAlarmAsync(alarmId);
|
||||
return Ok(new { status = "confirmed" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using IntegrationGateway.Core.Abstractions;
|
||||
using IntegrationGateway.Core.Infrastructure;
|
||||
using IntegrationGateway.Core.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace IntegrationGateway.Host.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/gateway/devices")]
|
||||
public class DevicesController : ControllerBase
|
||||
{
|
||||
private readonly AdapterRegistry _registry;
|
||||
|
||||
public DevicesController(AdapterRegistry registry) => _registry = registry;
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetDevices([FromQuery] string adapter, [FromQuery] int page = 1, [FromQuery] int size = 50)
|
||||
{
|
||||
var a = _registry.Get(adapter);
|
||||
if (a is not IHasFlatDevices f) return NotFound("Adapter not found or unsupported");
|
||||
return Ok(await f.GetDevicesAsync(page, size));
|
||||
}
|
||||
|
||||
[HttpGet("{adapter}/{deviceId}")]
|
||||
public async Task<IActionResult> GetDevice(string adapter, string deviceId)
|
||||
{
|
||||
var a = _registry.Get(adapter);
|
||||
if (a is not IHasFlatDevices f) return NotFound();
|
||||
var d = await f.GetDeviceAsync(deviceId);
|
||||
return d is null ? NotFound() : Ok(d);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using IntegrationGateway.Core.Infrastructure;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace IntegrationGateway.Host.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/gateway/health")]
|
||||
public class HealthController : ControllerBase
|
||||
{
|
||||
private readonly AdapterRegistry _registry;
|
||||
|
||||
public HealthController(AdapterRegistry registry) => _registry = registry;
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Get()
|
||||
{
|
||||
var status = new Dictionary<string, bool>();
|
||||
foreach (var adapter in _registry.GetAll())
|
||||
status[adapter.AdapterCode] = await adapter.HealthCheckAsync();
|
||||
return Ok(status);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using IntegrationGateway.Core.Abstractions;
|
||||
using IntegrationGateway.Core.Infrastructure;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace IntegrationGateway.Host.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/gateway/realtime")]
|
||||
public class PointsController : ControllerBase
|
||||
{
|
||||
private readonly AdapterRegistry _registry;
|
||||
|
||||
public PointsController(AdapterRegistry registry) => _registry = registry;
|
||||
|
||||
[HttpGet("{adapter}/{deviceId}")]
|
||||
public async Task<IActionResult> GetRealtime(string adapter, string deviceId)
|
||||
{
|
||||
var a = _registry.Get(adapter);
|
||||
if (a is not IHasPoints p) return NotFound();
|
||||
return Ok(await p.GetRealtimeValuesAsync(deviceId));
|
||||
}
|
||||
|
||||
[HttpPost("{adapter}/control")]
|
||||
public async Task<IActionResult> Control(string adapter, [FromBody] ControlRequest req)
|
||||
{
|
||||
var a = _registry.Get(adapter);
|
||||
if (a is not IHasPoints p) return NotFound();
|
||||
await p.SetPointValueAsync(req.DeviceId, req.PointIndex, req.Value);
|
||||
return Ok(new { status = "sent" });
|
||||
}
|
||||
}
|
||||
|
||||
public class ControlRequest
|
||||
{
|
||||
public string DeviceId { get; set; } = "";
|
||||
public int PointIndex { get; set; }
|
||||
public double Value { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using IntegrationGateway.Core.Abstractions;
|
||||
using IntegrationGateway.Core.Infrastructure;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace IntegrationGateway.Host.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/gateway/streams")]
|
||||
public class StreamsController : ControllerBase
|
||||
{
|
||||
private readonly AdapterRegistry _registry;
|
||||
|
||||
public StreamsController(AdapterRegistry registry) => _registry = registry;
|
||||
|
||||
[HttpGet("{adapter}/{channelId}/live")]
|
||||
public async Task<IActionResult> GetLive(string adapter, string channelId)
|
||||
{
|
||||
var a = _registry.Get(adapter);
|
||||
if (a is not IHasStreams s) return NotFound();
|
||||
return Ok(await s.GetLiveUrlAsync(channelId));
|
||||
}
|
||||
|
||||
[HttpPost("{adapter}/{channelId}/ptz")]
|
||||
public async Task<IActionResult> Ptz(string adapter, string channelId, [FromBody] PtzRequest req)
|
||||
{
|
||||
var a = _registry.Get(adapter);
|
||||
if (a is not IHasStreams s) return NotFound();
|
||||
await s.PtzControlAsync(channelId, req.Direction, req.Speed);
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
|
||||
public class PtzRequest
|
||||
{
|
||||
public string Direction { get; set; } = "stop";
|
||||
public float Speed { get; set; } = 0.5f;
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using IntegrationGateway.Core.Abstractions;
|
||||
using IntegrationGateway.Core.Infrastructure;
|
||||
using IntegrationGateway.Core.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace IntegrationGateway.Host.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/gateway")]
|
||||
public class SyncController : ControllerBase
|
||||
{
|
||||
private readonly AdapterRegistry _registry;
|
||||
|
||||
public SyncController(AdapterRegistry registry) => _registry = registry;
|
||||
|
||||
[HttpGet("devices/sync")]
|
||||
public async Task<IActionResult> SyncDevices([FromQuery] string adapter)
|
||||
{
|
||||
var a = _registry.Get(adapter) ?? throw new InvalidOperationException($"Adapter '{adapter}' not found");
|
||||
|
||||
var report = new SyncReport { AdapterCode = adapter, StartTime = DateTime.UtcNow };
|
||||
|
||||
if (a is IHasFlatDevices f)
|
||||
report = await SyncFlatDevices(f, report);
|
||||
else if (a is IHasOwnDeviceTree t)
|
||||
report = await SyncTreeDevices(t, report);
|
||||
else
|
||||
return BadRequest("Adapter does not support device sync");
|
||||
|
||||
report.EndTime = DateTime.UtcNow;
|
||||
return Ok(report);
|
||||
}
|
||||
|
||||
private async Task<SyncReport> SyncFlatDevices(IHasFlatDevices adapter, SyncReport report)
|
||||
{
|
||||
var devices = await adapter.GetAllDevicesAsync();
|
||||
report.Added = devices.Count;
|
||||
return report;
|
||||
}
|
||||
|
||||
private async Task<SyncReport> SyncTreeDevices(IHasOwnDeviceTree adapter, SyncReport report)
|
||||
{
|
||||
var tree = await adapter.GetObjectTreeAsync();
|
||||
int count = CountDeviceNodes(tree);
|
||||
report.Added = count;
|
||||
return report;
|
||||
}
|
||||
|
||||
private int CountDeviceNodes(List<DeviceTreeNode> nodes)
|
||||
{
|
||||
int count = 0;
|
||||
foreach (var n in nodes)
|
||||
{
|
||||
if (n.NodeType == 2) count++;
|
||||
count += CountDeviceNodes(n.Children);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.27" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\IntegrationGateway.Core\IntegrationGateway.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,6 @@
|
||||
@IntegrationGateway.Host_HostAddress = http://localhost:5294
|
||||
|
||||
GET {{IntegrationGateway.Host_HostAddress}}/weatherforecast/
|
||||
Accept: application/json
|
||||
|
||||
###
|
||||
14
gateway/src/IntegrationGateway.Host/Program.cs
Normal file
14
gateway/src/IntegrationGateway.Host/Program.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using IntegrationGateway.Core.Infrastructure;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddMemoryCache();
|
||||
builder.Services.AddSingleton<AdapterRegistry>();
|
||||
builder.Services.AddSingleton<TokenManager>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
@@ -0,0 +1,31 @@
|
||||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:35846",
|
||||
"sslPort": 0
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "http://localhost:5294",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
9
gateway/src/IntegrationGateway.Host/appsettings.json
Normal file
9
gateway/src/IntegrationGateway.Host/appsettings.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
Reference in New Issue
Block a user