huangxiaoqiang
2025-07-29 f23e0326aa05a1c5b47d4aec4c06e73d0d86b8e7
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
using HslCommunication;
using Mapster;
using Newtonsoft.Json;
using SqlSugar;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_DTO.TaskInfo;
using WIDESEAWCS_Model.BasicInfo;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DeviceBase;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.Models;
using WIDESEAWCS_Tasks.ConveyorLineJob;
using WIDESEAWCS_Tasks.StackerCraneJob;
 
namespace WIDESEAWCS_Tasks
{
    public partial class CommonConveyorLineJob
    {
        public async Task HandleNewTaskAsync(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode, Dt_Task task = null)
        {
            var stationManager = _stationManagerRepository.QueryFirst(x => x.stationChildCode == childDeviceCode && x.stationPLC == conveyorLine.DeviceCode);
 
            switch (stationManager.stationType)
            {
                case 1:
                    await RequestTask(conveyorLine, command, childDeviceCode, stationManager);
                    break;
                case 9:
                    RequestWMSTask(conveyorLine, stationManager);
                    break;
                case 2:
                case 3:
                    RequestNextAddress(command, conveyorLine, stationManager, task);
                    break;
                case 4:
                case 5:
                    task = _taskService.QueryConveyorLineFinishTask(stationManager.stationChildCode, command.TaskNum);
                    if (task != null)
                    {
                        _taskService.UpdateTaskStatusToNext(task);
                    }
                    break;
                default:
                    break;
            }
        }
 
        private async Task RequestTask(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode, Dt_StationManager stationManager)
        {
            var hasTask = await _taskRepository.QueryFirstAsync(x => x.SourceAddress == childDeviceCode && x.TaskState < (int)TaskInStatusEnum.RGV_InExecutingFinish && x.TaskState >= (int)TaskInStatusEnum.InNew);
            if (hasTask != null)
            {
                var log = $"【{conveyorLine._deviceName}】任务号:【{hasTask.TaskNum}】,托盘条码:【{hasTask.PalletCode}】已到达【{childDeviceCode}】输送线存在任务";
                ConsoleHelper.WriteWarningLine(log);
 
                await _noticeService.Logs(userTokenIds, new { conveyorLine.DeviceName, log = log, time = DateTime.Now.ToString("G"), color = "red" });
                WriteInfo(conveyorLine.DeviceName, log);
                return;
            }
            var RGVName = string.Empty;
            if (stationManager.stationFloor == "1F")
            {
                RGVName = stationManager.RGVName;
            }
            else
            {
                RGVName = "RGV03";
            }
            Dt_Task task = new Dt_Task()
            {
                TaskNum = _taskRepository.GetTaskNo().Result,
                TaskType = (int)TaskInboundTypeEnum.Inbound,
                TaskState = (int)TaskInStatusEnum.InNew,
                SourceAddress = childDeviceCode,
                Dispatchertime = DateTime.Now,
                Grade = 1,
                Creater = "System",
                Floor = stationManager.stationFloor,
                RGVName = RGVName,
                SourceStation= stationManager.remark,
            };
 
            _taskRepository.AddData(task);
        }
        private void RequestWMSTask(CommonConveyorLine conveyorLine,  Dt_StationManager stationManager)
        {
            if (stationManager.stationChildCode == "3002")
            {
                DeviceProDTO? devicePro = conveyorLine.DeviceProDTOs.Where(x => x.DeviceChildCode == stationManager.stationChildCode && x.DeviceProParamName == "ConveyorLineBarcode").FirstOrDefault();
                var x = conveyorLine.Communicator.Read(devicePro.DeviceProAddress, 5);
 
                string Barcode = Encoding.UTF8.GetString(x);
 
                if (Barcode == null)
                {
                    conveyorLine.SetValue(ConveyorLineDBName.WriteInteractiveSignal, Convert.ToSByte(3), stationManager.stationChildCode);
                }
                var task = _taskRepository.QueryFirst(x => x.PalletCode == Barcode && x.TaskState == (int)TaskInStatusEnum.HoistNew);
 
                if (task != null)
                {
                    ConveyorLineTaskCommandWrite taskCommand = new ConveyorLineTaskCommandWrite()
                    {
                        TaskNum = Convert.ToInt16(task.TaskNum),
                        TargetAddress = Convert.ToInt16(task.NextAddress),
                        WriteInteractiveSignal = task.TaskType == (int)TaskOutboundTypeEnum.Outbound ? (byte)Convert.ToSByte(2) : (byte)Convert.ToSByte(1)
                    };
                    var result = SendCommand(taskCommand, conveyorLine, stationManager.stationChildCode);
                    if (result)
                    {
                        task.TaskState = (int)TaskInStatusEnum.HoistInExecuting;
                        _taskRepository.UpdateData(task);
                    }
                }
                else
                {
                    var config = _sys_ConfigService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
                    var wmsBase = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.WMSIP_BASE)?.ConfigValue;
                    var requestTask = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.RequestTask)?.ConfigValue;
 
                    WMSTaskDTO taskDTO = new WMSTaskDTO()
                    {
                        TPbarcode = Barcode,
                        WhCode = "1001",
                        BeginPoint = stationManager.stationChildCode
                    };
                    if (wmsBase == null || requestTask == null)
                    {
                        throw new InvalidOperationException("WMS IP 未配置");
                    }
                    var wmsIpAddress = wmsBase + requestTask;
                    var result = WIDESEA_Comm.Http.HttpHelper.PostAsync(wmsIpAddress, taskDTO.ToJsonString()).Result;
                }
            }
            else
            {
                DeviceProDTO? devicePro = conveyorLine.DeviceProDTOs.Where(x => x.DeviceChildCode == stationManager.stationChildCode && x.DeviceProParamName == "ConveyorLineBarcode").FirstOrDefault();
                var x = conveyorLine.Communicator.Read(devicePro.DeviceProAddress, 5);
 
                string Barcode = Encoding.UTF8.GetString(x);
                if (Barcode == "")
                {
                    conveyorLine.SetValue(ConveyorLineDBName.WriteInteractiveSignal, Convert.ToSByte(3), stationManager.stationChildCode);
                }
                var task = _taskRepository.QueryFirst(x => x.PalletCode == Barcode);
                if (task != null)
                {
 
                }
                else
                {
                    var config = _sys_ConfigService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
                    var wmsBase = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.WMSIP_BASE)?.ConfigValue;
                    var requestTask = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.RequestTask)?.ConfigValue;
                    
                    WMSTaskDTO taskDTO = new WMSTaskDTO()
                    {
                        TPbarcode = Barcode,
                        WhCode = "1001",
                        BeginPoint = stationManager.stationChildCode
                    };
                    if (wmsBase == null || requestTask == null)
                    {
                        throw new InvalidOperationException("WMS IP 未配置");
                    }
                    var wmsIpAddress = wmsBase + requestTask;
                    var result = WIDESEA_Comm.Http.HttpHelper.PostAsync(wmsIpAddress, taskDTO.ToJsonString()).Result;
                }
            } 
        }
    }
}