first commit -push
This commit is contained in:
103
RobotNet.RobotManager/Controllers/OpenACSSettingsController.cs
Normal file
103
RobotNet.RobotManager/Controllers/OpenACSSettingsController.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using RobotNet.RobotManager.Services;
|
||||
using RobotNet.RobotManager.Services.OpenACS;
|
||||
using RobotNet.RobotShares.OpenACS;
|
||||
using RobotNet.Shares;
|
||||
|
||||
namespace RobotNet.RobotManager.Controllers;
|
||||
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class OpenACSSettingsController(OpenACSManager OpenACSManager, LoggerController<OpenACSSettingsController> Logger) : ControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
public Task<MessageResult<OpenACSSettingsDto>> GetOpenACSSettings()
|
||||
{
|
||||
try
|
||||
{
|
||||
var settings = new OpenACSSettingsDto
|
||||
{
|
||||
PublishSetting = new(OpenACSManager.PublishEnable, OpenACSManager.PublishURL, OpenACSManager.PublishURLUsed, OpenACSManager.PublishInterval),
|
||||
TrafficSetting = new(OpenACSManager.TrafficEnable, OpenACSManager.TrafficURL, OpenACSManager.TrafficURLUsed)
|
||||
};
|
||||
return Task.FromResult<MessageResult<OpenACSSettingsDto>>(new(true, "") { Data = settings });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"Lấy cấu hình OpenACS xảy ra lỗi: {ex.Message}");
|
||||
return Task.FromResult<MessageResult<OpenACSSettingsDto>>(new(false, "Hệ thống có lỗi xảy ra"));
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("publish")]
|
||||
public async Task<MessageResult<OpenACSPublishSettingDto>> UpdatePublishSetting([FromBody] OpenACSPublishSettingModel settingModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
await OpenACSManager.UpdatePublishURL(settingModel.Url);
|
||||
await OpenACSManager.UpdatePublishInterval(settingModel.Interval);
|
||||
return new(true, "") { Data = new(OpenACSManager.PublishEnable, OpenACSManager.PublishURL, OpenACSManager.PublishURLUsed, OpenACSManager.PublishInterval) };
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"Cập nhật cấu hình publish OpenACS xảy ra lỗi: {ex.Message}");
|
||||
return new(false, "Hệ thống có lỗi xảy ra");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("traffic")]
|
||||
public async Task<MessageResult<OpenACSTrafficSettingDto>> UpdateTrafficSetting([FromBody] OpenACSTrafficSettingModel settingModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
await OpenACSManager.UpdateTrafficURL(settingModel.Url);
|
||||
return new(true, "") { Data = new(OpenACSManager.TrafficEnable, OpenACSManager.TrafficURL, OpenACSManager.TrafficURLUsed) };
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"Cập nhật cấu hình traffic OpenACS xảy ra lỗi: {ex.Message}");
|
||||
return new(false, "Hệ thống có lỗi xảy ra");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("publish")]
|
||||
public async Task<MessageResult> UpdatePublishEnable([FromQuery] bool enable)
|
||||
{
|
||||
try
|
||||
{
|
||||
await OpenACSManager.UpdatePublishEnable(enable);
|
||||
return new(true, "");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"Cập nhật cấu hình publish OpenACS xảy ra lỗi: {ex.Message}");
|
||||
return new(false, "Hệ thống có lỗi xảy ra");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("publish/count")]
|
||||
[AllowAnonymous]
|
||||
public int GetStatusPublishCount([FromServices] OpenACSPublisher publisher) => publisher.PublishCount;
|
||||
|
||||
[HttpGet]
|
||||
[Route("traffic")]
|
||||
public async Task<MessageResult> UpdateTrafficEnable([FromQuery] bool enable)
|
||||
{
|
||||
try
|
||||
{
|
||||
await OpenACSManager.UpdateTrafficEnable(enable);
|
||||
return new(true, "");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"Cập nhật cấu hình publish OpenACS xảy ra lỗi: {ex.Message}");
|
||||
return new(false, "Hệ thống có lỗi xảy ra");
|
||||
}
|
||||
}
|
||||
}
|
||||
179
RobotNet.RobotManager/Controllers/RobotManagerController.cs
Normal file
179
RobotNet.RobotManager/Controllers/RobotManagerController.cs
Normal file
@@ -0,0 +1,179 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using RobotNet.MapShares.Models;
|
||||
using RobotNet.RobotManager.Data;
|
||||
using RobotNet.RobotManager.Services;
|
||||
using RobotNet.RobotShares.Models;
|
||||
using RobotNet.Script.Expressions;
|
||||
using RobotNet.Shares;
|
||||
using Serialize.Linq.Serializers;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace RobotNet.RobotManager.Controllers;
|
||||
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
[AllowAnonymous]
|
||||
public class RobotManagerController(RobotEditorDbContext RobotDb, MapManager MapManager, Services.RobotManager RobotManager, ILogger<RobotManagerController> Logger) : ControllerBase
|
||||
{
|
||||
private static readonly ExpressionSerializer expressionSerializer;
|
||||
|
||||
static RobotManagerController()
|
||||
{
|
||||
var jss = new Serialize.Linq.Serializers.JsonSerializer();
|
||||
expressionSerializer = new ExpressionSerializer(jss);
|
||||
|
||||
expressionSerializer.AddKnownType(typeof(RobotState));
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("MoveToNode")]
|
||||
public async Task<MessageResult> MoveToNode([FromBody] 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.StateMsg.NewBaseRequest) return new(false, "The robot is busy.");
|
||||
|
||||
var move = await robotController.MoveToNode(node.Name, model.Actions, model.LastAngle);
|
||||
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, "Request failed.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError("RobotManager API Controller Log: Goi MoveToNode Robot {RobotId} di chuyển tới node {NodeName} xảy ra ngoại lệ: {Message}", model.RobotId, model.NodeName, ex.Message);
|
||||
return new(false, "An error occurred.");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpDelete]
|
||||
[Route("MoveToNode/{robotId}")]
|
||||
public async Task<MessageResult> Cancel(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 Robot {RobotId} xảy ra ngoại lệ: {Message}", robotId, ex.Message);
|
||||
return new(false, "An error occurred.");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("InstantActions")]
|
||||
public async Task<MessageResult> InstantAction([FromBody] 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 new(true);
|
||||
}
|
||||
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 ngoại lệ: {Message}", model.RobotId, model.Action.ActionType, ex.Message);
|
||||
return new(false, "An error occurred.");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("State/{robotId}")]
|
||||
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(false, "The robot has not logged into the system.");
|
||||
|
||||
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 ngoại lệ: {Message}", robotId, ex.Message);
|
||||
return new(false, "An error occurred.");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("Search")]
|
||||
public async Task<MessageResult<string[]>> SearchRobot([FromBody] RobotSearchExpressionModel model)
|
||||
{
|
||||
var expr = expressionSerializer.DeserializeText(model.Expression);
|
||||
var lambda = (Expression<Func<RobotState, bool>>)expr;
|
||||
|
||||
// Compile và chạy:
|
||||
var func = lambda.Compile();
|
||||
return await RobotManager.SearchRobot(model.ModelName, model.MapName, func);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using RobotNet.RobotManager.Services;
|
||||
|
||||
namespace RobotNet.RobotManager.Controllers;
|
||||
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
[AllowAnonymous]
|
||||
public class RobotManagerLoggerController(LoggerController<RobotManagerLoggerController> Logger) : ControllerBase
|
||||
{
|
||||
private readonly string LoggerDirectory = "robotManagerlogs";
|
||||
private readonly string OpenACSLoggerDirectory = "openACSlogs";
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IEnumerable<string>> GetLogs([FromQuery(Name = "date")] DateTime date)
|
||||
{
|
||||
string temp = "";
|
||||
try
|
||||
{
|
||||
string fileName = $"{date:yyyy-MM-dd}.log";
|
||||
string path = Path.Combine(LoggerDirectory, fileName);
|
||||
if (!Path.GetFullPath(path).StartsWith(Path.GetFullPath(LoggerDirectory)))
|
||||
{
|
||||
Logger.Warning($"GetLogs: phát hiện đường dẫn không hợp lệ.");
|
||||
return [];
|
||||
}
|
||||
|
||||
if (!System.IO.File.Exists(path))
|
||||
{
|
||||
Logger.Warning($"GetLogs: không tìm thấy file log của ngày {date.ToShortDateString()} - {path}.");
|
||||
return [];
|
||||
}
|
||||
|
||||
temp = Path.Combine(LoggerDirectory, $"{Guid.NewGuid()}.log");
|
||||
System.IO.File.Copy(path, temp);
|
||||
|
||||
return await System.IO.File.ReadAllLinesAsync(temp);
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
Logger.Warning($"GetLogs: Hệ thống có lỗi xảy ra - {ex.Message}");
|
||||
return [];
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (System.IO.File.Exists(temp)) System.IO.File.Delete(temp);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("open-acs")]
|
||||
public async Task<IEnumerable<string>> GetLogsACS([FromQuery(Name = "date")] DateTime date)
|
||||
{
|
||||
string temp = "";
|
||||
try
|
||||
{
|
||||
string fileName = $"{date:yyyy-MM-dd}.log";
|
||||
string path = Path.Combine(OpenACSLoggerDirectory, fileName);
|
||||
if (!Path.GetFullPath(path).StartsWith(Path.GetFullPath(OpenACSLoggerDirectory)))
|
||||
{
|
||||
Logger.Warning($"GetLogs: phát hiện đường dẫn không hợp lệ.");
|
||||
return [];
|
||||
}
|
||||
|
||||
if (!System.IO.File.Exists(path))
|
||||
{
|
||||
Logger.Warning($"GetLogs: không tìm thấy file log của ngày {date.ToShortDateString()} - {path}.");
|
||||
return [];
|
||||
}
|
||||
|
||||
temp = Path.Combine(OpenACSLoggerDirectory, $"{Guid.NewGuid()}.log");
|
||||
System.IO.File.Copy(path, temp);
|
||||
|
||||
return await System.IO.File.ReadAllLinesAsync(temp);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"GetLogs: Hệ thống có lỗi xảy ra - {ex.Message}");
|
||||
return [];
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (System.IO.File.Exists(temp)) System.IO.File.Delete(temp);
|
||||
}
|
||||
}
|
||||
}
|
||||
258
RobotNet.RobotManager/Controllers/RobotModelsController.cs
Normal file
258
RobotNet.RobotManager/Controllers/RobotModelsController.cs
Normal file
@@ -0,0 +1,258 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using RobotNet.RobotManager.Data;
|
||||
using RobotNet.RobotManager.Services;
|
||||
using RobotNet.RobotShares.Dtos;
|
||||
using RobotNet.Shares;
|
||||
|
||||
namespace RobotNet.RobotManager.Controllers;
|
||||
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class RobotModelsController(RobotEditorDbContext RobotDb, RobotEditorStorageRepository RobotStorage, IHttpClientFactory HttpClientFactory, LoggerController<RobotManagerController> Logger) : ControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
public async Task<IEnumerable<RobotModelDto>> GetRobotModels([FromQuery(Name = "txtSearch")] string? txtSearch)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(txtSearch))
|
||||
{
|
||||
return await (from robotmodel in RobotDb.RobotModels
|
||||
select new RobotModelDto()
|
||||
{
|
||||
Id = robotmodel.Id,
|
||||
ModelName = robotmodel.ModelName,
|
||||
OriginX = robotmodel.OriginX,
|
||||
OriginY = robotmodel.OriginY,
|
||||
ImageWidth = robotmodel.ImageWidth,
|
||||
ImageHeight = robotmodel.ImageHeight,
|
||||
Width = Math.Round(robotmodel.Width, 2),
|
||||
Length = Math.Round(robotmodel.Length, 2),
|
||||
NavigationType = robotmodel.NavigationType,
|
||||
}).ToListAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
return await (from robotmodel in RobotDb.RobotModels
|
||||
where !string.IsNullOrEmpty(robotmodel.ModelName) && robotmodel.ModelName.Contains(txtSearch)
|
||||
select new RobotModelDto()
|
||||
{
|
||||
Id = robotmodel.Id,
|
||||
ModelName = robotmodel.ModelName,
|
||||
OriginX = robotmodel.OriginX,
|
||||
OriginY = robotmodel.OriginY,
|
||||
ImageWidth = robotmodel.ImageWidth,
|
||||
ImageHeight = robotmodel.ImageHeight,
|
||||
Width = Math.Round(robotmodel.Width, 2),
|
||||
Length = Math.Round(robotmodel.Length, 2),
|
||||
NavigationType = robotmodel.NavigationType,
|
||||
}).ToListAsync();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"GetRobotModels: Hệ thống có lỗi xảy ra - {ex.Message}");
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("{robotModelId}")]
|
||||
public async Task<MessageResult<RobotModelDto?>> GetRobotModel(Guid robotModelId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var robotmodel = await RobotDb.RobotModels.FirstOrDefaultAsync(model => model.Id == robotModelId);
|
||||
var robotmodelDto = robotmodel is null ? null : new RobotModelDto()
|
||||
{
|
||||
Id = robotmodel.Id,
|
||||
ModelName = robotmodel.ModelName,
|
||||
OriginX = robotmodel.OriginX,
|
||||
OriginY = robotmodel.OriginY,
|
||||
ImageWidth = robotmodel.ImageWidth,
|
||||
ImageHeight = robotmodel.ImageHeight,
|
||||
Width = Math.Round(robotmodel.Width, 2),
|
||||
Length = Math.Round(robotmodel.Length, 2),
|
||||
NavigationType = robotmodel.NavigationType,
|
||||
};
|
||||
return new(true) { Data = robotmodelDto };
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"GetRobotModel {robotModelId}: Hệ thống có lỗi xảy ra - {ex.Message}");
|
||||
return new(false, $"Hệ thống có lỗi xảy ra");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
[Route("image/{robotmodelId}")]
|
||||
public async Task<IActionResult> GetImage(Guid robotmodelId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var (usingLocal, url) = RobotStorage.GetUrl("RobotModels", robotmodelId.ToString());
|
||||
if (!usingLocal)
|
||||
{
|
||||
var http = HttpClientFactory.CreateClient();
|
||||
var imageBytes = await http.GetByteArrayAsync(url);
|
||||
if (imageBytes != null && imageBytes.Length > 0) return File(imageBytes, "image/png");
|
||||
else return NotFound("Không thể lấy được ảnh map.");
|
||||
}
|
||||
if (System.IO.File.Exists(url)) return PhysicalFile(url, "image/png");
|
||||
else return NotFound();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"GetImage {robotmodelId}: Hệ thống có lỗi xảy ra - {ex.Message}");
|
||||
return NotFound();
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<MessageResult<RobotModelDto>> CreateRobotModel([FromForm] RobotModelCreateModel robotmodel, [FromForm(Name = "Image")] IFormFile imageUpload)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (imageUpload.ContentType != "image/png") return new(false, "Robot image format chỉ hỗ trợ định dạng image/png");
|
||||
if (await RobotDb.RobotModels.AnyAsync(model => model.ModelName == robotmodel.ModelName)) return new(false, "Tên của robot model đã tồn tại, Hãy đặt tên khác!");
|
||||
|
||||
var image = SixLabors.ImageSharp.Image.Load(imageUpload.OpenReadStream());
|
||||
var entityRobotModel = await RobotDb.RobotModels.AddAsync(new RobotModel()
|
||||
{
|
||||
ModelName = robotmodel.ModelName,
|
||||
OriginX = robotmodel.OriginX,
|
||||
OriginY = robotmodel.OriginY,
|
||||
ImageHeight = image.Height,
|
||||
ImageWidth = image.Width,
|
||||
Length = robotmodel.Length,
|
||||
Width = robotmodel.Width,
|
||||
NavigationType = robotmodel.NavigationType,
|
||||
});
|
||||
|
||||
await RobotDb.SaveChangesAsync();
|
||||
|
||||
var (isSuccess, message) = await RobotStorage.UploadAsync("RobotModels", $"{entityRobotModel.Entity.Id}", imageUpload.OpenReadStream(), imageUpload.Length, imageUpload.ContentType, CancellationToken.None);
|
||||
if (!isSuccess)
|
||||
{
|
||||
RobotDb.RobotModels.Remove(entityRobotModel.Entity);
|
||||
await RobotDb.SaveChangesAsync();
|
||||
return new(false, message);
|
||||
}
|
||||
|
||||
return new(true)
|
||||
{
|
||||
Data = new()
|
||||
{
|
||||
Id = entityRobotModel.Entity.Id,
|
||||
ModelName = entityRobotModel.Entity.ModelName,
|
||||
OriginX = entityRobotModel.Entity.OriginX,
|
||||
OriginY = entityRobotModel.Entity.OriginY,
|
||||
ImageWidth = entityRobotModel.Entity.ImageWidth,
|
||||
ImageHeight = entityRobotModel.Entity.ImageHeight,
|
||||
Width = Math.Round(entityRobotModel.Entity.Width, 2),
|
||||
Length = Math.Round(entityRobotModel.Entity.Length, 2),
|
||||
NavigationType = entityRobotModel.Entity.NavigationType,
|
||||
}
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"CreateRobotModel : Hệ thống có lỗi xảy ra - {ex.Message}");
|
||||
return new(false, "Hệ thống có lỗi xảy ra");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPut]
|
||||
public async Task<MessageResult<RobotModelDto>> UpdateRobotModel([FromBody] RobotModelUpdateModel robotmodel)
|
||||
{
|
||||
try
|
||||
{
|
||||
var robotModelDb = await RobotDb.RobotModels.FindAsync(robotmodel.Id);
|
||||
if (robotModelDb == null) return new(false, $"Không tồn tại robot model id = {robotmodel.Id}");
|
||||
|
||||
if (robotmodel.ModelName != robotModelDb.ModelName && await RobotDb.RobotModels.AnyAsync(m => m.ModelName == robotmodel.ModelName))
|
||||
{
|
||||
return new(false, "Tên của robot model đã tồn tại, Hãy đặt tên khác!");
|
||||
}
|
||||
|
||||
robotModelDb.ModelName = robotmodel.ModelName;
|
||||
robotModelDb.OriginX = robotmodel.OriginX;
|
||||
robotModelDb.OriginY = robotmodel.OriginY;
|
||||
robotModelDb.Length = robotmodel.Length;
|
||||
robotModelDb.Width = robotmodel.Width;
|
||||
|
||||
await RobotDb.SaveChangesAsync();
|
||||
return new(true)
|
||||
{
|
||||
Data = new()
|
||||
{
|
||||
ModelName = robotModelDb.ModelName,
|
||||
OriginX = robotModelDb.OriginX,
|
||||
OriginY = robotModelDb.OriginY,
|
||||
NavigationType = robotModelDb.NavigationType,
|
||||
ImageWidth = robotModelDb.ImageWidth,
|
||||
ImageHeight = robotModelDb.ImageHeight,
|
||||
Width = Math.Round(robotModelDb.Width, 2),
|
||||
Length = Math.Round(robotModelDb.Length, 2),
|
||||
}
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"UpdateRobotModel : Hệ thống có lỗi xảy ra - {ex.Message}");
|
||||
return new(false, $"Hệ thống có lỗi xảy ra");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpDelete]
|
||||
[Route("{robotModelId}")]
|
||||
public async Task<MessageResult> DeleteRobotModel(Guid robotModelId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var robotModelDb = await RobotDb.RobotModels.FindAsync(robotModelId);
|
||||
if (robotModelDb == null) return new(false, $"Không tồn tại robot model id = {robotModelId}");
|
||||
|
||||
if (RobotDb.Robots.Any(r => r.ModelId == robotModelId)) return new(false, "Tồn tại robot đang sử dụng model này");
|
||||
|
||||
await RobotStorage.DeleteAsync("RobotModels", robotModelId.ToString(), CancellationToken.None);
|
||||
RobotDb.RobotModels.Remove(robotModelDb);
|
||||
await RobotDb.SaveChangesAsync();
|
||||
return new(true, "");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"DeleteRobotModel {robotModelId} : Hệ thống có lỗi xảy ra - {ex.Message}");
|
||||
return new(false, $"Hệ thống có lỗi xảy ra");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPut]
|
||||
[Route("image/{robotModelId}")]
|
||||
public async Task<MessageResult> UpdateImage(Guid robotModelId, [FromForm(Name = "image")] IFormFile image)
|
||||
{
|
||||
try
|
||||
{
|
||||
var robotModel = await RobotDb.RobotModels.FindAsync(robotModelId);
|
||||
if (robotModel == null) return new(false, $"Không tồn tại robot model id = {robotModelId}");
|
||||
|
||||
var imageStream = image.OpenReadStream();
|
||||
var imageUpdate = SixLabors.ImageSharp.Image.Load(imageStream);
|
||||
robotModel.ImageWidth = imageUpdate.Width;
|
||||
robotModel.ImageHeight = imageUpdate.Height;
|
||||
await RobotDb.SaveChangesAsync();
|
||||
|
||||
var (isSuccess, message) = await RobotStorage.UploadAsync("RobotModels", robotModelId.ToString(), image.OpenReadStream(), image.Length, image.ContentType, CancellationToken.None);
|
||||
return new(true, "");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"UpdateImage: Hệ thống có lỗi xảy ra - {ex.Message}");
|
||||
return new(false, $"Hệ thống có lỗi xảy ra");
|
||||
}
|
||||
}
|
||||
}
|
||||
246
RobotNet.RobotManager/Controllers/RobotsController.cs
Normal file
246
RobotNet.RobotManager/Controllers/RobotsController.cs
Normal file
@@ -0,0 +1,246 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using RobotNet.RobotManager.Data;
|
||||
using RobotNet.RobotManager.Services;
|
||||
using RobotNet.RobotShares.Dtos;
|
||||
using RobotNet.RobotShares.Enums;
|
||||
using RobotNet.Shares;
|
||||
|
||||
namespace RobotNet.RobotManager.Controllers;
|
||||
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class RobotsController(RobotEditorDbContext RobotDb, Services.RobotManager RobotManager, MapManager MapManager, LoggerController<RobotsController> Logger) : ControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
public async Task<MessageResult<IEnumerable<RobotDto>>> GetRobots()
|
||||
{
|
||||
try
|
||||
{
|
||||
var robots = await (from robot in RobotDb.Robots
|
||||
where !string.IsNullOrEmpty(robot.Name) && !string.IsNullOrEmpty(robot.RobotId)
|
||||
join robotmodel in RobotDb.RobotModels on robot.ModelId equals robotmodel.Id
|
||||
select new RobotDto()
|
||||
{
|
||||
Id = robot.Id,
|
||||
RobotId = robot.RobotId,
|
||||
Name = robot.Name,
|
||||
ModelName = robotmodel.ModelName,
|
||||
ModelId = robotmodel.Id,
|
||||
MapId = robot.MapId,
|
||||
NavigationType = robotmodel.NavigationType,
|
||||
Online = false,
|
||||
}).ToListAsync();
|
||||
foreach (var robot in robots)
|
||||
{
|
||||
var map = await MapManager.GetMapInfo(robot.MapId);
|
||||
if (map is not null && map.Data is not null) robot.MapName = map.Data.Name;
|
||||
else robot.MapName = string.Empty;
|
||||
var robotController = RobotManager[robot.RobotId];
|
||||
robot.Online = robotController is not null && robotController.IsOnline;
|
||||
robot.State = robotController is not null ? robotController.State : "OFFLINE";
|
||||
}
|
||||
return new(true) { Data = robots };
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"GetRobots: Hệ thống có lỗi xảy ra - {ex.Message}");
|
||||
return new(false, $"Hệ thống có lỗi xảy ra");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("{robotId}")]
|
||||
public async Task<MessageResult<RobotDto?>> GetRobot(string robotId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var robot = await RobotDb.Robots.FirstOrDefaultAsync(model => model.RobotId == robotId);
|
||||
if (robot is not null)
|
||||
{
|
||||
var map = await MapManager.GetMapInfo(robot.MapId);
|
||||
var robotModel = await RobotDb.RobotModels.FirstOrDefaultAsync(m => m.Id == robot.ModelId);
|
||||
return new(true)
|
||||
{
|
||||
Data = new RobotDto()
|
||||
{
|
||||
Id = robot.Id,
|
||||
RobotId = robot.RobotId,
|
||||
Name = robot.Name,
|
||||
ModelName = robotModel is not null ? robotModel.ModelName : string.Empty,
|
||||
ModelId = robot.ModelId,
|
||||
MapId = robot.MapId,
|
||||
MapName = map is not null && map.Data is not null ? map.Data.Name : string.Empty,
|
||||
NavigationType = robotModel is not null ? robotModel.NavigationType : NavigationType.Differential,
|
||||
Online = RobotManager[robot.RobotId] is not null,
|
||||
}
|
||||
};
|
||||
}
|
||||
return new(false, "RobotId không tồn tại");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"GetRobot: Hệ thống có lỗi xảy ra - {ex.Message}");
|
||||
return new(false, $"Hệ thống có lỗi xảy ra");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<MessageResult<RobotDto>> CreateRobot([FromBody] RobotCreateModel robot)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (await RobotDb.Robots.AnyAsync(r => r.Name == robot.Name)) return new(false, "Tên của robot đã tồn tại, Hãy đặt tên khác!");
|
||||
if (await RobotDb.Robots.AnyAsync(r => r.RobotId == robot.RobotId)) return new(false, "Robot Id đã tồn tại, Hãy đặt tên khác!");
|
||||
|
||||
var robotModel = await RobotDb.RobotModels.FirstOrDefaultAsync(m => m.Id == robot.ModelId);
|
||||
if (robotModel is null) return new(false, "Robot Model không tồn tại");
|
||||
|
||||
var entityRobotModel = await RobotDb.Robots.AddAsync(new Robot()
|
||||
{
|
||||
RobotId = robot.RobotId,
|
||||
Name = robot.Name,
|
||||
ModelId = robot.ModelId,
|
||||
});
|
||||
|
||||
await RobotDb.SaveChangesAsync();
|
||||
|
||||
return new(true)
|
||||
{
|
||||
Data = new RobotDto()
|
||||
{
|
||||
Id = entityRobotModel.Entity.Id,
|
||||
RobotId = entityRobotModel.Entity.RobotId,
|
||||
Name = entityRobotModel.Entity.Name,
|
||||
ModelId = entityRobotModel.Entity.ModelId,
|
||||
ModelName = robotModel.ModelName,
|
||||
NavigationType = robotModel.NavigationType,
|
||||
}
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"CreateRobot : Hệ thống có lỗi xảy ra - {ex.Message}");
|
||||
return new(false, $"Hệ thống có lỗi xảy ra");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPut]
|
||||
public async Task<MessageResult> UpdateRobot([FromBody] RobotUpdateModel robot)
|
||||
{
|
||||
try
|
||||
{
|
||||
var robotDb = await RobotDb.Robots.FindAsync(robot.RobotId);
|
||||
if (robotDb == null) return new(false, $"Không tồn tại robot model id = {robot.RobotId}");
|
||||
|
||||
if (robotDb.Name != robot.Name && await RobotDb.Robots.AnyAsync(r => r.Name == robot.Name))
|
||||
{
|
||||
return new(false, "Tên của robot đã tồn tại, Hãy đặt tên khác!");
|
||||
}
|
||||
|
||||
robotDb.Name = robot.Name;
|
||||
robotDb.ModelId = robot.ModelId;
|
||||
robotDb.MapId = robot.MapId;
|
||||
|
||||
await RobotDb.SaveChangesAsync();
|
||||
return new(true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"UpdateRobot : Hệ thống có lỗi xảy ra - {ex.Message}");
|
||||
return new(false, $"Hệ thống có lỗi xảy ra");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpDelete]
|
||||
[Route("{robotId}")]
|
||||
public async Task<MessageResult> DeleteRobot(string robotId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var robotDb = await RobotDb.Robots.FirstOrDefaultAsync(r => r.RobotId == robotId);
|
||||
if (robotDb == null) return new(false, $"Không tồn tại robot id = {robotId}");
|
||||
|
||||
RobotManager.DeleteRobot(robotId);
|
||||
RobotDb.Robots.Remove(robotDb);
|
||||
await RobotDb.SaveChangesAsync();
|
||||
return new(true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"DeleteRobot : Hệ thống có lỗi xảy ra - {ex.Message}");
|
||||
return new(false, $"Hệ thống có lỗi xảy ra");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("Model/{robotId}")]
|
||||
public async Task<MessageResult<RobotModelDto?>> GetModel(string robotId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var robot = await RobotDb.Robots.FirstOrDefaultAsync(model => model.RobotId == robotId);
|
||||
if (robot is not null)
|
||||
{
|
||||
var robotModel = await RobotDb.RobotModels.FirstOrDefaultAsync(m => m.Id == robot.ModelId);
|
||||
if (robotModel is null) return new(false, "Robot model không tồn tại");
|
||||
return new(true)
|
||||
{
|
||||
Data = new RobotModelDto()
|
||||
{
|
||||
Id = robotModel.Id,
|
||||
ModelName = robotModel.ModelName,
|
||||
OriginX = robotModel.OriginX,
|
||||
OriginY = robotModel.OriginY,
|
||||
NavigationType = robotModel.NavigationType,
|
||||
ImageWidth = robotModel.ImageWidth,
|
||||
ImageHeight = robotModel.ImageHeight,
|
||||
Width = robotModel.Width,
|
||||
Length = robotModel.Length
|
||||
}
|
||||
};
|
||||
}
|
||||
return new(false, "RobotId không tồn tại");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"GetModel : Hệ thống có lỗi xảy ra - {ex.Message}");
|
||||
return new(false, $"Hệ thống có lỗi xảy ra");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("simulation")]
|
||||
public MessageResult CreateRobotSimulation([FromBody] IEnumerable<string> robotIds)
|
||||
{
|
||||
try
|
||||
{
|
||||
RobotManager.AddRobotSimulation(robotIds);
|
||||
return new(true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"CreateRobotSimulation: Hệ thống có lỗi xảy ra - {ex.Message}");
|
||||
return new(false, $"Hệ thống có lỗi xảy ra");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("simulation")]
|
||||
[AllowAnonymous]
|
||||
public MessageResult DeleteRobotSimulation([FromQuery] string robotId)
|
||||
{
|
||||
try
|
||||
{
|
||||
RobotManager.DeleteRobot(robotId);
|
||||
return new(true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"CreateRobotSimulation: Hệ thống có lỗi xảy ra - {ex.Message}");
|
||||
return new(false, $"Hệ thống có lỗi xảy ra");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using RobotNet.RobotManager.Services.OpenACS;
|
||||
using RobotNet.RobotShares.OpenACS;
|
||||
using RobotNet.Shares;
|
||||
|
||||
namespace RobotNet.RobotManager.Controllers;
|
||||
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class TrafficACSRequestController(TrafficACS TrafficACS, Services.RobotManager RobotManager) : ControllerBase
|
||||
{
|
||||
[HttpPost]
|
||||
public async Task<MessageResult<bool>> UpdateTrafficSetting([FromBody] TrafficACSRequestModel model)
|
||||
=> model.Type switch
|
||||
{
|
||||
TrafficRequestType.IN => await TrafficACS.RequestIn(model.RobotId, model.ZoneId),
|
||||
TrafficRequestType.OUT => await TrafficACS.RequestOut(model.RobotId, model.ZoneId),
|
||||
_ => await TrafficACS.RequestOut(model.RobotId, model.ZoneId),
|
||||
};
|
||||
|
||||
[HttpGet]
|
||||
public RobotACSLockedDto[] GetRobotLocked() => RobotManager.GetRobotACSLocked();
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using RobotNet.RobotManager.Services;
|
||||
using RobotNet.RobotManager.Services.Traffic;
|
||||
using RobotNet.RobotShares.Dtos;
|
||||
using RobotNet.RobotShares.Models;
|
||||
using RobotNet.Shares;
|
||||
|
||||
namespace RobotNet.RobotManager.Controllers;
|
||||
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class TrafficManagerController(TrafficManager TrafficManager, LoggerController<TrafficManagerController> Logger) : ControllerBase
|
||||
{
|
||||
[HttpPut]
|
||||
public MessageResult UpdateLocker(UpdateAgentLockerModel model)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (TrafficManager.TrafficMaps.TryGetValue(model.MapId, out TrafficMap? trafficmap) && trafficmap is not null)
|
||||
{
|
||||
trafficmap.UpdateLocker(model.AgentId, [.. model.LockedNodes]);
|
||||
}
|
||||
return new(true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"UpdateLocker: Hệ thống có lỗi xảy ra - {ex.Message}");
|
||||
return new(false, $"Hệ thống có lỗi xảy ra");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpDelete]
|
||||
[Route("{mapId}/{robotId}")]
|
||||
public MessageResult DeleteAgent([FromRoute] Guid mapId, [FromRoute] string robotId)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (TrafficManager.TrafficMaps.TryGetValue(mapId, out TrafficMap? trafficmap) && trafficmap is not null)
|
||||
{
|
||||
trafficmap.Locked.Remove(robotId);
|
||||
}
|
||||
return TrafficManager.DeleteAgent(robotId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning($"DeleteAgent: 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