using Autofac.Core;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Text;
using System.Text.RegularExpressions;
using WIDESEA_DTO.Agv;
using WIDESEA_External.Model;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.APIEnum;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Enums;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core.LogHelper;
using WIDESEAWCS_DTO;
using WIDESEAWCS_DTO.Agv;
using WIDESEAWCS_DTO.TaskInfo;
using WIDESEAWCS_IBasicInfoRepository;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.Models;
using WIDESEAWCS_QuartzJob.Repository;
using WIDESEAWCS_Tasks;
using WIDESEAWCS_Tasks.DBNames;
using static Dm.net.buffer.ByteArrayBuffer;
namespace WIDESEAWCS_Server.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class AGVController : ControllerBase
{
private readonly IStationMangerRepository _stationMangerRepository;
private readonly ITaskService _taskService;
private readonly ITaskRepository _taskRepository;
private readonly IRouterRepository _routerRepository;
public AGVController(IStationMangerRepository stationMangerRepository, ITaskService taskService, ITaskRepository taskRepository,IRouterRepository routerRepository)
{
_stationMangerRepository = stationMangerRepository;
_taskService = taskService;
_taskRepository = taskRepository;
_routerRepository = routerRepository;
}
///
/// AGV任务更新
///
///
[HttpPost, HttpGet, Route("Callback"), AllowAnonymous]
public WebResponseContent? Callback([FromBody]AgvUpdateDTO agvUpdateDTO)
{
WebResponseContent content = new WebResponseContent();
try
{
var task = _taskRepository.QueryFirst(x => (agvUpdateDTO.ContainerCode ?? "")==x.PalletCode);
switch (agvUpdateDTO.MissionStatus)
{
case nameof(AGVStatusEnum.PICKER_RECEIVE):
if (task == null) throw new Exception($"未找到料箱【{agvUpdateDTO.ContainerCode}】任务");
_taskService.UpdateTask(task, TaskStatusEnum.AGV_TakeFinish);
break;
case nameof(AGVStatusEnum.PICKER_SEND):
if (task == null) throw new Exception($"未找到料箱【{agvUpdateDTO.ContainerCode}】任务");
_taskService.TaskCompleted(task.TaskNum);
break;
case nameof(AGVStatusEnum.WAITFEEDBACK):
//AGV放行
List stationMangers = _stationMangerRepository.QueryData(x=>x.StationType==StationTypeEnum.StationType_OnlyInbound.ObjToInt());
if (!stationMangers.Select(x=>x.StationCode).Contains(agvUpdateDTO.CurrentPosition))
{
WebResponseContent responseContent = _taskService.AgvTaskFlow(agvUpdateDTO.MissionCode);
if (!responseContent.Status) throw new Exception($"{responseContent.Message}");
}
break;
default:
break;
}
content.OK();
}
catch (Exception ex)
{
content.Error(ex.Message);
}
return content;
}
///
/// AGV任务请求
///
///
[HttpPost, HttpGet, Route("WorkRequest"), AllowAnonymous]
public AgvTaskReqContent WorkRequest([FromBody] AgvTaskRequestDTO agvTaskRequestDTO)
{
AgvTaskReqContent content = new AgvTaskReqContent();
try
{
//获取任务
Dt_Task? taskExist = _taskRepository.QueryFirst(x=>x.PalletCode==agvTaskRequestDTO.ContainerCode) ?? throw new Exception($"未找到料箱{agvTaskRequestDTO.ContainerCode}任务");
//获取站台
Dt_StationManger? stationManger = _stationMangerRepository.QueryFirst(x => x.StationCode == agvTaskRequestDTO.PositionId) ?? throw new Exception($"未找到{agvTaskRequestDTO.PositionId}站台位置");
IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode) ?? throw new Exception($"未找到对应设备{stationManger.StationDeviceCode}");
OtherDevice commonConveyorLine = (OtherDevice)device;
if (stationManger.StationType==StationTypeEnum.StationType_OnlyOutbound.ObjToInt())
{
short IsPut = commonConveyorLine.Communicator.Read("0");
if (IsPut != 256) throw new Exception($"{agvTaskRequestDTO.PositionId}禁止放箱");
}
else
{
short IsTake = commonConveyorLine.Communicator.Read("11");
if (IsTake != 256) throw new Exception($"{agvTaskRequestDTO.PositionId}禁止取箱");
}
content.OK();
}
catch (Exception ex)
{
content.Error(ex.Message);
}
return content;
}
///
/// AGV作业完成
///
///
[HttpPost, HttpGet, Route("WorkFinish"), AllowAnonymous]
public AgvTaskReqContent WorkFinish([FromBody] AgvTaskRequestDTO agvTaskRequestDTO)
{
AgvTaskReqContent content = new AgvTaskReqContent();
try
{
//获取站台
Dt_StationManger? stationManger = _stationMangerRepository.QueryFirst(x => x.StationCode == agvTaskRequestDTO.PositionId) ?? throw new Exception($"未找到{agvTaskRequestDTO.PositionId}站台位置");
IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode) ?? throw new Exception($"未找到对应设备{stationManger.StationDeviceCode}");
OtherDevice commonConveyorLine = (OtherDevice)device;
if (stationManger.StationType == StationTypeEnum.StationType_OnlyOutbound.ObjToInt())
{
commonConveyorLine.Communicator.Write("21", new byte[] { 1, 0 });
}
else
{
commonConveyorLine.Communicator.Write("23", new byte[] { 1, 0 });
}
content.OK();
}
catch (Exception ex)
{
content.Error(ex.Message);
}
return content;
}
}
}