BUG修复
This commit is contained in:
parent
fa231d9d44
commit
3f6f29f4da
|
@ -12,10 +12,8 @@ import org.apache.commons.lang3.ObjectUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
import org.springframework.core.ParameterizedTypeReference;
|
import org.springframework.core.ParameterizedTypeReference;
|
||||||
import org.springframework.data.redis.core.Cursor;
|
import org.springframework.dao.DataAccessException;
|
||||||
import org.springframework.data.redis.core.HashOperations;
|
import org.springframework.data.redis.core.*;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
|
||||||
import org.springframework.data.redis.core.ScanOptions;
|
|
||||||
import org.springframework.http.RequestEntity;
|
import org.springframework.http.RequestEntity;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
@ -23,7 +21,10 @@ import org.springframework.scheduling.annotation.Scheduled;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.concurrent.ArrayBlockingQueue;
|
import java.util.concurrent.ArrayBlockingQueue;
|
||||||
import java.util.concurrent.ThreadFactory;
|
import java.util.concurrent.ThreadFactory;
|
||||||
import java.util.concurrent.ThreadPoolExecutor;
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
|
@ -71,7 +72,7 @@ public class Redis2DBPersistenceService {
|
||||||
|
|
||||||
@Scheduled(cron = "20/30 * * * * ? ")
|
@Scheduled(cron = "20/30 * * * * ? ")
|
||||||
public void write() {
|
public void write() {
|
||||||
Map<Long, String> idGatewayMappingMap = writeOnlineGateway();
|
writeOnlineGateway();
|
||||||
HashOperations operations = this.redisTemplate.opsForHash();
|
HashOperations operations = this.redisTemplate.opsForHash();
|
||||||
try (Cursor<String> cursor = this.redisTemplate.scan(ScanOptions.scanOptions()
|
try (Cursor<String> cursor = this.redisTemplate.scan(ScanOptions.scanOptions()
|
||||||
.match(PersistenceHandler.DEVICE_DATA_CACHE + "*").build())) {
|
.match(PersistenceHandler.DEVICE_DATA_CACHE + "*").build())) {
|
||||||
|
@ -89,7 +90,7 @@ public class Redis2DBPersistenceService {
|
||||||
if (Objects.isNull(deviceId)) {
|
if (Objects.isNull(deviceId)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
this.persistenceThreadPool.execute(new Worker(deviceId, cacheKey, operations, deviceMap, idGatewayMappingMap));
|
this.persistenceThreadPool.execute(new Worker(deviceId, cacheKey, operations, deviceMap));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -97,16 +98,12 @@ public class Redis2DBPersistenceService {
|
||||||
private void persistenceDeviceData(Long deviceId,
|
private void persistenceDeviceData(Long deviceId,
|
||||||
String cacheKey,
|
String cacheKey,
|
||||||
HashOperations operations,
|
HashOperations operations,
|
||||||
Map<String, Object> deviceMap,
|
Map<String, Object> deviceMap) {
|
||||||
Map<Long, String> idGatewayMappingMap) {
|
|
||||||
String tableName;
|
String tableName;
|
||||||
String queueName;
|
String queueName;
|
||||||
PersistenceHandler persistenceHandler;
|
PersistenceHandler persistenceHandler;
|
||||||
List<Map<String, Object>> existsTableList;
|
List<Map<String, Object>> existsTableList;
|
||||||
try {
|
try {
|
||||||
if (Objects.nonNull(idGatewayMappingMap)) {
|
|
||||||
updateDeviceOnlineStatus(operations, deviceId, String.valueOf(idGatewayMappingMap.containsKey(deviceId)));
|
|
||||||
}
|
|
||||||
String modbusDeviceProductCode = (String) operations.get(
|
String modbusDeviceProductCode = (String) operations.get(
|
||||||
PersistenceHandler.DEVICE_INFO_CACHE + deviceId, "modbus_device_product_code");
|
PersistenceHandler.DEVICE_INFO_CACHE + deviceId, "modbus_device_product_code");
|
||||||
if (StringUtils.isEmpty(modbusDeviceProductCode)) {
|
if (StringUtils.isEmpty(modbusDeviceProductCode)) {
|
||||||
|
@ -146,41 +143,70 @@ public class Redis2DBPersistenceService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<Long, String> writeOnlineGateway() {
|
private void writeOnlineGateway() {
|
||||||
try {
|
try {
|
||||||
RequestEntity request = RequestEntity.get("http://localhost:9999/modbus-tcp/online").build();
|
RequestEntity request = RequestEntity.get("http://localhost:9999/modbus-tcp/online").build();
|
||||||
ResponseEntity<Response<List<String>>> response = this.restTemplate.exchange(request,
|
ResponseEntity<Response<List<String>>> response = this.restTemplate.exchange(request,
|
||||||
new ParameterizedTypeReference<Response<List<String>>>() {
|
new ParameterizedTypeReference<Response<List<String>>>() {
|
||||||
});
|
});
|
||||||
if (Objects.isNull(response) || Objects.isNull(response.getBody())) {
|
if (Objects.isNull(response) || Objects.isNull(response.getBody())) {
|
||||||
return null;
|
return;
|
||||||
}
|
}
|
||||||
if (ObjectUtils.isEmpty(response.getBody().getData())) {
|
if (ObjectUtils.isEmpty(response.getBody().getData())) {
|
||||||
return Collections.emptyMap();
|
allOffline();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
List<Map<String, Object>> idGatewayMappingList = this.jdbcTemplate.queryForList("select id, gateway_sn"
|
String onlineGatewaySns = response.getBody()
|
||||||
+ " from device where gateway_sn in ("
|
|
||||||
+ response.getBody()
|
|
||||||
.getData()
|
.getData()
|
||||||
.stream()
|
.stream()
|
||||||
|
.filter(Objects::nonNull)
|
||||||
.map(value -> new String(HexUtil.hexStringToBytes(value)))
|
.map(value -> new String(HexUtil.hexStringToBytes(value)))
|
||||||
|
.filter(StringUtils::isNotBlank)
|
||||||
.map(value -> "'" + value + "'")
|
.map(value -> "'" + value + "'")
|
||||||
.collect(Collectors.joining(",")) + ")");
|
.collect(Collectors.joining(", "));
|
||||||
|
if (StringUtils.isBlank(onlineGatewaySns)) {
|
||||||
|
onlineGatewaySns = "''";
|
||||||
|
}
|
||||||
|
List<Map<String, Object>> idGatewayMappingList = this.jdbcTemplate.queryForList("SELECT a.id, " +
|
||||||
|
"IF(b.gateway_sn IS NOT NULL, 'true', 'false') AS status FROM device a LEFT JOIN (" +
|
||||||
|
"SELECT id, gateway_sn FROM device WHERE gateway_sn IN (" +
|
||||||
|
onlineGatewaySns + ")) b ON a.id = b.id ORDER BY a.id");
|
||||||
if (ObjectUtils.isEmpty(idGatewayMappingList)) {
|
if (ObjectUtils.isEmpty(idGatewayMappingList)) {
|
||||||
|
allOffline();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.redisTemplate.executePipelined(new SessionCallback<Object>() {
|
||||||
|
@Override
|
||||||
|
public <K, V> Object execute(RedisOperations<K, V> operations) throws DataAccessException {
|
||||||
|
String deviceId, status;
|
||||||
|
HashOperations hashOperations = operations.opsForHash();
|
||||||
|
for (Map<String, Object> map : idGatewayMappingList) {
|
||||||
|
deviceId = MapUtil.getStr(map, "id");
|
||||||
|
status = MapUtil.getStr(map, "status");
|
||||||
|
hashOperations.put(PersistenceHandler.ONLINE_DEVICE_CACHE, deviceId, status);
|
||||||
|
hashOperations.put(PersistenceHandler.DEVICE_DATA_CACHE + deviceId, "online", status);
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
Long deviceId;
|
});
|
||||||
HashOperations operations = this.redisTemplate.opsForHash();
|
|
||||||
Map<Long, String> idGatewayMappingMap = new HashMap<Long, String>();
|
|
||||||
for (Map<String, Object> map : idGatewayMappingList) {
|
|
||||||
idGatewayMappingMap.put(deviceId = MapUtil.getLong(map, "id"), MapUtil.getStr(map, "gateway_sn"));
|
|
||||||
updateDeviceOnlineStatus(operations, deviceId, String.valueOf(idGatewayMappingMap.containsKey(deviceId)));
|
|
||||||
}
|
|
||||||
return idGatewayMappingMap;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("Get online devices list fail", e);
|
log.warn("Get online devices list fail", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void allOffline() {
|
||||||
|
HashOperations hashOperations = this.redisTemplate.opsForHash();
|
||||||
|
Map<String, String> onlineDeviceStatusMap = hashOperations.entries(PersistenceHandler.ONLINE_DEVICE_CACHE);
|
||||||
|
this.redisTemplate.executePipelined(new SessionCallback<Object>() {
|
||||||
|
@Override
|
||||||
|
public <K, V> Object execute(RedisOperations<K, V> operations) throws DataAccessException {
|
||||||
|
onlineDeviceStatusMap.forEach((deviceId, status) -> {
|
||||||
|
hashOperations.put(PersistenceHandler.ONLINE_DEVICE_CACHE, deviceId, "false");
|
||||||
|
hashOperations.put(PersistenceHandler.DEVICE_DATA_CACHE + deviceId, "online", "false");
|
||||||
|
});
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateDeviceOnlineStatus(HashOperations operations, Long deviceId, String onlineStatus) {
|
private void updateDeviceOnlineStatus(HashOperations operations, Long deviceId, String onlineStatus) {
|
||||||
|
@ -194,11 +220,10 @@ public class Redis2DBPersistenceService {
|
||||||
private final String cacheKey;
|
private final String cacheKey;
|
||||||
private final HashOperations operations;
|
private final HashOperations operations;
|
||||||
private final Map<String, Object> deviceMap;
|
private final Map<String, Object> deviceMap;
|
||||||
private final Map<Long, String> idGatewayMappingMap;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
Redis2DBPersistenceService.this.persistenceDeviceData(deviceId, cacheKey, operations, deviceMap, idGatewayMappingMap);
|
Redis2DBPersistenceService.this.persistenceDeviceData(deviceId, cacheKey, operations, deviceMap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,9 @@ public class LocalDateTimeDecodeHandler implements DecodeHandler {
|
||||||
if (StringUtils.isBlank(value)) {
|
if (StringUtils.isBlank(value)) {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
if (StringUtils.startsWith(value, "0-0-0 ")) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
return LocalDateTime.parse(value, IN_FORMATTER).format(OUT_FORMATTER);
|
return LocalDateTime.parse(value, IN_FORMATTER).format(OUT_FORMATTER);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,14 @@ public class SimpleLocalDateTimeDecodeHandler implements DecodeHandler {
|
||||||
if (StringUtils.isBlank(value)) {
|
if (StringUtils.isBlank(value)) {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
if (StringUtils.startsWith(value, "0-0-0 ")) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
try {
|
||||||
return LocalDateTime.parse(value, IN_FORMATTER).format(OUT_FORMATTER);
|
return LocalDateTime.parse(value, IN_FORMATTER).format(OUT_FORMATTER);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -17,7 +17,6 @@ import java.nio.charset.StandardCharsets;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author <a href="mailto:scwsl@foxmail.com">王仕龙</a>
|
* @author <a href="mailto:scwsl@foxmail.com">王仕龙</a>
|
||||||
|
@ -58,7 +57,7 @@ public abstract class AbstractPersistenceHandler implements PersistenceHandler {
|
||||||
|
|
||||||
protected void setValue(PreparedStatement ps, Map<String, Object> row, int index, String key, int sqlType) throws SQLException {
|
protected void setValue(PreparedStatement ps, Map<String, Object> row, int index, String key, int sqlType) throws SQLException {
|
||||||
String value = MapUtil.getStr(row, key);
|
String value = MapUtil.getStr(row, key);
|
||||||
if (Objects.isNull(value)) {
|
if (StringUtils.isBlank(value)) {
|
||||||
ps.setNull(index, sqlType);
|
ps.setNull(index, sqlType);
|
||||||
} else {
|
} else {
|
||||||
ps.setObject(index, value);
|
ps.setObject(index, value);
|
||||||
|
|
|
@ -135,7 +135,7 @@ public class ScssPersistenceHandler extends AbstractPersistenceHandler {
|
||||||
ScssPersistenceHandler.this.setValue(ps, newRow, 12, "prePressure", Types.INTEGER);
|
ScssPersistenceHandler.this.setValue(ps, newRow, 12, "prePressure", Types.INTEGER);
|
||||||
ScssPersistenceHandler.this.setValue(ps, newRow, 13, "internetTraffic", Types.INTEGER);
|
ScssPersistenceHandler.this.setValue(ps, newRow, 13, "internetTraffic", Types.INTEGER);
|
||||||
ScssPersistenceHandler.this.setValue(ps, newRow, 14, "loadFactor", Types.INTEGER);
|
ScssPersistenceHandler.this.setValue(ps, newRow, 14, "loadFactor", Types.INTEGER);
|
||||||
ScssPersistenceHandler.this.setValue(ps, newRow, 15, "dataTime", Types.TIMESTAMP);
|
ScssPersistenceHandler.this.setValue(ps, newRow, 15, "dataTime", Types.DATE);
|
||||||
ScssPersistenceHandler.this.setValue(ps, newRow, 16, "showDelay", Types.INTEGER);
|
ScssPersistenceHandler.this.setValue(ps, newRow, 16, "showDelay", Types.INTEGER);
|
||||||
ScssPersistenceHandler.this.setValue(ps, newRow, 17, "openWellSamplingInterval", Types.INTEGER);
|
ScssPersistenceHandler.this.setValue(ps, newRow, 17, "openWellSamplingInterval", Types.INTEGER);
|
||||||
ScssPersistenceHandler.this.setValue(ps, newRow, 18, "closeWellSamplingInterval", Types.INTEGER);
|
ScssPersistenceHandler.this.setValue(ps, newRow, 18, "closeWellSamplingInterval", Types.INTEGER);
|
||||||
|
|
|
@ -48,7 +48,10 @@ public class DeviceOptLogServiceImpl extends ServiceImpl<DeviceOptLogDao, Device
|
||||||
deviceOptLog.setGasStation(deviceVO.getGasStation());
|
deviceOptLog.setGasStation(deviceVO.getGasStation());
|
||||||
String content = "";
|
String content = "";
|
||||||
if ("device".equalsIgnoreCase(source)) {
|
if ("device".equalsIgnoreCase(source)) {
|
||||||
content = isOpen == null ? "" : isOpen == 1 ? "检测到气井开启" : "检测到气井关闭";
|
content =
|
||||||
|
|
||||||
|
|
||||||
|
isOpen == 1 ? "检测到气井开启" : "检测到气井关闭";
|
||||||
deviceOptLog.setUsername("系统");
|
deviceOptLog.setUsername("系统");
|
||||||
deviceOptLog.setName("系统");
|
deviceOptLog.setName("系统");
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue