ModBus-rtcp/src/main/java/com/iot/modbus_rtcp/config/EquipmentIPProperties.java

44 lines
1.4 KiB
Java
Raw Normal View History

2024-11-24 12:07:47 +08:00
package com.iot.modbus_rtcp.config;
2024-11-28 20:27:37 +08:00
import cn.hutool.core.map.MapUtil;
import com.iot.modbus_rtcp.utils.HexUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
2024-11-24 12:07:47 +08:00
import java.util.Map;
2024-11-28 20:27:37 +08:00
import java.util.concurrent.ConcurrentHashMap;
2024-11-24 12:07:47 +08:00
2024-11-28 20:27:37 +08:00
@Service
@RequiredArgsConstructor
2024-11-24 12:07:47 +08:00
public class EquipmentIPProperties {
2024-11-28 20:27:37 +08:00
private final JdbcTemplate jdbcTemplate;
private final Map<String, String> identifiers = new ConcurrentHashMap<>();
2024-11-24 12:07:47 +08:00
public String get(String key) {
return this.identifiers.get(key);
}
2024-11-28 20:27:37 +08:00
public String put(String gatewaySn) {
String gatewayHeartbeat = HexUtil.bytesToHexString(gatewaySn.getBytes()).toUpperCase();
this.identifiers.put(gatewayHeartbeat, gatewaySn);
return gatewayHeartbeat;
}
public boolean contains(String gatewayHeartbeat) {
if (this.identifiers.containsKey(gatewayHeartbeat)) {
return true;
}
gatewayHeartbeat = gatewayHeartbeat.toUpperCase();
String gatewaySn = new String(HexUtil.hexStringToBytes(gatewayHeartbeat));
Map<String, Object> countMap = this.jdbcTemplate.queryForMap("select count(*) as ctn from device where gateway_sn = '" + gatewaySn + "'");
if (MapUtil.getInt(countMap, "ctn") > 0) {
this.identifiers.put(gatewayHeartbeat, gatewaySn);
return true;
}
return false;
}
2024-11-24 12:07:47 +08:00
}