145 lines
4.6 KiB
C#
145 lines
4.6 KiB
C#
using Azure;
|
|
using FastEndpoints;
|
|
using MoviconWebApi.Common;
|
|
|
|
namespace MoviconWebApi.API.DeviceStateApi
|
|
{
|
|
/// <summary>
|
|
/// 获取设备状态列表
|
|
/// </summary>
|
|
public class GetListEndpoint : Endpoint<DeviceStateRequest, ApiResponse<List<DeviceStateResponse>>>
|
|
{
|
|
private readonly IFreeSql _db;
|
|
|
|
public GetListEndpoint(IFreeSql db)
|
|
{
|
|
_db = db;
|
|
}
|
|
|
|
public override void Configure()
|
|
{
|
|
Get("/devicestate/list");
|
|
AllowAnonymous();
|
|
Summary(s =>
|
|
{
|
|
s.Summary = "获取设备状态列表";
|
|
s.Description = "根据设备编号和时间范围查询设备状态数据";
|
|
s.Response<ApiResponse<List<DeviceStateResponse>>>(200, "成功返回设备状态数据列表");
|
|
});
|
|
}
|
|
|
|
public override async Task HandleAsync(DeviceStateRequest req, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
var data = await Data.GetDeviceStateListAsync(_db, req);
|
|
Response = ApiResponse<List<DeviceStateResponse>>.Success(data, "success");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Response = ApiResponse<List<DeviceStateResponse>>.Error(
|
|
"500",
|
|
$"查询失败: {ex.Message}",
|
|
new List<DeviceStateResponse>());
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 分页获取设备状态
|
|
/// </summary>
|
|
public class GetPagedEndpoint : Endpoint<DeviceStatePagedRequest, ApiResponse<DeviceStatePagedResponse>>
|
|
{
|
|
private readonly IFreeSql _db;
|
|
|
|
public GetPagedEndpoint(IFreeSql db)
|
|
{
|
|
_db = db;
|
|
}
|
|
|
|
public override void Configure()
|
|
{
|
|
Post("/devicestate/paged");
|
|
AllowAnonymous();
|
|
Summary(s =>
|
|
{
|
|
s.Summary = "分页获取设备状态";
|
|
s.Description = "分页查询设备状态数据,支持设备编号和时间范围筛选";
|
|
s.Response<ApiResponse<DeviceStatePagedResponse>>(200, "成功返回分页数据");
|
|
});
|
|
}
|
|
|
|
public override async Task HandleAsync(DeviceStatePagedRequest req, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
var result = await Data.GetDeviceStatePagedAsync(_db, req);
|
|
var items = result.Item1;
|
|
var total = result.Item2;
|
|
var pagedResponse = new DeviceStatePagedResponse
|
|
{
|
|
Items = items,
|
|
Total = total
|
|
};
|
|
Response = ApiResponse<DeviceStatePagedResponse>.Success(pagedResponse, "success");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Response = ApiResponse<DeviceStatePagedResponse>.Error(
|
|
"500",
|
|
$"查询失败: {ex.Message}",
|
|
new DeviceStatePagedResponse());
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取最新的设备状态记录
|
|
/// </summary>
|
|
public class GetLatestEndpoint : Endpoint<DeviceStateRequest, ApiResponse<DeviceStateResponse>>
|
|
{
|
|
private readonly IFreeSql _db;
|
|
|
|
public GetLatestEndpoint(IFreeSql db)
|
|
{
|
|
_db = db;
|
|
}
|
|
|
|
public override void Configure()
|
|
{
|
|
Get("/devicestate/latest");
|
|
AllowAnonymous();
|
|
Summary(s =>
|
|
{
|
|
s.Summary = "获取最新的设备状态记录";
|
|
s.Description = "获取指定设备的最新状态记录";
|
|
s.Response<ApiResponse<DeviceStateResponse>>(200, "成功返回最新记录");
|
|
s.Response<ApiResponse<DeviceStateResponse>>(404, "未找到记录");
|
|
});
|
|
}
|
|
|
|
public override async Task HandleAsync(DeviceStateRequest req, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
var data = await Data.GetLatestDeviceStateAsync(_db, req);
|
|
if (data == null)
|
|
{
|
|
Response = ApiResponse<DeviceStateResponse>.Error("404", "未找到数据", null);
|
|
}
|
|
else
|
|
{
|
|
Response = ApiResponse<DeviceStateResponse>.Success(data, "success");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Response = ApiResponse<DeviceStateResponse>.Error(
|
|
"500",
|
|
$"查询失败: {ex.Message}",
|
|
null);
|
|
}
|
|
}
|
|
}
|
|
}
|