| | |
| | | using Microsoft.AspNetCore.Mvc; |
| | | using Microsoft.AspNetCore.Mvc.RazorPages; |
| | | using WIDESEAWCS_S7Simulator.Core.Entities; |
| | | using WIDESEAWCS_S7Simulator.Web.Services; |
| | | |
| | | namespace WIDESEAWCS_S7Simulator.Web.Pages; |
| | | |
| | | /// <summary> |
| | | /// 实例详情页 |
| | | /// </summary> |
| | | public class DetailsModel : PageModel |
| | | public class DetailsModel : BasePageModel |
| | | { |
| | | private readonly ILogger<DetailsModel> _logger; |
| | | private readonly IHttpClientFactory _httpClientFactory; |
| | | private readonly ApiHttpClient _apiClient; |
| | | |
| | | public DetailsModel(ILogger<DetailsModel> logger, IHttpClientFactory httpClientFactory) |
| | | public DetailsModel(ILogger<DetailsModel> logger, IConfiguration configuration, ApiHttpClient apiClient) |
| | | : base(configuration) |
| | | { |
| | | _logger = logger; |
| | | _httpClientFactory = httpClientFactory; |
| | | _apiClient = apiClient; |
| | | } |
| | | |
| | | public InstanceState? Instance { get; private set; } |
| | |
| | | |
| | | try |
| | | { |
| | | var httpClient = _httpClientFactory.CreateClient(); |
| | | httpClient.BaseAddress = new Uri($"{Request.Scheme}://{Request.Host}"); |
| | | Instance = await _apiClient.GetInstanceAsync(id); |
| | | |
| | | var response = await httpClient.GetAsync($"/api/SimulatorInstances/{Uri.EscapeDataString(id)}"); |
| | | |
| | | if (response.IsSuccessStatusCode) |
| | | { |
| | | Instance = await response.Content.ReadFromJsonAsync<InstanceState>(); |
| | | |
| | | // Load clients |
| | | var clientsResponse = await httpClient.GetAsync($"/api/instances/{Uri.EscapeDataString(id)}/Clients"); |
| | | if (clientsResponse.IsSuccessStatusCode) |
| | | { |
| | | Clients = await clientsResponse.Content.ReadFromJsonAsync<List<S7ClientConnection>>() ?? new(); |
| | | } |
| | | } |
| | | else if (response.StatusCode == System.Net.HttpStatusCode.NotFound) |
| | | if (Instance == null) |
| | | { |
| | | return NotFound($"实例 \"{id}\" 不存在"); |
| | | } |
| | | |
| | | // 客户端信息已包含在 InstanceState 中 |
| | | if (Instance.Clients != null) |
| | | { |
| | | Clients = Instance.Clients; |
| | | } |
| | | } |
| | | catch (HttpRequestException ex) |
| | | { |