using System; using System.Collections.Generic; using System.Threading; using System.Threading.Channels; using System.Threading.Tasks; using ClassNotify.Shared.Models; namespace ClassNotify.Server.Services; /// /// 客户端在线升级发起结果 /// public sealed class DeviceUpdateCreationResult { public int StatusCode { get; init; } public string? ErrorCode { get; init; } public string? ErrorMessage { get; init; } public string? ExistingUpdateRunId { get; init; } public DeviceUpdateStatusDto? Dto { get; init; } public static DeviceUpdateCreationResult Accepted(DeviceUpdateStatusDto dto) => new() { StatusCode = 202, Dto = dto }; public static DeviceUpdateCreationResult Conflict(string errorCode, string existingRunId, string message) => new() { StatusCode = 409, ErrorCode = errorCode, ExistingUpdateRunId = existingRunId, ErrorMessage = message }; public static DeviceUpdateCreationResult Forbidden(string message) => new() { StatusCode = 403, ErrorCode = UpdateErrorCodes.Forbidden, ErrorMessage = message }; public static DeviceUpdateCreationResult NotFound(string message) => new() { StatusCode = 404, ErrorCode = UpdateErrorCodes.DeviceNotFound, ErrorMessage = message }; public static DeviceUpdateCreationResult BadRequest(string errorCode, string message) => new() { StatusCode = 400, ErrorCode = errorCode, ErrorMessage = message }; } /// /// 客户端在线升级状态查询结果 /// public sealed class DeviceUpdateQueryResult { public int StatusCode { get; init; } public string? ErrorCode { get; init; } public string? ErrorMessage { get; init; } public T? Data { get; init; } public static DeviceUpdateQueryResult Ok(T data) => new() { StatusCode = 200, Data = data }; public static DeviceUpdateQueryResult Forbidden(string message) => new() { StatusCode = 403, ErrorCode = UpdateErrorCodes.Forbidden, ErrorMessage = message }; public static DeviceUpdateQueryResult NotFound(string message) => new() { StatusCode = 404, ErrorCode = UpdateErrorCodes.ReleaseNotFound, ErrorMessage = message }; } /// /// 升级包下载准备结果 /// public sealed class DeviceUpdateDownloadResult { public int StatusCode { get; init; } public string? ErrorCode { get; init; } public string? ErrorMessage { get; init; } public string? PhysicalPath { get; init; } public string? DownloadFileName { get; init; } public string? Sha256 { get; init; } public long SizeBytes { get; init; } public static DeviceUpdateDownloadResult Ok(string physicalPath, string downloadFileName, string sha256, long sizeBytes) => new() { StatusCode = 200, PhysicalPath = physicalPath, DownloadFileName = downloadFileName, Sha256 = sha256, SizeBytes = sizeBytes }; public static DeviceUpdateDownloadResult Forbidden(string message) => new() { StatusCode = 403, ErrorCode = UpdateErrorCodes.Forbidden, ErrorMessage = message }; public static DeviceUpdateDownloadResult NotFound(string message) => new() { StatusCode = 404, ErrorCode = UpdateErrorCodes.ReleaseNotFound, ErrorMessage = message }; public static DeviceUpdateDownloadResult BadRequest(string errorCode, string message) => new() { StatusCode = 400, ErrorCode = errorCode, ErrorMessage = message }; public static DeviceUpdateDownloadResult ServerError(string errorCode, string message) => new() { StatusCode = 500, ErrorCode = errorCode, ErrorMessage = message }; } /// /// 升级指令调度唤醒通知器接口 /// public interface IDeviceUpdateDispatchNotifier { void Notify(); Task WaitAsync(CancellationToken ct); } /// /// 升级指令调度唤醒通知器实现 /// public sealed class DeviceUpdateDispatchNotifier : IDeviceUpdateDispatchNotifier { private readonly Channel _channel = Channel.CreateBounded(new BoundedChannelOptions(1) { FullMode = BoundedChannelFullMode.DropOldest }); public void Notify() { _channel.Writer.TryWrite(1); } public async Task WaitAsync(CancellationToken ct) { await _channel.Reader.WaitToReadAsync(ct); while (_channel.Reader.TryRead(out _)) { } } } /// /// 终端上报状态流转结果 /// public sealed class DeviceUpdateReportResult { public int StatusCode { get; init; } public string? ErrorCode { get; init; } public string? ErrorMessage { get; init; } public static DeviceUpdateReportResult Ok() => new() { StatusCode = 200 }; public static DeviceUpdateReportResult NotFound(string message) => new() { StatusCode = 404, ErrorCode = UpdateErrorCodes.DeviceNotFound, ErrorMessage = message }; public static DeviceUpdateReportResult Conflict(string errorCode, string message) => new() { StatusCode = 409, ErrorCode = errorCode, ErrorMessage = message }; } /// /// 客户端在线升级管控服务契约 (P2.5 RC1-4) /// public interface IDeviceUpdateService { /// /// SuperAdmin 触发指定设备升级 (U3: SuperAdmin-only, U5: Single active update) /// Task CreateUpdateRunAsync( string deviceId, CreateDeviceUpdateRequest request, string actorUser, string actorRole, string? actorTenantId, string clientIp, CancellationToken ct); /// /// 查询指定单次升级任务详情与状态 /// Task> GetDeviceUpdateRunAsync( string updateRunId, string actorRole, CancellationToken ct); /// /// 查询指定设备的历史升级任务记录 (支持刷新恢复) /// Task>> GetDeviceUpdateHistoryAsync( string deviceId, int take, string actorRole, CancellationToken ct); /// /// 终端鉴权安全下载指定 Release 安装包 (U1, U2, 路径防逃逸) /// Task PreparePackageDownloadAsync( string releaseId, string deviceId, CancellationToken ct); /// /// 终端上报升级任务阶段与执行结果 (严格状态机合法性校验,阻断终态复活与非法跳跃) /// Task ReportDeviceUpdateStatusAsync( string deviceId, string updateRunId, DeviceUpdateReportRequest request, CancellationToken ct); }