package com.northglass.listener;
|
|
import java.io.DataInputStream;
|
import java.io.DataOutputStream;
|
import java.io.IOException;
|
import java.io.PrintWriter;
|
import java.io.StringWriter;
|
import java.net.ConnectException;
|
import java.net.Socket;
|
import java.util.Date;
|
|
import org.slf4j.Logger;
|
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.support.GenericXmlApplicationContext;
|
|
import com.northglass.constants.ConnectState;
|
import com.northglass.entity.LoadMachine;
|
import com.northglass.entity.Shelf;
|
import com.northglass.log.GLoggerFactory;
|
import com.northglass.service.device.DeviceService;
|
import com.northglass.service.loadmachine.LoadMachineService;
|
import com.northglass.service.shelf.ShelfService;
|
import com.northglass.util.HexUtil;
|
|
public class LoadMachineClientListener implements Runnable {
|
|
private static final Logger LOGGER = GLoggerFactory.getLogger(LoadMachineClientListener.class);
|
|
private static final int INTERVAL = 5000;
|
|
private static final int MESSAGE_LENGTH_START = 10;
|
private static final int MESSAGE_LENGTH_END = 13;
|
|
private static final int DATA_START = 74;
|
|
/**
|
* 信息头,字符串“<STA>”的16进制表示
|
*/
|
private static final String HEAD = "3c5354413e";
|
|
/**
|
* 信息尾,字符串“<EOF>”的16进制表示
|
*/
|
private static final String END = "3c454f463e";
|
|
private StringBuffer MESSAGE_BUFFER = new StringBuffer();
|
|
private LoadMachine machineClient;
|
private LoadMachineService machineService;
|
protected static final int BUFFER_SIZE = 2048;
|
protected byte[] msg = new byte[BUFFER_SIZE];
|
protected int bufSize = 0;
|
|
// 该方法Jetty中用不到,但是Tomcat中可能会有问题
|
private ApplicationContext initializeSpringContext() {
|
GenericXmlApplicationContext context = new GenericXmlApplicationContext();
|
context.getEnvironment().setActiveProfiles("production");
|
context.load("applicationContext.xml");
|
context.refresh();
|
|
return context;
|
}
|
|
public LoadMachineClientListener(LoadMachine machineClient, LoadMachineService machineService) {
|
initializeSpringContext();
|
this.machineClient = machineClient;
|
this.machineService = machineService;
|
}
|
private void newconnect(){
|
try {
|
Socket socket = new Socket(machineClient.getIpAddress(), machineClient.getPort());
|
try {
|
machineService.setConnectState(machineClient, "已连接");
|
machineClient.getServerConnection().setSocket(socket);
|
try {
|
Thread.sleep(1000);
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
socket.setSoTimeout(6000);
|
while(true) {
|
DataOutputStream outToServer = new DataOutputStream(socket.getOutputStream());
|
outToServer.write(HexUtil.stringToInt(sendMessage()));
|
outToServer.flush();
|
DataInputStream in = new DataInputStream(socket.getInputStream());
|
bufSize = in.read(msg);
|
String message = HexUtil.byteToHexString(bufSize, msg);
|
String returnMessage = "";
|
try {
|
returnMessage = machineService.generateReturnMessage(message,machineClient);
|
} catch (Exception e) {
|
System.out.println(machineClient.getIpAddress()+":"+machineClient.getPort()+"代码M85");
|
}
|
if (returnMessage != null && !"".equals(returnMessage)) {
|
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
|
out.write(HexUtil.stringToInt(returnMessage));
|
out.flush();
|
} else {
|
try {
|
Thread.sleep(500);
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
} catch (Exception e) {
|
// TODO: handle exception
|
machineService.setConnectState(machineClient,"重连");
|
}
|
socket.close();
|
} catch (Exception e) {
|
// TODO: handle exception
|
machineService.setConnectState(machineClient,"检查网络");
|
}
|
}
|
private void connect() throws IOException {
|
Socket socket = new Socket(machineClient.getIpAddress(), machineClient.getPort());
|
LOGGER.debug("【" + machineClient.getNumber() + "】启动连接服务器,端口:" + machineClient.getPort());
|
machineService.setConnectState( machineClient ,ConnectState.CONNECTED);//ConnectState.CONNECTED
|
machineClient.getServerConnection().setSocket(socket);
|
DataOutputStream outToServer = new DataOutputStream(socket.getOutputStream());
|
// LOGGER.debug("发送信息1:" + HexUtil.stringToInt(sendMessage()));
|
try {
|
Thread.sleep(100);
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
String loaderror=getStackTraceInfo(e);
|
System.out.println("上片程序异常listen()错误!"+loaderror);
|
DeviceService.WriteFile("D:", "上片异常记录",new Date()+"listen()错误1:"+loaderror);
|
}
|
outToServer.write(HexUtil.stringToInt(sendMessage()));
|
outToServer.flush();
|
}
|
|
private void listen() throws IOException {
|
Socket socket = machineClient.getServerConnection().getSocket();
|
socket.setSoTimeout(5000);
|
|
while(true) {
|
DataInputStream in = new DataInputStream(socket.getInputStream());
|
bufSize = in.read(msg);
|
String message = HexUtil.byteToHexString(bufSize, msg);
|
String returnMessage = machineService.processMessage(message, machineClient);
|
if (returnMessage != null && !"".equals(returnMessage)) {
|
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
|
out.write(HexUtil.stringToInt(returnMessage));
|
out.flush();
|
} else {
|
DataOutputStream outToServer = new DataOutputStream(socket.getOutputStream());
|
try {
|
Thread.sleep(150);
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
String loaderror=getStackTraceInfo(e);
|
System.out.println("上片程序异常listen()错误!"+loaderror);
|
DeviceService.WriteFile("D:", "上片异常记录",new Date()+"listen()错误2:"+loaderror);
|
}
|
outToServer.write(HexUtil.stringToInt(sendMessage()));
|
outToServer.flush();
|
}
|
}
|
}
|
|
public 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;
|
}
|
|
/**
|
* 获取e.printStackTrace() 的具体信息,赋值给String 变量,并返回
|
*
|
* @param e
|
* Exception
|
* @return e.printStackTrace() 中 的信息
|
*/
|
public static String getStackTraceInfo(Exception e) {
|
StringWriter sw = null;
|
PrintWriter pw = null;
|
try {
|
sw = new StringWriter();
|
pw = new PrintWriter(sw);
|
e.printStackTrace(pw);//将出错的栈信息输出到printWriter中
|
pw.flush();
|
sw.flush();
|
return sw.toString();
|
} catch (Exception ex) {
|
return "printStackTrace()转换错误";
|
} finally {
|
if (sw != null) {
|
try {
|
sw.close();
|
} catch (IOException e1){
|
e1.printStackTrace();
|
}
|
}
|
if (pw != null) {
|
pw.close();
|
}
|
}
|
|
}
|
|
public 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);
|
|
return message;
|
}
|
|
@Override
|
public void run() {
|
while (true) {
|
try {
|
newconnect();
|
} catch (Exception e) {
|
// TODO: handle exception
|
}
|
}
|
}
|
|
private String sendMessage() {
|
String date = "00010000000601030000002D";
|
return date;
|
}
|
|
}
|