67 lines
2.3 KiB
C#
67 lines
2.3 KiB
C#
using System.Net.Http.Json;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
|
|
namespace IntegrationGateway.Core.Infrastructure;
|
|
|
|
public class GatewayClientFactory
|
|
{
|
|
private readonly IHttpClientFactory _httpFactory;
|
|
private readonly string _volProBaseUrl;
|
|
|
|
public GatewayClientFactory(IHttpClientFactory httpFactory, string volProBaseUrl)
|
|
{
|
|
_httpFactory = httpFactory;
|
|
_volProBaseUrl = volProBaseUrl.TrimEnd('/');
|
|
}
|
|
|
|
public HttpClient CreateClient() => _httpFactory.CreateClient("VolPro");
|
|
|
|
public async Task<JsonDocument?> RegisterAsync(GatewayRegisterRequest req)
|
|
{
|
|
var http = CreateClient();
|
|
var resp = await http.PostAsJsonAsync($"{_volProBaseUrl}/api/gateway/register", req);
|
|
if (!resp.IsSuccessStatusCode) return null;
|
|
return await resp.Content.ReadFromJsonAsync<JsonDocument>();
|
|
}
|
|
|
|
public async Task<bool> HeartbeatAsync(GatewayHeartbeatRequest req)
|
|
{
|
|
var http = CreateClient();
|
|
var resp = await http.PostAsJsonAsync($"{_volProBaseUrl}/api/gateway/heartbeat", req);
|
|
return resp.IsSuccessStatusCode;
|
|
}
|
|
|
|
public async Task<JsonDocument?> SyncDevicesAsync(string nodeCode, string token, List<object> devices)
|
|
{
|
|
var http = CreateClient();
|
|
var resp = await http.PostAsJsonAsync($"{_volProBaseUrl}/api/gateway/sync/devices",
|
|
new { nodeCode, token, devices });
|
|
if (!resp.IsSuccessStatusCode) return null;
|
|
return await resp.Content.ReadFromJsonAsync<JsonDocument>();
|
|
}
|
|
|
|
public async Task<JsonDocument?> SyncAlarmsAsync(string nodeCode, string token, List<object> alarms)
|
|
{
|
|
var http = CreateClient();
|
|
var resp = await http.PostAsJsonAsync($"{_volProBaseUrl}/api/gateway/sync/alarms",
|
|
new { nodeCode, token, alarms });
|
|
if (!resp.IsSuccessStatusCode) return null;
|
|
return await resp.Content.ReadFromJsonAsync<JsonDocument>();
|
|
}
|
|
}
|
|
|
|
public class GatewayRegisterRequest
|
|
{
|
|
public string NodeCode { get; set; } = "";
|
|
public string Token { get; set; } = "";
|
|
public string AdapterTypes { get; set; } = "";
|
|
public string BaseUrl { get; set; } = "";
|
|
}
|
|
|
|
public class GatewayHeartbeatRequest
|
|
{
|
|
public string NodeCode { get; set; } = "";
|
|
public string Token { get; set; } = "";
|
|
}
|