|  |  |  | 
|---|
|  |  |  | using Autofac.Core; | 
|---|
|  |  |  | using HslCommunication; | 
|---|
|  |  |  | using Mapster; | 
|---|
|  |  |  | using Newtonsoft.Json; | 
|---|
|  |  |  | using Quartz; | 
|---|
|  |  |  | using System.Diagnostics.CodeAnalysis; | 
|---|
|  |  |  | using WIDESEAWCS_BasicInfoRepository; | 
|---|
|  |  |  | using WIDESEAWCS_Common.TaskEnum; | 
|---|
|  |  |  | using WIDESEAWCS_Core.Caches; | 
|---|
|  |  |  | using WIDESEAWCS_Core.Helper; | 
|---|
|  |  |  | using WIDESEAWCS_Core.HttpContextUser; | 
|---|
|  |  |  | using WIDESEAWCS_DTO.MOM; | 
|---|
|  |  |  | using WIDESEAWCS_IProcessRepository; | 
|---|
|  |  |  | using WIDESEAWCS_ITaskInfo_HtyRepository; | 
|---|
|  |  |  | using WIDESEAWCS_ITaskInfoRepository; | 
|---|
|  |  |  | using WIDESEAWCS_ITaskInfoService; | 
|---|
|  |  |  | using WIDESEAWCS_Model; | 
|---|
|  |  |  | 
|---|
|  |  |  | using WIDESEAWCS_QuartzJob.Models; | 
|---|
|  |  |  | using WIDESEAWCS_QuartzJob.Service; | 
|---|
|  |  |  | using WIDESEAWCS_QuartzJob.StackerCrane.Enum; | 
|---|
|  |  |  | using WIDESEAWCS_SignalR; | 
|---|
|  |  |  | using WIDESEAWCS_Tasks.ConveyorLineJob; | 
|---|
|  |  |  | using WIDESEAWCS_Tasks.StackerCraneJob; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | namespace WIDESEAWCS_Tasks | 
|---|
|  |  |  | 
|---|
|  |  |  | private readonly ITaskRepository _taskRepository; | 
|---|
|  |  |  | private readonly IRouterService _routerService; | 
|---|
|  |  |  | private readonly IProcessRepository _processRepository; | 
|---|
|  |  |  | private readonly ICacheService _cacheService; | 
|---|
|  |  |  | private readonly INoticeService _noticeService; | 
|---|
|  |  |  | private readonly IDt_StationManagerRepository _stationManagerRepository; | 
|---|
|  |  |  | private readonly ITask_HtyRepository _htyRepository; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public CommonStackerCraneJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService, IProcessRepository processRepository) | 
|---|
|  |  |  | public CommonStackerCraneJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService, IProcessRepository processRepository, ICacheService cacheService, INoticeService noticeService, IDt_StationManagerRepository stationManagerRepository, ITask_HtyRepository htyRepository) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | _taskService = taskService; | 
|---|
|  |  |  | _taskExecuteDetailService = taskExecuteDetailService; | 
|---|
|  |  |  | _taskRepository = taskRepository; | 
|---|
|  |  |  | _routerService = routerService; | 
|---|
|  |  |  | _processRepository = processRepository; | 
|---|
|  |  |  | _cacheService = cacheService; | 
|---|
|  |  |  | _noticeService = noticeService; | 
|---|
|  |  |  | _stationManagerRepository = stationManagerRepository; | 
|---|
|  |  |  | _htyRepository = htyRepository; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public Task Execute(IJobExecutionContext context) | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | #region 调用事件总线通知前端 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | var tokenInfos = _cacheService.Get<List<UserInfo>>("Cache_UserToken"); | 
|---|
|  |  |  | if (tokenInfos == null || !tokenInfos.Any()) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | //throw new Exception(commonStackerCrane.DeviceName + "缓存中未找到Token缓存"); | 
|---|
|  |  |  | return Task.CompletedTask; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | var userTokenIds = tokenInfos?.Select(x => x.Token_ID).ToList(); | 
|---|
|  |  |  | var userIds = tokenInfos?.Select(x => x.UserId).ToList(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | object obj = new | 
|---|
|  |  |  | { | 
|---|
|  |  |  | commonStackerCrane.StackerCraneStatusDes, | 
|---|
|  |  |  | commonStackerCrane.StackerCraneAutoStatusDes, | 
|---|
|  |  |  | commonStackerCrane.StackerCraneWorkStatusDes, | 
|---|
|  |  |  | commonStackerCrane.DeviceCode, | 
|---|
|  |  |  | commonStackerCrane.DeviceName, | 
|---|
|  |  |  | commonStackerCrane.CurrentTaskNum, | 
|---|
|  |  |  | commonStackerCrane.LastTaskNum, | 
|---|
|  |  |  | }; | 
|---|
|  |  |  | _noticeService.StackerData(userIds?.FirstOrDefault(), userTokenIds, new { commonStackerCrane.DeviceName, data = obj }); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | #endregion 调用事件总线通知前端 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | catch (Exception ex) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | WriteError("CommonConveyorLineJob", "test", ex); | 
|---|
|  |  |  | ConsoleHelper.WriteErrorLine($"{ex.Message}"); | 
|---|
|  |  |  | //Console.WriteLine(nameof(CommonStackerCraneJob) + ":" + ex.ToString()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //WriteDebug("CommonConveyorLineJob", "test"); | 
|---|
|  |  |  | 
|---|
|  |  |  | CommonStackerCrane? commonStackerCrane = sender as CommonStackerCrane; | 
|---|
|  |  |  | if (commonStackerCrane != null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | if (commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.WorkType) != 5) | 
|---|
|  |  |  | //var x = commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.WorkType); | 
|---|
|  |  |  | //if (commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.WorkType) != 5) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | //Console.Out.WriteLine("TaskCompleted" + e.TaskNum); | 
|---|
|  |  |  | ConsoleHelper.WriteColorLine($"【{commonStackerCrane.DeviceName}】任务完成,任务号:【{e.TaskNum}】", ConsoleColor.Blue); | 
|---|
|  |  |  | var task = _taskRepository.QueryFirst(x => x.TaskNum == e.TaskNum); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (task == null) commonStackerCrane.SetValue(StackerCraneDBName.WorkType, 5); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (commonStackerCrane.DeviceCode.Contains("GW") && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | var station = _stationManagerRepository.QueryFirst(x => x.stationChildCode == task.TargetAddress); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == station.stationPLC); | 
|---|
|  |  |  | if (device != null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | CommonConveyorLine_GW conveyorLine = (CommonConveyorLine_GW)device; | 
|---|
|  |  |  | var isResult = conveyorLine.SetValue(ConveyorLineDBName_After.ConveyorLineBarcode, task.PalletCode, task.TargetAddress); | 
|---|
|  |  |  | if (!isResult) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | var result = conveyorLine.GetValue<ConveyorLineDBName_After, string>(ConveyorLineDBName_After.ConveyorLineBarcode, task.TargetAddress); | 
|---|
|  |  |  | if (result != task.PalletCode) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | conveyorLine.SetValue(ConveyorLineDBName_After.ConveyorLineBarcode, task.PalletCode, task.TargetAddress); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | _taskService.StackCraneTaskCompleted(e.TaskNum); | 
|---|
|  |  |  | if (commonStackerCrane.DeviceCode.Contains("CH") && task.TaskType == (int)TaskOutboundTypeEnum.Outbound) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | task = _taskRepository.QueryFirst(x => x.TaskNum == e.TaskNum); | 
|---|
|  |  |  | Dt_Task? newTask = _taskService.UpdatePosition(task.TaskNum, task.CurrentAddress); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup && task.TargetAddress == "1359-4") | 
|---|
|  |  |  | { | 
|---|
|  |  |  | var TASKHTY = task.Adapt<Dt_Task_Hty>(); | 
|---|
|  |  |  | _taskRepository.DeleteData(task); | 
|---|
|  |  |  | _htyRepository.AddData(TASKHTY); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | commonStackerCrane.SetValue(StackerCraneDBName.WorkType, 5); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | private Dt_Task? GetTask(CommonStackerCrane commonStackerCrane) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | Dt_Task task; | 
|---|
|  |  |  | //var taskRun = _taskRepository.QueryFirst(x => x.Roadway == commonStackerCrane.DeviceCode && (x.TaskState == (int)TaskOutStatusEnum.SC_OutExecuting || x.TaskState == (int)TaskInStatusEnum.SC_InExecuting)); | 
|---|
|  |  |  | //if (taskRun != null) { return null; } | 
|---|
|  |  |  | if (commonStackerCrane.LastTaskType == null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | task = _taskService.QueryStackerCraneTask(commonStackerCrane.DeviceCode); | 
|---|
|  |  |  | 
|---|
|  |  |  | { | 
|---|
|  |  |  | if (router != null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | IDevice? device = null; | 
|---|
|  |  |  | if (task.Roadway.Contains("CW")) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == router.ChildPosiDeviceCode); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | else | 
|---|
|  |  |  | { | 
|---|
|  |  |  | device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "1015"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (device != null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | CommonConveyorLine_GW conveyorLine = (CommonConveyorLine_GW)device; | 
|---|
|  |  |  | if (conveyorLine.IsOccupied(task.NextAddress))//出库站台未被占用 | 
|---|
|  |  |  | if (device != null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | return task; | 
|---|
|  |  |  | CommonConveyorLine_GW conveyorLine = (CommonConveyorLine_GW)device; | 
|---|
|  |  |  | if (conveyorLine.IsOccupied(task.NextAddress))//出库站台未被占用 | 
|---|
|  |  |  | { | 
|---|
|  |  |  | return task; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else | 
|---|
|  |  |  | { | 
|---|
|  |  |  | _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到出库站台【{task.TargetAddress}】对应的通讯对象,无法判断出库站台是否被占用"); | 
|---|
|  |  |  | else | 
|---|
|  |  |  | { | 
|---|
|  |  |  | _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到出库站台【{task.NextAddress}】对应的通讯对象,无法判断出库站台是否被占用"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else | 
|---|
|  |  |  | { | 
|---|
|  |  |  | _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法校验站台"); | 
|---|
|  |  |  | IDevice? device = null; | 
|---|
|  |  |  | if (task.Roadway.Contains("GW")) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "1015"); | 
|---|
|  |  |  | if (device != null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | CommonConveyorLine_GW conveyorLine = (CommonConveyorLine_GW)device; | 
|---|
|  |  |  | if (conveyorLine.IsOccupied(task.TargetAddress))//出库站台未被占用 | 
|---|
|  |  |  | { | 
|---|
|  |  |  | return task; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else | 
|---|
|  |  |  | { | 
|---|
|  |  |  | _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到出库站台【{task.TargetAddress}】对应的通讯对象,无法判断出库站台是否被占用"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.TargetAddress}】信息,无法校验站台"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else | 
|---|
|  |  |  | 
|---|
|  |  |  | if (device != null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | CommonConveyorLine conveyorLine = (CommonConveyorLine)device; | 
|---|
|  |  |  | if (conveyorLine.IsOccupied(router.ChildPosi))//出库站台未被占用 | 
|---|
|  |  |  | //if (conveyorLine.IsOccupied(router.ChildPosi))//出库站台未被占用 | 
|---|
|  |  |  | { | 
|---|
|  |  |  | return task; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return stackerCraneTaskCommand; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /// <summary> | 
|---|
|  |  |  | /// 设备心跳 | 
|---|
|  |  |  | /// </summary> | 
|---|
|  |  |  | /// <param name="commonStackerCrane"></param> | 
|---|
|  |  |  | //public async void EqptAlive(CommonStackerCrane commonStackerCrane) | 
|---|
|  |  |  | //{ | 
|---|
|  |  |  | //    if (commonStackerCrane.StackerOnline) | 
|---|
|  |  |  | //    { | 
|---|
|  |  |  | //        BasicDto dto = new BasicDto | 
|---|
|  |  |  | //        { | 
|---|
|  |  |  | //            EmployeeNo = "T00001", | 
|---|
|  |  |  | //            EquipmentCode = commonStackerCrane.DeviceCode, | 
|---|
|  |  |  | //            RequestTime = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffZ"), | 
|---|
|  |  |  | //            SessionId = Guid.NewGuid().ToString(), | 
|---|
|  |  |  | //            Software = commonStackerCrane.DeviceName, | 
|---|
|  |  |  | //        }; | 
|---|
|  |  |  | //        var respone = await HttpHelper.PostAsync("http://ts-momapp01:12020/api/MachineIntegration/EqptAlive", dto.ToJsonString()); | 
|---|
|  |  |  | //        if (respone != null) | 
|---|
|  |  |  | //        { | 
|---|
|  |  |  | //            var result = JsonConvert.DeserializeObject<ResponseEqptAliveDto>(respone); | 
|---|
|  |  |  | //            if (result != null && result.Success) | 
|---|
|  |  |  | //            { | 
|---|
|  |  |  | //                if (result.KeyFlag == "99") | 
|---|
|  |  |  | //                    commonStackerCrane.StackerOnline = true; | 
|---|
|  |  |  | //                else if (result.KeyFlag == "98") | 
|---|
|  |  |  | //                    commonStackerCrane.StackerOnline = false; | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | //    } | 
|---|
|  |  |  | //} | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|