| | |
| | | using System.ComponentModel.DataAnnotations; |
| | | using WIDESEAWCS_S7Simulator.Core.Entities; |
| | | using WIDESEAWCS_S7Simulator.Core.Enums; |
| | | using WIDESEAWCS_S7Simulator.Web.Services; |
| | | |
| | | namespace WIDESEAWCS_S7Simulator.Web.Pages; |
| | | |
| | | /// <summary> |
| | | /// 编辑实例页 |
| | | /// </summary> |
| | | public class EditModel : PageModel |
| | | public class EditModel : BasePageModel |
| | | { |
| | | private readonly ILogger<EditModel> _logger; |
| | | private readonly IHttpClientFactory _httpClientFactory; |
| | | private readonly ApiHttpClient _apiClient; |
| | | |
| | | public EditModel(ILogger<EditModel> logger, IHttpClientFactory httpClientFactory) |
| | | public EditModel(ILogger<EditModel> logger, IConfiguration configuration, ApiHttpClient apiClient) |
| | | : base(configuration) |
| | | { |
| | | _logger = logger; |
| | | _httpClientFactory = httpClientFactory; |
| | | _apiClient = apiClient; |
| | | } |
| | | |
| | | [BindProperty] |
| | |
| | | |
| | | try |
| | | { |
| | | var httpClient = _httpClientFactory.CreateClient(); |
| | | httpClient.BaseAddress = new Uri($"{Request.Scheme}://{Request.Host}"); |
| | | // 获取实例状态 |
| | | CurrentInstance = await _apiClient.GetInstanceAsync(id); |
| | | |
| | | var response = await httpClient.GetAsync($"/api/SimulatorInstances/{Uri.EscapeDataString(id)}"); |
| | | |
| | | if (response.IsSuccessStatusCode) |
| | | if (CurrentInstance != null) |
| | | { |
| | | CurrentInstance = await response.Content.ReadFromJsonAsync<InstanceState>(); |
| | | IsRunning = CurrentInstance?.Status == InstanceStatus.Running; |
| | | IsRunning = CurrentInstance.Status == InstanceStatus.Running; |
| | | |
| | | // Load existing config |
| | | var configResponse = await httpClient.GetAsync($"/api/SimulatorInstances"); |
| | | if (configResponse.IsSuccessStatusCode) |
| | | // 获取实例配置 |
| | | var config = await _apiClient.GetInstanceConfigAsync(id); |
| | | if (config != null) |
| | | { |
| | | var allInstances = await configResponse.Content.ReadFromJsonAsync<List<InstanceState>>(); |
| | | // For now, we'll initialize with defaults. In production, you'd have a separate endpoint to get instance config |
| | | } |
| | | Input.Id = config.Id; |
| | | Input.Name = config.Name; |
| | | Input.PLCType = config.PLCType; |
| | | Input.Port = config.Port; |
| | | Input.ActivationKey = config.ActivationKey; |
| | | Input.AutoStart = config.AutoStart; |
| | | |
| | | // Initialize input with current values (default for now) |
| | | Input.Id = id; |
| | | Input.Name = CurrentInstance?.InstanceId ?? id; |
| | | Input.Port = 102; // Default |
| | | // 内存配置 |
| | | Input.MRegionSize = config.MemoryConfig.MRegionSize; |
| | | Input.DBBlockCount = config.MemoryConfig.DBBlockCount; |
| | | Input.DBBlockSize = config.MemoryConfig.DBBlockSize; |
| | | Input.IRegionSize = config.MemoryConfig.IRegionSize; |
| | | Input.QRegionSize = config.MemoryConfig.QRegionSize; |
| | | Input.TRegionCount = config.MemoryConfig.TRegionCount; |
| | | Input.CRegionCount = config.MemoryConfig.CRegionCount; |
| | | } |
| | | } |
| | | else if (response.StatusCode == System.Net.HttpStatusCode.NotFound) |
| | | else |
| | | { |
| | | return NotFound($"实例 \"{id}\" 不存在"); |
| | | } |
| | |
| | | { |
| | | if (!ModelState.IsValid) |
| | | { |
| | | // Reload instance state |
| | | await LoadInstanceStateAsync(Input.Id); |
| | | return Page(); |
| | | } |
| | | |
| | | try |
| | | { |
| | | // 创建HttpClient并设置BaseAddress |
| | | var httpClient = _httpClientFactory.CreateClient(); |
| | | httpClient.BaseAddress = new Uri($"{Request.Scheme}://{Request.Host}"); |
| | | |
| | | var config = new InstanceConfig |
| | | { |
| | | Id = Input.Id, |
| | |
| | | } |
| | | }; |
| | | |
| | | var response = await httpClient.PutAsJsonAsync($"/api/SimulatorInstances/{Uri.EscapeDataString(Input.Id)}", config); |
| | | var result = await _apiClient.UpdateInstanceAsync(Input.Id, config); |
| | | |
| | | if (response.IsSuccessStatusCode) |
| | | if (result != null) |
| | | { |
| | | _logger.LogInformation("Instance {InstanceId} updated successfully", Input.Id); |
| | | TempData["SuccessMessage"] = $"实例 \"{Input.Id}\" 更新成功!"; |
| | | return RedirectToPage("/Index"); |
| | | } |
| | | else if (response.StatusCode == System.Net.HttpStatusCode.NotFound) |
| | | { |
| | | ModelState.AddModelError(string.Empty, "实例不存在"); |
| | | } |
| | | else |
| | | { |
| | | var error = await response.Content.ReadFromJsonAsync<object>(); |
| | | ModelState.AddModelError(string.Empty, error?.ToString() ?? "更新实例失败"); |
| | | ModelState.AddModelError(string.Empty, "更新实例失败"); |
| | | } |
| | | } |
| | | catch (HttpRequestException ex) |
| | |
| | | { |
| | | try |
| | | { |
| | | var httpClient = _httpClientFactory.CreateClient(); |
| | | httpClient.BaseAddress = new Uri($"{Request.Scheme}://{Request.Host}"); |
| | | |
| | | var response = await httpClient.GetAsync($"/api/SimulatorInstances/{Uri.EscapeDataString(id)}"); |
| | | if (response.IsSuccessStatusCode) |
| | | CurrentInstance = await _apiClient.GetInstanceAsync(id); |
| | | if (CurrentInstance != null) |
| | | { |
| | | CurrentInstance = await response.Content.ReadFromJsonAsync<InstanceState>(); |
| | | IsRunning = CurrentInstance?.Status == InstanceStatus.Running; |
| | | IsRunning = CurrentInstance.Status == InstanceStatus.Running; |
| | | } |
| | | } |
| | | catch |