|
@@ -1,514 +0,0 @@
|
|
|
-//package cn.oleauto.modbus.tcp.uitl;
|
|
|
|
|
-//
|
|
|
|
|
-//import cn.oleauto.modbus.tcp.dto.*;
|
|
|
|
|
-//import cn.oleauto.modbus.tcp.exception.ModbusDataException;
|
|
|
|
|
-//import cn.oleauto.modbus.tcp.service.DataParser;
|
|
|
|
|
-//import com.serotonin.modbus4j.ModbusMaster;
|
|
|
|
|
-//import com.serotonin.modbus4j.exception.ErrorResponseException;
|
|
|
|
|
-//import com.serotonin.modbus4j.exception.ModbusTransportException;
|
|
|
|
|
-//import com.serotonin.modbus4j.locator.BaseLocator;
|
|
|
|
|
-//import org.slf4j.Logger;
|
|
|
|
|
-//import org.slf4j.LoggerFactory;
|
|
|
|
|
-//
|
|
|
|
|
-//import java.util.*;
|
|
|
|
|
-//import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
-//import java.util.concurrent.ExecutorService;
|
|
|
|
|
-//import java.util.concurrent.Executors;
|
|
|
|
|
-//import java.util.stream.Collectors;
|
|
|
|
|
-//
|
|
|
|
|
-//public class ModbusDataReader {
|
|
|
|
|
-// private static final Logger logger = LoggerFactory.getLogger(ModbusDataReader.class);
|
|
|
|
|
-//
|
|
|
|
|
-// private final ModbusMasterManager masterManager;
|
|
|
|
|
-// private final Map<String, ParseResult> cacheMap = new ConcurrentHashMap<>();
|
|
|
|
|
-// private final Map<AreaType, List<RegisterConfig>> areaGroupMap = new ConcurrentHashMap<>();
|
|
|
|
|
-// private final Map<String, RegisterConfig> configMap = new ConcurrentHashMap<>();
|
|
|
|
|
-//
|
|
|
|
|
-// // 异步读取线程池
|
|
|
|
|
-// private final ExecutorService readExecutor = Executors.newFixedThreadPool(4);
|
|
|
|
|
-//
|
|
|
|
|
-// // 统计数据
|
|
|
|
|
-// private long totalReadCount = 0;
|
|
|
|
|
-// private long successReadCount = 0;
|
|
|
|
|
-// private long failedReadCount = 0;
|
|
|
|
|
-//
|
|
|
|
|
-// public ModbusDataReader(ModbusMasterManager masterManager) {
|
|
|
|
|
-// this.masterManager = masterManager;
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// /**
|
|
|
|
|
-// * 构建索引
|
|
|
|
|
-// */
|
|
|
|
|
-// public void buildIndex(List<RegisterConfig> registers) {
|
|
|
|
|
-// areaGroupMap.clear();
|
|
|
|
|
-// configMap.clear();
|
|
|
|
|
-//
|
|
|
|
|
-// for (RegisterConfig config : registers) {
|
|
|
|
|
-// if (!config.isActive()) continue;
|
|
|
|
|
-//
|
|
|
|
|
-// configMap.put(config.getName(), config);
|
|
|
|
|
-// AreaType area = config.getArea();
|
|
|
|
|
-// areaGroupMap.computeIfAbsent(area, k -> new ArrayList<>()).add(config);
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// // 按地址排序
|
|
|
|
|
-// for (List<RegisterConfig> list : areaGroupMap.values()) {
|
|
|
|
|
-// list.sort(Comparator.comparingInt(RegisterConfig::getAddress));
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// logger.info("索引构建完成,共{}个区域分组,{}个配置项",
|
|
|
|
|
-// areaGroupMap.size(), configMap.size());
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// /**
|
|
|
|
|
-// * 读取所有有效数据(同步)
|
|
|
|
|
-// */
|
|
|
|
|
-// public Map<String, ParseResult> readAll() {
|
|
|
|
|
-// Map<String, ParseResult> results = new LinkedHashMap<>();
|
|
|
|
|
-//
|
|
|
|
|
-// for (Map.Entry<AreaType, List<RegisterConfig>> entry : areaGroupMap.entrySet()) {
|
|
|
|
|
-// AreaType area = entry.getKey();
|
|
|
|
|
-// List<RegisterConfig> configs = entry.getValue();
|
|
|
|
|
-//
|
|
|
|
|
-// try {
|
|
|
|
|
-// Map<String, ParseResult> areaResults = readByArea(area, configs);
|
|
|
|
|
-// results.putAll(areaResults);
|
|
|
|
|
-// } catch (Exception e) {
|
|
|
|
|
-// logger.error("读取区域失败: {}", area, e);
|
|
|
|
|
-// // 为每个配置创建错误结果
|
|
|
|
|
-// for (RegisterConfig config : configs) {
|
|
|
|
|
-// ParseResult errorResult = new ParseResult();
|
|
|
|
|
-// errorResult.setName(config.getName());
|
|
|
|
|
-// errorResult.setAddress(config.getAddress());
|
|
|
|
|
-// errorResult.setArea(config.getArea());
|
|
|
|
|
-// errorResult.setType(config.getType());
|
|
|
|
|
-// errorResult.setSuccess(false);
|
|
|
|
|
-// errorResult.setErrorMessage("读取失败: " + e.getMessage());
|
|
|
|
|
-// results.put(config.getName(), errorResult);
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// // 更新缓存
|
|
|
|
|
-// cacheMap.putAll(results);
|
|
|
|
|
-// updateStatistics(results);
|
|
|
|
|
-//
|
|
|
|
|
-// return results;
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// /**
|
|
|
|
|
-// * 异步读取所有数据
|
|
|
|
|
-// */
|
|
|
|
|
-// public void readAllAsync(DataReadCallback callback) {
|
|
|
|
|
-// readExecutor.submit(() -> {
|
|
|
|
|
-// try {
|
|
|
|
|
-// Map<String, ParseResult> results = readAll();
|
|
|
|
|
-// if (callback != null) {
|
|
|
|
|
-// callback.onSuccess(results);
|
|
|
|
|
-// }
|
|
|
|
|
-// } catch (Exception e) {
|
|
|
|
|
-// logger.error("异步读取失败", e);
|
|
|
|
|
-// if (callback != null) {
|
|
|
|
|
-// callback.onError(e);
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
-// });
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// /**
|
|
|
|
|
-// * 按区域读取数据
|
|
|
|
|
-// */
|
|
|
|
|
-// private Map<String, ParseResult> readByArea(AreaType area, List<RegisterConfig> configs)
|
|
|
|
|
-// throws ModbusTransportException, ErrorResponseException {
|
|
|
|
|
-//
|
|
|
|
|
-// Map<String, ParseResult> results = new LinkedHashMap<>();
|
|
|
|
|
-//
|
|
|
|
|
-// if (configs == null || configs.isEmpty()) {
|
|
|
|
|
-// return results;
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// ModbusMaster master = masterManager.getMaster();
|
|
|
|
|
-// int slaveId = getSlaveId();
|
|
|
|
|
-//
|
|
|
|
|
-// // 过滤出可读的配置
|
|
|
|
|
-// List<RegisterConfig> readableConfigs = configs.stream()
|
|
|
|
|
-// .filter(RegisterConfig::isReadable)
|
|
|
|
|
-// .collect(Collectors.toList());
|
|
|
|
|
-//
|
|
|
|
|
-// if (readableConfigs.isEmpty()) {
|
|
|
|
|
-// return results;
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// // 按连续地址分组
|
|
|
|
|
-// List<List<RegisterConfig>> groups = groupContinuousAddresses(readableConfigs);
|
|
|
|
|
-//
|
|
|
|
|
-// for (List<RegisterConfig> group : groups) {
|
|
|
|
|
-// try {
|
|
|
|
|
-// Map<String, ParseResult> groupResults = readGroup(master, slaveId, area, group);
|
|
|
|
|
-// results.putAll(groupResults);
|
|
|
|
|
-// } catch (Exception e) {
|
|
|
|
|
-// logger.error("批量读取组失败: area={}, startAddress={}",
|
|
|
|
|
-// area, group.get(0).getAddress(), e);
|
|
|
|
|
-//
|
|
|
|
|
-// // 回退到单个读取
|
|
|
|
|
-// for (RegisterConfig config : group) {
|
|
|
|
|
-// try {
|
|
|
|
|
-// ParseResult result = readSingle(master, slaveId, config);
|
|
|
|
|
-// if (result != null) {
|
|
|
|
|
-// results.put(config.getName(), result);
|
|
|
|
|
-// }
|
|
|
|
|
-// } catch (Exception ex) {
|
|
|
|
|
-// logger.error("单个读取失败: {}", config.getName(), ex);
|
|
|
|
|
-// ParseResult errorResult = createErrorResult(config, ex.getMessage());
|
|
|
|
|
-// results.put(config.getName(), errorResult);
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// return results;
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// /**
|
|
|
|
|
-// * 按连续地址分组
|
|
|
|
|
-// */
|
|
|
|
|
-// private List<List<RegisterConfig>> groupContinuousAddresses(List<RegisterConfig> configs) {
|
|
|
|
|
-// List<List<RegisterConfig>> groups = new ArrayList<>();
|
|
|
|
|
-//
|
|
|
|
|
-// if (configs == null || configs.isEmpty()) {
|
|
|
|
|
-// return groups;
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// // 按地址排序
|
|
|
|
|
-// List<RegisterConfig> sorted = new ArrayList<>(configs);
|
|
|
|
|
-// sorted.sort(Comparator.comparingInt(RegisterConfig::getAddress));
|
|
|
|
|
-//
|
|
|
|
|
-// List<RegisterConfig> currentGroup = new ArrayList<>();
|
|
|
|
|
-//
|
|
|
|
|
-// for (int i = 0; i < sorted.size(); i++) {
|
|
|
|
|
-// RegisterConfig current = sorted.get(i);
|
|
|
|
|
-//
|
|
|
|
|
-// if (currentGroup.isEmpty()) {
|
|
|
|
|
-// currentGroup.add(current);
|
|
|
|
|
-// continue;
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// RegisterConfig previous = sorted.get(i - 1);
|
|
|
|
|
-// int previousEnd = previous.getAddress() + previous.getType().getRegisterCount();
|
|
|
|
|
-//
|
|
|
|
|
-// // 检查地址是否连续(且类型相同)
|
|
|
|
|
-// if (current.getAddress() == previousEnd &&
|
|
|
|
|
-// current.getType() == previous.getType() &&
|
|
|
|
|
-// current.getArea() == previous.getArea()) {
|
|
|
|
|
-// currentGroup.add(current);
|
|
|
|
|
-// } else {
|
|
|
|
|
-// if (!currentGroup.isEmpty()) {
|
|
|
|
|
-// groups.add(new ArrayList<>(currentGroup));
|
|
|
|
|
-// }
|
|
|
|
|
-// currentGroup.clear();
|
|
|
|
|
-// currentGroup.add(current);
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// if (!currentGroup.isEmpty()) {
|
|
|
|
|
-// groups.add(currentGroup);
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// return groups;
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// /**
|
|
|
|
|
-// * 批量读取一组配置
|
|
|
|
|
-// */
|
|
|
|
|
-// private Map<String, ParseResult> readGroup(ModbusMaster master, int slaveId,
|
|
|
|
|
-// AreaType area, List<RegisterConfig> group)
|
|
|
|
|
-// throws ModbusTransportException, ErrorResponseException {
|
|
|
|
|
-//
|
|
|
|
|
-// Map<String, ParseResult> results = new LinkedHashMap<>();
|
|
|
|
|
-// int startAddress = group.get(0).getAddress();
|
|
|
|
|
-//
|
|
|
|
|
-// if (area.isBitType()) {
|
|
|
|
|
-// // 读取位类型(线圈/离散输入)
|
|
|
|
|
-// int count = group.size();
|
|
|
|
|
-// boolean[] bitValues;
|
|
|
|
|
-//
|
|
|
|
|
-// if (area == AreaType.COIL) {
|
|
|
|
|
-// bitValues = master.getCoils(slaveId, startAddress, count);
|
|
|
|
|
-// } else {
|
|
|
|
|
-// bitValues = master.getDiscreteInputs(slaveId, startAddress, count);
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// for (int i = 0; i < group.size(); i++) {
|
|
|
|
|
-// RegisterConfig config = group.get(i);
|
|
|
|
|
-// boolean value = (i < bitValues.length) ? bitValues[i] : false;
|
|
|
|
|
-// ParseResult result = DataParser.parseBitValue(config, value);
|
|
|
|
|
-// results.put(config.getName(), result);
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// } else {
|
|
|
|
|
-// // 读取寄存器类型
|
|
|
|
|
-// int count = calculateGroupCount(group);
|
|
|
|
|
-// short[] rawData;
|
|
|
|
|
-//
|
|
|
|
|
-// if (area == AreaType.HOLDING) {
|
|
|
|
|
-// rawData = master.getHoldingRegisters(slaveId, startAddress, count);
|
|
|
|
|
-// } else {
|
|
|
|
|
-// rawData = master.getInputRegisters(slaveId, startAddress, count);
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// // 解析每个寄存器
|
|
|
|
|
-// for (RegisterConfig config : group) {
|
|
|
|
|
-// ParseResult result = DataParser.parseRegister(config, rawData, startAddress);
|
|
|
|
|
-// if (result != null) {
|
|
|
|
|
-// results.put(config.getName(), result);
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// return results;
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// /**
|
|
|
|
|
-// * 计算组需要读取的寄存器数量
|
|
|
|
|
-// */
|
|
|
|
|
-// private int calculateGroupCount(List<RegisterConfig> group) {
|
|
|
|
|
-// if (group.isEmpty()) return 0;
|
|
|
|
|
-// RegisterConfig last = group.get(group.size() - 1);
|
|
|
|
|
-// RegisterConfig first = group.get(0);
|
|
|
|
|
-// return last.getAddress() + last.getType().getRegisterCount() - first.getAddress();
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// /**
|
|
|
|
|
-// * 读取单个数据点
|
|
|
|
|
-// */
|
|
|
|
|
-// private ParseResult readSingle(ModbusMaster master, int slaveId, RegisterConfig config)
|
|
|
|
|
-// throws ModbusTransportException, ErrorResponseException {
|
|
|
|
|
-//
|
|
|
|
|
-// if (!config.isReadable()) {
|
|
|
|
|
-// return createErrorResult(config, "配置为只写,不可读");
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// if (config.isBitType()) {
|
|
|
|
|
-// boolean value;
|
|
|
|
|
-// if (config.getArea() == AreaType.COIL) {
|
|
|
|
|
-// value = master.getCoil(slaveId, config.getAddress());
|
|
|
|
|
-// } else {
|
|
|
|
|
-// value = master.getDiscreteInput(slaveId, config.getAddress());
|
|
|
|
|
-// }
|
|
|
|
|
-// return DataParser.parseBitValue(config, value);
|
|
|
|
|
-// } else {
|
|
|
|
|
-// // 寄存器类型
|
|
|
|
|
-// if (config.getType() == DataTypeEnum.BINARY) {
|
|
|
|
|
-// throw new IllegalArgumentException("寄存器不能使用BINARY类型: " + config.getName());
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// BaseLocator<Number> locator = createLocator(slaveId, config);
|
|
|
|
|
-// Number rawValue = master.getValue(locator);
|
|
|
|
|
-// return DataParser.parseNumberValue(config, rawValue);
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// /**
|
|
|
|
|
-// * 创建BaseLocator
|
|
|
|
|
-// */
|
|
|
|
|
-// private BaseLocator<Number> createLocator(int slaveId, RegisterConfig config) {
|
|
|
|
|
-// if (config.getArea() == AreaType.HOLDING) {
|
|
|
|
|
-// return BaseLocator.holdingRegister(slaveId, config.getAddress(),
|
|
|
|
|
-// config.getType().getModbus4jDataType());
|
|
|
|
|
-// } else if (config.getArea() == AreaType.INPUT) {
|
|
|
|
|
-// return BaseLocator.inputRegister(slaveId, config.getAddress(),
|
|
|
|
|
-// config.getType().getModbus4jDataType());
|
|
|
|
|
-// } else {
|
|
|
|
|
-// throw new IllegalArgumentException("不支持的寄存器区域: " + config.getArea());
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// /**
|
|
|
|
|
-// * 读取单个寄存器(公开方法)
|
|
|
|
|
-// */
|
|
|
|
|
-// public ParseResult readRegister(String name) throws ModbusDataException {
|
|
|
|
|
-// RegisterConfig config = configMap.get(name);
|
|
|
|
|
-// if (config == null) {
|
|
|
|
|
-// throw new ModbusDataException("未找到寄存器配置: " + name);
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// try {
|
|
|
|
|
-// ModbusMaster master = masterManager.getMaster();
|
|
|
|
|
-// int slaveId = getSlaveId();
|
|
|
|
|
-// return readSingle(master, slaveId, config);
|
|
|
|
|
-// } catch (ModbusTransportException | ErrorResponseException e) {
|
|
|
|
|
-// throw new ModbusDataException("读取失败: " + name, e, name, config.getAddress());
|
|
|
|
|
-// } catch (Exception e) {
|
|
|
|
|
-// throw new ModbusDataException("读取异常: " + name, e);
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// /**
|
|
|
|
|
-// * 读取单个寄存器(带配置对象)
|
|
|
|
|
-// */
|
|
|
|
|
-// public ParseResult readRegister(RegisterConfig config) throws ModbusDataException {
|
|
|
|
|
-// try {
|
|
|
|
|
-// ModbusMaster master = masterManager.getMaster();
|
|
|
|
|
-// int slaveId = getSlaveId();
|
|
|
|
|
-// return readSingle(master, slaveId, config);
|
|
|
|
|
-// } catch (ModbusTransportException | ErrorResponseException e) {
|
|
|
|
|
-// throw new ModbusDataException("读取失败: " + config.getName(), e,
|
|
|
|
|
-// config.getName(), config.getAddress());
|
|
|
|
|
-// } catch (Exception e) {
|
|
|
|
|
-// throw new ModbusDataException("读取异常: " + config.getName(), e);
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// /**
|
|
|
|
|
-// * 读取指定区域的多个寄存器
|
|
|
|
|
-// */
|
|
|
|
|
-// public Map<String, ParseResult> readRegistersByArea(AreaType area) {
|
|
|
|
|
-// List<RegisterConfig> configs = areaGroupMap.get(area);
|
|
|
|
|
-// if (configs == null || configs.isEmpty()) {
|
|
|
|
|
-// return Collections.emptyMap();
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// try {
|
|
|
|
|
-// return readByArea(area, configs);
|
|
|
|
|
-// } catch (Exception e) {
|
|
|
|
|
-// logger.error("读取区域失败: {}", area, e);
|
|
|
|
|
-// Map<String, ParseResult> errorResults = new LinkedHashMap<>();
|
|
|
|
|
-// for (RegisterConfig config : configs) {
|
|
|
|
|
-// errorResults.put(config.getName(), createErrorResult(config, e.getMessage()));
|
|
|
|
|
-// }
|
|
|
|
|
-// return errorResults;
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// /**
|
|
|
|
|
-// * 获取缓存的读取结果
|
|
|
|
|
-// */
|
|
|
|
|
-// public ParseResult getCachedResult(String name) {
|
|
|
|
|
-// return cacheMap.get(name);
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// /**
|
|
|
|
|
-// * 获取所有缓存结果
|
|
|
|
|
-// */
|
|
|
|
|
-// public Map<String, ParseResult> getAllCachedResults() {
|
|
|
|
|
-// return new LinkedHashMap<>(cacheMap);
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// /**
|
|
|
|
|
-// * 清除缓存
|
|
|
|
|
-// */
|
|
|
|
|
-// public void clearCache() {
|
|
|
|
|
-// cacheMap.clear();
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// /**
|
|
|
|
|
-// * 获取从站ID
|
|
|
|
|
-// */
|
|
|
|
|
-// private int getSlaveId() {
|
|
|
|
|
-// ModbusConfig config = masterManager.getCurrentConfig();
|
|
|
|
|
-// return config != null && config.getDevice() != null ?
|
|
|
|
|
-// config.getDevice().getSlaveId() : 1;
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// /**
|
|
|
|
|
-// * 创建错误结果
|
|
|
|
|
-// */
|
|
|
|
|
-// private ParseResult createErrorResult(RegisterConfig config, String errorMessage) {
|
|
|
|
|
-// ParseResult result = new ParseResult();
|
|
|
|
|
-// result.setName(config.getName());
|
|
|
|
|
-// result.setAddress(config.getAddress());
|
|
|
|
|
-// result.setArea(config.getArea());
|
|
|
|
|
-// result.setType(config.getType());
|
|
|
|
|
-// result.setUnit(config.getUnit());
|
|
|
|
|
-// result.setSuccess(false);
|
|
|
|
|
-// result.setErrorMessage(errorMessage);
|
|
|
|
|
-// return result;
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// /**
|
|
|
|
|
-// * 更新统计数据
|
|
|
|
|
-// */
|
|
|
|
|
-// private void updateStatistics(Map<String, ParseResult> results) {
|
|
|
|
|
-// totalReadCount++;
|
|
|
|
|
-// long success = results.values().stream().filter(ParseResult::isSuccess).count();
|
|
|
|
|
-// long failed = results.size() - success;
|
|
|
|
|
-//
|
|
|
|
|
-// successReadCount += success;
|
|
|
|
|
-// failedReadCount += failed;
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// /**
|
|
|
|
|
-// * 获取统计信息
|
|
|
|
|
-// */
|
|
|
|
|
-// public Map<String, Object> getStatistics() {
|
|
|
|
|
-// Map<String, Object> stats = new HashMap<>();
|
|
|
|
|
-// stats.put("totalReadCount", totalReadCount);
|
|
|
|
|
-// stats.put("successReadCount", successReadCount);
|
|
|
|
|
-// stats.put("failedReadCount", failedReadCount);
|
|
|
|
|
-// stats.put("successRate", totalReadCount > 0 ?
|
|
|
|
|
-// (double) successReadCount / totalReadCount * 100 : 0);
|
|
|
|
|
-// stats.put("cacheSize", cacheMap.size());
|
|
|
|
|
-// stats.put("configCount", configMap.size());
|
|
|
|
|
-// return stats;
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// /**
|
|
|
|
|
-// * 获取所有配置
|
|
|
|
|
-// */
|
|
|
|
|
-// public List<RegisterConfig> getAllConfigs() {
|
|
|
|
|
-// return new ArrayList<>(configMap.values());
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// /**
|
|
|
|
|
-// * 根据名称获取配置
|
|
|
|
|
-// */
|
|
|
|
|
-// public RegisterConfig getConfig(String name) {
|
|
|
|
|
-// return configMap.get(name);
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// /**
|
|
|
|
|
-// * 根据区域获取配置列表
|
|
|
|
|
-// */
|
|
|
|
|
-// public List<RegisterConfig> getConfigsByArea(AreaType area) {
|
|
|
|
|
-// return areaGroupMap.getOrDefault(area, Collections.emptyList());
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// /**
|
|
|
|
|
-// * 检查是否包含指定名称的配置
|
|
|
|
|
-// */
|
|
|
|
|
-// public boolean hasConfig(String name) {
|
|
|
|
|
-// return configMap.containsKey(name);
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// /**
|
|
|
|
|
-// * 获取配置数量
|
|
|
|
|
-// */
|
|
|
|
|
-// public int getConfigCount() {
|
|
|
|
|
-// return configMap.size();
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// /**
|
|
|
|
|
-// * 关闭资源
|
|
|
|
|
-// */
|
|
|
|
|
-// public void shutdown() {
|
|
|
|
|
-// readExecutor.shutdown();
|
|
|
|
|
-// try {
|
|
|
|
|
-// if (!readExecutor.awaitTermination(5, TimeUnit.SECONDS)) {
|
|
|
|
|
-// readExecutor.shutdownNow();
|
|
|
|
|
-// }
|
|
|
|
|
-// } catch (InterruptedException e) {
|
|
|
|
|
-// readExecutor.shutdownNow();
|
|
|
|
|
-// Thread.currentThread().interrupt();
|
|
|
|
|
-// }
|
|
|
|
|
-// clearCache();
|
|
|
|
|
-// logger.info("ModbusDataReader已关闭");
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// /**
|
|
|
|
|
-// * 数据读取回调接口
|
|
|
|
|
-// */
|
|
|
|
|
-// public interface DataReadCallback {
|
|
|
|
|
-// void onSuccess(Map<String, ParseResult> results);
|
|
|
|
|
-// void onError(Exception e);
|
|
|
|
|
-// }
|
|
|
|
|
-//}
|
|
|