using LogLibrary.Log;
|
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using WIDESEA_DTO.AGV;
|
using WIDESEA_IServices;
|
using WIDESEA_IStorageBasicRepository;
|
using WIDESEA_IStorageSocketServices;
|
using WIDESEA_IStorageTaskRepository;
|
using WIDESEA_IStoragIntegrationServices;
|
using WIDESEA_Model.Models.AGV;
|
using WIDESEA_Repository;
|
using WIDESEA_StorageSocketServices;
|
using WIDESEAWCS_BasicInfoRepository;
|
using WIDESEAWCS_Model.Models;
|
|
namespace WIDESEA_StoragIntegrationServices
|
{
|
public partial class AGVService : IAGVService
|
{
|
private readonly ILocationInfoRepository _locationRepository;
|
private readonly IDt_TaskRepository BaseDal;
|
private readonly IStockInfoRepository _stockInfoRepository;
|
private readonly ISys_ConfigService _configService;
|
private readonly IDt_StationManagerRepository _stationManagerRepository;
|
private readonly LogFactory LogFactory = new LogFactory();
|
private readonly ISocketClientServices _Socket;
|
private readonly IMapper _mapper;
|
private readonly IDt_DeviceInfoRepository _deviceInfoRepository;
|
|
public AGVService(ILocationInfoRepository locationRepository,IDt_TaskRepository taskRepository,IStockInfoRepository stockInfoRepository, ISys_ConfigService configService, IDt_StationManagerRepository stationManagerRepository,IDt_DeviceInfoRepository deviceInfoRepository, IMapper mapper, ISocketClientServices socketClientServices)
|
{
|
_locationRepository = locationRepository;
|
BaseDal = taskRepository;
|
_stockInfoRepository = stockInfoRepository;
|
_configService = configService;
|
_stationManagerRepository = stationManagerRepository;
|
_deviceInfoRepository = deviceInfoRepository;
|
_mapper = mapper;
|
_Socket= socketClientServices;
|
}
|
#region 外部接口方法
|
|
/// <summary>
|
/// 请求入库任务
|
/// </summary>
|
/// <param name="PallteCode"></param>
|
/// <param name="SourceAddress"></param>
|
/// <returns></returns>
|
public WebResponseContent RequestInTask(string PalletCode, string SourceAddress)
|
{
|
WebResponseContent content = new WebResponseContent();
|
try
|
{
|
Dt_StationManager Instation = _stationManagerRepository.QueryFirst(x => x.stationName == SourceAddress && x.stationType == 1);
|
List<Dt_StationManager> Outstation = _stationManagerRepository.QueryData(x => x.stationType == 2).ToList();
|
if (Instation == null)
|
{
|
return content.Error("未找到入库站台信息");
|
}
|
Dt_Task task = BaseDal.QueryFirst(x => x.PalletCode == PalletCode);
|
if (task != null)
|
{
|
return content.Error("该托盘已存在任务");
|
}
|
List<OutStationStatus> outStationStatus = new List<OutStationStatus>();
|
foreach (var item in Outstation)
|
{
|
StationStatus station = _Socket.GetStationStatus(item.stationName);
|
var taskStation = BaseDal.QueryFirst(x => x.SourceAddress == item.stationName || x.TargetAddress == item.stationName);
|
outStationStatus.Add(new OutStationStatus()
|
{
|
StationName = item.HostName,
|
StationEnable = station.StationEnable == "1" ? "0" : "1",
|
IsDistributionTask = taskStation == null ? "0" : "1",
|
Spare1 = "00"
|
});
|
}
|
AGVStatusRespone status = _Socket.GetAGVStatus();
|
HOSTAGVStatus AgvStatus = new HOSTAGVStatus()
|
{
|
RuntimeStatus = _Socket.CapitalizeFirstLetter(status.RuntimeStatus),
|
AutoStatus = status.AutoStatus== "MaintenanceMode"?"1":"0",
|
Ready = status.AutoStatus == "MaintenanceMode" ? "0" : "1",
|
};
|
InStationStatus inStationStatus = new InStationStatus()
|
{
|
StationName = Instation.stationName,
|
StationEnable = "0",
|
IsDistributionTask = "0",
|
PallteCode = task.PalletCode,
|
};
|
_Socket.DeviceRequestInbound(AgvStatus, outStationStatus, inStationStatus);
|
return content.OK("已下发入库任务");
|
}
|
catch (Autofac.Core.DependencyResolutionException ex)
|
{
|
return content.Error(ex.Message);
|
}
|
catch (Exception ex)
|
{
|
return content.Error(ex.Message);
|
}
|
|
|
}
|
|
public WebResponseContent ReceiveAGVRuntimeStatus(AGVStatus Status)
|
{
|
WebResponseContent content = new WebResponseContent();
|
try
|
{
|
Dt_DeviceInfo device = _deviceInfoRepository.QueryFirst(x => x.DeviceName == Status.AGVName);
|
if (device != null)
|
{
|
device.RuntimeStatus = Status.RuntimeStatus;
|
device.AutoStatus = Status.AutoStatus;
|
_deviceInfoRepository.UpdateData(device);
|
switch (Status.RuntimeStatus)
|
{
|
case "Run":
|
//运行
|
_Socket.DeviceStateReport("R");
|
Thread.Sleep(500);
|
break;
|
case "Idle":
|
//关机
|
_Socket.DeviceStateReport("I");
|
Thread.Sleep(500);
|
break;
|
case "Trouble":
|
//故障
|
_Socket.DeviceStateReport("T");
|
Thread.Sleep(500);
|
break;
|
case "Pause":
|
//暂停
|
_Socket.DeviceStateReport("S");
|
Thread.Sleep(500);
|
break;
|
case "Charge":
|
//充电
|
_Socket.DeviceStateReport("C");
|
Thread.Sleep(500);
|
break;
|
case "PowerOn":
|
//开机
|
_Socket.DeviceStateReport("P");
|
Thread.Sleep(500);
|
break;
|
case "PowerOFF":
|
//关机
|
_Socket.DeviceStateReport("O");
|
Thread.Sleep(500);
|
break;
|
default: break;
|
}
|
switch (Status.AutoStatus)
|
{
|
case "MaintenanceMode":
|
//手动
|
_Socket.DeviceAutoStatusReport("1");
|
Thread.Sleep(500);
|
break;
|
case "ControlMode":
|
//自动
|
_Socket.DeviceAutoStatusReport("0");
|
Thread.Sleep(500);
|
break;
|
default: break;
|
}
|
}
|
else
|
{
|
Dt_DeviceInfo deviceInfo = new Dt_DeviceInfo()
|
{
|
DeviceName = Status.AGVName,
|
RuntimeStatus = Status.RuntimeStatus,
|
AutoStatus = Status.AutoStatus,
|
};
|
_deviceInfoRepository.AddData(deviceInfo);
|
}
|
return content.OK();
|
}
|
catch (Exception ex)
|
{
|
return content.Error(ex.Message);
|
}
|
}
|
|
|
public WebResponseContent AGVStartOrEndJob(string Status, int TaskNum)
|
{
|
WebResponseContent content = new WebResponseContent();
|
try
|
{
|
Dt_Task task = BaseDal.QueryFirst(x => x.TaskNum == TaskNum);
|
if (task != null)
|
{
|
switch (task.TaskType)
|
{
|
case (int)TaskInboundTypeEnum.Inbound:
|
//入库
|
if (Status == "Start")
|
{
|
_Socket.JobStartOrEnd(Status.Substring(0, 1), task.SourceAddress, task.TargetAddress, "I", task.PalletCode);
|
task.TaskState = (int)TaskInStatusEnum.AGV_InExecuting;
|
}
|
else if (Status == "Loadel")
|
{
|
_Socket.PalletActionReport(Status.Substring(0, 1), task.SourceAddress, task.TargetAddress, "I", task.PalletCode);
|
}
|
else
|
{
|
_Socket.PalletActionReport(Status.Substring(0, 1), task.SourceAddress, task.TargetAddress, "I", task.PalletCode);
|
Thread.Sleep(2000);
|
_Socket.PalletActionReport(task.SourceAddress, task.TargetAddress, "I", task.PalletCode);
|
task.TaskState = (int)TaskInStatusEnum.AGV_InFinish;
|
}
|
break;
|
case (int)TaskOutboundTypeEnum.Outbound:
|
//出库
|
if (Status == "Start")
|
{
|
_Socket.JobStartOrEnd(Status.Substring(0, 1), task.SourceAddress, task.TargetAddress, "O", task.PalletCode);
|
task.TaskState = (int)TaskOutStatusEnum.AGV_OutExecuting;
|
}
|
else if (Status == "Loadel")
|
{
|
_Socket.PalletActionReport(Status.Substring(0, 1), task.SourceAddress, task.TargetAddress, "O", task.PalletCode);
|
}
|
else
|
{
|
_Socket.PalletActionReport(Status, task.SourceAddress, task.TargetAddress, "O", task.PalletCode);
|
Thread.Sleep(2000);
|
_Socket.PalletActionReport(task.SourceAddress, task.TargetAddress, "O", task.PalletCode);
|
task.TaskState = (int)TaskOutStatusEnum.AGV_OutFinish;
|
}
|
break;
|
case (int)TaskRelocationTypeEnum.Relocation:
|
if (Status == "Start")
|
{
|
_Socket.JobStartOrEnd(Status.Substring(0, 1), task.SourceAddress, task.TargetAddress, "R", task.PalletCode);
|
task.TaskState = (int)TaskRelocationStatusEnum.AGV_RelocationExecuting;
|
}
|
else if (Status == "Loadel")
|
{
|
_Socket.PalletActionReport(Status.Substring(0, 1), task.SourceAddress, task.TargetAddress, "R", task.PalletCode);
|
}
|
else
|
{
|
_Socket.PalletActionReport(Status, task.SourceAddress, task.TargetAddress, "R", task.PalletCode);
|
Thread.Sleep(2000);
|
_Socket.PalletActionReport(task.SourceAddress, task.TargetAddress, "R", task.PalletCode);
|
task.TaskState = (int)TaskRelocationStatusEnum.AGV_RelocationFinish;
|
}
|
break;
|
case (int)TaskStationTypeEnum.StationToStation:
|
if (Status == "Start")
|
{
|
_Socket.JobStartOrEnd(Status.Substring(0, 1), task.SourceAddress, task.TargetAddress, "S", task.PalletCode);
|
task.TaskState = (int)TaskOutStatusEnum.AGV_OutExecuting;
|
}
|
else if (Status == "Loadel")
|
{
|
_Socket.PalletActionReport(Status.Substring(0, 1), task.SourceAddress, task.TargetAddress, "S", task.PalletCode);
|
}
|
else
|
{
|
_Socket.PalletActionReport(Status, task.SourceAddress, task.TargetAddress, "S", task.PalletCode);
|
Thread.Sleep(2000);
|
_Socket.PalletActionReport(task.SourceAddress, task.TargetAddress, "S", task.PalletCode);
|
task.TaskState = (int)TaskOutStatusEnum.AGV_OutFinish;
|
}
|
break;
|
default: break;
|
}
|
BaseDal.Update(task);
|
return content.OK();
|
}
|
else
|
{
|
return content.Error("未找到任务!");
|
}
|
}
|
catch (Exception ex)
|
{
|
return content.Error(ex.Message);
|
}
|
}
|
|
|
public WebResponseContent DeviceErrorResponse(string Message,int TaskNum)
|
{
|
WebResponseContent content = new WebResponseContent();
|
try
|
{
|
var task = BaseDal.QueryFirst(x => x.TaskNum == TaskNum);
|
if (task != null)
|
{
|
switch(Message)
|
{
|
case "RepeatInbound":
|
|
break;
|
case "EmptyOutbound":
|
|
break;
|
default:break;
|
}
|
}
|
return content.OK();
|
}
|
catch (Exception ex)
|
{
|
return content.Error(ex.Message);
|
}
|
}
|
public WebResponseContent RequestOutTask(string PalletCode, string SourceAddress, string TargetAddress)
|
{
|
WebResponseContent content = new WebResponseContent();
|
try
|
{
|
//var tasks = BaseDal.QueryFirst(x => x.PalletCode == PalletCode);
|
//if (tasks != null)
|
//{
|
// return content.Error("该托盘已存在任务");
|
//}
|
Dt_Task newTask = new Dt_Task()
|
{
|
TaskNum = BaseDal.GetTaskNo().Result,
|
SourceAddress = SourceAddress,
|
CurrentAddress = SourceAddress,
|
TargetAddress = TargetAddress,
|
NextAddress = TargetAddress,
|
Grade = 1,
|
PalletCode = PalletCode,
|
TaskType = (int)TaskOutboundTypeEnum.Outbound,
|
TaskState = (int)TaskOutStatusEnum.OutNew,
|
Dispatchertime = DateTime.Now,
|
SeqNo = Convert.ToInt32(1),
|
CommandID = Convert.ToInt32(101)
|
};
|
task_call task_Call = new task_call()
|
{
|
d_task_type = newTask.TaskType == (int)TaskTypeEnum.Inbound ? 1 : 2,
|
d_floor = 1,
|
d_involed1 = newTask.SourceAddress,
|
d_involed2 = newTask.TargetAddress,
|
d_involed5 = newTask.TaskNum,
|
};
|
SqlSugarHelper.DbAGV.Insertable(task_Call).ExecuteCommand();
|
return content.OK();
|
|
}
|
catch (Exception ex)
|
{
|
return content.Error(ex.Message);
|
}
|
}
|
|
public WebResponseContent InTask(string PalletCode, string SourceAddress, string TargetAddress)
|
{
|
WebResponseContent content = new WebResponseContent();
|
try
|
{
|
Dt_Task newTask = new Dt_Task()
|
{
|
TaskNum = BaseDal.GetTaskNo().Result,
|
SourceAddress = SourceAddress,
|
TargetAddress = TargetAddress,
|
PalletCode = PalletCode,
|
TaskType = (int)TaskInboundTypeEnum.Inbound,
|
TaskState = (int)TaskInStatusEnum.InNew,
|
Dispatchertime = DateTime.Now,
|
SeqNo = Convert.ToInt32(1),
|
CommandID = Convert.ToInt32(101)
|
};
|
task_call task_Call = new task_call()
|
{
|
d_task_type = newTask.TaskType == (int)TaskTypeEnum.Inbound ? 1 : 2,
|
d_floor = 1,
|
d_involed1 = newTask.SourceAddress,
|
d_involed2 = newTask.TargetAddress,
|
d_involed5 = newTask.TaskNum,
|
};
|
SqlSugarHelper.DbAGV.Insertable(task_Call).ExecuteCommand();
|
return content.OK();
|
}
|
catch (Exception ex)
|
{
|
return content.Error(ex.Message);
|
}
|
}
|
public WebResponseContent GetAGVStatus()
|
{
|
try
|
{
|
var x = _Socket.GetAGVStatus();
|
return WebResponseContent.Instance.OK(data: x);
|
}
|
catch (Exception ex)
|
{
|
return WebResponseContent.Instance.Error(ex.Message);
|
}
|
}
|
public WebResponseContent GetStationStatus(string StationName)
|
{
|
try
|
{
|
var x = _Socket.GetStationStatus(StationName);
|
return WebResponseContent.Instance.OK(data: x);
|
}
|
catch (Exception ex)
|
{
|
return WebResponseContent.Instance.Error(ex.Message);
|
}
|
}
|
#endregion 外部接口方法
|
|
|
}
|
}
|