using ClassNotify.Server.Hubs; using ClassNotify.Shared.Contracts; using Microsoft.AspNetCore.SignalR; namespace ClassNotify.Server.Services; public interface IDeviceNotificationService { Task NotifyScreenEnterPairingModeAsync(Guid deviceId, string pairingCode, int ttlSeconds); Task NotifyScreenClaimedAsync(Guid deviceId, string classDisplayName, int teacherCount); } public class DeviceNotificationService : IDeviceNotificationService { private readonly IHubContext _deviceHub; private readonly ILogger _logger; public DeviceNotificationService(IHubContext deviceHub, ILogger logger) { _deviceHub = deviceHub; _logger = logger; } public async Task NotifyScreenEnterPairingModeAsync(Guid deviceId, string pairingCode, int ttlSeconds) { _logger.LogInformation("Notifying device {DeviceId} to enter pairing mode (Code: {Code}, TTL: {Ttl}s)", deviceId, pairingCode, ttlSeconds); // Device group convention in DeviceHub: "d:{deviceId}" await _deviceHub.Clients.Group($"d:{deviceId}").ReceivePairingMode(pairingCode, ttlSeconds); // Also broadcast by device string ID await _deviceHub.Clients.Group(deviceId.ToString()).ReceivePairingMode(pairingCode, ttlSeconds); } public async Task NotifyScreenClaimedAsync(Guid deviceId, string classDisplayName, int teacherCount) { _logger.LogInformation("Notifying device {DeviceId} binding updated: class={Class}, teachers={Teachers}", deviceId, classDisplayName, teacherCount); await _deviceHub.Clients.Group($"d:{deviceId}").DeviceBindingUpdated(classDisplayName, teacherCount); await _deviceHub.Clients.Group(deviceId.ToString()).DeviceBindingUpdated(classDisplayName, teacherCount); } }