严智鑫
2025-11-13 945bc394f40d8af1072a53da9a94f24207124e6d
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package com.northglass.service.message;
 
import java.util.Date;
import java.util.List;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
 
import com.northglass.entity.ArrangeMachineRank;
import com.northglass.entity.ArrangeMachineTask;
import com.northglass.entity.CountRank;
import com.northglass.entity.GaoliweiMachineTask;
import com.northglass.entity.GlassOrder;
import com.northglass.entity.RawPackageTxt;
import com.northglass.entity.ShelfStatu;
import com.northglass.entity.TidyMachine;
import com.northglass.entity.TougheningGlass;
import com.northglass.repository.ArrangeMachineRankDao;
import com.northglass.repository.ArrangeMachineTaskDao;
import com.northglass.repository.CountRankDao;
import com.northglass.repository.GaoliweiMachineDao;
import com.northglass.repository.GaoliweiMachineTaskDao;
import com.northglass.repository.GlassOrderDao;
import com.northglass.repository.RawPackageTxtDao;
import com.northglass.repository.ShelfStatuDao;
import com.northglass.util.HexUtil;
 
@Component
@Transactional
public class TidyMachineMessageProcessor extends AbstractMessageProcessor {
 
    private static final Logger LOGGER = LoggerFactory.getLogger(TidyMachineMessageProcessor.class);
    @Autowired
    private ShelfStatuDao shelfStatuDao;
 
    @Autowired
    private ArrangeMachineTaskDao arrangeMachineTaskDao;
 
    @Autowired
    private GaoliweiMachineTaskDao gaoliweiMachineTaskDao;
 
    @Autowired
    private ArrangeMachineRankDao arrangeMachineRankDao;
 
    @Autowired
    private GaoliweiMachineDao gaoliweiMachineDao;
 
    @Autowired
    private RawPackageTxtDao rawPackageTxtDao;
 
    @Autowired
    private GlassOrderDao glassOrderDao;
 
    @Autowired
    private CountRankDao countRankDao;
 
    private int flag = 0;
    // 请求信号
    private static final int TASK_ASK_START = 18;
    private static final int TASK_ASK_END = 21;
    // 请求ID
    // private static final int TASK_ASK_TD_START = 22;
    // private static final int TASK_ASK_ID_END = 25;
 
    // 任务汇报字
    private static final int TASK_FINISH_PLC_START = 38;
    private static final int TASK_FINISH_PLC_END = 41;
 
    // // 任务汇报ID
    // private static final int TASK_FINISH_PLC_ID_START = 50;
    // private static final int TASK_FINISH_PLC_ID_END = 53;
 
    // 任务发送字
    private static final int TASK_MES_START = 58;
    private static final int TASK_MES_END = 61;
 
    // 任务确认字
    private static final int TASK_SURE_START = 98;
    private static final int TASK_SURE_END = 101;
 
    // 任务报警字
    private static final int TASK_WARN_START = 138;
    private static final int TASK_WARN_END = 141;
 
    // 完成步骤
    private static final int FINISH_STEP_START = 178;
    private static final int FINISH_STEP_END = 181;
 
    /**
     * 解析16进制消息,转译为描述信息
     * 
     * @param messageHex
     * @return
     */
 
    public String generateReturnMessage(String sendMessageHex, TidyMachine arrangeMachine) {
        if (sendMessageHex.length() == 24) {
            String string = sendMessageHex.substring(8, 16);
            if (string.equals("00060010")) {
                // LOGGER.debug("上次消息发送成功!");
                return "";
            } else {
                // LOGGER.debug("上次消息发送失败!");
                return "";
            }
        } else if (sendMessageHex.length() == 198) {
            // 请求信号
            String taskAsk = sendMessageHex.substring(TASK_ASK_START, TASK_ASK_END + 1);
            // 请求ID
            // String taskIdAsk = sendMessageHex.substring(TASK_ASK_TD_START,
            // TASK_ASK_ID_END + 1);
            // // 1号请求ID
            // String taskOneAsk = sendMessageHex.substring(26, 29 + 1);
            // // 2号请求ID
            // String taskTwoAsk = sendMessageHex.substring(30, 33 + 1);
            // // 3号请求ID
            // String taskThreeAsk = sendMessageHex.substring(34, 37 + 1);
            // 任务汇报字1
            String taskFinish = sendMessageHex.substring(TASK_FINISH_PLC_START, TASK_FINISH_PLC_END + 1);
            // 任务汇报字2
            String taskTwoFinish = sendMessageHex.substring(42, 45 + 1);
            // 任务汇报字3
            String taskThreeFinish = sendMessageHex.substring(46, 49 + 1);
            // 任务汇报字ID
            // String taskFinishId =
            // sendMessageHex.substring(TASK_FINISH_PLC_ID_START,
            // TASK_FINISH_PLC_ID_END + 1);
            // 任务发送字
            String taskMes = sendMessageHex.substring(TASK_MES_START, TASK_MES_END + 1);
            // 任务确认字
            String taskSure = sendMessageHex.substring(TASK_SURE_START, TASK_SURE_END + 1);
            // 任务报警字
            String taskWarning = sendMessageHex.substring(TASK_WARN_START, TASK_WARN_END + 1);
            // 完成步骤
            String finishStep = sendMessageHex.substring(FINISH_STEP_START, FINISH_STEP_END + 1);
            String mes = sendMessageHex.substring(TASK_ASK_START, 198);// 信息
            ShelfStatu statu = shelfStatuDao.findByMachine("tidy");
            statu.setMessage(mes);
            statu.setPlcid(taskAsk);
            statu.setMesid(taskMes);
            statu.setErrorstate(taskWarning);
            statu.setLoadfinish(finishStep);
            statu.setSetTime(new Date());
            shelfStatuDao.save(statu);
            List<RawPackageTxt> txtes = rawPackageTxtDao.findByAcceptAndWorking();
            @SuppressWarnings("unused")
            RawPackageTxt txt = new RawPackageTxt();
            if (txtes.size() > 0) {
                txt = txtes.get(0);
            } else {
                LOGGER.debug("暂时没有任务,请等待!");
                return "";
            }
            List<ArrangeMachineTask> inWorktaskes = arrangeMachineTaskDao.findInWorkTask();// 工作任务
            // 完成比对
            if (taskFinish.equals("0001") || taskFinish.equals("0002")) {
                if (taskSure.equals("0000")) {
                    if (inWorktaskes.size() > 0) {
                        for (int i = 0; i < inWorktaskes.size(); i++) {
                            ArrangeMachineTask task = inWorktaskes.get(i);
                            String id = taskFinish;
                            if (i == 1) {
                                id = taskTwoFinish;
                            } else if (i == 2) {
                                id = taskThreeFinish;
                            }
                            if (id.equals("0001")) {
                                ArrangeMachineRank rank = task.getArrangeMachineRank();
                                if (task.getTaskType().equals("EXIT_RANK")) {
                                    task.setState("已完成");
                                    task.setCompleteTime(new Date());
                                    arrangeMachineTaskDao.save(task);
                                    TougheningGlass tougheningGlass = rank.getTougheningGlass();
                                    GaoliweiMachineTask gaoliweitask = new GaoliweiMachineTask(
                                            tougheningGlass.getActualLength(), tougheningGlass.getActualWidth(),
                                            HexUtil.intTo2ByteHex(tougheningGlass.getSendid()),
                                            tougheningGlass.getGlass(), gaoliweiMachineDao.findOne(1L),
                                            tougheningGlass.getGlass().getRawPackageTxt(), "等待中", new Date());
//                                    List<GlassOrder> orderes = glassOrderDao
//                                            .findByBatch(tougheningGlass.getGlass().getRawPackageTxt().getTxt_name());
//                                    List<GlassOrder> os = glassOrderDao.findByBatchEnd(
//                                            tougheningGlass.getGlass().getRawPackageTxt().getTxt_name());
//                                    CountRank countRank = countRankDao.findOne(1L);
//                                    if (orderes.size() > 0) {
//                                        GlassOrder order = orderes.get(0);
//                                        if (order.getCompletePieces() == 0 && os.size() > 0) {
//                                            if (order.getRank() != null) {
//                                                gaoliweitask.setRank(order.getRank());
//                                            } else {
//                                                if (countRank.getState().equals("1")) {
//                                                    gaoliweitask.setRank("2");
//                                                } else {
//                                                    gaoliweitask.setRank("1");
//                                                }
//                                            }
//                                        } else {
//                                            gaoliweitask.setRank(order.getRank());
//                                        }
//
//                                    } else {
//                                        gaoliweitask.setRank(countRank.getState());
//                                    }
                                    gaoliweitask.setRank(task.getRank());
                                    gaoliweiMachineTaskDao.save(gaoliweitask);
                                    rank.setPieces(rank.getPieces() - 1);
                                    if (rank.getPieces() == 0) {
                                        rank.setState("空闲");
                                        rank.setTougheningGlass(null);
                                        
                                    }else {
                                        rank.setState("使用中");
                                    }
                                    arrangeMachineRankDao.save(rank);
                                }
                            } else if (id.equals("0002")) {
                                if (this.flag < 5) {
                                    this.flag++;
                                    return "";
                                }
                                if (task.getTaskType().equals("ENTER_RANK")) {
                                    task.setState("已完成");
                                    task.setCompleteTime(new Date());
                                    arrangeMachineTaskDao.save(task);
                                    ArrangeMachineRank rank = task.getArrangeMachineRank();
                                    if (rank.getPieces() == 1) {
                                        rank.setState("空闲");
                                        rank.setTougheningGlass(null);
                                    } else {
                                        rank.setPieces(rank.getPieces() - 1);
                                        rank.setState("使用中");
                                    }
                                    this.arrangeMachineRankDao.save(rank);
                                } else if (task.getTaskType().equals("EXIT_RANK")) {
                                    ArrangeMachineRank rank = task.getArrangeMachineRank();
                                    task.setState("已完成");
                                    task.setCompleteTime(new Date());
                                    arrangeMachineTaskDao.save(task);
 
                                    rank.setPieces(rank.getPieces() - 1);
                                    if (rank.getPieces() == 0) {
                                        rank.setState("空闲");
                                        rank.setTougheningGlass(null);
                                    }else {
                                        rank.setState("使用中");
                                    }
                                    arrangeMachineRankDao.save(rank);
                                }
                            }
                        }
                    }
                    flag = 0;
                    return sendMessage("00010000", "0014");
                }
            } else {
                if (taskSure.equals("0001")) {
                    return sendMessage("00000000", "0014");
                }
            }
 
            if (taskAsk.equals("0001")) {
 
                if (taskMes.equals("0000") && inWorktaskes.size() > 0) {
                    String string = ((ArrangeMachineTask) inWorktaskes.get(0)).getTaskType();
                    String mes1 = "00010000";
                    if (string.equals("EXIT_RANK")) {
                        mes1 = mes1 + "0002";
                    } else {
                        mes1 = mes1 + "0001";
                    }
                    int pieces = inWorktaskes.size();
                    if (pieces > 3) {
                        pieces = 3;
                    }
                    for (int i = 0; i < pieces; i++) {
                        ArrangeMachineTask task = inWorktaskes.get(i);
                        if (task.getGoals().equals("0")) {
                            return "";
                        }
                        int rack = Integer.parseInt(task.getArrangeMachineRank().getId().toString());
                        LOGGER.debug("rack:" + rack);
                        mes1 = mes1 + HexUtil.intTo2ByteHex(rack) + "0000";
                        task.setGoals("0");
                        this.arrangeMachineTaskDao.save(task);
                    }
                    return sendMessage(mes1, "000a");
                }
            } else if (taskMes.equals("0001")) {
                return sendMessage("000000000000000000000000000000000000", "000a");
            }
            return "";
        } else {
            LOGGER.debug("接收的信息和设置的不一致,请查找原因,长度为:" + sendMessageHex.length());
            return "";
        }
    }
 
    /**
     * 消息发送
     * 
     * @param task
     * @return
     */
    private String sendMessage(String senddate, String address) {
        String Herd = "0010" + address;
        int length = senddate.length() / 4;
        String dates = Herd + HexUtil.intTo2ByteHex(length) + HexUtil.intTo1ByteHex(length * 2) + senddate;
        int lengths = dates.length() / 2;
        String date = "00000000" + HexUtil.intTo2ByteHex(lengths) + dates;
        LOGGER.debug("发送信息内容:" + date);
        return date;
    }
}