using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using WIDESEAWCS_S7Simulator.Core.Entities;
namespace WIDESEAWCS_S7Simulator.Web.Pages;
///
/// 实例详情页
///
public class DetailsModel : PageModel
{
private readonly ILogger _logger;
private readonly HttpClient _httpClient;
public DetailsModel(ILogger logger, IHttpClientFactory httpClientFactory)
{
_logger = logger;
_httpClient = httpClientFactory.CreateClient();
_httpClient.BaseAddress = new Uri($"{Request.Scheme}://{Request.Host}");
}
public InstanceState? Instance { get; private set; }
public List Clients { get; private set; } = new();
public async Task OnGetAsync(string id)
{
if (string.IsNullOrWhiteSpace(id))
{
return BadRequest("实例ID不能为空");
}
try
{
var response = await _httpClient.GetAsync($"/api/SimulatorInstances/{Uri.EscapeDataString(id)}");
if (response.IsSuccessStatusCode)
{
Instance = await response.Content.ReadFromJsonAsync();
// Load clients
var clientsResponse = await _httpClient.GetAsync($"/api/instances/{Uri.EscapeDataString(id)}/Clients");
if (clientsResponse.IsSuccessStatusCode)
{
Clients = await clientsResponse.Content.ReadFromJsonAsync>() ?? new();
}
}
else if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
{
return NotFound($"实例 \"{id}\" 不存在");
}
}
catch (HttpRequestException ex)
{
_logger.LogError(ex, "Failed to load instance details for {InstanceId}", id);
ModelState.AddModelError(string.Empty, "加载实例详情失败,请稍后重试");
}
return Page();
}
}