Browse Source

1、修改成工厂模式+策略模式进行数据采集发送解析;2、兼容jdk17

dev
mh 8 months ago
parent
commit
fc890b53e0
  1. 23
      user-service/pom.xml
  2. 3
      user-service/src/main/java/com/mh/user/serialport/SendAndReceiveByCom.java
  3. 20
      user-service/src/main/java/com/mh/user/serialport/SerialPortSingle2.java
  4. 9
      user-service/src/main/java/com/mh/user/serialport/SerialPortThread.java
  5. 49
      user-service/src/main/java/com/mh/user/strategy/TempControlStrategy.java
  6. 5
      user-service/src/main/java/com/mh/user/strategy/WaterLevelSwitchStrategy.java

23
user-service/pom.xml

@ -148,6 +148,29 @@
<version>4.4</version> <version>4.4</version>
</dependency> </dependency>
<!--解决高版本JDK问题-->
<!--javax.xml.bind.DatatypeConverter错误-->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies> </dependencies>
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>

3
user-service/src/main/java/com/mh/user/serialport/SendAndReceiveByCom.java

@ -37,9 +37,6 @@ public class SendAndReceiveByCom {
DeviceInstallService deviceInstallService = context.getBean(DeviceInstallService.class); DeviceInstallService deviceInstallService = context.getBean(DeviceInstallService.class);
NowDataService nowDataService = context.getBean(NowDataService.class); NowDataService nowDataService = context.getBean(NowDataService.class);
BuildingService buildingService = context.getBean(BuildingService.class); BuildingService buildingService = context.getBean(BuildingService.class);
AnalysisReceiveOrder485 analysisReceiveOrder485 = new AnalysisReceiveOrder485();
SysParamService sysParamService = context.getBean(SysParamService.class);
public void sendAndReceive(String sort, String thread) { public void sendAndReceive(String sort, String thread) {
SerialPort serialPort = null; SerialPort serialPort = null;

20
user-service/src/main/java/com/mh/user/serialport/SerialPortSingle2.java

@ -18,8 +18,6 @@ import java.io.IOException;
import java.util.Date; import java.util.Date;
import purejavacomm.SerialPort; import purejavacomm.SerialPort;
import purejavacomm.SerialPortEvent;
import purejavacomm.SerialPortEventListener;
/** /**
* @author nxr * @author nxr
@ -33,7 +31,6 @@ public class SerialPortSingle2 {
// 调用service // 调用service
ApplicationContext context = SpringBeanUtil.getApplicationContext(); ApplicationContext context = SpringBeanUtil.getApplicationContext();
AnalysisReceiveOrder485 analysisReceiveOrder485 = new AnalysisReceiveOrder485();
DeviceInstallService deviceInstallService = context.getBean(DeviceInstallService.class); DeviceInstallService deviceInstallService = context.getBean(DeviceInstallService.class);
NowDataService nowDataService = context.getBean(NowDataService.class); NowDataService nowDataService = context.getBean(NowDataService.class);
BuildingService buildingService = context.getBean(BuildingService.class); BuildingService buildingService = context.getBean(BuildingService.class);
@ -42,6 +39,9 @@ public class SerialPortSingle2 {
SerialPort serialPort = null; SerialPort serialPort = null;
String rtData = "fail"; String rtData = "fail";
String comName = deviceCodeParamEntity.getDataCom().toUpperCase(); String comName = deviceCodeParamEntity.getDataCom().toUpperCase();
if (StringUtils.isBlank(comName)) {
return rtData;
}
try { try {
int baudrate = deviceCodeParamEntity.getBaudrate(); int baudrate = deviceCodeParamEntity.getBaudrate();
String parity = deviceCodeParamEntity.getParity(); String parity = deviceCodeParamEntity.getParity();
@ -50,9 +50,12 @@ public class SerialPortSingle2 {
} else { } else {
serialPort = SerialTool.openPort(comName, baudrate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_EVEN); serialPort = SerialTool.openPort(comName, baudrate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_EVEN);
} }
if (serialPort == null) {
log.info("串口为空,无法进行采集控制");
return "fail";
}
// 创建设备报文 // 创建设备报文
Device device = DeviceFactory.createDevice(deviceCodeParamEntity.getDeviceType()); Device device = DeviceFactory.createDevice(deviceCodeParamEntity.getDeviceType());
if (serialPort != null) {
//向串口发送指令 //向串口发送指令
log.info("-----------------------------单抄向串口" + serialPort.getName() + "发送指令!-----------------------------"); log.info("-----------------------------单抄向串口" + serialPort.getName() + "发送指令!-----------------------------");
// String sendStr = SendOrderUtils.getSendStr(deviceCodeParamEntity); // String sendStr = SendOrderUtils.getSendStr(deviceCodeParamEntity);
@ -82,12 +85,7 @@ public class SerialPortSingle2 {
log.error("单抄串口" + finalSerialPort + "异常,关闭串口", e); log.error("单抄串口" + finalSerialPort + "异常,关闭串口", e);
} }
}, serialPort); }, serialPort);
} String receiveStr = "";
String receiveStr;
if (serialPort == null) {
log.info("串口为空,无法进行采集控制");
return "fail";
}
receiveStr = CacheTools.waitReceiveMsg(serialPort.getName()); receiveStr = CacheTools.waitReceiveMsg(serialPort.getName());
//去掉空格和null //去掉空格和null
if (StringUtils.isBlank(receiveStr)) { if (StringUtils.isBlank(receiveStr)) {
@ -152,8 +150,6 @@ public class SerialPortSingle2 {
// } // }
rtData = device.analysisReceiveData(DateUtil.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss"), rtData = device.analysisReceiveData(DateUtil.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss"),
deviceType, registerAddr, brand, buildingId, buildingName, dataStr); deviceType, registerAddr, brand, buildingId, buildingName, dataStr);
SerialTool.closePort(serialPort);
Thread.sleep(200);
log.info("-----------------------------" + serialPort.getName() + "单抄结束!-----------------------------"); log.info("-----------------------------" + serialPort.getName() + "单抄结束!-----------------------------");
return rtData; return rtData;
} catch (Exception e) { } catch (Exception e) {

9
user-service/src/main/java/com/mh/user/serialport/SerialPortThread.java

@ -18,8 +18,6 @@ public class SerialPortThread implements Runnable{
private String thread = "1"; private String thread = "1";
public static SerialPort serialPort = null; public static SerialPort serialPort = null;
private String name; private String name;
// 调用service
//ApplicationContext context = SpringBeanUtil.getApplicationContext();
private CountDownLatch countDownLatch; private CountDownLatch countDownLatch;
@ -37,10 +35,13 @@ public class SerialPortThread implements Runnable{
public void run(){ public void run(){
log.info("创建发送接收数据线程>>>>>>>>>>>>>>" + thread); log.info("创建发送接收数据线程>>>>>>>>>>>>>>" + thread);
// SerialPortSendReceive2 serial=new SerialPortSendReceive2();
// serial.serialPortSend(name,thread);
SendAndReceiveByCom sendAndReceiveByCom = new SendAndReceiveByCom(); SendAndReceiveByCom sendAndReceiveByCom = new SendAndReceiveByCom();
try {
sendAndReceiveByCom.sendAndReceive(name, thread); sendAndReceiveByCom.sendAndReceive(name, thread);
} catch (Exception e) {
log.error("串口通信发生异常: ", e);
} finally {
this.countDownLatch.countDown(); this.countDownLatch.countDown();
} }
} }
}

49
user-service/src/main/java/com/mh/user/strategy/TempControlStrategy.java

@ -7,11 +7,13 @@ import com.mh.user.entity.NowPublicDataEntity;
import com.mh.user.service.BuildingService; import com.mh.user.service.BuildingService;
import com.mh.user.service.NowDataService; import com.mh.user.service.NowDataService;
import com.mh.user.service.NowPublicDataService; import com.mh.user.service.NowPublicDataService;
import com.mh.user.utils.DateUtil;
import com.mh.user.utils.ExchangeStringUtil; import com.mh.user.utils.ExchangeStringUtil;
import com.mh.user.utils.SpringBeanUtil; import com.mh.user.utils.SpringBeanUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -75,54 +77,33 @@ public class TempControlStrategy implements DeviceStrategy {
@Override @Override
public String analysisReceiveData(String dateStr, String deviceType, String registerAddr, String brand, String buildingId, String buildingName, String dataStr) { public String analysisReceiveData(String dateStr, String deviceType, String registerAddr, String brand, String buildingId, String buildingName, String dataStr) {
String result = Constant.FAIL; String result = Constant.FAIL;
String checkStr = dataStr.substring(0, dataStr.length() - 4);// 检验报文 String checkStr = dataStr.substring(0, dataStr.length() - 4);
String checkWord = ExchangeStringUtil.getStrCRC16(checkStr);//生成校验码 String checkWord = ExchangeStringUtil.getStrCRC16(checkStr);//生成校验码
if (!checkWord.equalsIgnoreCase(dataStr.substring(dataStr.length() - 4))) { if (!checkWord.equalsIgnoreCase(dataStr.substring(dataStr.length() - 4))) {
log.info("温度变送器报文检验失败: " + dataStr); log.info("温报文检验失败: " + dataStr);
return result; return result;
} }
//log.info("温度变送器报文检验成功: " + receiveData);
String addr = ExchangeStringUtil.hexToDec(checkStr.substring(0, 2));//地址 String addr = ExchangeStringUtil.hexToDec(checkStr.substring(0, 2));//地址
//log.info("温度变送器:" + addr);
String data = ""; String data = "";
if (checkStr.substring(2, 4).equalsIgnoreCase("03")) {// 读 if (checkStr.substring(2, 4).equalsIgnoreCase("03")) {// 读
Map<String, String> map = new HashMap<>(); data = ExchangeStringUtil.hexToDec(checkStr.substring(6, 10));
String L1 = checkStr.substring(6, 10); //1路 Double fdata = Double.parseDouble(data) / 10;
map.put("1", String.valueOf(ExchangeStringUtil.parseHex4(L1) / 10)); if (fdata <= 25) {
String L2 = checkStr.substring(10, 14); //2路 fdata = 25.0;
map.put("2", String.valueOf(ExchangeStringUtil.parseHex4(L2) / 10)); } else if (fdata >= 65) {
String L3 = checkStr.substring(14, 18); //3路 fdata = 65.0;
map.put("3", String.valueOf(ExchangeStringUtil.parseHex4(L3) / 10));
String L4 = checkStr.substring(18, 22); //4路
map.put("4", String.valueOf(ExchangeStringUtil.parseHex4(L4) / 10));
String L5 = checkStr.substring(22, 26); //5路
map.put("5", String.valueOf(ExchangeStringUtil.parseHex4(L5) / 10));
String L6 = checkStr.substring(26, 30); //6路
map.put("6", String.valueOf(ExchangeStringUtil.parseHex4(L6) / 10));
String L7 = checkStr.substring(30, 34); //7路
map.put("7", String.valueOf(ExchangeStringUtil.parseHex4(L7) / 10));
String L8 = checkStr.substring(34, 38); //8路
map.put("8", String.valueOf(ExchangeStringUtil.parseHex4(L8) / 10));
int count = buildingService.selectPumpCount(buildingId); //楼栋热泵数
if (count > 0) {
for (Map.Entry<String, String> entry : map.entrySet()) {
addr = entry.getKey();
data = entry.getValue();
if (addr != null && Integer.parseInt(addr) <= count) {
nowDataService.saveNowHistoryData3(addr, "温度变送器", data, "waterTemp", buildingId);
nowDataService.proWaterTemp(dateStr, buildingId, addr); //保存时间点温度
}
} }
log.info("温度变送器:" + addr + ",保存数据库成功!楼栋名称:" + buildingName); nowDataService.saveNowHistoryData2(addr, "温控", String.valueOf(fdata), "waterTemp", buildingId);
nowDataService.proWaterTemp(dateStr, buildingId, "");//保存时间点温度
String avgTemp = nowDataService.selectAve(buildingId); String avgTemp = nowDataService.selectAve(buildingId);
NowPublicDataEntity publicData = new NowPublicDataEntity(); NowPublicDataEntity publicData = new NowPublicDataEntity();
publicData.setBuildingId(buildingId); publicData.setBuildingId(buildingId);
publicData.setUseWaterTemp(avgTemp); publicData.setUseWaterTemp(avgTemp);
publicData.setBackWaterTemp(avgTemp); publicData.setBackWaterTemp(avgTemp);
publicData.setSingleTemp(String.valueOf(fdata));//单箱温度
nowPublicDataService.saveNowHistoryPublicData(publicData); nowPublicDataService.saveNowHistoryPublicData(publicData);
result = JSON.toJSONString(map); //map转json字符串 log.info("温控号:" + addr + ",温度值:" + fdata + ",保存数据库成功!楼栋名称:" + buildingName);
} return String.valueOf(fdata);
} }
return result; return result;
} }

5
user-service/src/main/java/com/mh/user/strategy/WaterLevelSwitchStrategy.java

@ -204,8 +204,9 @@ public class WaterLevelSwitchStrategy implements DeviceStrategy {
SysParamEntity sysParamEntity = sysParamService.selectSysParam(); SysParamEntity sysParamEntity = sysParamService.selectSysParam();
String customName = sysParamEntity.getCustomName(); String customName = sysParamEntity.getCustomName();
if (!StringUtils.isBlank(customName)) { if (!StringUtils.isBlank(customName)) {
if (customName.contains(Constant.CUSTOM_NAME_HUAXIA) if ((customName.contains(Constant.CUSTOM_NAME_HUAXIA)
|| customName.contains(Constant.CUSTOM_NAME_GUANGSHANG)) { || customName.contains(Constant.CUSTOM_NAME_GUANGSHANG)) && !Constant.WEB_FLAG ) {
analysisPumpStateOrder(dateStr, dataStr, registerAddr, brand, buildingId, buildingName);
return analysisRelayOrder485(dateStr, dataStr,registerAddr,brand,buildingId, buildingName, customName); return analysisRelayOrder485(dateStr, dataStr,registerAddr,brand,buildingId, buildingName, customName);
} else { } else {
return analysisPumpStateOrder(dateStr, dataStr, registerAddr, brand, buildingId, buildingName); return analysisPumpStateOrder(dateStr, dataStr, registerAddr, brand, buildingId, buildingName);

Loading…
Cancel
Save