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 外部接口方法
///
/// 请求入库任务
///
///
///
///
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 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 = new List();
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.stationName,
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);
}
}
#endregion 外部接口方法
}
}