diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Hubs/DriverStatusHub.cs b/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Hubs/DriverStatusHub.cs new file mode 100644 index 00000000..88a70ee7 --- /dev/null +++ b/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Hubs/DriverStatusHub.cs @@ -0,0 +1,44 @@ +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. + /// + 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. + public static string GroupName(string driverInstanceId) => $"driver:{driverInstanceId}"; +} diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Hubs/HubRouteBuilderExtensions.cs b/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Hubs/HubRouteBuilderExtensions.cs index 86ea00ff..f30b5a55 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Hubs/HubRouteBuilderExtensions.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Hubs/HubRouteBuilderExtensions.cs @@ -15,6 +15,7 @@ public static class HubRouteBuilderExtensions app.MapHub(FleetStatusHub.Endpoint); app.MapHub(AlertHub.Endpoint); app.MapHub(ScriptLogHub.Endpoint); + app.MapHub(DriverStatusHub.Endpoint); return app; } }