wanshenmean
2026-03-13 58376d519aeb76ef78d38b737c0c57f8d982fb52
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using WIDESEAWCS_S7Simulator.Core.Entities;
 
namespace WIDESEAWCS_S7Simulator.Web.Pages;
 
/// <summary>
/// 实例详情页
/// </summary>
public class DetailsModel : PageModel
{
    private readonly ILogger<DetailsModel> _logger;
    private readonly HttpClient _httpClient;
 
    public DetailsModel(ILogger<DetailsModel> logger, IHttpClientFactory httpClientFactory)
    {
        _logger = logger;
        _httpClient = httpClientFactory.CreateClient();
        _httpClient.BaseAddress = new Uri($"{Request.Scheme}://{Request.Host}");
    }
 
    public InstanceState? Instance { get; private set; }
 
    public List<S7ClientConnection> Clients { get; private set; } = new();
 
    public async Task<IActionResult> 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<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)
            {
                return NotFound($"实例 \"{id}\" 不存在");
            }
        }
        catch (HttpRequestException ex)
        {
            _logger.LogError(ex, "Failed to load instance details for {InstanceId}", id);
            ModelState.AddModelError(string.Empty, "加载实例详情失败,请稍后重试");
        }
 
        return Page();
    }
}