Initial_commit_SecMPS_v2

This commit is contained in:
2026-05-15 23:22:48 +08:00
commit 23ea4fe05f
13830 changed files with 298675 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -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>

View File

@@ -0,0 +1,6 @@
@IntegrationGateway.Host_HostAddress = http://localhost:5294
GET {{IntegrationGateway.Host_HostAddress}}/weatherforecast/
Accept: application/json
###

View 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();

View File

@@ -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"
}
}
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}