刘磊
2025-01-08 92cb843826dfab531b4406102fe7dcd31b6b0775
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#region MyRegion
#region << 版 本 注 释 >>
 
/*----------------------------------------------------------------
 * 命名空间:WIDESEAWCS_Tasks.ConveyorLineJob
 * 创建者:胡童庆
 * 创建时间:2024/8/2 16:13:36
 * 版本:V1.0.0
 * 描述:
 *
 * ----------------------------------------------------------------
 * 修改人:
 * 修改时间:
 * 版本:V1.0.1
 * 修改说明:
 *
 *----------------------------------------------------------------*/
 
#endregion << 版 本 注 释 >>
 
using AutoMapper;
using HslCommunication;
using Microsoft.AspNetCore.Server.HttpSys;
using Newtonsoft.Json;
using Quartz;
using SqlSugar;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Reflection;
using System.Threading.Tasks;
using WIDESEAWCS_BasicInfoRepository;
using WIDESEAWCS_BasicInfoService;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Caches;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core.HttpContextUser;
using WIDESEAWCS_DTO.MOM;
using WIDESEAWCS_DTO.TaskInfo;
using WIDESEAWCS_IProcessRepository;
using WIDESEAWCS_ISystemRepository;
using WIDESEAWCS_ISystemServices;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DeviceBase;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.Service;
using WIDESEAWCS_SignalR;
using WIDESEAWCS_Tasks.ConveyorLineJob;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
using ICacheService = WIDESEAWCS_Core.Caches.ICacheService;
 
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public partial class CommonConveyorLine_BZJob : JobBase, IJob
    {
        public readonly ITaskService _taskService;
        private readonly ITaskRepository _taskRepository;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly IRouterService _routerService;
        private readonly IPlatFormRepository _platFormRepository;
        private readonly ISys_ConfigService _sys_ConfigService;
        private readonly IMapper _mapper;
        private readonly IDt_StationManagerService _stationManagerService;
        private readonly IDt_StationManagerRepository _stationManagerRepository;
        private readonly ICacheService _cacheService;
        private readonly INoticeService _noticeService;
        private readonly IDt_needBarcodeRepository _needBarcodeRepository;
 
        public CommonConveyorLine_BZJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, IMapper mapper, ITaskRepository taskRepository, IPlatFormRepository platFormRepository, ISys_ConfigService sys_ConfigService, IDt_StationManagerRepository stationManagerRepository, ICacheService cacheService, INoticeService noticeService, IDt_StationManagerService stationManagerService, IDt_needBarcodeRepository needBarcodeRepository)
        {
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _routerService = routerService;
            _mapper = mapper;
            _taskRepository = taskRepository;
            _platFormRepository = platFormRepository;
            _sys_ConfigService = sys_ConfigService;
            _stationManagerRepository = stationManagerRepository;
            _cacheService = cacheService;
            _noticeService = noticeService;
            _stationManagerService = stationManagerService;
            _needBarcodeRepository = needBarcodeRepository;
        }
 
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                CommonConveyorLine_BZ conveyorLine = (CommonConveyorLine_BZ)context.JobDetail.JobDataMap.Get("JobParams");
                if (conveyorLine != null)
                {
                    #region 站台方式
                    List<Dt_StationManager> stationManagers = _stationManagerService.GetAllStationByDeviceCode(conveyorLine.DeviceCode);
 
                    foreach (var station in stationManagers)
                    {
                        ConveyorLineTaskCommand_After command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand_After>(station.stationChildCode);
 
                        DeviceProtocolDetailDTO? deviceProtocolDetails = conveyorLine.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(ConveyorLineTaskCommand_After.InteractiveSignal) && x.ProtocalDetailValue == command.InteractiveSignal.ToString());
                        if (deviceProtocolDetails != null)
                        {
                            MethodInfo? method = GetType().GetMethod(deviceProtocolDetails.ProtocolDetailType);
                            if (method != null)
                            {
                                method.Invoke(this, new object[] { conveyorLine, command, station });
                            }
                        }
 
                        #region 调用事件总线通知前端
                        //var tokenInfos = _cacheService.Get<List<UserInfo>>("Cache_UserToken");
                        //if (tokenInfos == null || !tokenInfos.Any())
                        //{
                        //    //throw new Exception(conveyorLine.DeviceName + "缓存中未找到Token缓存");
                        //    continue;
                        //}
                        //var userTokenIds = tokenInfos?.Select(x => x.Token_ID).ToList();
                        //var userIds = tokenInfos?.Select(x => x.UserId).ToList();
 
                        //object obj = new
                        //{
                        //    station.stationChildCode,
                        //    commandAfter = command,
                        //};
                        //_noticeService.StackerData(userIds?.FirstOrDefault(), userTokenIds, new { conveyorLine.DeviceName, data = obj });
 
                        #endregion 调用事件总线通知前端
                    }
                    #endregion
 
                    #region 路由方式
                    //List<string> childDeviceCodes = _routerService.QueryAllPositions(conveyorLine.DeviceCode);
                    //foreach (string childDeviceCode in childDeviceCodes)
                    //{
                    //    ConveyorLineTaskCommand_After command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand_After>(childDeviceCode);
 
                    //    if (command == null) continue;
                    //    if (command.ConveyorLineBarcode.Trim().Contains("\0")) command.ConveyorLineBarcode = "";
 
                    //    DeviceProtocolDetailDTO? deviceProtocolDetails = conveyorLine.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(ConveyorLineTaskCommand_After.InteractiveSignal) && x.ProtocalDetailValue == command.InteractiveSignal.ToString());
                    //    if (deviceProtocolDetails != null)
                    //    {
                    //        MethodInfo? method = GetType().GetMethod(deviceProtocolDetails.ProtocolDetailType);
                    //        if (method != null)
                    //        {
                    //            method.Invoke(this, new object[] { conveyorLine, command, childDeviceCode });
                    //        }
                    //    }
 
                    //    if (childDeviceCode == "1670")
                    //    {
                    //        Platform platform = _platFormRepository.QueryFirst(x => x.PLCCode == conveyorLine.DeviceCode && x.PlatCode == childDeviceCode && x.Status == "Active");
                    //        if (platform != null)
                    //        {
                    //            if (command.HasPallet != 1)
                    //            {
                    //                MethodInfo? method = GetType().GetMethod(platform.ExecutionMethod);
                    //                if (method != null)
                    //                {
                    //                    int count = string.IsNullOrEmpty(platform.Location) ? 0 + 1 : platform.Location.Split(',').Count() + 1;
                    //                    method.Invoke(this, new object[] { conveyorLine, command, childDeviceCode, count, platform });
                    //                }
                    //            }
                    //        }
                    //    }
                    //}
                    #endregion
                }
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine(nameof(CommonConveyorLine_BZJob) + ":" + ex.ToString());
            }
            finally
            {
                //WriteDebug("CommonConveyorLineJob", "test");
                //Console.Out.WriteLine(DateTime.Now);
            }
            return Task.CompletedTask;
        }
 
        /// <summary>
        /// 输送线请求出信息
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="childDeviceCode">子设备编号</param>
        /// <param name="station">线体当前bool读取偏移地址</param>
        public void RequestOutbound(CommonConveyorLine_BZ conveyorLine, ConveyorLineTaskCommand_After command, Dt_StationManager station)
        {
            //查询对应产线的在途数据
            dt_needBarcode needBarcode = _needBarcodeRepository.QueryFirst(x => x.productLine == station.productLine && x.toArea == station.stationChildCode);
 
            needBarcode.inLineNum = _taskRepository.QueryData(x => x.TargetAddress == station.stationChildCode).Count();
 
            //若在途数量小于目标位置的缓存数量 则寻找对应常温库存中常温3工序的可出库数据 并建立出库任务
            if (needBarcode.inLineNum <= needBarcode.cacheNum)
            {
                if (_taskService.RequestOutTaskToBZAsync(station).Result.Status)
                {
                    needBarcode.inLineNum++;
                    _needBarcodeRepository.UpdateData(needBarcode);
                }
            }
        }
 
        /// <summary>
        /// 到达目标位置后  将对应任务清除 并更新在途数据
        /// </summary>
        /// <param name="conveyorLine"></param>
        /// <param name="command"></param>
        /// <param name="station"></param>
        public void RecivcePalletBarcode(CommonConveyorLine_BZ conveyorLine, ConveyorLineTaskCommand_After command, Dt_StationManager station)
        {
            Dt_Task task = _taskRepository.QueryFirst(x => x.PalletCode == command.ConveyorLineBarcode && x.CurrentAddress == station.stationChildCode);
 
            dt_needBarcode needBarcode = _needBarcodeRepository.QueryFirst(x => x.toArea == station.stationChildCode);
            if (needBarcode == null) throw new Exception($"未配置相关{station.stationChildCode}的产线在途数据");
 
            if (task != null) //如任务不为空 则为送包装料 写入下一地址 清除对应任务 并更新在途数据
            {
                conveyorLine.SetValue(ConveyorLineDBName_After.ConveyorLineTargetAddress, task.NextAddress, station.stationChildCode);
                _taskRepository.DeleteData(task);
 
                if (needBarcode.inLineNum-- <= 0) needBarcode.inLineNum = 0;
                _needBarcodeRepository.UpdateData(needBarcode);
            }
            else  //如无任务 则判定为从NG口上料 调取MOM接口  判断返回参数 无异常则送入包装,有异常则退回NG口
            {
                var config = _sys_ConfigService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
                var wmsBase = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.MOMIP_BASE)?.ConfigValue;
                var ipAddress = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.TrayCellsStatus)?.ConfigValue;
                if (wmsBase == null || ipAddress == null)
                {
                    throw new InvalidOperationException("MOM IP 未配置");
                }
                TrayCellsStatusDto trayCells = new TrayCellsStatusDto()
                {
                    Software = "WMS",
                    TrayBarcode = command.ConveyorLineBarcode,
                    EquipmentCode = station.stationEquipMOM,
                    SessionId = Guid.NewGuid().ToString(),
                    EmployeeNo = "MITest",
                    //SceneType = "1",
                    RequestTime = TimeZoneInfo.ConvertTimeToUtc(DateTime.Now).ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
                };
 
                var MOMIpAddress = wmsBase + ipAddress;
 
                var result = HttpHelper.PostAsync(MOMIpAddress, trayCells.ToJsonString()).Result;
                ResultTrayCellsStatus result1 = JsonConvert.DeserializeObject<ResultTrayCellsStatus>(result);
 
                if (!result1.Success)
                {
                    conveyorLine.SetValue(ConveyorLineDBName_After.ConveyorLineTargetAddress, station.stationNGChildCode, station.stationChildCode);
                }
                else
                {
                    //写入对应产线的目标地址
                    if (station.productLine == "ZJ-7")
                    {
                        conveyorLine.SetValue(ConveyorLineDBName_After.ConveyorLineTargetAddress, "", station.stationChildCode);
                    }
                    else
                    {
                        conveyorLine.SetValue(ConveyorLineDBName_After.ConveyorLineTargetAddress, "", station.stationChildCode);
                    }
                }
            }
        }
    }
}
#endregion