first commit -push
This commit is contained in:
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user