using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.SignalR; using ZB.MOM.WW.OtOpcUa.Commons.Messages.Drivers; namespace ZB.MOM.WW.OtOpcUa.AdminUI.Hubs; /// /// SignalR hub clients subscribe to for live driver-health updates. Clients invoke /// once per page-load to receive the current snapshot + future /// state transitions for one driver instance. /// [Authorize] public sealed class DriverStatusHub : Hub { public const string Endpoint = "/hubs/driverstatus"; /// SignalR method name used to push snapshots to clients. public const string MethodName = "status"; private readonly IDriverStatusSnapshotStore _store; /// Initializes a new instance of . /// In-memory snapshot store supplying current state on join. public DriverStatusHub(IDriverStatusSnapshotStore store) => _store = store; /// /// Adds the calling connection to a per-driver-instance group. Immediately replies with /// the current snapshot from the in-memory store so the client renders state without /// waiting for the next change event. /// /// Identifier of the driver instance to join. /// A task that represents the asynchronous operation. public async Task JoinDriver(string driverInstanceId) { var groupName = GroupName(driverInstanceId); await Groups.AddToGroupAsync(Context.ConnectionId, groupName); if (_store.TryGet(driverInstanceId, out var snapshot)) { await Clients.Caller.SendAsync(MethodName, snapshot); } } /// Builds the SignalR group name for a given driver instance. /// Identifier of the driver instance. /// The SignalR group name for the driver instance. public static string GroupName(string driverInstanceId) => $"driver:{driverInstanceId}"; }