Initial commit
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
using RobotNet10.FleetManager.Services.ConfigManager;
|
||||
using RobotNet10.Shared;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace RobotNet10.FleetManager.Services.OpenACS;
|
||||
|
||||
public class TrafficACS(IACSTrafficConfig OpenACSManager, Logger<TrafficACS> Logger)
|
||||
{
|
||||
private static readonly JsonSerializerOptions jsonSerializeOptions = new() {
|
||||
WriteIndented = true,
|
||||
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
|
||||
};
|
||||
|
||||
public async Task<MessageResult<bool>> RequestIn(string robotId, string zoneId)
|
||||
{
|
||||
var model = new TrafficACSRequest(robotId, zoneId, TrafficRequestType.IN);
|
||||
TrafficACSResponse? response = null;
|
||||
HttpResponseMessage? responseStr = null;
|
||||
try
|
||||
{
|
||||
if (!OpenACSManager.TrafficEnable) return new(true, true, "Kết nối với hệ thống traffic ACS không được bật");
|
||||
using var HttpClient = new HttpClient() { Timeout = TimeSpan.FromSeconds(15) };
|
||||
|
||||
responseStr = await HttpClient.PostAsJsonAsync(OpenACSManager.TrafficURL, model);
|
||||
response = await responseStr.Content.ReadFromJsonAsync<TrafficACSResponse>() ?? throw new OpenACSException("Lỗi giao tiếp với hệ thống traffic ACS");
|
||||
|
||||
if (response.AgvId != robotId) throw new OpenACSException($"Dữ liệu hệ thống traffic ACS agv_id trả về {response.AgvId} không trùng với dữ liệu gửi đi {robotId}");
|
||||
if (response.TrafficZoneId != zoneId) throw new OpenACSException($"Dữ liệu hệ thống traffic ACS traffic_zone_id trả về {response.TrafficZoneId} không trùng với dữ liệu gửi đi {zoneId}");
|
||||
if (response.InOut != TrafficRequestType.IN) throw new OpenACSException($"Dữ liệu hệ thống traffic ACS inout trả về {response.InOut} không trùng với dữ liệu gửi đi in");
|
||||
if (response.Result != TrafficACSResult.GO) throw new OpenACSException($"Dữ liệu hệ thống traffic ACS result trả về {response.Result} - không cho phép đi vào vùng {zoneId}");
|
||||
|
||||
Logger.Info($"{robotId} request into traffic zone {zoneId} succeeded");
|
||||
return new(true, true, "Request into traffic zone succeeded");
|
||||
}
|
||||
catch (OpenACSException ex)
|
||||
{
|
||||
Logger.Warning($"{robotId} request in error: {ex.Message}. \nRequest: {JsonSerializer.Serialize(model, jsonSerializeOptions)}\n Response: {(response is null ? "" : JsonSerializer.Serialize(response, jsonSerializeOptions))}, Raw: {(responseStr is null ? "" : await responseStr.Content.ReadAsStringAsync())}");
|
||||
return new(false, false, ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"{robotId} request In error: {ex.Message} \nRaw: {(responseStr is null ? "" : await responseStr.Content.ReadAsStringAsync())}");
|
||||
return new(false, false, "Traffic ACS communication error");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<MessageResult<bool>> RequestOut(string robotId, string zoneId)
|
||||
{
|
||||
var model = new TrafficACSRequest(robotId, zoneId, TrafficRequestType.OUT);
|
||||
TrafficACSResponse? response = null;
|
||||
try
|
||||
{
|
||||
if (!OpenACSManager.TrafficEnable) return new(true, true, "Kết nối với hệ thống traffic ACS không được bật");
|
||||
using var HttpClient = new HttpClient() { Timeout = TimeSpan.FromSeconds(15) };
|
||||
|
||||
response = await (await HttpClient.PostAsJsonAsync(OpenACSManager.TrafficURL, model)).Content.ReadFromJsonAsync<TrafficACSResponse>() ??
|
||||
throw new OpenACSException("Lỗi giao tiếp với hệ thống traffic ACS");
|
||||
|
||||
if (response.AgvId != robotId) throw new OpenACSException($"Dữ liệu hệ thống traffic ACS agv_id trả về {response.AgvId} không trùng với dữ liệu gửi đi {robotId}");
|
||||
if (response.TrafficZoneId != zoneId) throw new OpenACSException($"Dữ liệu hệ thống traffic ACS traffic_zone_id trả về {response.TrafficZoneId} không trùng với dữ liệu gửi đi {zoneId}");
|
||||
if (response.InOut != TrafficRequestType.OUT) throw new OpenACSException($"Dữ liệu hệ thống traffic ACS inout trả về {response.InOut} không trùng với dữ liệu gửi đi out");
|
||||
if (response.Result != TrafficACSResult.GO) throw new OpenACSException($"Dữ liệu hệ thống traffic ACS result trả về {response.Result} - không cho phép xóa bỏ vùng {zoneId}");
|
||||
|
||||
Logger.Info($"{robotId} request out of traffic zone {zoneId} succeeded");
|
||||
return new(true, true, "Request out of traffic zone succeeded");
|
||||
}
|
||||
catch (OpenACSException ex)
|
||||
{
|
||||
Logger.Warning($"{robotId} request out error: {ex.Message}. \nRequest: {JsonSerializer.Serialize(model, jsonSerializeOptions)}\n Response: {(response is null ? "" : JsonSerializer.Serialize(response, jsonSerializeOptions))}");
|
||||
return new(false, false, ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"{robotId} request Out error: {ex.Message}");
|
||||
return new(false, false, "Traffic ACS communication error");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user