调整采集能力,支持按设备品牌采集数据

This commit is contained in:
wangshilong 2024-11-26 13:39:35 +08:00
parent 90ec96f7be
commit 803f3248f3
3 changed files with 36 additions and 49 deletions

View File

@ -1,40 +0,0 @@
package com.isu.gaswellwatch.modbus.data;
import lombok.Getter;
import java.util.HashMap;
import java.util.Map;
@Getter
public enum ModbusDeviceTypeEnum {
KNPCV1("4", PersistenceHandler.KNPCV1_MODBUS_TYPE),
ETC("5", PersistenceHandler.ETC_MODBUS_TYPE),
SCSS("6", PersistenceHandler.SCSS_MODBUS_TYPE);
private final String id;
private final String code;
ModbusDeviceTypeEnum(String id, String code) {
this.id = id;
this.code = code;
}
@Override
public String toString() {
return getCode();
}
public static String getCodeById(String id) {
return enumMap.get(id);
}
private static final Map<String, String> enumMap = new HashMap<String, String>();
static {
for (ModbusDeviceTypeEnum type : ModbusDeviceTypeEnum.values()) {
enumMap.put(type.id, type.code);
}
}
}

View File

@ -37,8 +37,10 @@ import java.util.stream.Collectors;
public class Redis2DBPersistenceService {
public static final String DEFAULT_DATA_TABLE = "t_device_data_";
private static final String DEVICE_INFO_SQL = "SELECT d.*, dp.code as modbus_device_product_code from device d " + "join dictionary dp on dp.id = d.product where id = ";
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$'";
private static final String DEVICE_INFO_SQL = "SELECT d.*, dp.code as modbus_device_product_code from device d "
+ "join dictionary dp on dp.id = d.product where id = ";
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 = "stringRedisTemplate")
@ -53,7 +55,8 @@ public class Redis2DBPersistenceService {
public void write() {
Map<Long, String> idGatewayMappingMap = getOnlineGateway();
HashOperations operations = this.redisTemplate.opsForHash();
try (Cursor<String> cursor = this.redisTemplate.scan(ScanOptions.scanOptions().match(PersistenceHandler.DEVICE_DATA_CACHE + "*").build())) {
try (Cursor<String> cursor = this.redisTemplate.scan(ScanOptions.scanOptions()
.match(PersistenceHandler.DEVICE_DATA_CACHE + "*").build())) {
Long deviceId;
String cacheKey, tableName;
Map<String, Object> deviceMap;
@ -69,9 +72,11 @@ public class Redis2DBPersistenceService {
continue;
}
if (Objects.nonNull(idGatewayMappingMap)) {
operations.put(PersistenceHandler.DEVICE_DATA_CACHE + deviceId, "online", String.valueOf(idGatewayMappingMap.containsKey(deviceId)));
operations.put(PersistenceHandler.DEVICE_DATA_CACHE + deviceId,
"online", String.valueOf(idGatewayMappingMap.containsKey(deviceId)));
}
String modbusDeviceProductCode = (String) operations.get(PersistenceHandler.DEVICE_INFO_CACHE + deviceId, "modbus_device_product_code");
String modbusDeviceProductCode = (String) operations.get(
PersistenceHandler.DEVICE_INFO_CACHE + deviceId, "modbus_device_product_code");
if (StringUtils.isEmpty(modbusDeviceProductCode)) {
Map<String, Object> deviceInfo = this.jdbcTemplate.queryForMap(DEVICE_INFO_SQL + deviceId);
if (ObjectUtils.isNotEmpty(deviceInfo)) {
@ -84,7 +89,9 @@ public class Redis2DBPersistenceService {
persistenceHandler = persistenceHandlerMap.get(modbusDeviceProductCode);
tableName = DEFAULT_DATA_TABLE + deviceId;
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"))) {
if (ObjectUtils.isEmpty(existsTableList)
|| ObjectUtils.isEmpty(existsTableList.get(0))
|| StringUtils.isBlank(MapUtil.getStr(existsTableList.get(0), "TABLE_NAME"))) {
persistenceHandler.createTable(tableName, deviceId);
}
Map<String, Object> rowData = persistenceHandler.insert(tableName, cacheKey);
@ -105,12 +112,16 @@ public class Redis2DBPersistenceService {
private Map<Long, String> getOnlineGateway() {
try {
RequestEntity request = RequestEntity.get("http://127.0.0.1:9999/modbus-tcp/online").build();
ResponseEntity<Response<List<String>>> response = this.restTemplate.exchange(request, new ParameterizedTypeReference<Response<List<String>>>() {
ResponseEntity<Response<List<String>>> response = this.restTemplate.exchange(request,
new ParameterizedTypeReference<Response<List<String>>>() {
});
if (Objects.isNull(response) || Objects.isNull(response.getBody()) || ObjectUtils.isEmpty(response.getBody().getData())) {
return null;
}
List<Map<String, Object>> idGatewayMappingList = this.jdbcTemplate.queryForList("select id, gateway_sn" + " from device where gateway_sn in (" + response.getBody().getData().stream().map(value -> "'" + value + "'").collect(Collectors.joining(",")) + ")");
List<Map<String, Object>> idGatewayMappingList = this.jdbcTemplate.queryForList("select id, gateway_sn"
+ " from device where gateway_sn in ("
+ response.getBody().getData().stream().map(value -> "'"
+ value + "'").collect(Collectors.joining(",")) + ")");
if (ObjectUtils.isEmpty(idGatewayMappingList)) {
return null;
}

View File

@ -0,0 +1,16 @@
ALTER TABLE `commands`
MODIFY COLUMN `ref_type` varchar(20) NOT NULL COMMENT '引用类型DEVICE:设备DEVICE_PRODUCT_CODE:设备类型' AFTER `id`;
ALTER TABLE `command_points`
DROP COLUMN `device_id`;
update commands
set ref_type = 'DEVICE_PRODUCT_CODE',
ref_id = 4
where ref_type = 'DEVICE'
and ref_id = 1;
update commands
set ref_type = 'DEVICE_PRODUCT_CODE',
ref_id = 6
where ref_type = 'DEVICE'
and ref_id = 2;