严智鑫
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
package com.northglass.listener;
 
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.context.ApplicationContext;
 
import com.northglass.constants.StateConstants.ConnectState;
import com.northglass.constants.StateConstants.GaoliweiFunctionNumber;
import com.northglass.entity.GaoliweiMachine;
import com.northglass.service.gaoliwei.GaoliweiMachineService;
import com.northglass.util.CRCUtil;
import com.northglass.util.HexUtil;
import com.northglass.web.gaoliwei.GaoliweiController;
 
public class GaoliweiMachineServerListener extends AbstractServerListener implements Runnable {
 
    private static final Logger LOGGER = LoggerFactory.getLogger(GaoliweiMachineServerListener.class);
    
    private GaoliweiMachine gaoliweiMachine;
    private GaoliweiMachineService service;
    
    /**
     * 备用,共8个字节
     */
    protected static final String BACKUP = "0000000000000000";
    
    /**
     * 加密方式00,表示不加密
     */
    protected static final String NO_ENCRYPT = "00";
    
    public String button = "0";
    public String button1 = "0";
    
    public String getButton() {
        return button;
    }
 
    public void setButton(String button) {
        this.button = button;
    }
 
    public String getButton1() {
        return button1;
    }
 
    public void setButton1(String button1) {
        this.button1 = button1;
    }
 
 
    private StringBuffer MESSAGE_BUFFER = new StringBuffer();
    
    public GaoliweiMachineServerListener(Long gaoliweiMachineId) {
        ApplicationContext context = initializeSpringContext();
        service = context.getBean(GaoliweiMachineService.class);
        this.gaoliweiMachine = service.getById(gaoliweiMachineId);
        
        MDC.put("logFileName", "GaoliweiMachine_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()));
    }
    
    /**
     * 启动服务器监听连接,等待客户端连接
     * 
     * @throws IOException
     */
    private void connect(ServerSocket serverSocket) throws IOException {
        if (serverSocket.isClosed()) {
            return;
        }
        LOGGER.debug(gaoliweiMachine.getConnectState());
        Socket socket = serverSocket.accept();
        LOGGER.debug("客户端已连接,客户端IP:" + socket.getInetAddress());
        service.setConnectState(gaoliweiMachine, ConnectState.CONNECTED);
        gaoliweiMachine.getServerConnection().setSocket(socket);
        
        //添加连接时间
        service.getendtime("gaoliweiMachine"+gaoliweiMachine.getNumber());
    }
    
    /**
     * 服务器监听消息循环,先从客户端接收消息,再向客户端发送消息
     * 
     * @throws IOException
     */
    private void listen() throws IOException {
        Socket socket = gaoliweiMachine.getServerConnection().getSocket();
        LOGGER.debug(gaoliweiMachine.getConnectState());
        socket.setSoTimeout(10000); 
        while (true) {
            DataInputStream in = new DataInputStream(socket.getInputStream());
            byte b = in.readByte();
            MESSAGE_BUFFER.append(HexUtil.byteToHexString(b));
            
            if (!existMessage()) {
                continue;
            }
            String message = getNextMessage();
//            machineService.saveMessage(MessageType.CLIENT, message, socket.getInetAddress().getHostAddress(), socket.getLocalPort());
 
          String returnMessage="";
          try{
              returnMessage = service.generateReturnMessage(message, gaoliweiMachine);
          }catch(ThreadDeath se){
              LOGGER.debug("磨边机【"+gaoliweiMachine.getNumber()+"】线程出现死锁!");
              gaoliweiMachine.setState("线程死锁");
          }
  //              String returnMessage = service.generateReturnMessage(message, gaoliweiMachine);
            
            if (returnMessage.equals("connect")) {
                in.close();
                socket.close();
                ServerSocket serverSocket = null;
                  try {
                      serverSocket = new ServerSocket(gaoliweiMachine.getPort());
                      LOGGER.debug("高力威【" + gaoliweiMachine.getNumber() + "】启动监听,等待客户端连接,端口:" + gaoliweiMachine.getPort());
                      gaoliweiMachine.getServerConnection().setServerSocket(serverSocket);
                      
                      connect(serverSocket);
                      
                      // 一直循环,若连接断开,则在Exception处理环节重新连接,并继续监听消息
                      while (!Thread.currentThread().isInterrupted()) {
                          try {
                              listen();
                          }
                          catch (IOException se) {
                              service.getright("gaoliweiMachine"+gaoliweiMachine.getNumber());
                              LOGGER.debug("高力威连接已断开!");
                              se.printStackTrace();
                              LOGGER.debug("高力威等待重新连接……");
                              service.setConnectState(gaoliweiMachine, ConnectState.CONNECTING);
                              connect(serverSocket);
                          }
                      }
                  }
                  catch (IOException e) {
                      e.printStackTrace();
                  }
            }else if (returnMessage != null && !"".equals(returnMessage) && !returnMessage.equals("connect")) {
                DataOutputStream out = new DataOutputStream(socket.getOutputStream());
                out.write(HexUtil.stringToInt(returnMessage));
                out.flush();
            }else if (GaoliweiController.getButton().equals("1") && gaoliweiMachine.getId()==1L) {
                LOGGER.debug("MES连线系统主动完成任务1!");
                 returnMessage = getFinishId();
                 GaoliweiController.setButton("0");
                 DataOutputStream out = new DataOutputStream(socket.getOutputStream());
                 out.write(HexUtil.stringToInt(returnMessage));
                 out.flush();
            }else if (GaoliweiController.getButton1().equals("1") && gaoliweiMachine.getId()==2L) {
                LOGGER.debug("MES连线系统主动完成任务2!");
                 returnMessage = getFinishId();
                 GaoliweiController.setButton1("0");
                 DataOutputStream out = new DataOutputStream(socket.getOutputStream());
                 out.write(HexUtil.stringToInt(returnMessage));
                 out.flush();
            }
            
 
          
//            DataInputStream in = new DataInputStream(socket.getInputStream());
//            bufSize = in.read(msg);
//            String message = HexUtil.byteToHexString(bufSize, msg);
//            
//            String returnMessage = service.generateReturnMessage(message, identifyMachine);
//            
//            // 客户端会一直向服务器发数据
//            if(returnMessage!=null){
//                DataOutputStream out = new DataOutputStream(socket.getOutputStream());
//                out.write(HexUtil.stringToInt(returnMessage));
//                out.flush();
//            }
        }
    }
    
    private boolean existMessage() {
 
        // 若缓冲区长度小于消息最小长度,则缓冲区中的消息无效。
        if (MESSAGE_BUFFER.length() < 84) {
            return false;
        }
        
        String head = MESSAGE_BUFFER.substring(0, 10);
        LOGGER.trace("head: " + head);
        
        // 若缓冲区不以HEAD为起始,则删掉字符;
        // 一个字符一个字符删是避免一次删多导致消息损坏。
        if (!head.equalsIgnoreCase(HEAD)) {
            MESSAGE_BUFFER.delete(0, 2);
            return false;
        }
        
        // 获取消息长度
        String messageLengthHex = MESSAGE_BUFFER.substring(MESSAGE_LENGTH_START, MESSAGE_LENGTH_END + 1);
        int messageLength = HexUtil.hexToInt(messageLengthHex);
        LOGGER.trace("messageLength: " + messageLength);
        
        if (MESSAGE_BUFFER.length() < DATA_START + messageLength*2) {
            return false;
        }
        
        String dataAndEnd = MESSAGE_BUFFER.substring(DATA_START, DATA_START + messageLength*2);
        String end = dataAndEnd.substring(dataAndEnd.length() - 10, dataAndEnd.length());
        LOGGER.trace("end: " + end);
        
        if (head.equals(HEAD) && end.equals(END)) {
            return true;
        }
                
        return false;
    }
    
    private String getNextMessage() {
        // 获取消息长度
        String messageLengthHex = MESSAGE_BUFFER.substring(MESSAGE_LENGTH_START, MESSAGE_LENGTH_END + 1);
        int messageLength = HexUtil.hexToInt(messageLengthHex);
        LOGGER.trace("messageLength: " + messageLength);
        
        String message = MESSAGE_BUFFER.substring(0, DATA_START + messageLength*2);
        MESSAGE_BUFFER.delete(0, DATA_START + messageLength*2);
        
//        int messageEnd = MESSAGE_BUFFER.indexOf("3c454f463e");
//        String message = "";
//        
//        if (messageEnd >= 0) {
//            message = MESSAGE_BUFFER.substring(0, messageEnd + 10);
//            MESSAGE_BUFFER = MESSAGE_BUFFER.delete(0, messageEnd + 10);
//        }
        
        return message;
    }
 
    
    @Override
    public void run() {
        ServerSocket serverSocket = null;
        
        try {
            serverSocket = new ServerSocket(gaoliweiMachine.getPort());
            LOGGER.debug("高力威【" + gaoliweiMachine.getNumber() + "】启动监听,等待客户端连接,端口:" + gaoliweiMachine.getPort());
            gaoliweiMachine.getServerConnection().setServerSocket(serverSocket);
            
            connect(serverSocket);
            
            // 一直循环,若连接断开,则在Exception处理环节重新连接,并继续监听消息
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    listen();
                }
                catch (IOException se) {
                    service.getright("gaoliweiMachine"+gaoliweiMachine.getNumber());
                    LOGGER.debug("高力威连接已断开!");
                    se.printStackTrace();
                    
                    LOGGER.debug("高力威等待重新连接……");
                    service.setConnectState(gaoliweiMachine, ConnectState.CONNECTING);
                    connect(serverSocket);
                }
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public String getFinishId(){
        String data = CRCUtil.sourceCRC("011000320001020020");
        String head = generateHead(data,GaoliweiFunctionNumber.CONFIRM_SIGN);
        return head + data + END;
    }
    
      /**
     * 构造消息头部
     * 
     * 通信头部 = 开始标志 + 信息长度 + 订单编号 + 功能号 + 加密方式 + 发送时刻 + 备用
     * 
     * @param data
     * @param functionNumber
     * @return
     */
    protected String generateHead(String data, String functionNumber) {
        // 信息长度
        int length = (data + END).length() / 2;
        
        String orderNumber = "000000000000000000000000";
        
        String head = HEAD 
                + HexUtil.intTo2ByteHex(length) 
                + orderNumber 
                + functionNumber 
                + NO_ENCRYPT
                + HexUtil.timeToHex(new Date())
                + BACKUP;
        
        return head;
    }
 
}