first commit -push
This commit is contained in:
178
RobotNet.RobotManager/Hubs/RobotHub.cs
Normal file
178
RobotNet.RobotManager/Hubs/RobotHub.cs
Normal file
@@ -0,0 +1,178 @@
|
||||
using Azure.Core.Serialization;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using RobotNet.MapShares.Dtos;
|
||||
using RobotNet.RobotManager.Data;
|
||||
using RobotNet.RobotManager.Services;
|
||||
using RobotNet.RobotShares.VDA5050.InstantAction;
|
||||
using RobotNet.Shares;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace RobotNet.RobotManager.Hubs;
|
||||
|
||||
[Authorize]
|
||||
public class RobotHub(RobotPublisher RobotPublisher, Services.RobotManager RobotManager, RobotEditorDbContext RobotDb, LoggerController<RobotHub> Logger) : Hub
|
||||
{
|
||||
public async Task MapActive(Guid mapId)
|
||||
{
|
||||
var keysToRemove = RobotPublisher.MapActive.Where(kvp => kvp.Value == Context.ConnectionId)
|
||||
.Select(kvp => kvp.Key)
|
||||
.ToList();
|
||||
foreach (var key in keysToRemove)
|
||||
{
|
||||
RobotPublisher.MapActive.Remove(key);
|
||||
}
|
||||
|
||||
RobotPublisher.MapActive[mapId] = Context.ConnectionId;
|
||||
await Clients.AllExcept([.. RobotPublisher.MapActive.Values]).SendAsync("MapDeactive");
|
||||
}
|
||||
|
||||
public async Task RobotDetailActive(string robotId)
|
||||
{
|
||||
var keysToRemove = RobotPublisher.RobotDetailActive.Where(kvp => kvp.Value == Context.ConnectionId)
|
||||
.Select(kvp => kvp.Key)
|
||||
.ToList();
|
||||
|
||||
foreach (var key in keysToRemove)
|
||||
{
|
||||
RobotPublisher.RobotDetailActive.Remove(key);
|
||||
}
|
||||
|
||||
RobotPublisher.RobotDetailActive[robotId] = Context.ConnectionId;
|
||||
await Clients.AllExcept([.. RobotPublisher.RobotDetailActive.Values]).SendAsync("RobotDetailDeactive");
|
||||
}
|
||||
|
||||
public MessageResult SetInitialPose(string robotId, double x, double y, double yaw)
|
||||
{
|
||||
try
|
||||
{
|
||||
var robot = RobotManager[robotId];
|
||||
if (robot is not null)
|
||||
{
|
||||
robot.Initialize(x, y, yaw * 180 / Math.PI);
|
||||
return new(true);
|
||||
}
|
||||
return new(false, "Robot không tồn tại");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"SetInitialPose: Hệ thống có lỗi xảy ra - {ex.Message}");
|
||||
return new(false, "Hệ thống có lỗi xảy ra");
|
||||
}
|
||||
}
|
||||
public MessageResult MoveStraight(string robotId, double x, double y, double theta)
|
||||
{
|
||||
try
|
||||
{
|
||||
var robot = RobotManager[robotId];
|
||||
if (robot is not null) return robot.MoveStraight(x, y);
|
||||
return new(false, "Robot không tồn tại");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"MoveStraight: Hệ thống có lỗi xảy ra - {ex.Message}");
|
||||
return new(false, "Hệ thống có lỗi xảy ra");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<MessageResult> MoveToNode(string robotId, string nodename)
|
||||
{
|
||||
try
|
||||
{
|
||||
var robot = RobotManager[robotId];
|
||||
if (robot is not null) return await robot.MoveToNode(nodename);
|
||||
return new(false, "Robot không tồn tại");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"MoveToNode: Hệ thống có lỗi xảy ra - {ex.Message}");
|
||||
return new(false, "Hệ thống có lỗi xảy ra");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<MessageResult> MoveRandom(string robotId, List<string> nodes)
|
||||
{
|
||||
try
|
||||
{
|
||||
var robot = RobotManager[robotId];
|
||||
if (robot is not null) return await robot.MoveRandom(nodes);
|
||||
return new(false, "Robot không tồn tại");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"MoveToNode: Hệ thống có lỗi xảy ra - {ex.Message}");
|
||||
return new(false, "Hệ thống có lỗi xảy ra");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<MessageResult> CancelNavigation(string robotId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var robot = RobotManager[robotId];
|
||||
if (robot is not null) return await robot.CancelOrder();
|
||||
return new(false, "Robot không tồn tại");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"CancelNavigation: Hệ thống có lỗi xảy ra - {ex.Message}");
|
||||
return new(false, "Hệ thống có lỗi xảy ra");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<MessageResult> SetMap(string robotId, Guid mapId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var robot = RobotDb.Robots.FirstOrDefault(x => x.RobotId == robotId);
|
||||
if (robot is not null)
|
||||
{
|
||||
robot.MapId = mapId;
|
||||
await RobotDb.SaveChangesAsync();
|
||||
return new(true);
|
||||
}
|
||||
return new(false, "Robot không tồn tại");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"SetMap: Hệ thống có lỗi xảy ra - {ex.Message}");
|
||||
return new(false, "Hệ thống có lỗi xảy ra");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<MessageResult> SendAction(string robotId, ActionDto action)
|
||||
{
|
||||
try
|
||||
{
|
||||
var robot = RobotManager[robotId];
|
||||
var actionVDA = JsonSerializer.Deserialize<RobotShares.VDA5050.InstantAction.Action>(action.Content, JsonOptionExtends.Read);
|
||||
if (robot is not null)
|
||||
{
|
||||
if (actionVDA is null) return new(false, "Action không hợp lệ");
|
||||
var send = await robot.InstantAction(actionVDA, false);
|
||||
return new(send.IsSuccess, send.Message);
|
||||
}
|
||||
return new(false, "Robot không tồn tại");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"MoveToNode: Hệ thống có lỗi xảy ra - {ex.Message}");
|
||||
return new(false, "Hệ thống có lỗi xảy ra");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<MessageResult> CancelAction(string robotId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var robot = RobotManager[robotId];
|
||||
if (robot is not null) return await robot.CancelAction();
|
||||
return new(false, "Robot không tồn tại");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"CancelAction: Hệ thống có lỗi xảy ra - {ex.Message}");
|
||||
return new(false, "Hệ thống có lỗi xảy ra");
|
||||
}
|
||||
}
|
||||
}
|
||||
244
RobotNet.RobotManager/Hubs/RobotManagerHub.cs
Normal file
244
RobotNet.RobotManager/Hubs/RobotManagerHub.cs
Normal file
@@ -0,0 +1,244 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using RobotNet.RobotManager.Controllers;
|
||||
using RobotNet.RobotManager.Data;
|
||||
using RobotNet.RobotManager.Services;
|
||||
using RobotNet.RobotManager.Services.OpenACS;
|
||||
using RobotNet.RobotShares.Models;
|
||||
using RobotNet.Shares;
|
||||
|
||||
namespace RobotNet.RobotManager.Hubs;
|
||||
|
||||
[Authorize]
|
||||
public class RobotManagerHub(RobotEditorDbContext RobotDb, MapManager MapManager, Services.RobotManager RobotManager, TrafficACS TrafficACS, ILogger<RobotManagerController> Logger) : Hub
|
||||
{
|
||||
public async Task<MessageResult> MoveToNode(RobotMoveToNodeModel model)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.NodeName)) return new(false, "NodeName cannot be empty..");
|
||||
|
||||
var robot = await RobotDb.Robots.FirstOrDefaultAsync(r => r.RobotId == model.RobotId);
|
||||
if (robot is null) return new(false, "RobotId does not exist.");
|
||||
|
||||
var robotController = RobotManager[robot.RobotId];
|
||||
if (robotController is null || !robotController.IsOnline) return new(false, "The robot is not online.");
|
||||
|
||||
var map = await MapManager.GetMapData(robot.MapId);
|
||||
if (!map.IsSuccess) return new(false, map.Message);
|
||||
if (map is null || map.Data is null) return new(false, "The robot has not been assigned a map.");
|
||||
|
||||
var node = map.Data.Nodes.FirstOrDefault(n => n.Name == model.NodeName && n.MapId == map.Data.Id);
|
||||
if (node is null) return new(false, "This Node does not exist.");
|
||||
|
||||
if (robotController.IsWorking) return new(false, "The robot is busy.");
|
||||
|
||||
var move = await robotController.MoveToNode(node.Name, model.Actions, model.OverrideLastAngle ? model.LastAngle : null);
|
||||
if (move.IsSuccess)
|
||||
{
|
||||
robotController.Log($"RobotManager API Controller Log: Goi Robot {model.RobotId} di chuyển tới node {model.NodeName} thành công ");
|
||||
return new(true);
|
||||
}
|
||||
robotController.Log($"RobotManager API Controller Log: Goi Robot {model.RobotId} di chuyển tới node {model.NodeName} không thành công thành công do {move.Message}");
|
||||
return new(false, move.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError("RobotManager API Controller Log: Goi MoveToNode Robot {RobotId} di chuyển tới node {NodeName} xảy ra lỗi: {Message}", model.RobotId, model.NodeName, ex.Message);
|
||||
return new(false, "An error occurred.");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<MessageResult> MoveStraight(RobotMoveStraightModel model)
|
||||
{
|
||||
try
|
||||
{
|
||||
var robot = await RobotDb.Robots.FirstOrDefaultAsync(r => r.RobotId == model.RobotId);
|
||||
if (robot is null) return new(false, "RobotId does not exist.");
|
||||
|
||||
var robotController = RobotManager[robot.RobotId];
|
||||
if (robotController is null || !robotController.IsOnline) return new(false, "The robot is not online.");
|
||||
if (robotController.IsWorking) return new(false, "The robot is busy.");
|
||||
|
||||
var move = robotController.MoveStraight(model.X, model.Y);
|
||||
if (move.IsSuccess)
|
||||
{
|
||||
robotController.Log($"RobotManager API Controller Log: Goi Robot {model.RobotId} di chuyển tới tọa độ [{model.X} - {model.Y}] thành công ");
|
||||
return new(true);
|
||||
}
|
||||
robotController.Log($"RobotManager API Controller Log: Goi Robot {model.RobotId} di chuyển tới tọa độ [{model.X} - {model.Y}] không thành công thành công do {move.Message}");
|
||||
return new(false, "Request failed.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError("RobotManager API Controller Log: Goi Rotate Robot {RobotId} di chuyển tới tọa độ [{model.X} - {model.Y}] xảy ra lỗi: {Message}", model.RobotId, model.X, model.Y, ex.Message);
|
||||
return new(false, "An error occurred.");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<MessageResult> Rotate(RobotRotateModel model)
|
||||
{
|
||||
try
|
||||
{
|
||||
var robot = await RobotDb.Robots.FirstOrDefaultAsync(r => r.RobotId == model.RobotId);
|
||||
if (robot is null) return new(false, "RobotId does not exist.");
|
||||
|
||||
var robotController = RobotManager[robot.RobotId];
|
||||
if (robotController is null || !robotController.IsOnline) return new(false, "The robot is not online.");
|
||||
if (robotController.IsWorking) return new(false, "The robot is busy.");
|
||||
|
||||
var move = robotController.Rotate(model.Angle);
|
||||
if (move.IsSuccess)
|
||||
{
|
||||
robotController.Log($"RobotManager API Controller Log: Goi Robot {model.RobotId} quay tới góc {model.Angle} thành công ");
|
||||
return new(true);
|
||||
}
|
||||
robotController.Log($"RobotManager API Controller Log: Goi Robot {model.RobotId} quay tới góc {model.Angle} không thành công thành công do {move.Message}");
|
||||
return new(false, "Request failed.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError("RobotManager API Controller Log: Goi Rotate Robot {RobotId} quay tới góc {model.Angle} xảy ra lỗi: {Message}", model.RobotId, model.Angle, ex.Message);
|
||||
return new(false, "An error occurred.");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<MessageResult> CancelOrder(string robotId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var robot = await RobotDb.Robots.FirstOrDefaultAsync(r => r.RobotId == robotId);
|
||||
if (robot is null) return new(false, "RobotId does not exist.");
|
||||
|
||||
var robotController = RobotManager[robot.RobotId];
|
||||
if (robotController is null || !robotController.IsOnline) return new(false, "The robot is not online.");
|
||||
|
||||
var cancel = await robotController.CancelOrder();
|
||||
if (cancel.IsSuccess)
|
||||
{
|
||||
robotController.Log($"RobotManager API Controller Log: Hủy bỏ nhiệm vụ của Robot {robotId} thành công ");
|
||||
return new(true);
|
||||
}
|
||||
robotController.Log($"RobotManager API Controller Log: Hủy bỏ nhiệm vụ của Robot {robotId} không thành công do {cancel.Message}");
|
||||
return new(false, "Request failed.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError("RobotManager API Controller Log: Goi Cancel Order Robot {RobotId} xảy ra lỗi: {Message}", robotId, ex.Message);
|
||||
return new(false, "An error occurred.");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<MessageResult> CancelAction(string robotId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var robot = await RobotDb.Robots.FirstOrDefaultAsync(r => r.RobotId == robotId);
|
||||
if (robot is null) return new(false, "RobotId does not exist.");
|
||||
|
||||
var robotController = RobotManager[robot.RobotId];
|
||||
if (robotController is null || !robotController.IsOnline) return new(false, "The robot is not online.");
|
||||
|
||||
var cancel = await robotController.CancelAction();
|
||||
if (cancel.IsSuccess)
|
||||
{
|
||||
robotController.Log($"RobotManager API Controller Log: Hủy bỏ nhiệm vụ của Robot {robotId} thành công ");
|
||||
return new(true);
|
||||
}
|
||||
robotController.Log($"RobotManager API Controller Log: Hủy bỏ nhiệm vụ của Robot {robotId} không thành công do {cancel.Message}");
|
||||
return new(false, "Request failed.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError("RobotManager API Controller Log: Goi Cancel Order Robot {RobotId} xảy ra lỗi: {Message}", robotId, ex.Message);
|
||||
return new(false, "An error occurred.");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<MessageResult<string>> InstantAction(RobotInstantActionModel model)
|
||||
{
|
||||
try
|
||||
{
|
||||
var robot = await RobotDb.Robots.FirstOrDefaultAsync(r => r.RobotId == model.RobotId);
|
||||
if (robot is null) return new(false, "RobotId does not exist.");
|
||||
|
||||
var robotController = RobotManager[robot.RobotId];
|
||||
if (robotController is null || !robotController.IsOnline) return new(false, "The robot is not online.");
|
||||
|
||||
var instantAction = await robotController.InstantAction(model.Action, false);
|
||||
if (instantAction.IsSuccess)
|
||||
{
|
||||
robotController.Log($"RobotManager API Controller Log: Gửi Action Robot {model.RobotId} thành công ");
|
||||
return instantAction;
|
||||
}
|
||||
robotController.Log($"RobotManager API Controller Log: Gửi Action Robot {model.RobotId} không thành công do {instantAction.Message}");
|
||||
return new(false, "Request failed.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError("RobotManager API Controller Log: Goi InstantAction Robot {RobotId}, Action type {action} xảy ra lỗi: {Message}", model.RobotId, model.Action.ActionType, ex.Message);
|
||||
return new(false, "An error occurred.");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<MessageResult<RobotStateModel>> GetState(string robotId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var robot = await RobotDb.Robots.FirstOrDefaultAsync(r => r.RobotId == robotId);
|
||||
if (robot is null) return new(false, "RobotId does not exist.");
|
||||
|
||||
var robotController = RobotManager[robot.RobotId];
|
||||
if (robotController is null) return new(true, "")
|
||||
{
|
||||
Data = new()
|
||||
{
|
||||
RobotId = robotId,
|
||||
IsOnline = false,
|
||||
MapId = robot.MapId,
|
||||
}
|
||||
};
|
||||
|
||||
return new(true)
|
||||
{
|
||||
Data = new()
|
||||
{
|
||||
RobotId = robotId,
|
||||
MapId = robot.MapId,
|
||||
IsOnline = robotController.IsOnline,
|
||||
State = robotController.State,
|
||||
OrderState = robotController.OrderState,
|
||||
NewBaseRequest = robotController.StateMsg.NewBaseRequest,
|
||||
NodeStates = [.. robotController.StateMsg.NodeStates],
|
||||
EdgeStates = [.. robotController.StateMsg.EdgeStates],
|
||||
Loads = [.. robotController.StateMsg.Loads],
|
||||
ActionStates = robotController.ActionStates,
|
||||
BatteryState = robotController.StateMsg.BatteryState,
|
||||
Errors = [.. robotController.StateMsg.Errors],
|
||||
Information = [.. robotController.StateMsg.Information],
|
||||
SafetyState = robotController.StateMsg.SafetyState,
|
||||
AgvPosition = robotController.VisualizationMsg.AgvPosition,
|
||||
Velocity = robotController.VisualizationMsg.Velocity,
|
||||
}
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError("RobotManager API Controller Log: Goi GetState Robot {RobotId} xảy ra lỗi: {Message}", robotId, ex.Message);
|
||||
return new(false, "An error occurred.");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> RequestACSIn(string robotId, string id)
|
||||
{
|
||||
var result = await TrafficACS.RequestIn(robotId, id);
|
||||
return result.Data;
|
||||
}
|
||||
|
||||
public async Task<bool> RequestACSOut(string robotId, string id)
|
||||
{
|
||||
var result = await TrafficACS.RequestOut(robotId, id);
|
||||
return result.Data;
|
||||
}
|
||||
}
|
||||
70
RobotNet.RobotManager/Hubs/TrafficHub.cs
Normal file
70
RobotNet.RobotManager/Hubs/TrafficHub.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using RobotNet.RobotManager.Services;
|
||||
using RobotNet.RobotManager.Services.Traffic;
|
||||
using RobotNet.RobotShares.Dtos;
|
||||
using RobotNet.Shares;
|
||||
|
||||
namespace RobotNet.RobotManager.Hubs;
|
||||
|
||||
[Authorize]
|
||||
public class TrafficHub(TrafficPublisher TrafficPublisher, TrafficManager TrafficManager, MapManager MapManager, LoggerController<RobotHub> Logger) : Hub
|
||||
{
|
||||
public async Task TrafficActive(Guid mapId)
|
||||
{
|
||||
var keysToRemove = TrafficPublisher.TrafficMapActive.Where(kvp => kvp.Value == Context.ConnectionId)
|
||||
.Select(kvp => kvp.Key)
|
||||
.ToList();
|
||||
foreach (var key in keysToRemove)
|
||||
{
|
||||
TrafficPublisher.TrafficMapActive.Remove(key);
|
||||
}
|
||||
|
||||
TrafficPublisher.TrafficMapActive[mapId] = Context.ConnectionId;
|
||||
await Clients.AllExcept([.. TrafficPublisher.TrafficMapActive.Values]).SendAsync("TrafficManagerDeactive");
|
||||
}
|
||||
|
||||
public async Task<MessageResult<IEnumerable<TrafficMapDto>>> LoadTrafficMaps()
|
||||
{
|
||||
try
|
||||
{
|
||||
List<TrafficMapDto> trafficMaps = [];
|
||||
foreach (var trafficMap in TrafficManager.TrafficMaps)
|
||||
{
|
||||
var map = await MapManager.GetMapData(trafficMap.Key);
|
||||
trafficMaps.Add(new()
|
||||
{
|
||||
MapId = trafficMap.Key,
|
||||
Agents = TrafficPublisher.GetAgents(trafficMap.Key),
|
||||
MapName = map.Data is null ? "" : map.Data.Name,
|
||||
});
|
||||
}
|
||||
return new(true) { Data = trafficMaps };
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"LoadTrafficMaps: Hệ thống có lỗi xảy ra - {ex.Message}");
|
||||
return new(false, "Hệ thống có lỗi xảy ra");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<MessageResult<TrafficMapDto>> LoadTrafficMap(Guid mapId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var map = await MapManager.GetMapData(mapId);
|
||||
var trafficMap = new TrafficMapDto()
|
||||
{
|
||||
MapId = mapId,
|
||||
Agents = TrafficPublisher.GetAgents(mapId),
|
||||
MapName = map.Data is null ? "" : map.Data.Name,
|
||||
};
|
||||
return new(true) { Data = trafficMap };
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"LoadTrafficMaps: Hệ thống có lỗi xảy ra - {ex.Message}");
|
||||
return new(false, "Hệ thống có lỗi xảy ra");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user