97 lines
4.2 KiB
C#
97 lines
4.2 KiB
C#
using RobotApp.Interfaces;
|
|
using RobotApp.Services.Exceptions;
|
|
using RobotApp.VDA5050.Order;
|
|
using RobotApp.VDA5050.State;
|
|
|
|
namespace RobotApp.Services.Robot;
|
|
|
|
public class RobotController(IOrder OrderManager,
|
|
INavigation NavigationManager,
|
|
IInstanceActions ActionManager,
|
|
IBattery BatteryManager,
|
|
ILocalization Localization,
|
|
IPeripheral PeripheralManager,
|
|
ISafety SafetyManager,
|
|
IError ErrorManager,
|
|
IInfomation InfomationManager,
|
|
Logger<RobotController> Logger,
|
|
RobotConnection ConnectionManager) : BackgroundService
|
|
{
|
|
private readonly Mutex NewOrderMutex = new();
|
|
private readonly Mutex NewInstanceMutex = new();
|
|
|
|
private WatchTimer<RobotController>? UpdateStateTimer;
|
|
private const int UpdateStateInterval = 1000;
|
|
|
|
protected override Task ExecuteAsync(CancellationToken stoppingToken)
|
|
{
|
|
UpdateStateTimer = new(UpdateStateInterval, UpdateStateHandler, Logger);
|
|
UpdateStateTimer.Start();
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
private void UpdateStateHandler()
|
|
{
|
|
// xử lý cập nhật trạng thái robot và gửi thông tin qua kết nối
|
|
}
|
|
|
|
public void NewOrderUpdated(OrderMsg order)
|
|
{
|
|
if (NewOrderMutex.WaitOne(2000))
|
|
{
|
|
try
|
|
{
|
|
if (!string.IsNullOrEmpty(OrderManager.OrderId))
|
|
{
|
|
if (order.OrderId != OrderManager.OrderId) throw new OrderException(RobotErrors.CreateError(ErrorType.INITIALIZE_ORDER, "1001", ErrorLevel.WARNING, $"Có order đang được thực hiện. OrderId: {OrderManager.OrderId}, OrderId mới: {order.OrderId}"));
|
|
OrderManager.UpdateOrder(order.OrderUpdateId, order.Nodes, order.Edges);
|
|
}
|
|
else if (PeripheralManager.OperatingMode != Interfaces.OperatingMode.AUTOMATIC) throw new OrderException(RobotErrors.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: {PeripheralManager.OperatingMode}"));
|
|
else if(ActionManager.HasActionRunning) throw new OrderException(RobotErrors.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."));
|
|
else if(ErrorManager.HasFatalError) throw new OrderException(RobotErrors.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."));
|
|
else if(NavigationManager.Driving) throw new OrderException(RobotErrors.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."));
|
|
else OrderManager.StartOrder(order.OrderId, order.Nodes, order.Edges);
|
|
}
|
|
catch (OrderException orEx)
|
|
{
|
|
if (orEx.Error is not null)
|
|
{
|
|
ErrorManager.AddError(orEx.Error, TimeSpan.FromSeconds(10));
|
|
Logger.Warning($"Lỗi khi xử lí Order: {orEx.Error.ErrorDescription}");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.Warning($"Lỗi khi xử lí Order: {ex.Message}");
|
|
}
|
|
finally
|
|
{
|
|
NewOrderMutex.ReleaseMutex();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void NewInstanceActionUpdated()
|
|
{
|
|
if (NewInstanceMutex.WaitOne(2000))
|
|
{
|
|
try
|
|
{
|
|
|
|
}
|
|
catch (ActionException acEx)
|
|
{
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
finally
|
|
{
|
|
NewInstanceMutex.ReleaseMutex();
|
|
}
|
|
}
|
|
}
|
|
}
|