using AmrControl.ADS;
using AmrControl.Common;
using AmrControl.DB.Models;
using AmrControl.Dto;
using AmrControl.JGR;
using AmrControl.services;
using AmrControl.Vo;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.Formula.Functions;
using Org.BouncyCastle.Asn1.Ocsp;
using System.Threading.Tasks;
namespace AmrControl.Controllers
{
[ApiController]
public class JgrController : ControllerBase
{
///
/// 给车的命令
///
///
///
[HttpPost]
[Route("api/v1/jgr/cmd")]
public HttpStatus SetCmd([FromBody] JgrDebugDto debugDto)
{
string str = JGRManager.GetInstance().SetCmd(debugDto);
if(str == "0")
{
HttpStatus status = new HttpStatus();
status.Code = Dto.Code.Success;
return status;
}
else
{
HttpStatus status = new HttpStatus();
status.Code = Dto.Code.Error;
status.Data = str;
return status;
}
}
[HttpGet]
[Route("api/v1/jgr/call-car")]
public HttpStatus CallCar([FromQuery] string stanCode)
{
HttpStatus status = new HttpStatus();
if (!string.IsNullOrEmpty(stanCode) && !string.IsNullOrWhiteSpace(stanCode))
{
var station = JGRManager.GetInstance().getStationByNo(stanCode);
if (station == null || station == default)
{
status.Code = Dto.Code.Error;
status.Msg = "非法的工站编号";
return status;
}
//获取空的地车
string taskId = TaskExecManager.getInstance().genTaskId();
Proxy taskProxy = new Proxy(() =>
{
JGRManager.GetInstance().moveCarToDestAsync(JgrType.RailCar, station.positionX, station.positionY);
});
TaskExecManager.getInstance().addTask(taskProxy, taskId);
status.Code = Dto.Code.Success;
status.Data = taskId;
return status;
}
status.Code = Dto.Code.Error;
status.Msg = "非法的工站编号";
return status;
}
///
/// 呼叫空闲轨道车到工位
///
///
///
[HttpPost]
[Route("/api/v1/jgr/call-amr-car")]
public HttpStatus CallAmrCar([FromBody] CallAmrCarDTO dto)
{
HttpStatus status = new HttpStatus();
if (!string.IsNullOrEmpty(dto.stanCode) && !string.IsNullOrWhiteSpace(dto.stanCode))
{
var station = JGRManager.GetInstance().getStationByNo(dto.stanCode);
if (station == null || station == default)
{
status.Code = Dto.Code.Error;
status.Msg = "非法的工站编号";
return status;
}
string taskId = TaskExecManager.getInstance().genTaskId();
Proxy taskProxy = new Proxy(() =>
{
JGRManager.GetInstance().moveCarToDestAsync(JgrType.RailCar, station.positionX, station.positionY);
});
TaskExecManager.getInstance().addTask(taskProxy, taskId);
status.Code = Dto.Code.Success;
status.Data = taskId;
return status;
}
status.Code = Dto.Code.Error;
status.Msg = "非法的工站编号";
return status;
}
///
/// 移动车辆到指定位置
///
///
///
[HttpPost]
[Route("/api/v1/jgr/call-car-to-position")]
public HttpStatus callCarToPosition([FromBody] MoveCarToPosDTO dto)
{
HttpStatus status = new();
if (!string.IsNullOrEmpty(dto.carId) && !string.IsNullOrWhiteSpace(dto.carId))
{
//查找车辆信息
List carStatus = JGRManager.GetInstance().GetJgrs();
var carInfo = carStatus.Where(m => m.jgrSN == dto.carId).FirstOrDefault();
if (carInfo == null)
{
status.Code = Dto.Code.Error;
status.Msg = "非法的车辆编号信息";
return status;
}
string taskId = TaskExecManager.getInstance().genTaskId();
int dx = -1, dy = -1;
if (!string.IsNullOrEmpty(dto.stanCode) && !string.IsNullOrWhiteSpace(dto.stanCode))
{
var station = JGRManager.GetInstance().getStationByNo(dto.stanCode);
if (station == null || station == default)
{
status.Code = Dto.Code.Error;
status.Msg = "非法的工站编号";
return status;
}
dx = station.positionX;
dy = station.positionY;
} else
{
//判断新坐标数据是否存在
if(dto.x>0 && dto.y>0)
{
dx = dto.x;
dy = dto.y;
} else
{
status.Code = Dto.Code.Error;
status.Msg = "位置坐标异常";
return status;
}
}
if (dx > 0 && dy > 0)
{
Proxy taskProxy = new Proxy(() =>
{
JGRManager.GetInstance().moveCarToDestAsync(carInfo, dx, dy);
});
TaskExecManager.getInstance().addTask(taskProxy, taskId);
status.Code = Dto.Code.Success;
status.Data = taskId;
return status;
}
else
{
status.Code = Dto.Code.Error;
status.Msg = "位置坐标异常";
return status;
}
}
status.Code = Dto.Code.Error;
status.Msg = "非法的车辆编号信息";
return status;
}
///
/// 查询指定车辆状态
///
///
///
[HttpGet]
[Route("api/v1/jgr/query-car-state")]
public HttpStatus QueryCarState([FromQuery] string carId)
{
return new() {
Code = Dto.Code.Success,
Data = JGRManager.GetInstance().Get_Tc_Models().Where(item => item.jgrSN == carId).First()
};
}
///
/// 设置车或者充电桩的当前位置
///
///
///
[HttpPost]
[Route("api/v1/jgr/xy")]
public HttpStatus SetXY([FromBody] NewJgrDto pos)
{
HttpStatus status = new HttpStatus();
status.Code = Dto.Code.Success;
status.Data = JGRManager.GetInstance().SetNewXY(pos.SN, pos.positionX, pos.positionY);
return status;
}
}
}