using Akka.Actor; using Akka.Cluster.Tools.PublishSubscribe; using Akka.Event; using Microsoft.AspNetCore.SignalR; using ZB.MOM.WW.OtOpcUa.Commons.Messages.Alerts; namespace ZB.MOM.WW.OtOpcUa.AdminUI.Hubs; /// /// Akka actor that subscribes to the alerts DistributedPubSub topic and forwards each /// to every SignalR client connected to . /// Mirrors FleetStatusSignalRBridge's design — one bridge per admin node, hub fan-out is /// per-node, no cluster-singleton needed. /// public sealed class AlertSignalRBridge : ReceiveActor { public const string TopicName = "alerts"; private readonly IHubContext _hub; private readonly IInProcessBroadcaster _broadcaster; private readonly ILoggingAdapter _log = Context.GetLogger(); /// /// Creates actor props for the AlertSignalRBridge. /// /// The SignalR hub context to send alerts to. /// In-process fan-out read directly by the Blazor Server Alerts page. public static Props Props(IHubContext hub, IInProcessBroadcaster broadcaster) => Akka.Actor.Props.Create(() => new AlertSignalRBridge(hub, broadcaster)); /// /// Initializes a new instance of the AlertSignalRBridge actor. /// /// The SignalR hub context to send alerts to. /// In-process fan-out read directly by the Blazor Server Alerts page. public AlertSignalRBridge(IHubContext hub, IInProcessBroadcaster broadcaster) { _hub = hub; _broadcaster = broadcaster; ReceiveAsync(ForwardAsync); // DPS subscription is now live — mark the feed connected so the Blazor "live" pill lights up. Receive(_ => _broadcaster.SetConnected(true)); } /// protected override void PreStart() => DistributedPubSub.Get(Context.System).Mediator.Tell(new Subscribe(TopicName, Self)); /// protected override void PostStop() { // Bridge stopping — the feed is no longer live, drop the "live" pill. _broadcaster.SetConnected(false); base.PostStop(); } private async Task ForwardAsync(AlarmTransitionEvent msg) { // In-process fan-out first — this is what the Blazor Server Alerts page reads. The hub push // is kept for any out-of-process (e.g. WASM) SignalR client. _broadcaster.Publish(msg); try { await _hub.Clients.All.SendAsync(AlertHub.MethodName, msg); } catch (Exception ex) { _log.Warning(ex, "AlertSignalRBridge: SignalR push failed for {AlarmId}", msg.AlarmId); } } }