严智鑫
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
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.constants.FunctionNumber.ShelfFunctionNumber;
import com.northglass.constants.StateConstants.GlassState;
import com.northglass.constants.StateConstants.IdentifyPatternState;
import com.northglass.entity.ChamferMachineTask;
import com.northglass.entity.CountMachineTask;
import com.northglass.entity.GaoliweiMachine;
import com.northglass.entity.GaoliweiMachineTask;
import com.northglass.entity.Glass;
import com.northglass.entity.IdentifyMachine;
import com.northglass.entity.IdentifyPattern;
import com.northglass.entity.PreprocessingGlass;
import com.northglass.entity.ShelfStatu;
import com.northglass.entity.TougheningGlass;
import com.northglass.entity.TransferMachine;
import com.northglass.entity.TransferMachineTask;
import com.northglass.repository.ChamferMachineTaskDao;
import com.northglass.repository.CountMachineTaskDao;
import com.northglass.repository.GaoliweiMachineDao;
import com.northglass.repository.GaoliweiMachineTaskDao;
import com.northglass.repository.GlassDao;
import com.northglass.repository.IdentifyPatternDao;
import com.northglass.repository.PreprocessingGlassDao;
import com.northglass.repository.ShelfStatuDao;
import com.northglass.repository.TougheningGlassDao;
import com.northglass.repository.TransferMachineDao;
import com.northglass.repository.TransferMachineTaskDao;
import com.northglass.service.identifymachine.IdentifyMachineService;
import com.northglass.util.HexUtil;
 
@Component
@Transactional
public class IdentifyMachineMessageProcessor extends AbstractMessageProcessor {
 
    private static final Logger LOGGER = LoggerFactory.getLogger(IdentifyMachineMessageProcessor.class);
 
    // // 请求信号
    // 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 = 42;
    private static final int TASK_FINISH_PLC_ID_END = 45;
 
    // 长
    private static final int LENGTH_START = 46;
    private static final int LENGTH_END = 49;
 
    // 宽
    private static final int WIDTH_START = 50;
    private static final int WIDTH_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;
 
    @Autowired
    private IdentifyMachineService service;
 
    @Autowired
    private GlassDao glassDao;
 
    @Autowired
    private IdentifyPatternDao identifyPatternDao;
    
    @Autowired
    private PreprocessingGlassDao preprocessingGlassDao;
    
    @Autowired
    private ShelfStatuDao shelfStatuDao;
 
    @Autowired
    private TougheningGlassDao tougheningGlassDao;
    
    @Autowired
    private TransferMachineTaskDao transferMachineTaskDao;
    
    @Autowired
    private TransferMachineDao transferMachineDao;
    
    @Autowired
    private GaoliweiMachineTaskDao gaoliweiMachineTaskDao;
    
    @Autowired
    private GaoliweiMachineDao gaoliweiMachineDao;
    /**
     * 解析16进制消息,转译为描述信息
     * 
     * @param messageHex
     * @return
     */
    public String parse(String messageHex) {
        LOGGER.trace("> Start parse");
 
        if (messageHex.length() < 84) {
            LOGGER.error("消息无效:消息长度为至少42个字节!");
            LOGGER.error("无效消息:" + messageHex);
            return "无效消息:" + messageHex;
        }
 
        String startSign = messageHex.substring(START_SIGN_START, START_SIGN_END + 1);
        if (!startSign.equals("3c5354413e")) {
            LOGGER.error("消息无效:消息没有以<STA>开头!");
            LOGGER.error("无效消息:" + messageHex);
            return "无效消息:" + messageHex;
        }
 
        StringBuffer messageDescription = new StringBuffer();
 
        // 消息长度
        messageDescription.append(parseInt(messageHex, MESSAGE_LENGTH_START, MESSAGE_LENGTH_END, "消息长度"));
 
        // 订单编号
        messageDescription.append(parseString(messageHex, ORDER_NUMBER_START, ORDER_NUMBER_END, "订单编号"));
 
        // 功能号
        messageDescription.append(parseString(messageHex, FUNCTION_NUMBER_START, FUNCTION_NUMBER_END, "功能号"));
 
        // 加密方式
        messageDescription.append(parseString(messageHex, ENCRYPT_START, ENCRYPT_END, "加密方式"));
 
        // 发送时刻
        messageDescription.append(parseTime(messageHex, SEND_TIME_START, SEND_TIME_END, "发送时刻"));
 
        // 解析数据区
        messageDescription.append(parseData(messageHex));
 
        LOGGER.trace("> End parse");
        return messageDescription.toString();
    }
 
    /**
     * 根据消息长度判断消息为服务器发送给控制台的消息(63字节)或控制台返回给服务器的消息(54字节) - 若为服务器发送给控制台的消息,按照(数据区
     * = 任务号(5字节) + 操作类型(1字节) + 起始位号(1字节) + 到达位号(1字节) + 玻璃数量(1字节) + 玻璃规格(6字节))解析
     * - 若为控制台发送给服务器的消息,按照(数据区 = 任务号(5字节) + 操作类型(1字节) + 玻璃数量(1字节) + 到位信息(1字节))解析
     * 
     * @param messageHex
     * @return
     */
    private String parseData(String messageHex) {
        String functionNumber = messageHex.substring(FUNCTION_NUMBER_START, FUNCTION_NUMBER_END + 1);
 
        if (functionNumber.equals(ShelfFunctionNumber.HEART_BEAT)) {
            return "心跳包";
        } else if (functionNumber.equals(ShelfFunctionNumber.UPDATE_RTC)) {
            return "更新RTC";
        }
        // 若盒子没有启动,则可能接收到功能号为0002数据区为空的信息
        else if (functionNumber.equals(ShelfFunctionNumber.SHELF_STATUS)) {
        }
        return "无法解析数据区";
    }
 
    public String generateReturnMessage(String sendMessageHex, IdentifyMachine machineClient) {
        LOGGER.debug("收到测量台信息:" + sendMessageHex);
        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 Texts[]=ResultTexts(sendMessageHex);
            // 任务汇报字
            String taskFinish =Texts[5];
            // 任务汇报字ID
            String taskFinishId =Texts[6];
            // 长   ,宽
            double length = (double) HexUtil.hexToInt(Texts[7])/ (double) 10;
            double width = (double) HexUtil.hexToInt(Texts[8])/ (double) 10;
            // 任务确认字
            String taskSure = Texts[20];
            // 任务确认ID
            String taskSureID = Texts[21];
            // 输送台速度
            String TransferSpeeh = Texts[23];
            // 任务报警字
            String taskWarning = Texts[30];
            String message = sendMessageHex.substring(18);            
            String PLCString="PLC:  \n"+
                     "||任务汇报字:" + taskFinish+
                     "||任务汇报字ID:" + taskFinishId+
                     "||长:" + length + 
                     "||宽:" + width + 
                     "||任务报警字:" + taskSure;
                     LOGGER.debug(PLCString);
            String MesString="Mes:  \n"+
                     "||任务确认字:" + taskSure+
                     "||任务确认ID:" + taskSureID+
                     "||输送台速度:" + TransferSpeeh;
            LOGGER.debug(MesString);
            // 收到当前的消息
            //测量参数范围,模式
            IdentifyPattern statu = identifyPatternDao.findOne(machineClient.getId());
            //TransferMachine transferMachine=transferMachineDao.findOne(1L);
            //找到测量台的机械信息,进行修改
            ShelfStatu status = shelfStatuDao.findByMachine("identify");
            status.setSetTime(new Date());
            status.setMessage(message);
            shelfStatuDao.save(status);
            
            //破损
            //判断任务汇报字 情况
            if(taskFinish.equals("0001")){
                //进行匹配数据  ,误差范围内通过,确认字至1   不通过至2
                if(taskSure.equals("0000")){
                    if (length == 0 || width == 0) {
                        LOGGER.debug("玻璃长度或宽度为0,请确认玻璃状态!");
                        return "";
                    }
                    PreprocessingGlass gs = preprocessingGlassDao.findOne(machineClient.getId());
                    if (statu.getPattern().equals(IdentifyPatternState.PATTERN_OLD)) {// 玻璃连接仓储使用匹配
                        // 待识别的 GLASS
                        List<Glass> glasses = glassDao.findGlassByStatu(GlassState.TO_IDENTIFY);
                        Glass glass = new Glass();
                        //测量台有  待识别任务   进行循环匹配
                        if (glasses.size() > 0) {
                            glass = service.getOnlyGlass(machineClient, glasses, length, width);
                        }
                        if (glass != null) {
                            int idname= 1;
                            List<TougheningGlass> tougheningGlasses = tougheningGlassDao.findWaitingGlass();
                            if (tougheningGlasses.size()>0) {
                                TougheningGlass tougheningGlass=tougheningGlasses.get(tougheningGlasses.size()-1);
                                TougheningGlass tougheningGlass2=tougheningGlasses.get(tougheningGlasses.size()-2);
                                if(tougheningGlass2!=null){
                                    idname=tougheningGlass2.getSendid()+1;
                                }
                                if (idname>9999) {
                                    idname=1;
                                }
                                tougheningGlass.setSendid(idname);
                                tougheningGlassDao.save(tougheningGlass);
                                double Length=glass.getLength();
                                double Width=glass.getWidth();
                                boolean Ce1=length>width;
                                boolean Glass1=Length>Width;
                                boolean Ce2=length<width;
                                boolean Glass2=Length<Width;
                                if(Ce1&&Glass1){            
                                }else if(Ce2&&Glass2){    
                                }else{
                                    Length=glass.getWidth();
                                    Width=glass.getLength();
                                }
                                //创建传送任务:长度+宽度+流程卡号+玻璃信息+玻璃+玻璃任务包+工作状态(等待中)+创建时间+infoID+机器ID+Rank(走向)
                                //TransferMachineTask task = new TransferMachineTask(tougheningGlass.getActualLength()-cha,tougheningGlass.getActualWidth()-cha,glass.getApart_id(),glass,glass.getRawPackageTxt(),"等待中",new Date());
                                TransferMachineTask task = new TransferMachineTask(Length,Width,glass.getApart_id(),glass,glass.getRawPackageTxt(),"等待中",new Date());
                                task.setInfoId(String.valueOf(idname));
                                task.settransferMachine(transferMachineDao.findOne(1L));
                                //查找TransferMachineTask 任务中 1   和任务2的任务量 返回 1或2 走向
                                /*String Fx=Fx(width)+"";*/
                                task.setRank("1");
                                gs.setFlag(1);
                                task.setDescription(gs.getTxtname());
                                transferMachineTaskDao.save(task);
                                preprocessingGlassDao.save(gs);
                                return message("0001"+ HexUtil.intTo2ByteHex(idname)+"0000"+"0000","1C30");
                            }
                            
                        }
                        gs.setFlag(0);
                        preprocessingGlassDao.save(gs);
                        // 测量失败,查找原因
                        return message("0002","1C30");
                    }
                }
                //return message("0000","1C30");
            }else if(taskFinish.equals("0002")){
                //    MES   完成人工添加Glass id选择   添加GlassTest表(length,width,new data)  添加TougheningGlass表(ActualWidth,Actualheight,glassid,statu=‘等待中’)
                //   GlassTest表(ActualWidth,Actualheight,length,width,new data)   修改PreprocessingGlass表(Lenght,width,statu=‘匹配成功’,glassid)
                if(taskSure.equals("0002")){
                    //人工MES匹配通过:MES完成,修改数据库   确认字至3
                    PreprocessingGlass gs = preprocessingGlassDao.findOne(machineClient.getId());
                    if (gs.getFlag()==1) {//人工MES界面上匹配成功  后 Flag至1    添加相应数据
                        //测量模式表中的  
                        int idname= 1;
                        //测量完成的的小片信息
                        List<TougheningGlass> tougheningGlasses = tougheningGlassDao.findWaitingGlass();
                        if (tougheningGlasses.size()>0) {
                            TougheningGlass tougheningGlass=tougheningGlasses.get(tougheningGlasses.size()-1);
                            TougheningGlass tougheningGlass2=tougheningGlasses.get(tougheningGlasses.size()-2);
                            Glass glass = tougheningGlass.getGlass();
                            if(tougheningGlass2!=null){
                                idname=tougheningGlass2.getSendid()+1;
                            }
                            if (idname>9999) {
                                idname=1;
                            }
                            tougheningGlass.setSendid(idname);
                            tougheningGlassDao.save(tougheningGlass);
                            double Length=glass.getLength();
                            double Width=glass.getWidth();
                            boolean Ce1=length>width;
                            boolean Glass1=Length>Width;
                            boolean Ce2=length<width;
                            boolean Glass2=Length<Width;
                            if(Ce1&&Glass1){
                                        
                            }else if(Ce2&&Glass2){
                                
                            }else{
                                Length=glass.getWidth();
                                Width=glass.getLength();
                            }
                            //创建传送任务:长度+宽度+流程卡号+玻璃信息+玻璃+玻璃任务包+工作状态(等待中)+创建时间+infoID+机器ID+Rank(走向)
                            TransferMachineTask task = new TransferMachineTask(Length,Width,glass.getApart_id(),glass,glass.getRawPackageTxt(),"等待中",new Date());
                            //TransferMachineTask task = new TransferMachineTask(glass.getLength(),glass.getWidth(),glass.getApart_id(),glass,glass.getRawPackageTxt(),"等待中",new Date());
                            task.setDescription(gs.getTxtname());
                            task.setInfoId(String.valueOf(idname));
                            task.settransferMachine(transferMachineDao.findOne(1L));
                            //查找TransferMachineTask 任务中 1   和任务2的任务量 返回 1或2 走向
                            /*String Fx=Fx(width)+"";*/
                            //task.setRank("1");
                            task.setRank("1");
                            task.setDescription(gs.getTxtname());
                            gs.setStatu("匹配成功");
                            tougheningGlassDao.save(tougheningGlass);
                            transferMachineTaskDao.save(task);
                            preprocessingGlassDao.save(gs);
                            return message("0003"+"0000"+"0000"+"0005","1C30");
                        }                
                    }
                    if (gs.getFlag()==2) {//代表人工匹配不成功,拿走玻璃
                        gs.setFlag(0);
                        gs.setStatu("匹配失败");
                        preprocessingGlassDao.save(gs);
                        return message("0004","1C30");
                    }
                    if (gs.getFlag()==3) {//破损     消除ID
                        gs.setFlag(0);
                        gs.setStatu("匹配失败");
                        preprocessingGlassDao.save(gs);
                        return message("0005","1C30");
                    }
                    //return message("0003","1c30");
                }
            }else if(taskFinish.equals("0000")){
                //当PLC确认收到信息时,MES确认字清空
                    return message("0000","1c30");    
            }
            return "";
        } else {
            LOGGER.debug("接收的信息和设置的不一致,请查找原因,长度为:" + sendMessageHex.length());
            return "";
        }
            
    }
    private int Fx(double width){
        //int Direction=1;
        List<TransferMachineTask> WaitingTaskone=transferMachineTaskDao.findInWorkTaskByGaoliweiMachine(1L,"1");
        List<TransferMachineTask> WaitingTaskTwo=transferMachineTaskDao.findInWorkTaskByGaoliweiMachine(1L,"2");
        double taskLenthone=0;
        double taskLenthtwo=0;
        //两块玻璃 总长不超过295
        for(TransferMachineTask WaitingTaskones:WaitingTaskone){
            taskLenthone+= WaitingTaskones.getWidth();
        }
        for(TransferMachineTask WaitingTaskones:WaitingTaskTwo){
            taskLenthtwo+= WaitingTaskones.getWidth();
        }
        if(taskLenthone>taskLenthtwo){ 
            return 2;
        }else if(taskLenthone<taskLenthtwo){
            return 1;
        }/*else{
            GaoliweiMachineTask TaskLastone=WaitingTaskone.get(WaitingTaskone.size()-1);
            GaoliweiMachineTask TaskLasttwo=WaitingTaskTwo.get(WaitingTaskone.size()-1);
            double floorone=TaskLastone.getWidth()-width;
            double floortwe=TaskLasttwo.getWidth()-width;
            if(){
                
            }
        }*/
        
        return 2;
    }
    //得到所有的45个字
    private String[] ResultTexts(String Str) {
        String []InfoTex =new String[45];
        Str=Str.substring(18);
        for(int i=0;i<45;i++){
            InfoTex[i]=Str.substring(0,4);
            Str=Str.substring(4);
        }
        return InfoTex;
    }
    private String message(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;
    }
 
}