在线状态更新

This commit is contained in:
wangshilong 2024-12-20 17:41:42 +08:00
parent 13dbc9eabb
commit 6ee2875c30
1 changed files with 17 additions and 12 deletions

View File

@ -22,10 +22,7 @@ import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
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.*;
import java.util.stream.Collectors;
/**
@ -54,7 +51,7 @@ public class Redis2DBPersistenceService {
@Scheduled(cron = "20/30 * * * * ? ")
public void write() {
Map<Long, String> idGatewayMappingMap = getOnlineGateway();
Map<Long, String> idGatewayMappingMap = writeOnlineGateway();
HashOperations operations = this.redisTemplate.opsForHash();
try (Cursor<String> cursor = this.redisTemplate.scan(ScanOptions.scanOptions()
.match(PersistenceHandler.DEVICE_DATA_CACHE + "*").build())) {
@ -74,10 +71,7 @@ public class Redis2DBPersistenceService {
}
try {
if (Objects.nonNull(idGatewayMappingMap)) {
operations.put(PersistenceHandler.DEVICE_DATA_CACHE + deviceId,
"online", String.valueOf(idGatewayMappingMap.containsKey(deviceId)));
operations.put(PersistenceHandler.ONLINE_DEVICE_CACHE, String.valueOf(deviceId),
String.valueOf(idGatewayMappingMap.containsKey(deviceId)));
updateDeviceOnlineStatus(operations, deviceId, String.valueOf(idGatewayMappingMap.containsKey(deviceId)));
}
String modbusDeviceProductCode = (String) operations.get(
PersistenceHandler.DEVICE_INFO_CACHE + deviceId, "modbus_device_product_code");
@ -120,15 +114,18 @@ public class Redis2DBPersistenceService {
}
}
private Map<Long, String> getOnlineGateway() {
private Map<Long, String> writeOnlineGateway() {
try {
RequestEntity request = RequestEntity.get("http://localhost:9999/modbus-tcp/online").build();
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())) {
if (Objects.isNull(response) || Objects.isNull(response.getBody())) {
return null;
}
if (ObjectUtils.isEmpty(response.getBody().getData())) {
return Collections.emptyMap();
}
List<Map<String, Object>> idGatewayMappingList = this.jdbcTemplate.queryForList("select id, gateway_sn"
+ " from device where gateway_sn in ("
+ response.getBody()
@ -140,9 +137,12 @@ public class Redis2DBPersistenceService {
if (ObjectUtils.isEmpty(idGatewayMappingList)) {
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(MapUtil.getLong(map, "id"), MapUtil.getStr(map, "gateway_sn"));
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) {
@ -151,4 +151,9 @@ public class Redis2DBPersistenceService {
}
}
private void updateDeviceOnlineStatus(HashOperations operations, Long deviceId, String onlineStatus) {
operations.put(PersistenceHandler.ONLINE_DEVICE_CACHE, String.valueOf(deviceId), onlineStatus);
operations.put(PersistenceHandler.DEVICE_DATA_CACHE + deviceId, "online", onlineStatus);
}
}