using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Autofac.Core; using HslCommunication.WebSocket; using Microsoft.AspNetCore.Components; using Quartz; using SqlSugar; using WIDESEA_Model.Models.Basic; using WIDESEAWCS_Common.TaskEnum; using WIDESEAWCS_Core.BaseRepository; using WIDESEAWCS_Core.Helper; using WIDESEAWCS_Core.LogHelper; using WIDESEAWCS_DTO.MES; using WIDESEAWCS_DTO.Stock; using WIDESEAWCS_ITaskInfoService; using WIDESEAWCS_Model.Models.System; using WIDESEAWCS_Model.Models.TaskInfo; using WIDESEAWCS_QuartzJob; using WIDESEAWCS_QuartzJob.DTO; using WIDESEAWCS_Tasks.Command; using WIDESEAWCS_Tasks.DBname; using WIDESEAWCS_Tasks.DBName; using static Dm.net.buffer.ByteArrayBuffer; namespace WIDESEAWCS_Tasks { [DisallowConcurrentExecution] public class ConveyorLineJob : JobBase, IJob { private readonly IRepository _stationMangerRepository; private readonly IRepository _TaskRepository; private readonly IRepository _FillingOrderRepository; private readonly ITaskService _ITaskService; private static Stock Stock = new Stock(); private static bool k = true; private static int i = 0; //判断是否把出库信号发给WMS private static bool m = true; private static bool n = true; //存储信号 private readonly Commands comm = new Commands(); public ConveyorLineJob(WebSocketServer webSocketServer, IRepository stationMangerRepository, IRepository TaskRepository, IRepository FillingOrderRepository, ITaskService ITaskService) { _stationMangerRepository = stationMangerRepository; _TaskRepository = TaskRepository; _ITaskService = ITaskService; _FillingOrderRepository = FillingOrderRepository; } public Task Execute(IJobExecutionContext context) { //堆垛机设备状态 int sc = comm.GETR_TaskStatus(); //连接设备 bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value); if (flag && value != null) { OtherDevice device = (OtherDevice)value; List deviceStations = device.DeviceProDTOs.Select(x => x.DeviceChildCode).Distinct().ToList(); List stationMangers = _stationMangerRepository.QueryData(x => x.StationDeviceCode == device.DeviceCode); try { //具体设备 foreach (var item in stationMangers.Where(x => deviceStations.Contains(x.StationCode))) { i++; //读取信号 DeviceProDTO? deviceProRead = device.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode).OrderBy(x => x.DeviceProOffset).FirstOrDefault(); //DeviceProDTO? deviceProWrite = device.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode ).OrderBy(x => x.DeviceProOffset).FirstOrDefault(); if (deviceProRead != null) { ConveyorLineCommand conveyorLineInfoRead = device.Communicator.ReadCustomer(deviceProRead.DeviceProAddress); //空桶入库任务 if (item.StationName == "空桶入") { comm.R_StatusA((short)conveyorLineInfoRead.R_State); comm.R_PickA((short)conveyorLineInfoRead.R_Pick); if (conveyorLineInfoRead.R_Pick == 1 && conveyorLineInfoRead.R_State == 1 && sc == 2) { //判断任务是否重复 Dt_Task task = _TaskRepository.QueryFirst(x => x.TaskType == (int)TaskTypeEnum.InEmpty); if (task == null) { LogLock.OutLogAOP("输送线任务日志", new string[] { device.DeviceCode, $"{"空桶入库任务生成" + "申请取(放)料:" + conveyorLineInfoRead.R_Pick + "设备状态:" + conveyorLineInfoRead.R_State}" }); //WMS生成任务 string address = AppSettings.Get("WMSApiAddress"); if (!string.IsNullOrEmpty(address)) { HttpHelper.Post($"{address}/api/Task/addInTask/?location={item.location}", ""); } } } } //满桶入库任务 if (item.StationName == "满桶入") { comm.R_StatusC((short)conveyorLineInfoRead.R_State); comm.R_PickC((short)conveyorLineInfoRead.R_Pick); if (conveyorLineInfoRead.R_Pick == 1 && conveyorLineInfoRead.R_State == 1 && sc == 2) { //清空物料编号 if (k) { Stock.MaterialCode1 = ""; Stock.MaterialCode2 = ""; Stock.MaterialCode3 = ""; Stock.MaterialCode4 = ""; k = false; } //存储物料编号 Stock.MaterialCode1 = conveyorLineInfoRead.R_NumberA; Stock.MaterialCode2 = conveyorLineInfoRead.R_NumberB; Stock.MaterialCode3 = conveyorLineInfoRead.R_NumberC; Stock.MaterialCode4 = conveyorLineInfoRead.R_NumberD; //判断任务是否重复 Dt_Task task = _TaskRepository.QueryFirst(x => x.TaskType == (int)TaskTypeEnum.Infull || x.TaskType == (int)TaskTypeEnum.OutDirect); if (task == null) { LogLock.OutLogAOP("输送线任务日志", new string[] { device.DeviceCode, $"{"满桶入库任务生成" + "申请取(放)料:" + conveyorLineInfoRead.R_Pick + "设备状态:" + conveyorLineInfoRead.R_State + "//" + conveyorLineInfoRead.R_NumberA + "/" + conveyorLineInfoRead.R_NumberB + "/" + conveyorLineInfoRead.R_NumberC + "/" + conveyorLineInfoRead.R_NumberD}" }); Stock.location = item.location; //WMS生成任务 string address = AppSettings.Get("WMSApiAddress"); if (!string.IsNullOrEmpty(address)) { HttpHelper.Post($"{address}/api/Task/addOutTask/", Stock.Serialize()); //发送收到编号信号 device.SetValue(ConveyorLineName.W_ReceivedD, 1, item.StationCode); k = true; } } } } //空桶出 if (item.StationName == "空桶出") { comm.R_StatusB((short)conveyorLineInfoRead.R_State); comm.R_PickB((short)conveyorLineInfoRead.R_Pick); //if (conveyorLineInfoRead.R_Pick == 1 && conveyorLineInfoRead.R_State == 1 && sc == 2) //{ // //判断任务是否重复 // Dt_Task task = _TaskRepository.QueryFirst(x => x.TaskType == (int)TaskTypeEnum.OutEmpty); // if (task == null) // { // LogLock.OutLogAOP("输送线任务日志", new string[] { device.DeviceCode, $"{"空桶出库任务生成" + "申请取(放)料:" + conveyorLineInfoRead.R_Pick + "设备状态:" + conveyorLineInfoRead.R_State}" }); // //WMS生成任务 // string address = AppSettings.Get("WMSApiAddress"); // if (!string.IsNullOrEmpty(address)) // { // HttpHelper.Post($"{address}/api/Task/addOutEmptyTask/?location={item.location}", ""); // } // } //} } //满桶出 if (item.StationName == "满桶出") { comm.R_StatusD((short)conveyorLineInfoRead.R_State); comm.R_PickD((short)conveyorLineInfoRead.R_Pick); } } } i = 0; } catch (Exception ex) { //写入日志 WriteInfo(device.DeviceCode, $"{ex.Message}"); } } return Task.CompletedTask; } } }