严智鑫
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
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.StateConstants.CountMachineTaskState;
import com.northglass.entity.ChamferMachine;
import com.northglass.entity.ChamferMachineTask;
import com.northglass.entity.MachineStatu;
import com.northglass.entity.ShelfStatu;
import com.northglass.repository.ChamferMachineTaskDao;
import com.northglass.repository.GaoliweiMachineTaskDao;
import com.northglass.repository.MachineStatuDao;
import com.northglass.repository.ShelfStatuDao;
import com.northglass.service.device.DeviceService;
import com.northglass.util.HexUtil;
 
@Component
@Transactional
public class ChamferMessageProcessor extends AbstractMessageProcessor {
    // 倒角机
    private static final Logger LOGGER = LoggerFactory.getLogger(ChamferMessageProcessor.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 DAMAGE_START = 46;
    private static final int DAMAGE_END = 49;
    // 破损请求Id
    private static final int DAMAGE_ID_START = 50;
    private static final int DAMAGE_ID_END = 53;
 
    // 任务汇报字
    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 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 DAMAGE_SURE_START = 106;
    private static final int DAMAGE_SURE_END = 109;
 
    // 联机状态
    private static final int AUTO_START = 158;
    private static final int AUTO_END = 161;
 
    // 任务报警字
    private static final int TASK_WARN_START = 138;
    private static final int TASK_WARN_END = 141;
 
    @Autowired
    private ShelfStatuDao shelfStatuDao;
 
    @Autowired
    private ChamferMachineTaskDao chamferMachineTaskDao;
 
    public String generateReturnMessage(String sendMessageHex, ChamferMachine chamferMachine) {
        // 请求信号
        LOGGER.debug("收到倒角机信息:" + sendMessageHex);
        if (sendMessageHex.length() == 24) {
            String string = sendMessageHex.substring(8, 16);
            if (string.equals("00060110")) {
                // LOGGER.debug("倒角机消息发送成功!");
                return "";
            } else {
                // LOGGER.debug("倒角机消息发送失败!");
                return "";
            }
        } else if (sendMessageHex.length() == 198) {
            String Texts[]=ResultTexts(sendMessageHex);
            
            String taskAsk = Texts[0];      // 任务请求字
            String taskIdAsk = Texts[1];    // 任务请求ID
            String taskFinish = Texts[5];    // 任务完成字
            String taskFinishId = Texts[6]; // 任务完成ID
            String damageAsk = Texts[7];     // 破损汇报字
            String damageIdAsk = Texts[8];  // 破损汇报ID
            
            String taskMes = Texts[10];      // 任务发送字
            String taskMesID = Texts[11];      // 任务发送字ID
            String glassLong = Texts[12];      // 长
            String glassWidth = Texts[13];     // 宽
            String glassthinss = Texts[14];     // 厚
            String chamfersize = Texts[15];     // 倒角大小
            String mospeeh = Texts[16];     // 进片输送台和2#磨边机配速速度
            String finishSure = Texts[20];   // 任务完成确认
            String damageSure = Texts[22];   // 任务破损确认字
            String damageSureID = Texts[22];   // 任务破损确认字ID
            String taskWarning = Texts[30];  // 任务报警字
 
            String auto = Texts[35];// 联机信息
            String message = sendMessageHex.substring(TASK_ASK_START);// 信息
 
            ShelfStatu statu = shelfStatuDao.findByMachine("chamfer" + chamferMachine.getId());
            ShelfStatu Gaoliweistatus = shelfStatuDao.findByMachine("gaoliwei"+chamferMachine.getId());
            String Gaoliweispeeh=Gaoliweistatus.getMessage().substring(12,16);
            if (statu != null) {
                statu.setMessage(message);
                statu.setAutostate(auto);
                statu.setSetTime(new Date());
                shelfStatuDao.save(statu);
            }
            String PLCString="PLC:  \n"+
                     "||请求信号:" + taskAsk+
                     "||请求ID:" + taskIdAsk+
                     "||任务汇报字:" + taskFinish + 
                     "||任务汇报字ID:" + taskFinishId + 
                     "||破损请求:" + damageAsk+
                     "||破损请求ID::" + damageIdAsk;
            LOGGER.debug(PLCString);
    
            String MesString="Mes:  \n"+
                     "||任务发送字:" + taskMes+
                     "||任务发送字ID:" + taskMesID+
                     "||任务完成确认:" + finishSure+
                     "||任务破损确认字:" + damageSure+
                     "||长:" + glassLong+
                     "||宽:" + glassWidth+
                     "||任务报警字:" + taskWarning+
                     "||进片输送台和2#磨边机配速速度:" + mospeeh;
            LOGGER.debug(MesString);
            //破损
            if(damageAsk.equals("0001")){
                if(damageSure.equals("0000")){
                    ChamferMachineTask task=chamferMachineTaskDao.SelectTaskByINfoId(chamferMachine.getId(), damageIdAsk);
                    task.setState("破损");
                    chamferMachineTaskDao.save(task);
                    return message("0001","1b6e");
                }
            }else if(damageAsk.equals("0000")){
                if(damageSure.equals("0001")){
                    return message("0000","1b6e");
                }
            }
            //完成任务
            if(taskFinish.equals("0001")){
                if(finishSure.equals("0000")){
                    //PLC已完成,MES进行数据完成 ,完成确认字至1
                    ChamferMachineTask task =chamferMachineTaskDao.SelectTaskByINfoId(chamferMachine.getId(),Integer.parseInt(taskFinishId,16)+"");
                    if(task!=null){
                        task.setState("已完成");
                        task.setCompleteTime(new Date());
                        chamferMachineTaskDao.save(task);
                        return message("0001", "1b6c");
                    }
                    LOGGER.debug("没有此任务!");
                }
            }else{
                if(finishSure.equals("0001")){
                    //PLC清空,MES未清空:完成确认字至0
                    return message("0000", "1b6c");
                }
            }
            if (taskAsk.equals("0001")) {
                if(taskMes.equals("0000")){
                    //请求数据
                    List<ChamferMachineTask> tasks =chamferMachineTaskDao.findWaitingTasksByChamferMachine(chamferMachine.getId());
                    if (tasks.size()>0) {
                        ChamferMachineTask task = tasks.get(0);
                        if(task!=null){
                            //发送数据:任务发送字+infoid+长+宽+厚+倒角大小+磨边机速度
                            String date = "0001"+HexUtil.intTo2ByteHex(Integer.valueOf(task.getInfoId()))+
                                    HexUtil.intTo2ByteHex(new Double(task.getLength()).intValue())+
                                    HexUtil.intTo2ByteHex(new Double(task.getWidth()).intValue())+
                                    HexUtil.intTo2ByteHex(new Double(task.getGlass().getThickness()).intValue())+
                                    "0000"+
                                    Gaoliweispeeh;
                            task.setState("正在工作");
                            DeviceService.WriteFile("D:", "倒角记录", new Date()+"\rPLC接到数据:"+message);
                            return message(date, "1b62");
                        }
                        
                    }
                }
                /*if(!taskMesID.equals("0000")){
                    return message("0001", "1b62");
                }else{
                    //请求数据
                    List<ChamferMachineTask> tasks =chamferMachineTaskDao.findWaitingTasksByChamferMachine(chamferMachine.getId());
                    if (tasks.size()>0) {
                        ChamferMachineTask task = tasks.get(0);
                        //发送数据:任务发送字+infoid+长+宽+厚+倒角大小+磨边机速度
                        String date = HexUtil.intTo2ByteHex(Integer.valueOf(task.getInfoId()))+
                                HexUtil.intTo2ByteHex(new Double(task.getLength()).intValue())+
                                HexUtil.intTo2ByteHex(new Double(task.getWidth()).intValue())+
                                HexUtil.intTo2ByteHex(new Double(task.getGlass().getThickness()).intValue())+
                                "0000"+
                                Gaoliweispeeh;
                        task.setState("正在工作");
                        return message(date, "1b63");
                    }
                }*/
            } else {
                if (taskMes.equals("0001")) {
                    //return message("0000", "1b6c");
                    return message("0000"+"0000"+"0000"+"0000"+"0000"+"0000", "1b62");
                }
            }
 
            return message(Gaoliweispeeh,"1b68");
        }
        
        return "";
    }
    //得到所有的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 = "0110" + 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;
    }
}