Files
SE-UGW/src/Gateway.Api/Services/HostNetworkService.cs
T
jhartworks 98c48d9d78 Add comprehensive smoke and feature tests for Gateway API
- Introduced LtdFeatureTests to validate BACnet and OPC UA point updates, trend logging, and alarm rule functionality.
- Added OpcUaClientTests to ensure proper OPC UA client configuration, point value synchronization, and session management.
- Created OpcUaFeatureTests to test OPC UA server capabilities, including browsing, reading, writing, and subscription notifications.
- Implemented native_bacnet_test.py for isolated BACnet wire testing, covering discovery, read/write operations, and command processing.
- Added ui-theme-tabs.test.cjs to verify theme switching, tab interactions, and protocol form accessibility in the UI.
2026-11-19 16:06:54 +01:00

40 lines
1.9 KiB
C#

using System.Net.Sockets;
using System.Text.Json;
namespace Gateway.Api.Services;
public sealed record HostNetworkProfile(string Interface, string Mode, string? Address, int? PrefixLength,
string? Gateway, string[]? Dns, int? Metric);
public sealed record HostNetworkConfirmation(string Id);
public sealed class HostNetworkService
{
private const string SocketPath = "/run/seiugw-network/control.sock";
private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web);
public async Task<IResult> SendAsync(object request, CancellationToken cancellationToken)
{
if (!OperatingSystem.IsLinux())
return Results.Json(new { error = "Die Netzwerkkonfiguration benötigt den NixOS-Netzwerkdienst." }, statusCode: 503);
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
timeout.CancelAfter(TimeSpan.FromSeconds(8));
try
{
using var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
await socket.ConnectAsync(new UnixDomainSocketEndPoint(SocketPath), timeout.Token);
using var stream = new NetworkStream(socket);
await stream.WriteAsync(JsonSerializer.SerializeToUtf8Bytes(request, JsonOptions), timeout.Token);
socket.Shutdown(SocketShutdown.Send);
using var response = await JsonDocument.ParseAsync(stream, cancellationToken: timeout.Token);
var result = response.RootElement;
return Results.Json(result.Clone(), statusCode: result.TryGetProperty("error", out _) ? 400 : 200);
}
catch (Exception ex) when (ex is SocketException or IOException or JsonException ||
ex is OperationCanceledException && !cancellationToken.IsCancellationRequested)
{
return Results.Json(new { error = "Der NixOS-Netzwerkdienst ist gerade nicht erreichbar." }, statusCode: 503);
}
}
}