Commit 16a45a69 authored by wanghao's avatar wanghao

1 每次读取后 关闭 socket连接

parent 223e5917
...@@ -3,12 +3,14 @@ package com.zehong; ...@@ -3,12 +3,14 @@ package com.zehong;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.scheduling.annotation.EnableScheduling;
/** /**
* 启动程序 * 启动程序
* *
* @author zehong * @author zehong
*/ */
@EnableScheduling
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }) @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class ZhMesAgecalApplication public class ZhMesAgecalApplication
{ {
......
...@@ -53,59 +53,57 @@ public class EquipmentDataCollection { ...@@ -53,59 +53,57 @@ public class EquipmentDataCollection {
log.error("设备列表查询结果为空"); log.error("设备列表查询结果为空");
throw new Exception("无设备信息!"); throw new Exception("无设备信息!");
} }
if(equipmentInfos.size() > 0){ String sendMsg = "00010000000601040000000A";
String sendMsg = "00010000000601040000000A"; String receiveMsg;
String receiveMsg;
TEquipmentAlarmData alarmData = new TEquipmentAlarmData(); TEquipmentAlarmData alarmData = new TEquipmentAlarmData();
for(TEquipmentInfo info : equipmentInfos){ for(TEquipmentInfo info : equipmentInfos){
/* 1.老化柜、标定柜通讯 */ /* 1.老化柜、标定柜通讯 */
receiveMsg = agingCabinetCommunication(info, sendMsg); receiveMsg = agingCabinetCommunication(info, sendMsg);
if(receiveMsg == null){ if(receiveMsg == null){
// 记录异常数据 // 记录异常数据
alarmData.setfAlarmType("01"); // 01.老化柜 02.标定柜 03.机械臂 04.老化层 05.点位 alarmData.setfAlarmType("01"); // 01.老化柜 02.标定柜 03.机械臂 04.老化层 05.点位
alarmData.setfEquipmentCode(info.getfEquipmentCode()); alarmData.setfEquipmentCode(info.getfEquipmentCode());
alarmData.setfAlarmData("老化柜通讯失败,无响应"); alarmData.setfAlarmData("老化柜通讯失败,无响应");
tEquipmentAlarmDataService.insertTEquipmentAlarmData(alarmData); tEquipmentAlarmDataService.insertTEquipmentAlarmData(alarmData);
// 更新老化柜状态(“0”空闲,“1”运行,“2”故障)
info.setfStatus("2");
tEquipmentInfoService.updateTEquipmentInfo(info);
} else if (receiveMsg.length() != 58) {
// 记录异常数据
alarmData.setfAlarmType("01"); // 01.老化柜 02.标定柜 03.机械臂 04.老化层 05.点位
alarmData.setfEquipmentCode(info.getfEquipmentCode());
alarmData.setfAlarmData("老化柜IP或端口号为空");
tEquipmentAlarmDataService.insertTEquipmentAlarmData(alarmData);
} else {
// 解析服务器返回信息
// 示例:00 01 00 00 00 17 01 04 14 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
String flag = receiveMsg.substring(18, 58);
if("0000000000000000000000000000000000000000".equals(flag) && !"0".equals(info.getfStatus())){
// 更新老化柜状态(“0”空闲,“1”运行,“2”故障) // 更新老化柜状态(“0”空闲,“1”运行,“2”故障)
info.setfStatus("2"); info.setfStatus("0");
info.setfUpdateTime(new Date());
tEquipmentInfoService.updateTEquipmentInfo(info);
} else if (!"0000000000000000000000000000000000000000".equals(flag) && "0".equals(info.getfStatus())){
info.setfStatus("1");
info.setfUpdateTime(new Date());
tEquipmentInfoService.updateTEquipmentInfo(info); tEquipmentInfoService.updateTEquipmentInfo(info);
} else if (receiveMsg.length() != 58) {
// 记录异常数据
alarmData.setfAlarmType("01"); // 01.老化柜 02.标定柜 03.机械臂 04.老化层 05.点位
alarmData.setfEquipmentCode(info.getfEquipmentCode());
alarmData.setfAlarmData("老化柜IP或端口号为空");
tEquipmentAlarmDataService.insertTEquipmentAlarmData(alarmData);
} else if (receiveMsg.length() == 58){
// 解析服务器返回信息
// 示例:00 01 00 00 00 17 01 04 14 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
String flag = receiveMsg.substring(18, 58);
if("0000000000000000000000000000000000000000".equals(flag) && !"0".equals(info.getfStatus())){
// 更新老化柜状态(“0”空闲,“1”运行,“2”故障)
info.setfStatus("0");
info.setfUpdateTime(new Date());
tEquipmentInfoService.updateTEquipmentInfo(info);
} else if (!"0000000000000000000000000000000000000000".equals(flag) && "0".equals(info.getfStatus())){
info.setfStatus("1");
info.setfUpdateTime(new Date());
tEquipmentInfoService.updateTEquipmentInfo(info);
// 解析层状态,根据层编号修改状态 // 解析层状态,根据层编号修改状态
int startIndex; int startIndex;
int endIndex; int endIndex;
int code; //组成老化层编号 int code; //组成老化层编号
String status; String status;
TStoreyInfo storeyInfo = new TStoreyInfo(); TStoreyInfo storeyInfo = new TStoreyInfo();
for(startIndex = 18,endIndex = 22,code = 1; endIndex <= receiveMsg.length(); startIndex += 4,endIndex += 4,code++){ for(startIndex = 18,endIndex = 22,code = 1; endIndex <= receiveMsg.length(); startIndex += 4,endIndex += 4,code++){
status = receiveMsg.substring(startIndex, endIndex); status = receiveMsg.substring(startIndex, endIndex);
storeyInfo.setfStoreyCode(info.getfEquipmentCode() + "-" + code); storeyInfo.setfStoreyCode(info.getfEquipmentCode() + "-" + code);
if("0000".equals(status)){ if("0000".equals(status)){
/* 2.老化层断电 */ /* 2.老化层断电 */
storeyInfo.setfStatus("4"); //断电 storeyInfo.setfStatus("4"); //断电
tStoreyInfoService.updateTStoreyInfoByCode(storeyInfo); tStoreyInfoService.updateTStoreyInfoByCode(storeyInfo);
}
} }
} }
} }
......
...@@ -12,7 +12,7 @@ import java.net.Socket; ...@@ -12,7 +12,7 @@ import java.net.Socket;
* *
* @author zehong * @author zehong
*/ */
public class ReceiverThread extends Thread implements Runnable { public class ReceiverThread extends Thread {
private static final Logger log = LoggerFactory.getLogger(ReceiverThread.class); private static final Logger log = LoggerFactory.getLogger(ReceiverThread.class);
public static String hexResponse; public static String hexResponse;
private final Socket socket; private final Socket socket;
...@@ -34,7 +34,11 @@ public class ReceiverThread extends Thread implements Runnable { ...@@ -34,7 +34,11 @@ public class ReceiverThread extends Thread implements Runnable {
// 将接收到的字节转换为十六进制字符串显示 // 将接收到的字节转换为十六进制字符串显示
hexResponse = HexUtils.bytesToHex(responseBytes); hexResponse = HexUtils.bytesToHex(responseBytes);
log.info("收到服务器响应: " + hexResponse); log.info("收到服务器响应: " + hexResponse);
if(hexResponse.length() == 58) {
socket.close();
}
} }
log.info("解析完了啊啊啊啊啊: " + hexResponse);
} catch (IOException e) { } catch (IOException e) {
log.error("接收数据失败: " + e.getMessage()); log.error("接收数据失败: " + e.getMessage());
} }
......
...@@ -34,13 +34,15 @@ public class TCPClient { ...@@ -34,13 +34,15 @@ public class TCPClient {
log.info("已发送数据: " + sendMsg); log.info("已发送数据: " + sendMsg);
receiverThread = new ReceiverThread(socket); receiverThread = new ReceiverThread(socket);
hexResponse = receiverThread.hexResponse;
receiverThread.start(); receiverThread.start();
// receiverThread.join(); receiverThread.join();
hexResponse = ReceiverThread.hexResponse;
} catch (IOException e) { } catch (IOException e) {
System.err.println("通信异常: " + e.getMessage()); System.err.println("通信异常: " + e.getMessage());
e.printStackTrace(); e.printStackTrace();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} }
return hexResponse; return hexResponse;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment