using Microsoft.AspNetCore.Routing;
|
using Newtonsoft.Json;
|
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
|
using Quartz;
|
using SqlSugar;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Runtime.InteropServices;
|
using System.Security.Policy;
|
using System.Text;
|
using System.Threading.Tasks;
|
using WIDESEAWCS_Common;
|
using WIDESEAWCS_Common.Helper;
|
using WIDESEAWCS_Common.TaskEnum;
|
using WIDESEAWCS_Core;
|
using WIDESEAWCS_Core.Caches;
|
using WIDESEAWCS_Core.Helper;
|
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_QuartzJob.Service;
|
using WIDESEAWCS_TaskInfoService;
|
using WIDESEAWCS_Tasks.ConveyorLineJob;
|
using ICacheService = WIDESEAWCS_Core.Caches.ICacheService;
|
|
namespace WIDESEAWCS_Tasks
|
{
|
[DisallowConcurrentExecution]
|
public class ConveyorLineJob_BC : JobBase, IJob
|
{
|
private readonly ICacheService _cacheService;
|
private readonly ITaskService _taskService;
|
private readonly ITaskExecuteDetailService _taskExecuteDetailService;
|
private readonly ITaskRepository _taskRepository;
|
private readonly IStationMangerRepository _stationMangerRepository;
|
private readonly IRouterRepository _routerRepository;
|
private readonly IRouterService _routerService;
|
private readonly IRouterExtension _routerExtension;
|
private readonly List<Dt_WarehouseDevice> warehouseDevices;
|
|
public ConveyorLineJob_BC(ICacheService cacheService, ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IStationMangerRepository stationMangerRepository, IRouterRepository routerRepository, IRouterService routerService, IRouterExtension routerExtension)
|
{
|
_cacheService = cacheService;
|
_taskService = taskService;
|
_taskExecuteDetailService = taskExecuteDetailService;
|
_taskRepository = taskRepository;
|
_stationMangerRepository = stationMangerRepository;
|
_routerRepository = routerRepository;
|
_routerService = routerService;
|
_routerExtension = routerExtension;
|
|
string? warehouseDevicesStr = _cacheService.Get<string>(nameof(Dt_WarehouseDevice));
|
if (!string.IsNullOrEmpty(warehouseDevicesStr))
|
{
|
warehouseDevices = JsonConvert.DeserializeObject<List<Dt_WarehouseDevice>>(warehouseDevicesStr) ?? new List<Dt_WarehouseDevice>();
|
}
|
else
|
{
|
warehouseDevices = new List<Dt_WarehouseDevice>();
|
}
|
}
|
|
public Task Execute(IJobExecutionContext context)
|
{
|
bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value);
|
if (flag && value != null)
|
{
|
OtherDevice device = (OtherDevice)value;
|
Dt_WarehouseDevice? warehouseDevice = warehouseDevices.FirstOrDefault(x => x.DeviceCode == device.DeviceCode);
|
if (warehouseDevice == null)
|
{
|
WriteError(device.DeviceName, $"请配置仓库设备信息");
|
return Task.CompletedTask;
|
}
|
List<string> deviceStations = device.DeviceProDTOs.Select(x => x.DeviceChildCode).ToList();
|
List<Dt_StationManger> stationMangers = _stationMangerRepository.QueryData(x => x.StationDeviceCode == device.DeviceCode);
|
foreach (var item in stationMangers.Where(x => deviceStations.Contains(x.StationCode)))
|
{
|
DeviceProDTO? deviceProRead = device.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode && x.DeviceProParamType == nameof(R_ConveyorLineDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
|
|
DeviceProDTO? deviceProWrite = device.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode && x.DeviceProParamType == nameof(W_ConveyorLineDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
|
|
if (deviceProRead != null && deviceProWrite != null)
|
{
|
R_ConveyorLineInfo conveyorLineInfoRead = device.Communicator.ReadCustomer<R_ConveyorLineInfo>(deviceProRead.DeviceProAddress);
|
|
W_ConveyorLineInfo conveyorLineInfoWrite = device.Communicator.ReadCustomer<W_ConveyorLineInfo>(deviceProWrite.DeviceProAddress);
|
if (conveyorLineInfoRead == null || conveyorLineInfoWrite == null)
|
{
|
continue;
|
}
|
|
R_ConveyorLineStatus conveyorLineStatus = conveyorLineInfoRead.Status.ByteToBoolObject<R_ConveyorLineStatus>();
|
|
ConveyorLineSignal conveyorLineSignalRead = conveyorLineInfoRead.Signal.ByteToBoolObject<ConveyorLineSignal>();
|
|
ConveyorLineSignal conveyorLineSignalWrite = conveyorLineInfoWrite.Signal.ByteToBoolObject<ConveyorLineSignal>();
|
|
|
if (item.StationType == StationTypeEnum.StationType_OnlyInbound.ObjToInt())
|
{
|
|
if (conveyorLineSignalRead.STB && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && !conveyorLineSignalWrite.ACK)
|
{
|
if (conveyorLineInfoRead.TaskNo == 0 && !string.IsNullOrEmpty(conveyorLineInfoRead.Barcode))//采购入库
|
{
|
#region 先找允许入库的输送线入口
|
List<Dt_Router> routers = _routerRepository.QueryData(x => x.StartPosi == item.StationCode).ToList();
|
foreach (var rout in routers)
|
{
|
deviceProRead = device.DeviceProDTOs.Where(x => x.DeviceChildCode == rout.NextPosi && x.DeviceProParamType == nameof(R_ConveyorLineDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
|
if (deviceProRead != null)
|
{
|
R_ConveyorLineInfo conveyorLineInfoRead1 = device.Communicator.ReadCustomer<R_ConveyorLineInfo>(deviceProRead.DeviceProAddress);
|
R_ConveyorLineStatus conveyorLineStatus1 = conveyorLineInfoRead1.Status.ByteToBoolObject<R_ConveyorLineStatus>();
|
if (conveyorLineInfoRead1.Status == 6 && conveyorLineStatus1.Free && conveyorLineStatus1.Online && !conveyorLineStatus1.Goods)
|
{
|
|
#region 请求WMS生成任务后给输送线启动信号
|
WebResponseContent content = _taskService.RequestWMSTaskSimple(conveyorLineInfoRead.Barcode, rout.NextPosi);
|
if (content.Status)
|
{
|
Dt_Task task = _taskRepository.QueryFirst(x => x.PalletCode == conveyorLineInfoRead.Barcode && x.CurrentAddress == item.StationCode && x.DeviceCode == device.DeviceCode && x.TaskState == TaskStatusEnum.New.ObjToInt());
|
if (task != null)
|
{
|
_taskService.UpdateTask(task, TaskStatusEnum.RGV_NEW, sourceAddress: rout.StartPosi);
|
device.SetValue(W_ConveyorLineDB.ACK, true, item.StationCode);
|
}
|
|
}
|
|
|
|
#endregion
|
}
|
}
|
}
|
#endregion
|
}
|
else
|
{
|
Dt_Task task = _taskRepository.QueryFirst(x => x.SourceAddress == item.StationCode && x.DeviceCode == item.StationDeviceCode && x.TaskState == TaskStatusEnum.RGV_Finish.ObjToInt());
|
if (task != null)
|
{
|
_taskService.UpdateTask(task, TaskStatusEnum.SC_Execute, deviceCode: task.Roadway, sourceAddress: item.StackerCraneStationCode);
|
if (conveyorLineSignalRead.STB && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && !conveyorLineSignalWrite.ACK && conveyorLineInfoRead.TaskNo != 0)
|
{
|
device.SetValue(W_ConveyorLineDB.ACK, true, item.StationCode);
|
}
|
}
|
|
}
|
|
}
|
else if (!conveyorLineSignalRead.STB && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm)
|
{
|
device.SetValue(W_ConveyorLineDB.ACK, false, item.StationCode);
|
}
|
|
}
|
else
|
{
|
if (!conveyorLineSignalRead.STB && !conveyorLineSignalRead.ACK && conveyorLineStatus.Online && !conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && !conveyorLineSignalWrite.STB && !conveyorLineSignalWrite.ACK)//出库
|
{
|
Dt_Router routers = _routerRepository.QueryFirst(x => x.StartPosi == item.StationCode);
|
if (routers != null)
|
{
|
|
deviceProRead = device.DeviceProDTOs.Where(x => x.DeviceChildCode == routers.NextPosi && x.DeviceProParamType == nameof(R_ConveyorLineDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
|
if (deviceProRead != null)
|
{
|
conveyorLineInfoRead = device.Communicator.ReadCustomer<R_ConveyorLineInfo>(deviceProRead.DeviceProAddress);
|
|
conveyorLineSignalRead = conveyorLineInfoRead.Signal.ByteToBoolObject<ConveyorLineSignal>();
|
|
if (!conveyorLineSignalRead.STB && !conveyorLineSignalRead.ACK && conveyorLineStatus.Online && conveyorLineStatus.Free && !conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && !conveyorLineSignalWrite.STB && !conveyorLineSignalWrite.ACK)//判断出库口是否空闲
|
{
|
Dt_Task task = _taskRepository.QueryFirst(x => x.NextAddress == item.StationCode && x.DeviceCode == item.StationDeviceCode && x.TaskState == TaskStatusEnum.New.ObjToInt());
|
|
if (task != null)
|
{
|
_taskService.UpdateTask(task, TaskStatusEnum.SC_Execute, deviceCode: task.Roadway, nextAddress: item.StackerCraneStationCode);
|
}
|
}
|
|
if (conveyorLineSignalRead.STB)
|
{
|
device.SetValue(W_ConveyorLineDB.ACK, true, item.StationCode);
|
}
|
}
|
|
}
|
}
|
else if (conveyorLineSignalRead.STB && !conveyorLineSignalRead.ACK && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && !conveyorLineSignalWrite.STB && !conveyorLineSignalWrite.ACK)
|
{
|
Dt_Router routers = _routerRepository.QueryFirst(x => x.StartPosi == item.StationCode);
|
if (routers != null)
|
{
|
deviceProRead = device.DeviceProDTOs.Where(x => x.DeviceChildCode == routers.NextPosi && x.DeviceProParamType == nameof(R_ConveyorLineDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
|
if (deviceProRead != null)
|
{
|
conveyorLineInfoRead = device.Communicator.ReadCustomer<R_ConveyorLineInfo>(deviceProRead.DeviceProAddress);
|
|
conveyorLineStatus = conveyorLineInfoRead.Status.ByteToBoolObject<R_ConveyorLineStatus>();
|
|
conveyorLineSignalRead = conveyorLineInfoRead.Signal.ByteToBoolObject<ConveyorLineSignal>();
|
|
if (!conveyorLineSignalRead.STB && !conveyorLineSignalRead.ACK && conveyorLineStatus.Online && conveyorLineStatus.Free && !conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && !conveyorLineSignalWrite.STB && !conveyorLineSignalWrite.ACK)//判断出库口是否空闲
|
{
|
Dt_Task task = _taskRepository.QueryFirst(x => x.CurrentAddress == item.StationCode && x.DeviceCode == device.DeviceCode && x.TaskState == TaskStatusEnum.Line_Execute.ObjToInt());
|
if (task != null)
|
{
|
_taskService.UpdateTask(task, TaskStatusEnum.RGV_Execute, sourceAddress: item.StationCode, targetAddress: routers.NextPosi, nextAddress: routers.NextPosi);
|
device.SetValue(W_ConveyorLineDB.ACK, true, item.StationCode);
|
}
|
}
|
}
|
|
}
|
|
}
|
bool ACK = device.Communicator.Read<bool>("DB501.518.1");
|
if (!conveyorLineSignalRead.STB && !conveyorLineSignalRead.ACK && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && !conveyorLineSignalWrite.STB && ACK)
|
{
|
device.SetValue(W_ConveyorLineDB.ACK, false, item.StationCode);
|
}
|
|
}
|
if (!conveyorLineSignalRead.STB && conveyorLineSignalRead.ACK && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && conveyorLineSignalWrite.STB && !conveyorLineSignalWrite.ACK)
|
{
|
device.SetValue(W_ConveyorLineDB.STB, false, item.StationCode);
|
}
|
else
|
{
|
WriteError(device.DeviceName, $"未找到设备子编号{item.StationCode}的协议信息");
|
}
|
}
|
|
}
|
|
|
}
|
return Task.CompletedTask;
|
}
|
}
|
}
|