在线状态更新

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.stereotype.Component;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.util.HashMap; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -54,7 +51,7 @@ public class Redis2DBPersistenceService {
@Scheduled(cron = "20/30 * * * * ? ") @Scheduled(cron = "20/30 * * * * ? ")
public void write() { public void write() {
Map<Long, String> idGatewayMappingMap = getOnlineGateway(); Map<Long, String> idGatewayMappingMap = 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())) {
@ -74,10 +71,7 @@ public class Redis2DBPersistenceService {
} }
try { try {
if (Objects.nonNull(idGatewayMappingMap)) { if (Objects.nonNull(idGatewayMappingMap)) {
operations.put(PersistenceHandler.DEVICE_DATA_CACHE + deviceId, updateDeviceOnlineStatus(operations, deviceId, String.valueOf(idGatewayMappingMap.containsKey(deviceId)));
"online", String.valueOf(idGatewayMappingMap.containsKey(deviceId)));
operations.put(PersistenceHandler.ONLINE_DEVICE_CACHE, String.valueOf(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");
@ -120,15 +114,18 @@ public class Redis2DBPersistenceService {
} }
} }
private Map<Long, String> getOnlineGateway() { private Map<Long, String> 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()) || ObjectUtils.isEmpty(response.getBody().getData())) { if (Objects.isNull(response) || Objects.isNull(response.getBody())) {
return null; return null;
} }
if (ObjectUtils.isEmpty(response.getBody().getData())) {
return Collections.emptyMap();
}
List<Map<String, Object>> idGatewayMappingList = this.jdbcTemplate.queryForList("select id, gateway_sn" List<Map<String, Object>> idGatewayMappingList = this.jdbcTemplate.queryForList("select id, gateway_sn"
+ " from device where gateway_sn in (" + " from device where gateway_sn in ("
+ response.getBody() + response.getBody()
@ -140,9 +137,12 @@ public class Redis2DBPersistenceService {
if (ObjectUtils.isEmpty(idGatewayMappingList)) { if (ObjectUtils.isEmpty(idGatewayMappingList)) {
return null; return null;
} }
Long deviceId;
HashOperations operations = this.redisTemplate.opsForHash();
Map<Long, String> idGatewayMappingMap = new HashMap<Long, String>(); Map<Long, String> idGatewayMappingMap = new HashMap<Long, String>();
for (Map<String, Object> map : idGatewayMappingList) { 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; return idGatewayMappingMap;
} catch (Exception e) { } 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);
}
} }