Communication Layer (WP-1–5): - 8 message patterns with correlation IDs, per-pattern timeouts - Central/Site communication actors, transport heartbeat config - Connection failure handling (no central buffering, debug streams killed) Data Connection Layer (WP-6–14, WP-34): - Connection actor with Become/Stash lifecycle (Connecting/Connected/Reconnecting) - OPC UA + LmxProxy adapters behind IDataConnection - Auto-reconnect, bad quality propagation, transparent re-subscribe - Write-back, tag path resolution with retry, health reporting - Protocol extensibility via DataConnectionFactory Site Runtime (WP-15–25, WP-32–33): - ScriptActor/ScriptExecutionActor (triggers, concurrent execution, blocking I/O dispatcher) - AlarmActor/AlarmExecutionActor (ValueMatch/RangeViolation/RateOfChange, in-memory state) - SharedScriptLibrary (inline execution), ScriptRuntimeContext (API) - ScriptCompilationService (Roslyn, forbidden API enforcement, execution timeout) - Recursion limit (default 10), call direction enforcement - SiteStreamManager (per-subscriber bounded buffers, fire-and-forget) - Debug view backend (snapshot + stream), concurrency serialization - Local artifact storage (4 SQLite tables) Health Monitoring (WP-26–28): - SiteHealthCollector (thread-safe counters, connection state) - HealthReportSender (30s interval, monotonic sequence numbers) - CentralHealthAggregator (offline detection 60s, online recovery) Site Event Logging (WP-29–31): - SiteEventLogger (SQLite, 6 event categories, ISO 8601 UTC) - EventLogPurgeService (30-day retention, 1GB cap) - EventLogQueryService (filters, keyword search, keyset pagination) 541 tests pass, zero warnings.
153 lines
6.1 KiB
C#
153 lines
6.1 KiB
C#
using Akka.Actor;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using ScadaLink.Commons.Messages.Artifacts;
|
|
using ScadaLink.Commons.Messages.DebugView;
|
|
using ScadaLink.Commons.Messages.Deployment;
|
|
using ScadaLink.Commons.Messages.Health;
|
|
using ScadaLink.Commons.Messages.Integration;
|
|
using ScadaLink.Commons.Messages.Lifecycle;
|
|
using ScadaLink.Commons.Messages.RemoteQuery;
|
|
|
|
namespace ScadaLink.Communication;
|
|
|
|
/// <summary>
|
|
/// Central-side service that wraps the Akka Ask pattern with per-pattern timeouts.
|
|
/// Provides a typed API for sending messages to sites and awaiting responses.
|
|
/// On connection drop, the ask times out (no central buffering per design).
|
|
/// </summary>
|
|
public class CommunicationService
|
|
{
|
|
private readonly CommunicationOptions _options;
|
|
private readonly ILogger<CommunicationService> _logger;
|
|
private IActorRef? _centralCommunicationActor;
|
|
|
|
public CommunicationService(
|
|
IOptions<CommunicationOptions> options,
|
|
ILogger<CommunicationService> logger)
|
|
{
|
|
_options = options.Value;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the central communication actor reference. Called during actor system startup.
|
|
/// </summary>
|
|
public void SetCommunicationActor(IActorRef centralCommunicationActor)
|
|
{
|
|
_centralCommunicationActor = centralCommunicationActor;
|
|
}
|
|
|
|
private IActorRef GetActor()
|
|
{
|
|
return _centralCommunicationActor
|
|
?? throw new InvalidOperationException("CommunicationService not initialized. CentralCommunicationActor not set.");
|
|
}
|
|
|
|
// ── Pattern 1: Instance Deployment ──
|
|
|
|
public async Task<DeploymentStatusResponse> DeployInstanceAsync(
|
|
string siteId, DeployInstanceCommand command, CancellationToken cancellationToken = default)
|
|
{
|
|
_logger.LogDebug(
|
|
"Sending DeployInstanceCommand to site {SiteId}, instance={Instance}, correlationId={DeploymentId}",
|
|
siteId, command.InstanceUniqueName, command.DeploymentId);
|
|
|
|
var envelope = new SiteEnvelope(siteId, command);
|
|
return await GetActor().Ask<DeploymentStatusResponse>(
|
|
envelope, _options.DeploymentTimeout, cancellationToken);
|
|
}
|
|
|
|
// ── Pattern 2: Lifecycle ──
|
|
|
|
public async Task<InstanceLifecycleResponse> DisableInstanceAsync(
|
|
string siteId, DisableInstanceCommand command, CancellationToken cancellationToken = default)
|
|
{
|
|
var envelope = new SiteEnvelope(siteId, command);
|
|
return await GetActor().Ask<InstanceLifecycleResponse>(
|
|
envelope, _options.LifecycleTimeout, cancellationToken);
|
|
}
|
|
|
|
public async Task<InstanceLifecycleResponse> EnableInstanceAsync(
|
|
string siteId, EnableInstanceCommand command, CancellationToken cancellationToken = default)
|
|
{
|
|
var envelope = new SiteEnvelope(siteId, command);
|
|
return await GetActor().Ask<InstanceLifecycleResponse>(
|
|
envelope, _options.LifecycleTimeout, cancellationToken);
|
|
}
|
|
|
|
public async Task<InstanceLifecycleResponse> DeleteInstanceAsync(
|
|
string siteId, DeleteInstanceCommand command, CancellationToken cancellationToken = default)
|
|
{
|
|
var envelope = new SiteEnvelope(siteId, command);
|
|
return await GetActor().Ask<InstanceLifecycleResponse>(
|
|
envelope, _options.LifecycleTimeout, cancellationToken);
|
|
}
|
|
|
|
// ── Pattern 3: Artifact Deployment ──
|
|
|
|
public async Task<ArtifactDeploymentResponse> DeployArtifactsAsync(
|
|
string siteId, DeployArtifactsCommand command, CancellationToken cancellationToken = default)
|
|
{
|
|
var envelope = new SiteEnvelope(siteId, command);
|
|
return await GetActor().Ask<ArtifactDeploymentResponse>(
|
|
envelope, _options.ArtifactDeploymentTimeout, cancellationToken);
|
|
}
|
|
|
|
// ── Pattern 4: Integration Routing ──
|
|
|
|
public async Task<IntegrationCallResponse> RouteIntegrationCallAsync(
|
|
string siteId, IntegrationCallRequest request, CancellationToken cancellationToken = default)
|
|
{
|
|
var envelope = new SiteEnvelope(siteId, request);
|
|
return await GetActor().Ask<IntegrationCallResponse>(
|
|
envelope, _options.IntegrationTimeout, cancellationToken);
|
|
}
|
|
|
|
// ── Pattern 5: Debug View ──
|
|
|
|
public async Task<DebugViewSnapshot> SubscribeDebugViewAsync(
|
|
string siteId, SubscribeDebugViewRequest request, CancellationToken cancellationToken = default)
|
|
{
|
|
var envelope = new SiteEnvelope(siteId, request);
|
|
return await GetActor().Ask<DebugViewSnapshot>(
|
|
envelope, _options.DebugViewTimeout, cancellationToken);
|
|
}
|
|
|
|
public void UnsubscribeDebugView(string siteId, UnsubscribeDebugViewRequest request)
|
|
{
|
|
// Tell (fire-and-forget) — no response expected
|
|
GetActor().Tell(new SiteEnvelope(siteId, request));
|
|
}
|
|
|
|
// ── Pattern 6: Health Reporting (site→central, Tell) ──
|
|
// Health reports are received by central, not sent. No method needed here.
|
|
|
|
// ── Pattern 7: Remote Queries ──
|
|
|
|
public async Task<EventLogQueryResponse> QueryEventLogsAsync(
|
|
string siteId, EventLogQueryRequest request, CancellationToken cancellationToken = default)
|
|
{
|
|
var envelope = new SiteEnvelope(siteId, request);
|
|
return await GetActor().Ask<EventLogQueryResponse>(
|
|
envelope, _options.QueryTimeout, cancellationToken);
|
|
}
|
|
|
|
public async Task<ParkedMessageQueryResponse> QueryParkedMessagesAsync(
|
|
string siteId, ParkedMessageQueryRequest request, CancellationToken cancellationToken = default)
|
|
{
|
|
var envelope = new SiteEnvelope(siteId, request);
|
|
return await GetActor().Ask<ParkedMessageQueryResponse>(
|
|
envelope, _options.QueryTimeout, cancellationToken);
|
|
}
|
|
|
|
// ── Pattern 8: Heartbeat (site→central, Tell) ──
|
|
// Heartbeats are received by central, not sent. No method needed here.
|
|
}
|
|
|
|
/// <summary>
|
|
/// Envelope that wraps any message with a target site ID for routing.
|
|
/// Used by CentralCommunicationActor to resolve the site actor path.
|
|
/// </summary>
|
|
public record SiteEnvelope(string SiteId, object Message);
|