wangxinhui
昨天 9ce6731460179c71f0f2c636b2a1598324d5194e
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
using Microsoft.AspNetCore.Components.Routing;
using Newtonsoft.Json;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using Org.BouncyCastle.Tls;
using Quartz;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Communicator;
using WIDESEAWCS_Core;
using WIDESEAWCS_DTO.TaskInfo;
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_Tasks.ConveyorLineJob;
using ICacheService = WIDESEAWCS_Core.Caches.ICacheService;
 
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class ConveyorLineJob_CPH : 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;
        private static object lockObj = 0;//
 
        public ConveyorLineJob_CPH(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)
        {
            lock (lockObj)
            {
                if (lockObj.ToString()=="0")
                {
                    lockObj = 1;
                    Task task = Task.Run(() =>
                    {
                        try
                        {
                            while (true)
                            {
                                bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value);
                                if (flag && value != null)
                                {
                                    //获取当前设备
                                    OtherDevice device = (OtherDevice)value;
                                    List<string> deviceStations = device.DeviceProDTOs.Select(x => x.DeviceChildCode).Distinct().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_CLineCPHDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
                                        DeviceProDTO? deviceProWrite = device.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode && x.DeviceProParamType == nameof(W_CLineCPHDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
                                        if (item.StationType == StationTypeEnum.StationType_OnlyInbound.ObjToInt() && deviceProRead != null && deviceProWrite != null)
                                        {
                                            R_CLineCPHInfo conveyorLineInfoRead = device.Communicator.ReadCustomer<R_CLineCPHInfo>(deviceProRead.DeviceProAddress);
                                            //码垛环线请求任务 
                                            if (conveyorLineInfoRead != null && (conveyorLineInfoRead.R_State == 2 || conveyorLineInfoRead.R_State == 3) && conveyorLineInfoRead.R_TaskNo <= 0 && !string.IsNullOrEmpty(conveyorLineInfoRead.R_BoxCode) && conveyorLineInfoRead.R_Request == 1)
                                            {
                                                //WebResponseContent content = _taskService.RequestWMSTaskSimple(conveyorLineInfoRead.R_BoxCode, item.StationCode);
                                                ////向WMS请求任务
                                                //if (true)
                                                //{
 
                                                //}
                                                //写入执行数据
                                                device.SetValue(W_CLineCPHDB.W_TaskNo, 1002, item.StationCode);
                                                device.SetValue(W_CLineCPHDB.W_Channel, 5, item.StationCode);
                                                device.SetValue(W_CLineCPHDB.W_Long, 400, item.StationCode);
                                                device.SetValue(W_CLineCPHDB.W_Wide, 370, item.StationCode);
                                                device.SetValue(W_CLineCPHDB.W_High, 300, item.StationCode);
                                                device.SetValue(W_CLineCPHDB.W_TMID, conveyorLineInfoRead.R_BoxCode, item.StationCode);
                                                device.SetValue(W_CLineCPHDB.W_Request, 1, item.StationCode);
 
                                            }
                                        }
                                        else if (item.StationType == StationTypeEnum.StationType_OnlyOutbound.ObjToInt() && deviceProRead != null)
                                        {
                                            R_CLineCPHInfo conveyorLineInfoRead = device.Communicator.ReadCustomer<R_CLineCPHInfo>(deviceProRead.DeviceProAddress);
                                            //获取码垛口任务更新任务状态
                                            if (conveyorLineInfoRead != null && (conveyorLineInfoRead.R_State == 2 || conveyorLineInfoRead.R_State == 3) && conveyorLineInfoRead.R_TaskNo > 0)
                                            {
                                                Dt_Task task = _taskRepository.QueryFirst(x => x.TaskNum == conveyorLineInfoRead.R_TaskNo && x.NextAddress == item.StationCode && x.TaskState == TaskStatusEnum.Line_Executing.ObjToInt());
                                                if (task != null)
                                                {
                                                    task.CurrentAddress = item.StationCode;
                                                    task.NextAddress = task.TargetAddress;
                                                    task.DeviceCode = item.StackerCraneCode;
                                                    _taskService.UpdateTask(task, TaskStatusEnum.MD_Executing);
                                                }
                                            }
                                        }
                                        else
                                        {
                                            WriteError(item.StationName, $"未找到设备子编号{item.StationCode}的协议信息");
                                        }
                                    }
                                }
                                Thread.Sleep(100);
                                
                            }
                        }
                        catch (Exception ex)
                        {
 
                            lockObj = 0;
                            WriteError(nameof(ConveyorLineJob_CPH), $"错误信息:{ex.Message}");
                        }
                        
                    });
                }
                
            }
            return Task.CompletedTask;
        }
    }
}