111
zhanghonglin
2026-01-05 f797a0e6d079aa25aee23d7aa4064d144d1efa74
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HslCommunication.WebSocket;
using Quartz;
using SqlSugar;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core.BaseRepository;
using WIDESEAWCS_Core.Helper;
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<Dt_StationManger> _stationMangerRepository;
        private readonly IRepository<Dt_Task> _TaskRepository;
        private readonly ITaskService _ITaskService;
        private static Stock Stock = new Stock();
        private static bool k = true;
        private static int i = 0;
        private static bool m = true;
        private static bool n = true;
 
        //存储信号
        private readonly Commands comm = new Commands();
 
        public ConveyorLineJob(WebSocketServer webSocketServer, IRepository<Dt_StationManger> stationMangerRepository, IRepository<Dt_Task> TaskRepository, ITaskService ITaskService)
        {
            _stationMangerRepository = stationMangerRepository;
            _TaskRepository = TaskRepository;
            _ITaskService = ITaskService;
        }
 
        public Task Execute(IJobExecutionContext context)
        {
            //连接设备
            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)))
                {
                    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<ConveyorLineCommand>(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)
                            {
                                try
                                {
                                    //判断任务是否重复
                                    Dt_Task task = _TaskRepository.QueryFirst(x => x.TaskType == (int)TaskTypeEnum.InEmpty);
                                    if (task == null)
                                    {
                                        //WMS生成任务
                                        string address = AppSettings.Get("WMSApiAddress");
                                        if (!string.IsNullOrEmpty(address))
                                        {
                                            HttpHelper.Post($"{address}/api/Task/addInTask/?location={item.location}", "");
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    //写入日志
                                    WriteInfo(device.DeviceCode, $"{ex.Message}");
                                }
                            }
                        }
                        //满桶入库任务
                        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)
                            {
                                try
                                {
                                    //清空物料编号
                                    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);
                                    if (task == null)
                                    {
                                        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;
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    //写入日志
                                    WriteInfo(device.DeviceCode, $"{ex.Message}");
                                }
                            }
                        }
                        //空桶出
                        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)
                            {
                                try
                                {
                                    //判断任务是否重复
                                    Dt_Task task = _TaskRepository.QueryFirst(x => x.TaskType == (int)TaskTypeEnum.OutEmpty);
                                    if (task == null)
                                    {
                                        //WMS生成任务
                                        string address = AppSettings.Get("WMSApiAddress");
                                        if (!string.IsNullOrEmpty(address))
                                        {
                                            HttpHelper.Post($"{address}/api/Task/addOutEmptyTask/?location={item.location}", "");
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    //写入日志
                                    WriteInfo(device.DeviceCode, $"{ex.Message}");
                                }
                            }
                        }
                        //满桶出
                        if (item.StationName == "满桶出")
                        {
                            comm.R_StatusD((short)conveyorLineInfoRead.R_State);
                            comm.R_PickD((short)conveyorLineInfoRead.R_Pick);
                            //告诉WMS可以出库
                            if (conveyorLineInfoRead.R_Pick == 1 && conveyorLineInfoRead.R_State == 1)
                            {
                                if (m)
                                {
                                    //WMS生成任务
                                    string address = AppSettings.Get("WMSApiAddress");
                                    if (!string.IsNullOrEmpty(address))
                                    {
                                        HttpHelper.Post($"{address}/api/Task/WCSSignal/?k=1", "");
                                    }
                                    m = false;
                                    n = true;
                                }
                            }
                            else
                            {
                                if (n)
                                {
                                    //WMS生成任务
                                    string address = AppSettings.Get("WMSApiAddress");
                                    if (!string.IsNullOrEmpty(address))
                                    {
                                        HttpHelper.Post($"{address}/api/Task/WCSSignal/?k=0", "");
                                    }
                                    n = false;
                                    m = true;
                                }
                            }
                        }
                    }
                }
                i = 0;
            }
            return Task.CompletedTask;
        }
    }
}