修改modbus读取设备数据连接池配置
This commit is contained in:
@ -1,11 +1,27 @@
|
||||
package com.xzzn.framework.manager;
|
||||
|
||||
import com.ghgande.j2mod.modbus.net.SerialConnection;
|
||||
import com.ghgande.j2mod.modbus.net.TCPMasterConnection;
|
||||
import com.ghgande.j2mod.modbus.util.SerialParameters;
|
||||
import com.xzzn.common.enums.DeviceType;
|
||||
import com.xzzn.ems.domain.EmsDevicesSetting;
|
||||
import com.xzzn.ems.mapper.EmsDevicesSettingMapper;
|
||||
import com.xzzn.ems.service.IEmsAlarmRecordsService;
|
||||
import com.xzzn.ems.service.IEmsDeviceSettingService;
|
||||
import com.xzzn.ems.service.IEmsEnergyPriceConfigService;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
import org.apache.commons.pool2.impl.GenericObjectPool;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -14,14 +30,6 @@ import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
import java.net.InetAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
public class ModbusConnectionManager implements ApplicationRunner {
|
||||
private static final Logger logger = LoggerFactory.getLogger(ModbusConnectionManager.class);
|
||||
@ -34,6 +42,7 @@ public class ModbusConnectionManager implements ApplicationRunner {
|
||||
private long maxWaitMillis = 3000;
|
||||
private long timeBetweenEvictionRunsMillis = 30000;
|
||||
private long minEvictableIdleTimeMillis = 60000;
|
||||
private int connectTimeOut = 5000;
|
||||
|
||||
private ScheduledExecutorService scheduler;
|
||||
@Autowired
|
||||
@ -93,14 +102,29 @@ public class ModbusConnectionManager implements ApplicationRunner {
|
||||
/**
|
||||
* 创建原始Modbus连接
|
||||
*/
|
||||
private TCPMasterConnection createRawConnection(EmsDevicesSetting device) throws Exception {
|
||||
private Object createRawConnection(EmsDevicesSetting device) throws Exception {
|
||||
try {
|
||||
InetAddress addr = InetAddress.getByName("10.1.0.230");
|
||||
TCPMasterConnection connection = new TCPMasterConnection(addr);
|
||||
connection.setPort(502);
|
||||
connection.setTimeout(5000);
|
||||
connection.connect();
|
||||
return connection;
|
||||
if (DeviceType.TCP.name().equals(device.getDeviceType())) {
|
||||
InetAddress addr = InetAddress.getByName(device.getIpAddress());
|
||||
TCPMasterConnection connection = new TCPMasterConnection(addr);
|
||||
connection.setPort(device.getIpPort().intValue());
|
||||
connection.setTimeout(connectTimeOut);
|
||||
connection.connect();
|
||||
return connection;
|
||||
} else if (DeviceType.RTU.name().equals(device.getDeviceType())) {
|
||||
SerialParameters parameters = new SerialParameters();
|
||||
parameters.setPortName(device.getSerialPort());
|
||||
parameters.setBaudRate(device.getBaudRate().intValue());
|
||||
parameters.setDatabits(device.getDataBits().intValue());
|
||||
parameters.setStopbits(device.getStopBits().intValue());
|
||||
parameters.setParity(device.getParity());
|
||||
SerialConnection connection = new SerialConnection(parameters);
|
||||
connection.setTimeout(connectTimeOut);
|
||||
connection.open();
|
||||
return connection;
|
||||
} else {
|
||||
throw new IllegalArgumentException("不支持的设备类型: " + device.getDeviceType());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("创建Modbus连接失败: {}", device, e);
|
||||
throw e;
|
||||
|
||||
Reference in New Issue
Block a user