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.
143 lines
5.1 KiB
C#
143 lines
5.1 KiB
C#
using Akka.Actor;
|
|
using Akka.Event;
|
|
using ScadaLink.Commons.Interfaces.Protocol;
|
|
using ScadaLink.Commons.Messages.DataConnection;
|
|
|
|
namespace ScadaLink.DataConnectionLayer.Actors;
|
|
|
|
/// <summary>
|
|
/// WP-34: Protocol extensibility — manages DataConnectionActor instances.
|
|
/// Routes messages to the correct connection actor based on connection name.
|
|
/// Adding a new protocol = implement IDataConnection + register with IDataConnectionFactory.
|
|
/// </summary>
|
|
public class DataConnectionManagerActor : ReceiveActor
|
|
{
|
|
private readonly ILoggingAdapter _log = Context.GetLogger();
|
|
private readonly IDataConnectionFactory _factory;
|
|
private readonly DataConnectionOptions _options;
|
|
private readonly Dictionary<string, IActorRef> _connectionActors = new();
|
|
|
|
public DataConnectionManagerActor(
|
|
IDataConnectionFactory factory,
|
|
DataConnectionOptions options)
|
|
{
|
|
_factory = factory;
|
|
_options = options;
|
|
|
|
Receive<CreateConnectionCommand>(HandleCreateConnection);
|
|
Receive<SubscribeTagsRequest>(HandleRoute);
|
|
Receive<UnsubscribeTagsRequest>(HandleRoute);
|
|
Receive<WriteTagRequest>(HandleRouteWrite);
|
|
Receive<RemoveConnectionCommand>(HandleRemoveConnection);
|
|
Receive<GetAllHealthReports>(HandleGetAllHealthReports);
|
|
}
|
|
|
|
private void HandleCreateConnection(CreateConnectionCommand command)
|
|
{
|
|
if (_connectionActors.ContainsKey(command.ConnectionName))
|
|
{
|
|
_log.Warning("Connection {0} already exists", command.ConnectionName);
|
|
return;
|
|
}
|
|
|
|
// WP-34: Factory creates the correct adapter based on protocol type
|
|
var adapter = _factory.Create(command.ProtocolType, command.ConnectionDetails);
|
|
|
|
var props = Props.Create(() => new DataConnectionActor(
|
|
command.ConnectionName, adapter, _options));
|
|
|
|
var actorRef = Context.ActorOf(props, command.ConnectionName);
|
|
_connectionActors[command.ConnectionName] = actorRef;
|
|
|
|
_log.Info("Created DataConnectionActor for {0} (protocol={1})",
|
|
command.ConnectionName, command.ProtocolType);
|
|
}
|
|
|
|
private void HandleRoute(SubscribeTagsRequest request)
|
|
{
|
|
if (_connectionActors.TryGetValue(request.ConnectionName, out var actor))
|
|
actor.Forward(request);
|
|
else
|
|
{
|
|
_log.Warning("No connection actor for {0}", request.ConnectionName);
|
|
Sender.Tell(new SubscribeTagsResponse(
|
|
request.CorrelationId, request.InstanceUniqueName, false,
|
|
$"Unknown connection: {request.ConnectionName}", DateTimeOffset.UtcNow));
|
|
}
|
|
}
|
|
|
|
private void HandleRoute(UnsubscribeTagsRequest request)
|
|
{
|
|
if (_connectionActors.TryGetValue(request.ConnectionName, out var actor))
|
|
actor.Forward(request);
|
|
else
|
|
_log.Warning("No connection actor for {0} during unsubscribe", request.ConnectionName);
|
|
}
|
|
|
|
private void HandleRouteWrite(WriteTagRequest request)
|
|
{
|
|
if (_connectionActors.TryGetValue(request.ConnectionName, out var actor))
|
|
actor.Forward(request);
|
|
else
|
|
{
|
|
_log.Warning("No connection actor for {0}", request.ConnectionName);
|
|
Sender.Tell(new WriteTagResponse(
|
|
request.CorrelationId, false,
|
|
$"Unknown connection: {request.ConnectionName}", DateTimeOffset.UtcNow));
|
|
}
|
|
}
|
|
|
|
private void HandleRemoveConnection(RemoveConnectionCommand command)
|
|
{
|
|
if (_connectionActors.TryGetValue(command.ConnectionName, out var actor))
|
|
{
|
|
Context.Stop(actor);
|
|
_connectionActors.Remove(command.ConnectionName);
|
|
_log.Info("Removed DataConnectionActor for {0}", command.ConnectionName);
|
|
}
|
|
}
|
|
|
|
private void HandleGetAllHealthReports(GetAllHealthReports _)
|
|
{
|
|
// Forward health report requests to all connection actors
|
|
foreach (var actor in _connectionActors.Values)
|
|
{
|
|
actor.Forward(new DataConnectionActor.GetHealthReport());
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// OneForOneStrategy with Restart for connection actors — a failed connection
|
|
/// should restart and attempt reconnection.
|
|
/// </summary>
|
|
protected override SupervisorStrategy SupervisorStrategy()
|
|
{
|
|
return new OneForOneStrategy(
|
|
maxNrOfRetries: 10,
|
|
withinTimeRange: TimeSpan.FromMinutes(1),
|
|
decider: Decider.From(ex =>
|
|
{
|
|
_log.Warning(ex, "DataConnectionActor threw exception, restarting");
|
|
return Directive.Restart;
|
|
}));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Command to create a new data connection actor for a specific protocol.
|
|
/// </summary>
|
|
public record CreateConnectionCommand(
|
|
string ConnectionName,
|
|
string ProtocolType,
|
|
IDictionary<string, string> ConnectionDetails);
|
|
|
|
/// <summary>
|
|
/// Command to remove a data connection actor.
|
|
/// </summary>
|
|
public record RemoveConnectionCommand(string ConnectionName);
|
|
|
|
/// <summary>
|
|
/// Request for health reports from all active connections.
|
|
/// </summary>
|
|
public record GetAllHealthReports;
|