using System.Text.Json; using Microsoft.AspNetCore.Http; using WIDESEAWCS_S7Simulator.Core.Entities; namespace WIDESEAWCS_S7Simulator.Web.Services { /// /// API HTTP 客户端辅助类 /// public class ApiHttpClient { private readonly HttpClient _httpClient; private readonly string _apiBaseUrl; private readonly JsonSerializerOptions _jsonOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, PropertyNameCaseInsensitive = true }; public ApiHttpClient(string apiBaseUrl, HttpClient httpClient) { _apiBaseUrl = apiBaseUrl.TrimEnd('/'); _httpClient = httpClient; _httpClient.BaseAddress = new Uri(_apiBaseUrl); } /// /// 获取所有实例 /// public async Task> GetAllInstancesAsync() { var response = await _httpClient.GetAsync($"{_apiBaseUrl}/api/SimulatorInstances"); response.EnsureSuccessStatusCode(); var json = await response.Content.ReadAsStringAsync(); return JsonSerializer.Deserialize>(json, _jsonOptions) ?? new(); } /// /// 获取指定实例状态 /// public async Task GetInstanceAsync(string id) { var response = await _httpClient.GetAsync($"{_apiBaseUrl}/api/SimulatorInstances/{Uri.EscapeDataString(id)}"); if (response.StatusCode == System.Net.HttpStatusCode.NotFound) return null; response.EnsureSuccessStatusCode(); var json = await response.Content.ReadAsStringAsync(); return JsonSerializer.Deserialize(json, _jsonOptions); } /// /// 获取实例配置 /// public async Task GetInstanceConfigAsync(string id) { var response = await _httpClient.GetAsync($"{_apiBaseUrl}/api/SimulatorInstances/{Uri.EscapeDataString(id)}/config"); if (response.StatusCode == System.Net.HttpStatusCode.NotFound) return null; response.EnsureSuccessStatusCode(); var json = await response.Content.ReadAsStringAsync(); return JsonSerializer.Deserialize(json, _jsonOptions); } /// /// 创建实例 /// public async Task CreateInstanceAsync(InstanceConfig config) { var json = JsonSerializer.Serialize(config, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json"); var response = await _httpClient.PostAsync($"{_apiBaseUrl}/api/SimulatorInstances", content); if (!response.IsSuccessStatusCode) return null; var resultJson = await response.Content.ReadAsStringAsync(); return JsonSerializer.Deserialize(resultJson, _jsonOptions); } /// /// 更新实例 /// public async Task UpdateInstanceAsync(string id, InstanceConfig config) { var json = JsonSerializer.Serialize(config, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json"); var response = await _httpClient.PutAsync($"{_apiBaseUrl}/api/SimulatorInstances/{Uri.EscapeDataString(id)}", content); if (!response.IsSuccessStatusCode) return null; var resultJson = await response.Content.ReadAsStringAsync(); return JsonSerializer.Deserialize(resultJson, _jsonOptions); } /// /// 删除实例 /// public async Task DeleteInstanceAsync(string id, bool deleteConfig = true) { var response = await _httpClient.DeleteAsync($"{_apiBaseUrl}/api/SimulatorInstances/{Uri.EscapeDataString(id)}?deleteConfig={deleteConfig}"); return response.IsSuccessStatusCode; } /// /// 启动实例 /// public async Task StartInstanceAsync(string id) { var response = await _httpClient.PostAsync($"{_apiBaseUrl}/api/SimulatorInstances/{Uri.EscapeDataString(id)}/start", null); if (!response.IsSuccessStatusCode) return null; var resultJson = await response.Content.ReadAsStringAsync(); return JsonSerializer.Deserialize(resultJson, _jsonOptions); } /// /// 停止实例 /// public async Task StopInstanceAsync(string id) { var response = await _httpClient.PostAsync($"{_apiBaseUrl}/api/SimulatorInstances/{Uri.EscapeDataString(id)}/stop", null); if (!response.IsSuccessStatusCode) return null; var resultJson = await response.Content.ReadAsStringAsync(); return JsonSerializer.Deserialize(resultJson, _jsonOptions); } /// /// 重启实例 /// public async Task RestartInstanceAsync(string id) { var response = await _httpClient.PostAsync($"{_apiBaseUrl}/api/SimulatorInstances/{Uri.EscapeDataString(id)}/restart", null); if (!response.IsSuccessStatusCode) return null; var resultJson = await response.Content.ReadAsStringAsync(); return JsonSerializer.Deserialize(resultJson, _jsonOptions); } } /// /// 实例列表项 /// public class InstanceListItem { public string InstanceId { get; set; } = string.Empty; public string Name { get; set; } = string.Empty; public string PlcType { get; set; } = string.Empty; public int Port { get; set; } public string Status { get; set; } = string.Empty; public int ClientCount { get; set; } public long TotalRequests { get; set; } public DateTime? StartTime { get; set; } public DateTime? LastActivityTime { get; set; } public string? ErrorMessage { get; set; } } }