117 lines
7.1 KiB
C#
117 lines
7.1 KiB
C#
using RobotApp.Interfaces;
|
|
using RobotApp.VDA5050.State;
|
|
|
|
namespace RobotApp.Services.Robot;
|
|
|
|
public class RobotErrors : IError
|
|
{
|
|
public Error[] ErrorsState => [.. Errors];
|
|
public bool HasFatalError => Errors.Any(e => e.ErrorLevel == ErrorLevel.FATAL.ToString());
|
|
public event Action? OnNewFatalError;
|
|
|
|
private readonly List<Error> Errors = [];
|
|
|
|
public void AddError(Error error, TimeSpan? clearAfter = null)
|
|
{
|
|
if (Errors.Any(e => e.ErrorType == error.ErrorType && e.ErrorHint == error.ErrorHint)) return;
|
|
lock (Errors)
|
|
{
|
|
Errors.Add(error);
|
|
if(error.ErrorLevel == ErrorLevel.FATAL.ToString()) OnNewFatalError?.Invoke();
|
|
}
|
|
if (clearAfter is not null && clearAfter.HasValue)
|
|
{
|
|
if (clearAfter.Value < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(clearAfter), "TimeSpan cannot be negative.");
|
|
_ = Task.Run(async () =>
|
|
{
|
|
await Task.Delay(clearAfter.Value);
|
|
lock (Errors)
|
|
{
|
|
Errors.RemoveAll(e => e.ErrorHint == error.ErrorHint);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
public void DeleteErrorType(string errorType)
|
|
{
|
|
lock (Errors)
|
|
{
|
|
Errors.RemoveAll(e => e.ErrorType == errorType);
|
|
}
|
|
}
|
|
|
|
public void DeleteErrorHint(string errorHint)
|
|
{
|
|
lock (Errors)
|
|
{
|
|
Errors.RemoveAll(e => e.ErrorHint == errorHint);
|
|
}
|
|
}
|
|
|
|
public void ClearAllErrors()
|
|
{
|
|
lock (Errors)
|
|
{
|
|
Errors.Clear();
|
|
}
|
|
}
|
|
|
|
public static Error CreateError(ErrorType type, string hint, ErrorLevel level, string description)
|
|
{
|
|
return new Error()
|
|
{
|
|
ErrorType = type.ToString(),
|
|
ErrorLevel = level.ToString(),
|
|
ErrorDescription = description,
|
|
ErrorHint = hint,
|
|
ErrorReferences = []
|
|
};
|
|
}
|
|
|
|
public static Error Error1001(string oldOrderId, string newOrderId)
|
|
=> CreateError(ErrorType.INITIALIZE_ORDER, "1001", ErrorLevel.WARNING, $"Có order đang được thực hiện. OrderId: {oldOrderId}, OrderId mới: {newOrderId}");
|
|
public static Error Error1002(int nodesLength)
|
|
=> CreateError(ErrorType.INITIALIZE_ORDER, "1002", ErrorLevel.WARNING, $"Order Nodes không hợp lệ. Kích thước: {nodesLength}");
|
|
public static Error Error1003(int edgesLength)
|
|
=> CreateError(ErrorType.INITIALIZE_ORDER, "1003", ErrorLevel.WARNING, $"Order Edges không hợp lệ. Kích thước: {edgesLength}");
|
|
public static Error Error1004(int nodesLength, int edgesLength)
|
|
=> CreateError(ErrorType.INITIALIZE_ORDER, "1004", ErrorLevel.WARNING, $"Order không hợp lệ do kích thước giữa Nodes và Edges không phù hợp. Kích thước Edges: {edgesLength}, kích thước nodes: {nodesLength}");
|
|
public static Error Error1005()
|
|
=> CreateError(ErrorType.INITIALIZE_ORDER, "1005", ErrorLevel.WARNING, $"Không có order đang được thực hiện.");
|
|
public static Error Error1006(PeripheralMode peripheralMode)
|
|
=> CreateError(ErrorType.INITIALIZE_ORDER, "1006", ErrorLevel.WARNING, $"Không thể khởi tạo order mới khi chế độ vận hành không phải là TỰ ĐỘNG. Chế độ hiện tại: {peripheralMode}");
|
|
public static Error Error1007()
|
|
=> CreateError(ErrorType.INITIALIZE_ORDER, "1007", ErrorLevel.WARNING, $"Không thể khởi tạo order mới khi có action đang thực hiện. Vui lòng chờ hoàn thành các action hiện tại.");
|
|
public static Error Error1008()
|
|
=> CreateError(ErrorType.INITIALIZE_ORDER, "1008", ErrorLevel.WARNING, $"Không thể khởi tạo order mới khi có lỗi nghiêm trọng. Vui lòng kiểm tra và xử lý lỗi.");
|
|
public static Error Error1009()
|
|
=> CreateError(ErrorType.INITIALIZE_ORDER, "1009", ErrorLevel.WARNING, $"Không thể khởi tạo order mới khi robot đang di chuyển. Vui lòng dừng robot.");
|
|
public static Error Error1010(string nodeId, int sequenceId, int correctIndex)
|
|
=> CreateError(ErrorType.INITIALIZE_ORDER, "1010", ErrorLevel.WARNING, $"Order Nodes không đúng thứ tự. NodeId: {nodeId}, SequenceId: {sequenceId}, Vị trí đúng: {correctIndex}");
|
|
public static Error Error1011(string edgeId, int sequenceId, int correctIndex)
|
|
=> CreateError(ErrorType.INITIALIZE_ORDER, "1011", ErrorLevel.WARNING, $"Order Edges không đúng thứ tự. EdgeId: {edgeId}, SequenceId: {sequenceId}, Vị trí đúng: {correctIndex}");
|
|
public static Error Error1012(NavigationState state)
|
|
=> CreateError(ErrorType.INITIALIZE_ORDER, "1012", ErrorLevel.WARNING, $"Không thể khởi tạo order mới khi hệ thống điều hướng không ở trạng thái sẵn sàng. Trạng thái hiện tại: {state}");
|
|
public static Error Error1013(string rootState)
|
|
=> CreateError(ErrorType.INITIALIZE_ORDER, "1013", ErrorLevel.WARNING, $"Robot chưa sẵn sàng để nhận Order. Trạng thái hiện tại {rootState}");
|
|
public static Error Error1014(string edgeId, string nodeId)
|
|
=> CreateError(ErrorType.INITIALIZE_ORDER, "1014", ErrorLevel.WARNING, $"Edge {edgeId} chứa StartNodeId {nodeId} không tồn tại trong Nodes");
|
|
public static Error Error1015(string edgeId, string nodeId)
|
|
=> CreateError(ErrorType.INITIALIZE_ORDER, "1015", ErrorLevel.WARNING, $"Edge {edgeId} chứa {nodeId} không tồn tại trong Nodes");
|
|
public static Error Error1016(string lastNodeId, string newStartNodeId)
|
|
=> CreateError(ErrorType.INITIALIZE_ORDER, "Vui lòng kiểm tra lại order", ErrorLevel.WARNING, $"Order mới nhận được không phải là nối tiếp của order khi lastNodeId: {lastNodeId} mà node đầu tiên của order mới là: {newStartNodeId}");
|
|
public static Error Error1017(int lastNodeSequenceId, int newStartNodeSequenceId)
|
|
=> CreateError(ErrorType.INITIALIZE_ORDER, "Vui lòng kiểm tra lại order", ErrorLevel.WARNING, $"Order mới nhận được không phải là nối tiếp của order khi LastNodeSequenceId: {lastNodeSequenceId} mà node đầu tiên của order mới có sequence: {newStartNodeSequenceId}");
|
|
public static Error Error1018(int oldOrderUpdateId, int newOrderUpdateId)
|
|
=> CreateError(ErrorType.INITIALIZE_ORDER, "Vui lòng kiểm tra lại OrderUpdateId", ErrorLevel.WARNING, $"OrderUpdateId {newOrderUpdateId} nhận được nhỏ hơn OrderUpdateId hiện tại là {oldOrderUpdateId}");
|
|
|
|
|
|
public static Error Error2001()
|
|
=> CreateError(ErrorType.READ_PERIPHERAL_FAILURE, "2001", ErrorLevel.FATAL, "Có lỗi xảy ra trong quá trình đọc tín hiệu từ hệ thống ngoại vi(PLC)");
|
|
public static Error Error2002()
|
|
=> CreateError(ErrorType.READ_PERIPHERAL_FAILURE, "2002", ErrorLevel.FATAL, "Có lỗi xảy ra trong quá trình gửi tín hiệu tới hệ thống ngoại vi(PLC)");
|
|
public static Error Error2003()
|
|
=> CreateError(ErrorType.READ_PERIPHERAL_FAILURE, "2003", ErrorLevel.FATAL, "Mất kết nối với hệ thống ngoại vi(PLC)");
|
|
}
|