From ad34855cf7378a5dc8aba68954a1c1f4563eb70c Mon Sep 17 00:00:00 2001 From: wangshilong Date: Mon, 25 Nov 2024 13:07:43 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=A7=A3=E6=9E=90=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E8=90=BD=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gaswellwatch/GasWellWatchApplication.java | 2 + .../modbus/data/PersistenceHandler.java | 2 +- .../modbus/data/decode/DecodeHandler.java | 6 +- .../data/decode/impl/FactorDecodeHandler.java | 51 ++++++++ .../data/decode/impl/Knpcv1DecodeHandler.java | 36 ------ .../decode/impl/LocalDateDecodeHandler.java | 36 ++++++ .../impl/LocalDateTimeDecodeHandler.java | 36 ++++++ .../decode/impl/LocalTimeDecodeHandler.java | 36 ++++++ .../decode/impl/StringTimeDecodeHandler.java | 40 ++++++ .../ModbusMessagePersistListener.java | 86 +++++++++---- .../data/impl/Knpcv1PersistenceHandler.java | 115 +++++++++--------- .../data/impl/Redis2DBPersistenceService.java | 88 +++++++++----- src/main/resources/sql/CREATE_KNPCV1.sql | 66 +++++----- src/main/resources/sql/INSERT_KNPCV1.sql | 58 ++++++++- 14 files changed, 468 insertions(+), 190 deletions(-) create mode 100644 src/main/java/com/isu/gaswellwatch/modbus/data/decode/impl/FactorDecodeHandler.java delete mode 100644 src/main/java/com/isu/gaswellwatch/modbus/data/decode/impl/Knpcv1DecodeHandler.java create mode 100644 src/main/java/com/isu/gaswellwatch/modbus/data/decode/impl/LocalDateDecodeHandler.java create mode 100644 src/main/java/com/isu/gaswellwatch/modbus/data/decode/impl/LocalDateTimeDecodeHandler.java create mode 100644 src/main/java/com/isu/gaswellwatch/modbus/data/decode/impl/LocalTimeDecodeHandler.java create mode 100644 src/main/java/com/isu/gaswellwatch/modbus/data/decode/impl/StringTimeDecodeHandler.java diff --git a/src/main/java/com/isu/gaswellwatch/GasWellWatchApplication.java b/src/main/java/com/isu/gaswellwatch/GasWellWatchApplication.java index 46c402e..b80fcc9 100644 --- a/src/main/java/com/isu/gaswellwatch/GasWellWatchApplication.java +++ b/src/main/java/com/isu/gaswellwatch/GasWellWatchApplication.java @@ -2,7 +2,9 @@ package com.isu.gaswellwatch; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; +@EnableScheduling @SpringBootApplication public class GasWellWatchApplication { diff --git a/src/main/java/com/isu/gaswellwatch/modbus/data/PersistenceHandler.java b/src/main/java/com/isu/gaswellwatch/modbus/data/PersistenceHandler.java index 3b176ea..1c5efd0 100644 --- a/src/main/java/com/isu/gaswellwatch/modbus/data/PersistenceHandler.java +++ b/src/main/java/com/isu/gaswellwatch/modbus/data/PersistenceHandler.java @@ -10,7 +10,7 @@ public interface PersistenceHandler { public static final String MODBUS_DEVICE_TYPE = "KNPCV1"; String DEVICE_DATA_CACHE = "data:device:"; - void createTable(String tableName); + void createTable(String tableName, Long deviceId); void insert(String tableName, String cacheKey); diff --git a/src/main/java/com/isu/gaswellwatch/modbus/data/decode/DecodeHandler.java b/src/main/java/com/isu/gaswellwatch/modbus/data/decode/DecodeHandler.java index 4d65ef9..070d73c 100644 --- a/src/main/java/com/isu/gaswellwatch/modbus/data/decode/DecodeHandler.java +++ b/src/main/java/com/isu/gaswellwatch/modbus/data/decode/DecodeHandler.java @@ -4,6 +4,8 @@ import com.isu.gaswellwatch.modbus.data.ModbusMessage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Map; + /** * Modbus 数据解析器 * @@ -14,6 +16,8 @@ public interface DecodeHandler { Logger logger = LoggerFactory.getLogger("com.isu.gaswellwatch.decode"); - void decode(ModbusMessage.MessagePoint point); + String decode(Map commandPointMap, String value); + + void decode(Map commandPointMap, ModbusMessage.MessagePoint point); } diff --git a/src/main/java/com/isu/gaswellwatch/modbus/data/decode/impl/FactorDecodeHandler.java b/src/main/java/com/isu/gaswellwatch/modbus/data/decode/impl/FactorDecodeHandler.java new file mode 100644 index 0000000..7400676 --- /dev/null +++ b/src/main/java/com/isu/gaswellwatch/modbus/data/decode/impl/FactorDecodeHandler.java @@ -0,0 +1,51 @@ +package com.isu.gaswellwatch.modbus.data.decode.impl; + +import cn.hutool.core.map.MapUtil; +import com.isu.gaswellwatch.modbus.data.ModbusMessage; +import com.isu.gaswellwatch.modbus.data.decode.DecodeHandler; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Component; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.Map; + +/** + * 乘除系数解析器 + *
    + *
      系数为正数时,采用除法
    + *
  • 例如:输入值:2580,系数(factor):100,精度(precision):2,输出值:2580 / 100 = 25.8
  • + *
      系数为负数时,采用乘法
    + *
  • 例如:输入值:2580,系数(factor):100,精度(precision):2,输出值:2580 * 100 = 258000
  • + *
+ * + * @author 王仕龙 + * 2024/11/24 21:23 + */ +@Slf4j +@SuppressWarnings("all") +@Component("factorDecodeHandler") +public class FactorDecodeHandler implements DecodeHandler { + + @Override + public String decode(Map commandPointMap, String value) { + return value; + } + + @Override + public void decode(Map commandPointMap, ModbusMessage.MessagePoint point) { + point.setValue(decode(commandPointMap, point.getParseValue())); + String factorStr = MapUtil.getStr(commandPointMap, "factor"); + if (StringUtils.isNotBlank(factorStr) && StringUtils.isNotBlank(point.getParseValue())) { + BigDecimal factor = new BigDecimal(factorStr); + BigDecimal value = new BigDecimal(point.getParseValue()); + int precision = MapUtil.getInt(commandPointMap, "precision", 0); + switch (factor.compareTo(BigDecimal.ZERO)) { + case -1 -> point.setValue(value.multiply(factor.abs()).toString()); + case 1 -> point.setValue(value.divide(factor, precision, RoundingMode.HALF_UP).toString()); + } + } + } + +} diff --git a/src/main/java/com/isu/gaswellwatch/modbus/data/decode/impl/Knpcv1DecodeHandler.java b/src/main/java/com/isu/gaswellwatch/modbus/data/decode/impl/Knpcv1DecodeHandler.java deleted file mode 100644 index 8c2394b..0000000 --- a/src/main/java/com/isu/gaswellwatch/modbus/data/decode/impl/Knpcv1DecodeHandler.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.isu.gaswellwatch.modbus.data.decode.impl; - -import com.isu.gaswellwatch.modbus.data.ModbusMessage; -import com.isu.gaswellwatch.modbus.data.decode.DecodeHandler; -import jakarta.annotation.Resource; -import lombok.extern.slf4j.Slf4j; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.stereotype.Component; - -/** - * KNPCV1 柱塞器解析 - * - * @author 王仕龙 - * 2024/11/23 11:23 - */ -@Slf4j -@SuppressWarnings("all") -@Component(Knpcv1DecodeHandler.MODBUS_DEVICE_TYPE + "_1") -public class Knpcv1DecodeHandler implements DecodeHandler { - - public static final String MODBUS_DEVICE_TYPE = "KNPCV1"; - private static final String COMMAND_POINT_SQL = "select * from command_points where command_id = ?"; - - @Resource - private JdbcTemplate jdbcTemplate; - - @Resource(name = "redisTemplate") - private RedisTemplate redisTemplate; - - @Override - public void decode(ModbusMessage.MessagePoint point) { - - } - -} diff --git a/src/main/java/com/isu/gaswellwatch/modbus/data/decode/impl/LocalDateDecodeHandler.java b/src/main/java/com/isu/gaswellwatch/modbus/data/decode/impl/LocalDateDecodeHandler.java new file mode 100644 index 0000000..4b78c13 --- /dev/null +++ b/src/main/java/com/isu/gaswellwatch/modbus/data/decode/impl/LocalDateDecodeHandler.java @@ -0,0 +1,36 @@ +package com.isu.gaswellwatch.modbus.data.decode.impl; + +import com.isu.gaswellwatch.modbus.data.ModbusMessage; +import com.isu.gaswellwatch.modbus.data.decode.DecodeHandler; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.Map; + +/** + * 本地日期类型解析器 + * + * @author 王仕龙 + * 2024/11/25 11:51 + */ +@Slf4j +@SuppressWarnings("all") +@Component("localDateDecodeHandler") +public class LocalDateDecodeHandler implements DecodeHandler { + public static final DateTimeFormatter IN_FORMATTER = DateTimeFormatter.ofPattern("yyyy-M-d"); + public static final DateTimeFormatter OUT_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + + + @Override + public String decode(Map commandPointMap, String value) { + return LocalDate.parse(value, IN_FORMATTER).format(OUT_FORMATTER); + } + + @Override + public void decode(Map commandPointMap, ModbusMessage.MessagePoint point) { + point.setValue(decode(commandPointMap, point.getParseValue())); + } + +} diff --git a/src/main/java/com/isu/gaswellwatch/modbus/data/decode/impl/LocalDateTimeDecodeHandler.java b/src/main/java/com/isu/gaswellwatch/modbus/data/decode/impl/LocalDateTimeDecodeHandler.java new file mode 100644 index 0000000..8c86993 --- /dev/null +++ b/src/main/java/com/isu/gaswellwatch/modbus/data/decode/impl/LocalDateTimeDecodeHandler.java @@ -0,0 +1,36 @@ +package com.isu.gaswellwatch.modbus.data.decode.impl; + +import com.isu.gaswellwatch.modbus.data.ModbusMessage; +import com.isu.gaswellwatch.modbus.data.decode.DecodeHandler; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Map; + +/** + * 本地日期时间类型解析器 + * + * @author 王仕龙 + * 2024/11/24 21:34 + */ +@Slf4j +@SuppressWarnings("all") +@Component("localDateTimeDecodeHandler") +public class LocalDateTimeDecodeHandler implements DecodeHandler { + public static final DateTimeFormatter IN_FORMATTER = DateTimeFormatter.ofPattern("yyyy-M-d H:m:s"); + public static final DateTimeFormatter OUT_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + + + @Override + public String decode(Map commandPointMap, String value) { + return LocalDateTime.parse(value, IN_FORMATTER).format(OUT_FORMATTER); + } + + @Override + public void decode(Map commandPointMap, ModbusMessage.MessagePoint point) { + point.setValue(decode(commandPointMap, point.getParseValue())); + } + +} diff --git a/src/main/java/com/isu/gaswellwatch/modbus/data/decode/impl/LocalTimeDecodeHandler.java b/src/main/java/com/isu/gaswellwatch/modbus/data/decode/impl/LocalTimeDecodeHandler.java new file mode 100644 index 0000000..37ed01a --- /dev/null +++ b/src/main/java/com/isu/gaswellwatch/modbus/data/decode/impl/LocalTimeDecodeHandler.java @@ -0,0 +1,36 @@ +package com.isu.gaswellwatch.modbus.data.decode.impl; + +import com.isu.gaswellwatch.modbus.data.ModbusMessage; +import com.isu.gaswellwatch.modbus.data.decode.DecodeHandler; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; +import java.util.Map; + +/** + * 本地时间类型解析器 + * + * @author 王仕龙 + * 2024/11/25 11:58 + */ +@Slf4j +@SuppressWarnings("all") +@Component("localTimeDecodeHandler") +public class LocalTimeDecodeHandler implements DecodeHandler { + public static final DateTimeFormatter IN_FORMATTER = DateTimeFormatter.ofPattern("H:m:s"); + public static final DateTimeFormatter OUT_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss"); + + + @Override + public String decode(Map commandPointMap, String value) { + return LocalTime.parse(value, IN_FORMATTER).format(OUT_FORMATTER); + } + + @Override + public void decode(Map commandPointMap, ModbusMessage.MessagePoint point) { + point.setValue(decode(commandPointMap, point.getParseValue())); + } + +} diff --git a/src/main/java/com/isu/gaswellwatch/modbus/data/decode/impl/StringTimeDecodeHandler.java b/src/main/java/com/isu/gaswellwatch/modbus/data/decode/impl/StringTimeDecodeHandler.java new file mode 100644 index 0000000..3458295 --- /dev/null +++ b/src/main/java/com/isu/gaswellwatch/modbus/data/decode/impl/StringTimeDecodeHandler.java @@ -0,0 +1,40 @@ +package com.isu.gaswellwatch.modbus.data.decode.impl; + +import com.isu.gaswellwatch.modbus.data.ModbusMessage; +import com.isu.gaswellwatch.modbus.data.decode.DecodeHandler; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Component; + +import java.util.Arrays; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * 时间类型解析器 + * 支持解析:999:59:59, 实际使用中请不要转为LocalTime + * + * @author 王仕龙 + * 2024/11/24 21:38 + */ +@Slf4j +@SuppressWarnings("all") +@Component("stringTimeDecodeHandler") +public class StringTimeDecodeHandler implements DecodeHandler { + + @Override + public String decode(Map commandPointMap, String value) { + if (StringUtils.isBlank(value)) { + return value; + } + return Arrays.stream(StringUtils.split(value, ":")) + .map(item -> StringUtils.leftPad(item, 2, '0')) + .collect(Collectors.joining(":")); + } + + @Override + public void decode(Map commandPointMap, ModbusMessage.MessagePoint point) { + point.setValue(decode(commandPointMap, point.getParseValue())); + } + +} diff --git a/src/main/java/com/isu/gaswellwatch/modbus/data/decode/listener/ModbusMessagePersistListener.java b/src/main/java/com/isu/gaswellwatch/modbus/data/decode/listener/ModbusMessagePersistListener.java index a821e5a..5ee9e8a 100644 --- a/src/main/java/com/isu/gaswellwatch/modbus/data/decode/listener/ModbusMessagePersistListener.java +++ b/src/main/java/com/isu/gaswellwatch/modbus/data/decode/listener/ModbusMessagePersistListener.java @@ -5,7 +5,9 @@ import cn.hutool.json.JSONUtil; import com.isu.gaswellwatch.modbus.data.ModbusMessage; import com.isu.gaswellwatch.modbus.data.PersistenceHandler; import com.isu.gaswellwatch.modbus.data.decode.DecodeHandler; -import com.serotonin.modbus4j.msg.ReadInputRegistersResponse; +import com.isu.gaswellwatch.modbus.data.decode.impl.LocalDateTimeDecodeHandler; +import com.serotonin.modbus4j.code.FunctionCode; +import com.serotonin.modbus4j.msg.*; import com.serotonin.modbus4j.serial.rtu.RtuMessageParser; import com.serotonin.modbus4j.serial.rtu.RtuMessageResponse; import com.serotonin.modbus4j.sero.util.queue.ByteQueue; @@ -22,7 +24,13 @@ import org.springframework.data.redis.core.RedisTemplate; import org.springframework.jdbc.core.JdbcTemplate; import java.time.Duration; -import java.util.*; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; /** * @author 王仕龙 @@ -39,7 +47,7 @@ public class ModbusMessagePersistListener implements BatchMessageListener { @Resource private JdbcTemplate jdbcTemplate; - @Resource(name = "redisTemplate") + @Resource(name = "stringRedisTemplate") private RedisTemplate redisTemplate; @Resource private Map decodeHandlerMap; @@ -66,35 +74,50 @@ public class ModbusMessagePersistListener implements BatchMessageListener { continue; } + messagePointMap = new HashMap<>(); collectionMessage = messageSplit[5]; commandId = NumberUtils.createLong(messageSplit[2]); commandString = Objects.toString(this.redisTemplate.opsForValue().get(COMMAND_CACHE + commandId), ""); commandMap = StringUtils.isNotEmpty(commandString) ? JSONUtil.parseObj(commandString) : null; if (ObjectUtils.isEmpty(commandMap)) { commandMap = this.jdbcTemplate.queryForMap(COMMAND_SQL, commandId); - if (ObjectUtils.isNotEmpty(commandMap)) { + if (ObjectUtils.isEmpty(commandMap)) { log.error("指令[{}]不存在,数据: {}", commandId, messageString); continue; } this.redisTemplate.opsForValue() .setIfAbsent(COMMAND_CACHE + commandId, JSONUtil.toJsonStr(commandMap), Duration.ofDays(1)); } - DecodeHandler decodeHandler = Optional.ofNullable(MapUtil.getStr(commandMap, "decode_handler_name")) - .map(this.decodeHandlerMap::get) - .orElse(null); - if (Objects.isNull(decodeHandler)) { - log.error("指令[{}]未配置解析器,数据: {}", commandId, messageString); - continue; - } - messagePointMap = new HashMap<>(); try { String address; int index = MapUtil.getInt(commandMap, "start_address"), stepSize = 0; ByteQueue byteQueue = new ByteQueue(collectionMessage); RtuMessageParser masterParser = new RtuMessageParser(true); RtuMessageResponse response = (RtuMessageResponse) masterParser.parseMessage(byteQueue); - ReadInputRegistersResponse readInputRegistersResponse = (ReadInputRegistersResponse) response.getModbusResponse(); - for (short value : readInputRegistersResponse.getShortData()) { + ModbusResponse modbusResponse = response.getModbusResponse(); + + short[] values = null; + switch (modbusResponse.getFunctionCode()) { + case FunctionCode.READ_COILS -> { + values = ((ReadCoilsResponse) modbusResponse).getShortData(); + } + case FunctionCode.READ_DISCRETE_INPUTS -> { + values = ((ReadDiscreteInputsResponse) modbusResponse).getShortData(); + } + case FunctionCode.READ_INPUT_REGISTERS -> { + values = ((ReadInputRegistersResponse) modbusResponse).getShortData(); + } + case FunctionCode.READ_HOLDING_REGISTERS -> { + values = ((ReadHoldingRegistersResponse) modbusResponse).getShortData(); + } +// case FunctionCode.READ_EXCEPTION_STATUS -> { +// values = new short[]{((ReadExceptionStatusResponse) modbusResponse).getExceptionStatus()}; +// } + default -> { + throw new RuntimeException("Funcetion code not supported: " + modbusResponse.getFunctionCode()); + } + } + for (short value : values) { stepSize = index * 4; messagePointMap.put(StringUtils.leftPad(String.valueOf(index), 4, '0'), ModbusMessage.MessagePoint.builder() @@ -131,7 +154,7 @@ public class ModbusMessagePersistListener implements BatchMessageListener { List> commandPointList = StringUtils.isNotEmpty(cachePoints) ? (List) JSONUtil.parseArray(cachePoints) : null; if (ObjectUtils.isEmpty(commandPointList)) { commandPointList = this.jdbcTemplate.queryForList(COMMAND_POINT_SQL, commandId); - if (ObjectUtils.isNotEmpty(commandPointList)) { + if (ObjectUtils.isEmpty(commandPointList)) { log.error("指令[{}]点表配置为空", commandId, cachePoints); throw new RuntimeException("指令[" + commandId + "]点表未配置"); } @@ -144,6 +167,13 @@ public class ModbusMessagePersistListener implements BatchMessageListener { ModbusMessage.MessagePoint messagePoint; String deviceDataCacheName = PersistenceHandler.DEVICE_DATA_CACHE + modbusMessage.getDeviceId(); HashOperations hashOperations = this.redisTemplate.opsForHash(); + hashOperations.put(deviceDataCacheName, "deviceId", String.valueOf(modbusMessage.getDeviceId())); + hashOperations.put(deviceDataCacheName, "receiveTime", LocalDateTime.ofInstant(Instant + .ofEpochMilli(modbusMessage.getReceiveTime()), ZoneOffset.systemDefault()) + .format(LocalDateTimeDecodeHandler.OUT_FORMATTER)); + hashOperations.put(deviceDataCacheName, "collectionTime", LocalDateTime.ofInstant(Instant + .ofEpochMilli(modbusMessage.getCollectionTime()), ZoneOffset.systemDefault()) + .format(LocalDateTimeDecodeHandler.OUT_FORMATTER)); for (Map point : commandPointList) { fieldName = MapUtil.getStr(point, "field"); stepSize = MapUtil.getInt(point, "step_size"); @@ -153,35 +183,41 @@ public class ModbusMessagePersistListener implements BatchMessageListener { if (stepSize <= 1) { messagePoint = modbusMessage.getMessagePointMap() .get(StringUtils.leftPad(String.valueOf(startAddress), 4, '0')); - decodeMessage(decodeName, messagePoint); + decodeMessage(decodeName, point, messagePoint); value = messagePoint.getValue(); } else { - value = decodeCommandPoint(modbusMessage.getMessagePointMap(), decodeName, - MapUtil.getStr(point, "format"), startAddress, stepSize); + value = decodeCommandPoint(modbusMessage.getMessagePointMap(), decodeName, point, startAddress, stepSize); } hashOperations.put(deviceDataCacheName, fieldName, value); } } - private void decodeMessage(String decodeName, ModbusMessage.MessagePoint messagePoint) { + private void decodeMessage(String decodeName, Map commandPointMap, ModbusMessage.MessagePoint messagePoint) { if (StringUtils.isNotBlank(decodeName)) { - DecodeHandler decodeHandler = this.decodeHandlerMap.get(decodeName); + DecodeHandler decodeHandler = this.decodeHandlerMap.get(decodeName + "DecodeHandler"); if (Objects.nonNull(decodeHandler)) { - decodeHandler.decode(messagePoint); + decodeHandler.decode(commandPointMap, messagePoint); } } } - private String decodeCommandPoint(Map pointMap, - String decodeName, String format, int startAddress, int stepSize) { + private String decodeCommandPoint(Map pointMap, String decodeName, + Map commandPointMap, int startAddress, int stepSize) { String[] values = new String[stepSize]; ModbusMessage.MessagePoint messagePoint; for (int i = 0; i < stepSize; i++) { messagePoint = pointMap.get(StringUtils.leftPad(String.valueOf(startAddress + i), 4, '0')); - decodeMessage(decodeName, messagePoint); values[i] = messagePoint.getValue(); } - return String.format(format, values); + String format = MapUtil.getStr(commandPointMap, "format"); + String result = StringUtils.isBlank(format) ? StringUtils.join(values) : String.format(format, values); + if (StringUtils.isNotBlank(decodeName)) { + DecodeHandler decodeHandler = this.decodeHandlerMap.get(decodeName + "DecodeHandler"); + if (Objects.nonNull(decodeHandler)) { + return decodeHandler.decode(commandPointMap, result); + } + } + return result; } } diff --git a/src/main/java/com/isu/gaswellwatch/modbus/data/impl/Knpcv1PersistenceHandler.java b/src/main/java/com/isu/gaswellwatch/modbus/data/impl/Knpcv1PersistenceHandler.java index e0c2e27..81e29a2 100644 --- a/src/main/java/com/isu/gaswellwatch/modbus/data/impl/Knpcv1PersistenceHandler.java +++ b/src/main/java/com/isu/gaswellwatch/modbus/data/impl/Knpcv1PersistenceHandler.java @@ -6,7 +6,6 @@ import com.isu.gaswellwatch.modbus.data.PersistenceHandler; import jakarta.annotation.Resource; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; -import org.springframework.dao.DataAccessException; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.PreparedStatementCallback; @@ -34,13 +33,15 @@ public class Knpcv1PersistenceHandler implements PersistenceHandler { private JdbcTemplate jdbcTemplate; @Resource private SnowflakeConfig snowflakeConfig; - @Resource(name = "redisTemplate") + @Resource(name = "stringRedisTemplate") private RedisTemplate redisTemplate; @Override - public void createTable(String tableName) { + public void createTable(String tableName, Long deviceId) { String createTableSQL = getResource("sql/CREATE_KNPCV1.sql"); - this.jdbcTemplate.execute(StringUtils.replace(createTableSQL, "$TableName$", tableName)); + createTableSQL = StringUtils.replace(createTableSQL, "$TableName$", tableName); + createTableSQL = StringUtils.replace(createTableSQL, "$DeviceId$", String.valueOf(deviceId)); + this.jdbcTemplate.execute(createTableSQL); } @Override @@ -52,62 +53,60 @@ public class Knpcv1PersistenceHandler implements PersistenceHandler { this.jdbcTemplate.execute(insertTableSQL, new PreparedStatementCallback() { @Override - public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException { + public Object doInPreparedStatement(PreparedStatement ps) throws SQLException { ps.setLong(1, Knpcv1PersistenceHandler.this.snowflakeConfig.snowflakeId()); Knpcv1PersistenceHandler.this.setValue(ps, row, 2, "deviceId", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 3, "collectionTime", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 4, "receiveTime", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 5, "status", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 6, "device_time", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 7, "runMode", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 8, "wellStatus", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 9, "plugStatus", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 10, "statusOpenTime", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 11, "statusCloseTime", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 12, "oilPressure", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 13, "casPressure", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 14, "prePressure", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 15, "pipePressure", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 16, "liquidLevel", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 17, "temperature", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 18, "humidity", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 19, "opmode", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 20, "well_ctl", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 21, "timer1", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 22, "timer2", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 23, "timer1Open", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 24, "timer1Close", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 25, "timer2Open", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 26, "timer2Close", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 27, "timingOpen", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 28, "timingClose", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 29, "timingCelay", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 30, "presource", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 31, "pressureOpen", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 32, "pressureClose", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 33, "triggerType", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 34, "stabilityTime", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 35, "maxOpenWell", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 36, "maxCloseWell", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 37, "minOpenWell", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 38, "minCloseWell", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 39, "presproTect", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 40, "presproSource", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 41, "openWellLimitMax", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 42, "openWellLimitMin", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 43, "plugInitStatus", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 44, "plugSustainTime", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 45, "plugCloseTime", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 46, "tpInitStatus", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 47, "tpOpenSource", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 48, "tpOpenTrigger", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 49, "tpOpenPressure", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 50, "tpOpenTime", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 51, "tpCloseSource", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 52, "tpCloseTrigger", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 53, "tpClosePressure", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 54, "tpCloseTime", Types.BIGINT); - Knpcv1PersistenceHandler.this.setValue(ps, row, 55, "tpStabilityTime", Types.BIGINT); + Knpcv1PersistenceHandler.this.setValue(ps, row, 3, "collectionTime", Types.TIMESTAMP); + Knpcv1PersistenceHandler.this.setValue(ps, row, 4, "receiveTime", Types.TIMESTAMP); + Knpcv1PersistenceHandler.this.setValue(ps, row, 5, "deviceTime", Types.TIMESTAMP); + Knpcv1PersistenceHandler.this.setValue(ps, row, 6, "runMode", Types.INTEGER); + Knpcv1PersistenceHandler.this.setValue(ps, row, 7, "wellStatus", Types.INTEGER); + Knpcv1PersistenceHandler.this.setValue(ps, row, 8, "plugStatus", Types.INTEGER); + Knpcv1PersistenceHandler.this.setValue(ps, row, 9, "statusOpenTime", Types.TIME); + Knpcv1PersistenceHandler.this.setValue(ps, row, 10, "statusCloseTime", Types.TIME); + Knpcv1PersistenceHandler.this.setValue(ps, row, 11, "oilPressure", Types.DECIMAL); + Knpcv1PersistenceHandler.this.setValue(ps, row, 12, "casPressure", Types.DECIMAL); + Knpcv1PersistenceHandler.this.setValue(ps, row, 13, "prePressure", Types.DECIMAL); + Knpcv1PersistenceHandler.this.setValue(ps, row, 14, "pipePressure", Types.DECIMAL); + Knpcv1PersistenceHandler.this.setValue(ps, row, 15, "liquidLevel", Types.DECIMAL); + Knpcv1PersistenceHandler.this.setValue(ps, row, 16, "temperature", Types.DECIMAL); + Knpcv1PersistenceHandler.this.setValue(ps, row, 17, "humidity", Types.DECIMAL); + Knpcv1PersistenceHandler.this.setValue(ps, row, 18, "opmode", Types.INTEGER); + Knpcv1PersistenceHandler.this.setValue(ps, row, 19, "timer1", Types.INTEGER); + Knpcv1PersistenceHandler.this.setValue(ps, row, 20, "timer2", Types.INTEGER); + Knpcv1PersistenceHandler.this.setValue(ps, row, 21, "timer1Open", Types.TIME); + Knpcv1PersistenceHandler.this.setValue(ps, row, 22, "timer1Close", Types.TIME); + Knpcv1PersistenceHandler.this.setValue(ps, row, 23, "timer2Open", Types.TIME); + Knpcv1PersistenceHandler.this.setValue(ps, row, 24, "timer2Close", Types.TIME); + Knpcv1PersistenceHandler.this.setValue(ps, row, 25, "timingOpen", Types.VARCHAR); + Knpcv1PersistenceHandler.this.setValue(ps, row, 26, "timingClose", Types.VARCHAR); + Knpcv1PersistenceHandler.this.setValue(ps, row, 27, "timingCelay", Types.VARCHAR); + Knpcv1PersistenceHandler.this.setValue(ps, row, 28, "presource", Types.INTEGER); + Knpcv1PersistenceHandler.this.setValue(ps, row, 29, "pressureOpen", Types.DECIMAL); + Knpcv1PersistenceHandler.this.setValue(ps, row, 30, "pressureClose", Types.DECIMAL); + Knpcv1PersistenceHandler.this.setValue(ps, row, 31, "triggerType", Types.INTEGER); + Knpcv1PersistenceHandler.this.setValue(ps, row, 32, "stabilityTime", Types.INTEGER); + Knpcv1PersistenceHandler.this.setValue(ps, row, 33, "maxOpenWell", Types.VARCHAR); + Knpcv1PersistenceHandler.this.setValue(ps, row, 34, "maxCloseWell", Types.VARCHAR); + Knpcv1PersistenceHandler.this.setValue(ps, row, 35, "minOpenWell", Types.VARCHAR); + Knpcv1PersistenceHandler.this.setValue(ps, row, 36, "minCloseWell", Types.VARCHAR); + Knpcv1PersistenceHandler.this.setValue(ps, row, 37, "presproTect", Types.INTEGER); + Knpcv1PersistenceHandler.this.setValue(ps, row, 38, "presproSource", Types.INTEGER); + Knpcv1PersistenceHandler.this.setValue(ps, row, 39, "openWellLimitMax", Types.DECIMAL); + Knpcv1PersistenceHandler.this.setValue(ps, row, 40, "openWellLimitMin", Types.DECIMAL); + Knpcv1PersistenceHandler.this.setValue(ps, row, 41, "plugInitStatus", Types.INTEGER); + Knpcv1PersistenceHandler.this.setValue(ps, row, 42, "plugSustainTime", Types.VARCHAR); + Knpcv1PersistenceHandler.this.setValue(ps, row, 43, "plugCloseTime", Types.VARCHAR); + Knpcv1PersistenceHandler.this.setValue(ps, row, 44, "tpInitStatus", Types.INTEGER); + Knpcv1PersistenceHandler.this.setValue(ps, row, 45, "tpOpenSource", Types.INTEGER); + Knpcv1PersistenceHandler.this.setValue(ps, row, 46, "tpOpenTrigger", Types.INTEGER); + Knpcv1PersistenceHandler.this.setValue(ps, row, 47, "tpOpenPressure", Types.INTEGER); + Knpcv1PersistenceHandler.this.setValue(ps, row, 48, "tpOpenTime", Types.VARCHAR); + Knpcv1PersistenceHandler.this.setValue(ps, row, 49, "tpCloseSource", Types.INTEGER); + Knpcv1PersistenceHandler.this.setValue(ps, row, 50, "tpCloseTrigger", Types.INTEGER); + Knpcv1PersistenceHandler.this.setValue(ps, row, 51, "tpClosePressure", Types.INTEGER); + Knpcv1PersistenceHandler.this.setValue(ps, row, 52, "tpCloseTime", Types.VARCHAR); + Knpcv1PersistenceHandler.this.setValue(ps, row, 53, "tpStabilityTime", Types.INTEGER); return ps.executeUpdate(); } }); diff --git a/src/main/java/com/isu/gaswellwatch/modbus/data/impl/Redis2DBPersistenceService.java b/src/main/java/com/isu/gaswellwatch/modbus/data/impl/Redis2DBPersistenceService.java index fae477d..d999fc0 100644 --- a/src/main/java/com/isu/gaswellwatch/modbus/data/impl/Redis2DBPersistenceService.java +++ b/src/main/java/com/isu/gaswellwatch/modbus/data/impl/Redis2DBPersistenceService.java @@ -1,16 +1,19 @@ package com.isu.gaswellwatch.modbus.data.impl; import cn.hutool.core.map.MapUtil; +import com.isu.gaswellwatch.entity.Response; import com.isu.gaswellwatch.modbus.data.PersistenceHandler; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; -import org.springframework.core.annotation.Order; +import org.springframework.core.ParameterizedTypeReference; import org.springframework.data.redis.core.Cursor; import org.springframework.data.redis.core.HashOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ScanOptions; +import org.springframework.http.RequestEntity; +import org.springframework.http.ResponseEntity; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @@ -19,6 +22,8 @@ import org.springframework.web.client.RestTemplate; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; /** * @author 王仕龙 @@ -29,59 +34,80 @@ import java.util.Map; @SuppressWarnings("all") public class Redis2DBPersistenceService { - private static final String EXISTS_TABLE_SQL = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='gaswellwatch' AND TABLE_NAME=?"; + private static final String EXISTS_TABLE_SQL = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA" + + " IN ('gaswellwatch', 'gas_well_watch') AND TABLE_NAME='$TableName$'"; @Resource private JdbcTemplate jdbcTemplate; - @Resource(name = "redisTemplate") + @Resource(name = "stringRedisTemplate") private RedisTemplate redisTemplate; @Resource private Map persistenceHandlerMap; private RestTemplate restTemplate = new RestTemplate(); - @Order(100) - @Scheduled(cron = "5/30 * * * * ? ") + @Scheduled(cron = "20/30 * * * * ? ") public void write() { - Map idGatewayMappingMap = null; - try { - String onlineGateways = this.restTemplate.getForObject("http://127.0.0.1:9999/modbus-tcp/online", String.class); - if (StringUtils.isBlank(onlineGateways)) { - return; - } - List> idGatewayMappingList = this.jdbcTemplate - .queryForList("select id, gateway_sn from devices where gateway_sn in (" + onlineGateways + ")"); - if (ObjectUtils.isEmpty(idGatewayMappingList)) { - return; - } - idGatewayMappingMap = new HashMap(); - for (Map map : idGatewayMappingList) { - idGatewayMappingMap.put(MapUtil.getLong(map, "id"), MapUtil.getStr(map, "gateway_sn")); - } - } catch (Exception e) { - log.error("Get online devices list fail", e); - } - + Map idGatewayMappingMap = getOnlineGateway(); HashOperations operations = this.redisTemplate.opsForHash(); try (Cursor cursor = this.redisTemplate.scan(ScanOptions.scanOptions() .match(PersistenceHandler.DEVICE_DATA_CACHE + "*").build())) { Long deviceId; String cacheKey, tableName; - Map deviceMap, temp; + Map deviceMap; PersistenceHandler persistenceHandler; + List> existsTableList; while (cursor.hasNext()) { deviceMap = operations.entries(cacheKey = cursor.next()); + if (ObjectUtils.isEmpty(deviceMap)) { + continue; + } deviceId = MapUtil.getLong(deviceMap, "deviceId"); - operations.put(PersistenceHandler.DEVICE_DATA_CACHE + deviceId, - "online", idGatewayMappingMap.containsKey(deviceId)); - + if (Objects.isNull(deviceId)) { + continue; + } + if (Objects.nonNull(idGatewayMappingMap)) { + operations.put(PersistenceHandler.DEVICE_DATA_CACHE + deviceId, + "online", String.valueOf(idGatewayMappingMap.containsKey(deviceId))); + } persistenceHandler = persistenceHandlerMap.get("KNPCV1"); tableName = "t_device_data_" + deviceId; - temp = this.jdbcTemplate.queryForMap(EXISTS_TABLE_SQL, tableName); //$TableName$ - if (StringUtils.isBlank(MapUtil.getStr(temp, "TABLE_NAME"))) { - persistenceHandler.createTable(tableName); + existsTableList = this.jdbcTemplate.queryForList(StringUtils.replace(EXISTS_TABLE_SQL, "$TableName$", tableName)); + if (ObjectUtils.isEmpty(existsTableList) + || ObjectUtils.isEmpty(existsTableList.get(0)) + || StringUtils.isBlank(MapUtil.getStr(existsTableList.get(0), "TABLE_NAME"))) { + persistenceHandler.createTable(tableName, deviceId); } persistenceHandler.insert(tableName, cacheKey); } } } + private Map getOnlineGateway() { + try { + RequestEntity request = RequestEntity.get("http://127.0.0.1:9999/modbus-tcp/online").build(); + ResponseEntity>> response = this.restTemplate.exchange(request, + new ParameterizedTypeReference>>() { + }); + if (Objects.isNull(response) + || Objects.isNull(response.getBody()) + || ObjectUtils.isEmpty(response.getBody().getData())) { + return null; + } + List> idGatewayMappingList = this.jdbcTemplate + .queryForList("select id, gateway_sn from devices where gateway_sn in (" + + response.getBody().getData().stream().map(value -> "'" + value + "'") + .collect(Collectors.joining(",")) + ")"); + if (ObjectUtils.isEmpty(idGatewayMappingList)) { + return null; + } + Map idGatewayMappingMap = new HashMap(); + for (Map map : idGatewayMappingList) { + idGatewayMappingMap.put(MapUtil.getLong(map, "id"), MapUtil.getStr(map, "gateway_sn")); + } + return idGatewayMappingMap; + } catch (Exception e) { + log.error("Get online devices list fail", e); + return null; + } + } + } diff --git a/src/main/resources/sql/CREATE_KNPCV1.sql b/src/main/resources/sql/CREATE_KNPCV1.sql index 69df33d..3a4534b 100644 --- a/src/main/resources/sql/CREATE_KNPCV1.sql +++ b/src/main/resources/sql/CREATE_KNPCV1.sql @@ -5,11 +5,10 @@ CREATE TABLE `$TableName$` `created_time` datetime NOT NULL COMMENT '数据落库时间', `collection_time` datetime NOT NULL COMMENT '采集指令下发时间', `receive_time` datetime NOT NULL COMMENT '接收到数据时间', - `status` tinyint NULL DEFAULT NULL COMMENT '开关井。寄存器地址:0:0001,允许值:0:关井 ,1:开井', `device_time` datetime NULL DEFAULT NULL COMMENT '设备时间。\r\n寄存器地址:3:0000,3:0001,3:0002,3:0003,3:0004,3:0005\r\n', - `run_mode` tinyint NULL DEFAULT NULL COMMENT '运行模式:\r\n0:手动模式 \r\n1:定时器模式 \r\n2:计时器模式 \r\n3:压力模式\r\n4:柱塞模式\r\n5:时压模式', - `gas_status` tinyint NULL DEFAULT NULL COMMENT '气井状态:\r\n0:关闭 \r\n1:打开', - `plug_status` tinyint NULL DEFAULT NULL COMMENT '柱塞状态:\r\n0:离开\r\n1:上升中\r\n2:到达', + `run_mode` smallint NULL DEFAULT NULL COMMENT '运行模式:\r\n0:手动模式 \r\n1:定时器模式 \r\n2:计时器模式 \r\n3:压力模式\r\n4:柱塞模式\r\n5:时压模式', + `gas_status` smallint NULL DEFAULT NULL COMMENT '气井状态:\r\n0:关闭 \r\n1:打开', + `plug_status` smallint NULL DEFAULT NULL COMMENT '柱塞状态:\r\n0:离开\r\n1:上升中\r\n2:到达', `status_start_time` time NULL DEFAULT NULL COMMENT '当前状态开始时间', `status_end_time` time NULL DEFAULT NULL COMMENT '当前状态结束时间', `oil_pressure` decimal(10, 2) NULL DEFAULT NULL COMMENT '油压:单位Mpa', @@ -19,43 +18,42 @@ CREATE TABLE `$TableName$` `liquid_level` decimal(10, 2) NULL DEFAULT NULL COMMENT '流量:单位mm', `temperature` decimal(10, 2) NULL DEFAULT NULL COMMENT '温度:单位℃,*100,最高位表示正负,0正1负,即零下的温度加32768', `humidity` decimal(10, 2) NULL DEFAULT NULL COMMENT '湿度:单位%', - `op_mode` tinyint NULL DEFAULT NULL COMMENT '运行模式:\r\n0: 手动模式 hand_mode\r\n1:定时器模式 timer_mode\r\n2:计时器模式 t2mode\r\n3:压力模式 pressure_mode\r\n4:柱塞模式 piston_mode\r\n5:时压模式 tp_mode', - `well_ctl` tinyint NULL DEFAULT NULL COMMENT '开关井,保持现有的模式,同显示屏按钮:\r\n0:关井 close_well\r\n1:开井 open_well', - `timer_able1` tinyint NULL DEFAULT NULL COMMENT '定时模式定时器1使能:\r\n0:禁止 disable\r\n1:使能 enable', - `timer_able2` tinyint NULL DEFAULT NULL COMMENT '定时模式定时器2使能:\r\n0:禁止 disable\r\n1:使能 ', + `op_mode` smallint NULL DEFAULT NULL COMMENT '运行模式:\r\n0: 手动模式 hand_mode\r\n1:定时器模式 timer_mode\r\n2:计时器模式 t2mode\r\n3:压力模式 pressure_mode\r\n4:柱塞模式 piston_mode\r\n5:时压模式 tp_mode', + `timer_able1` smallint NULL DEFAULT NULL COMMENT '定时模式定时器1使能:\r\n0:禁止 disable\r\n1:使能 enable', + `timer_able2` smallint NULL DEFAULT NULL COMMENT '定时模式定时器2使能:\r\n0:禁止 disable\r\n1:使能 ', `timer_open1` time NULL DEFAULT NULL COMMENT '时0~23:分0~59:秒0~59', `timer_close1` time NULL DEFAULT NULL COMMENT '时0~23:分0~59:秒0~59', `timer_open2` time NULL DEFAULT NULL COMMENT '时0~23:分0~59:秒0~59', `timer_close2` time NULL DEFAULT NULL COMMENT '时0~23:分0~59:秒0~59', - `timing_open` time NULL DEFAULT NULL COMMENT '时0~23:分0~59:秒0~59', - `timing_close` time NULL DEFAULT NULL COMMENT '时0~23:分0~59:秒0~59', - `timing_delay` time NULL DEFAULT NULL COMMENT '时0~23:分0~59:秒0~59', - `presource` tinyint NULL DEFAULT NULL COMMENT '触发压力源:\r\n0:油压 oil_pressure\r\n1:套压 cas_pressure\r\n2:输压 pre_pressure\r\n3:差压 diff_pressure', + `timing_open` varchar(10) NULL DEFAULT NULL COMMENT '时0~23:分0~59:秒0~59', + `timing_close` varchar(10) NULL DEFAULT NULL COMMENT '时0~23:分0~59:秒0~59', + `timing_delay` varchar(10) NULL DEFAULT NULL COMMENT '时0~23:分0~59:秒0~59', + `presource` smallint NULL DEFAULT NULL COMMENT '触发压力源:\r\n0:油压 oil_pressure\r\n1:套压 cas_pressure\r\n2:输压 pre_pressure\r\n3:差压 diff_pressure', `pressure_open` decimal(10, 2) NULL DEFAULT NULL COMMENT '开井压力:单位Mpa,*100', `pressure_close` decimal(10, 2) NULL DEFAULT NULL COMMENT '关井压力: 单位Mpa,*100', - `trigger_type` tinyint NULL DEFAULT NULL COMMENT '压力触发类型:\n0:大于开井压力开井、小于关井压力关井 gre_less\n1:大于开井压力开井、大于关井压力关井 gre_gre\n2:小于开井压力开井、小于关井压力关井 less_less\n3:小于开井压力开井、大于关井压力关井 less_gre', - `stability_time` smallint NULL DEFAULT NULL COMMENT '传感器压力稳定时间:秒', - `max_open_well` time NULL DEFAULT NULL COMMENT '时0~999:分0~59:秒0~59', - `max_close_well` time NULL DEFAULT NULL COMMENT '时0~999:分0~59:秒0~59', - `min_open_well` time NULL DEFAULT NULL COMMENT '时0~999:分0~59:秒0~59', - `min_close_well` time NULL DEFAULT NULL COMMENT '时0~999:分0~59:秒0~59', - `prespro_tect` tinyint NULL DEFAULT NULL COMMENT '开井压力保护使能:\r\n0:禁止 disable\r\n1:使能 enable', - `prespro_source` tinyint NULL DEFAULT NULL COMMENT '开井压力保护源:\r\n0:油压 oil_pressure\r\n1:套压 cas_pressure\r\n2:输压 pre_pressure\r\n3:差压 diff_pressure', + `trigger_type` smallint NULL DEFAULT NULL COMMENT '压力触发类型:\n0:大于开井压力开井、小于关井压力关井 gre_less\n1:大于开井压力开井、大于关井压力关井 gre_gre\n2:小于开井压力开井、小于关井压力关井 less_less\n3:小于开井压力开井、大于关井压力关井 less_gre', + `stability_time` int NULL DEFAULT NULL COMMENT '传感器压力稳定时间:秒', + `max_open_well` varchar(10) NULL DEFAULT NULL COMMENT '时0~999:分0~59:秒0~59', + `max_close_well` varchar(10) NULL DEFAULT NULL COMMENT '时0~999:分0~59:秒0~59', + `min_open_well` varchar(10) NULL DEFAULT NULL COMMENT '时0~999:分0~59:秒0~59', + `min_close_well` varchar(10) NULL DEFAULT NULL COMMENT '时0~999:分0~59:秒0~59', + `prespro_tect` smallint NULL DEFAULT NULL COMMENT '开井压力保护使能:\r\n0:禁止 disable\r\n1:使能 enable', + `prespro_source` smallint NULL DEFAULT NULL COMMENT '开井压力保护源:\r\n0:油压 oil_pressure\r\n1:套压 cas_pressure\r\n2:输压 pre_pressure\r\n3:差压 diff_pressure', `open_well_limit_max` decimal(10, 2) NULL DEFAULT NULL COMMENT '开井压力限制上限:单位MPa *100', `open_well_limit_min` decimal(10, 2) NULL DEFAULT NULL COMMENT '开井压力限制下限:单位MPa *100', - `plug_init_status` tinyint NULL DEFAULT NULL COMMENT '柱塞模式初始气井状态:\r\n0:关井 close_well\r\n1:开井 open_well', - `plug_sustain_time` time NULL DEFAULT NULL COMMENT '时0~999:分0~59:秒0~59', - `plug_close_time` time NULL DEFAULT NULL COMMENT '时0~999:分0~59:秒0~59', - `tp_init_status` tinyint NULL DEFAULT NULL COMMENT '时压模式初始气井状态:\r\n0:关井 close_well\r\n1:开井 open_well', - `tp_open_source` tinyint NULL DEFAULT NULL COMMENT '时压模式开井源:\r\n0:油压 oil_pressure\r\n1:套压 cas_pressure\r\n2:输压 pre_pressure\r\n3:差压 diff_pressure\r\n4:时间 time_pressure', - `tp_open_trigger` tinyint NULL DEFAULT NULL COMMENT '时压模式开井压力触发模式:\r\n0:大于压力触发\r\n1:小于压力触发', - `tp_open_pressure` tinyint NULL DEFAULT NULL COMMENT '时压模式开井压力', - `tp_open_time` time NULL DEFAULT NULL COMMENT '时0~999:分0~59:秒0~59', - `tp_close_source` tinyint NULL DEFAULT NULL COMMENT '时压模式关井源:\r\n0:油压 oil_pressure\r\n1:套压 cas_pressure\r\n2:输压 pre_pressure\r\n3:差压 diff_pressure\r\n4:时间 time_pressure', - `tp_close_trigger` tinyint NULL DEFAULT NULL COMMENT '时压模式关井压力触发模式:\r\n0:大于压力触发 max_pressure\r\n1:小于压力触发 min_pressure', + `plug_init_status` smallint NULL DEFAULT NULL COMMENT '柱塞模式初始气井状态:\r\n0:关井 close_well\r\n1:开井 open_well', + `plug_sustain_time` varchar(10) NULL DEFAULT NULL COMMENT '时0~999:分0~59:秒0~59', + `plug_close_time` varchar(10) NULL DEFAULT NULL COMMENT '时0~999:分0~59:秒0~59', + `tp_init_status` smallint NULL DEFAULT NULL COMMENT '时压模式初始气井状态:\r\n0:关井 close_well\r\n1:开井 open_well', + `tp_open_source` smallint NULL DEFAULT NULL COMMENT '时压模式开井源:\r\n0:油压 oil_pressure\r\n1:套压 cas_pressure\r\n2:输压 pre_pressure\r\n3:差压 diff_pressure\r\n4:时间 time_pressure', + `tp_open_trigger` smallint NULL DEFAULT NULL COMMENT '时压模式开井压力触发模式:\r\n0:大于压力触发\r\n1:小于压力触发', + `tp_open_pressure` smallint NULL DEFAULT NULL COMMENT '时压模式开井压力', + `tp_open_time` varchar(10) NULL DEFAULT NULL COMMENT '时0~999:分0~59:秒0~59', + `tp_close_source` smallint NULL DEFAULT NULL COMMENT '时压模式关井源:\r\n0:油压 oil_pressure\r\n1:套压 cas_pressure\r\n2:输压 pre_pressure\r\n3:差压 diff_pressure\r\n4:时间 time_pressure', + `tp_close_trigger` smallint NULL DEFAULT NULL COMMENT '时压模式关井压力触发模式:\r\n0:大于压力触发 max_pressure\r\n1:小于压力触发 min_pressure', `tp_close_pressure` smallint NULL DEFAULT NULL COMMENT '时压模式关井压力', - `tp_close_time` time NULL DEFAULT NULL COMMENT '时0~999:分0~59:秒0~59', - `tp_stability_time` tinyint NULL DEFAULT NULL COMMENT '时压模式压力稳定时长:秒', + `tp_close_time` varchar(10) NULL DEFAULT NULL COMMENT '时0~999:分0~59:秒0~59', + `tp_stability_time` smallint NULL DEFAULT NULL COMMENT '时压模式压力稳定时长:秒', PRIMARY KEY (`id`) USING BTREE, - UNIQUE INDEX `udx_device_create_time` (`device_id` ASC, `created_time` ASC) USING BTREE COMMENT '设备采集数据唯一键' -) ENGINE = InnoDB COMMENT = '设备ID:1的采集数据'; \ No newline at end of file + UNIQUE INDEX `udx_device_create_time` (`device_id` ASC, `collection_time` ASC) USING BTREE COMMENT '设备采集数据唯一键' +) ENGINE = InnoDB COMMENT = '设备ID:$DeviceId$的采集数据' \ No newline at end of file diff --git a/src/main/resources/sql/INSERT_KNPCV1.sql b/src/main/resources/sql/INSERT_KNPCV1.sql index 459e9be..6cf39e7 100644 --- a/src/main/resources/sql/INSERT_KNPCV1.sql +++ b/src/main/resources/sql/INSERT_KNPCV1.sql @@ -1,7 +1,7 @@ -INSERT INTO `$TableName$` (`id`, `device_id`, `created_time`, `collection_time`, `receive_time`, `status`, +INSERT INTO `$TableName$` (`id`, `device_id`, `created_time`, `collection_time`, `receive_time`, `device_time`, `run_mode`, `gas_status`, `plug_status`, `status_start_time`, `status_end_time`, `oil_pressure`, `cas_pressure`, `pre_pressure`, `pipe_pressure`, - `liquid_level`, `temperature`, `humidity`, `op_mode`, `well_ctl`, `timer_able1`, + `liquid_level`, `temperature`, `humidity`, `op_mode`, `timer_able1`, `timer_able2`, `timer_open1`, `timer_close1`, `timer_open2`, `timer_close2`, `timing_open`, `timing_close`, `timing_delay`, `presource`, `pressure_open`, `pressure_close`, `trigger_type`, `stability_time`, `max_open_well`, `max_close_well`, @@ -10,5 +10,55 @@ INSERT INTO `$TableName$` (`id`, `device_id`, `created_time`, `collection_time`, `plug_close_time`, `tp_init_status`, `tp_open_source`, `tp_open_trigger`, `tp_open_pressure`, `tp_open_time`, `tp_close_source`, `tp_close_trigger`, `tp_close_pressure`, `tp_close_time`, `tp_stability_time`) -VALUES (?, ?, NOW(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, - ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?); +VALUES (?, ?, NOW(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, + ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) +ON DUPLICATE KEY UPDATE receive_time=VALUES(receive_time), + device_time=VALUES(device_time), + run_mode=VALUES(run_mode), + gas_status=VALUES(gas_status), + plug_status=VALUES(plug_status), + status_start_time=VALUES(status_start_time), + status_end_time=VALUES(status_end_time), + oil_pressure=VALUES(oil_pressure), + cas_pressure=VALUES(cas_pressure), + pre_pressure=VALUES(pre_pressure), + pipe_pressure=VALUES(pipe_pressure), + liquid_level=VALUES(liquid_level), + temperature=VALUES(temperature), + humidity=VALUES(humidity), + op_mode=VALUES(op_mode), + timer_able1=VALUES(timer_able1), + timer_able2=VALUES(timer_able2), + timer_open1=VALUES(timer_open1), + timer_close1=VALUES(timer_close1), + timer_open2=VALUES(timer_open2), + timer_close2=VALUES(timer_close2), + timing_open=VALUES(timing_open), + timing_close=VALUES(timing_close), + timing_delay=VALUES(timing_delay), + presource=VALUES(presource), + pressure_open=VALUES(pressure_open), + pressure_close=VALUES(pressure_close), + trigger_type=VALUES(trigger_type), + stability_time=VALUES(stability_time), + max_open_well=VALUES(max_open_well), + max_close_well=VALUES(max_close_well), + min_open_well=VALUES(min_open_well), + min_close_well=VALUES(min_close_well), + prespro_tect=VALUES(prespro_tect), + prespro_source=VALUES(prespro_source), + open_well_limit_max=VALUES(open_well_limit_max), + open_well_limit_min=VALUES(open_well_limit_min), + plug_init_status=VALUES(plug_init_status), + plug_sustain_time=VALUES(plug_sustain_time), + plug_close_time=VALUES(plug_close_time), + tp_init_status=VALUES(tp_init_status), + tp_open_source=VALUES(tp_open_source), + tp_open_trigger=VALUES(tp_open_trigger), + tp_open_pressure=VALUES(tp_open_pressure), + tp_open_time=VALUES(tp_open_time), + tp_close_source=VALUES(tp_close_source), + tp_close_trigger=VALUES(tp_close_trigger), + tp_close_pressure=VALUES(tp_close_pressure), + tp_close_time=VALUES(tp_close_time), + tp_stability_time=VALUES(tp_stability_time) \ No newline at end of file From 240f0dfd4c4f97b2cd75fcfca36526b6f992c8d7 Mon Sep 17 00:00:00 2001 From: wangshilong Date: Mon, 25 Nov 2024 14:49:16 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E8=A7=A3=E6=9E=90=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E8=90=BD=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pom.xml b/pom.xml index d8ccf78..efa8dd6 100644 --- a/pom.xml +++ b/pom.xml @@ -166,6 +166,11 @@ sa-token-spring-boot3-starter 1.38.0 + + org.bouncycastle + bcprov-jdk15on + 1.70 + com.infiniteautomation modbus4j