using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_DTO.Agv;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Model.Models;
using static Dm.net.buffer.ByteArrayBuffer;
using WIDESEAWCS_QuartzJob;
namespace WIDESEAWCS_Tasks
{
public partial class AGV_CSJJob
{
///
/// 下发AGV任务
///
public void SendAGVTask()
{
try
{
var newTasks = _taskService.Db.Queryable().Where(x => x.TaskState == TaskStatusEnum.AGV_Execute.ObjToInt() && nameof(AGV_CSJJob).Contains(x.DeviceCode)).ToList().OrderBy(x => x.Grade).ThenBy(x => x.CreateDate).ToList();
foreach (var agvTask in newTasks)
{
try
{
AgvTaskDTO taskDTO = new AgvTaskDTO()
{
ReqCode = Guid.NewGuid().ToString().Replace("-", ""),
TaskTyp = AgvTaskType(agvTask.TaskType, agvTask.DeviceCode),
PositionCodePath = new List()
{
new CodePath()
{
type="00",
positionCode=agvTask.CurrentAddress
},
new CodePath()
{
type="00",
positionCode=agvTask.NextAddress
}
},
TaskCode = agvTask.AgvTaskNum,
PodTyp = agvTask.PalletType < 3 ? "XX" : "DD",
};
WebResponseContent content = _taskService.AgvSendTask(taskDTO);
if (content.Status)
{
agvTask.TaskState = TaskStatusEnum.AGV_Executing.ObjToInt();
//agvTask.Remark = content.Data.ObjToString();
}
else
{
agvTask.TaskState = TaskStatusEnum.Exception.ObjToInt();
//agvTask.Remark = content.Data.ObjToString();
agvTask.ExceptionMessage = content.Message;
}
}
catch (Exception ex)
{
agvTask.TaskState = TaskStatusEnum.Exception.ObjToInt();
//agvTask.Remark = content.Data.ObjToString();
agvTask.ExceptionMessage = ex.Message;
WriteError(nameof(AGV_CSJJob), ex.Message, ex);
}
}
_taskService.UpdateData(newTasks);
}
catch (Exception ex)
{
WriteError(nameof(AGV_CSJJob), ex.Message, ex);
}
}
///
/// 下发AGV继续执行任务
///
public void SendAGVWaitToTask()
{
try
{
var WaitToTasks = _taskService.Db.Queryable().Where(x => x.TaskState == TaskStatusEnum.AGV_WaitToExecute.ObjToInt() && nameof(AGV_CSJJob).Contains(x.DeviceCode)).ToList().OrderBy(x => x.Grade).ThenBy(x => x.CreateDate).ToList();
foreach (var WaitToTask in WaitToTasks)
{
if (WaitToTask.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)
{
Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.AGVStationCode == WaitToTask.NextAddress);
if (stationManger == null)
{
continue;
}
IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode);
if (device == null)
{
continue;
}
OtherDevice otherDevice = (OtherDevice)device;
bool canPut = otherDevice.GetValue(GroundStationDBName.R_IsCanPut, stationManger.StationCode);
bool requestPut = otherDevice.GetValue(GroundStationDBName.W_PutRequest, stationManger.StationCode);
if (!requestPut)
{
otherDevice.SetValue(GroundStationDBName.W_PutRequest, true, stationManger.StationCode);
continue;
}
else if (!canPut)
{
continue;
}
}
else
{
Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.AGVStationCode == WaitToTask.CurrentAddress);
if (stationManger == null)
{
continue;
}
IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode);
if (device == null)
{
continue;
}
OtherDevice otherDevice = (OtherDevice)device;
bool canTake = otherDevice.GetValue(GroundStationDBName.R_IsCanTake, stationManger.StationCode);
bool requestTake = otherDevice.GetValue(GroundStationDBName.W_TakeRequest, stationManger.StationCode);
if (!requestTake)
{
otherDevice.SetValue(GroundStationDBName.W_TakeRequest, true, stationManger.StationCode);
continue;
}
else if (!canTake)
{
continue;
}
}
AgvSecureReplyDTO replyDTO = new AgvSecureReplyDTO()
{
ReqCode = Guid.NewGuid().ToString().Replace("-", ""), //WaitToTask.TaskNum.ToString(),
taskCode = WaitToTask.AgvTaskNum,
};
WebResponseContent content = _taskService.AgvSecureReply(replyDTO);
if (content.Status)
{
WaitToTask.TaskState = TaskStatusEnum.AGV_Executing.ObjToInt();
}
else
{
WaitToTask.TaskState = TaskStatusEnum.Exception.ObjToInt();
WaitToTask.ExceptionMessage = content.Message;
}
}
_taskService.UpdateData(WaitToTasks);
}
catch (Exception ex)
{
Console.Out.WriteLine(nameof(AGV_CSJJob) + ":" + ex.Message);
}
}
public string AgvTaskType(int TaskType, string DeviceCode)
{
switch (DeviceCode)
{
case "AGV_CSJ":
{
return TaskType == TaskTypeEnum.ProductionReturn.ObjToInt() ? "23" : "24";
}
case "AGV_ZH":
{
if (TaskType == TaskTypeEnum.InboundXB.ObjToInt())
return "20";
else if (TaskType == TaskTypeEnum.InboundJT.ObjToInt())
return "21";
else return "22";
}
default:
throw new Exception($"设备编号错误");
}
}
}
}