package com.iot.modbus_rtcp.config; 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; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @Service @RequiredArgsConstructor public class EquipmentIPProperties { private final JdbcTemplate jdbcTemplate; private final Map identifiers = new ConcurrentHashMap<>(); public String get(String key) { return this.identifiers.get(key); } 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 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; } }