更新心跳包检测逻辑
This commit is contained in:
parent
3e5fec9236
commit
f100573e82
|
@ -3,17 +3,20 @@ package com.iot.modbus_rtcp.config;
|
|||
import cn.hutool.core.map.MapUtil;
|
||||
import com.iot.modbus_rtcp.utils.HexUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class EquipmentIPProperties {
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
private final AtomicLong lastSyncEquipment = new AtomicLong(System.currentTimeMillis());
|
||||
private final Map<String, String> identifiers = new ConcurrentHashMap<>();
|
||||
|
||||
public String get(String key) {
|
||||
|
@ -30,14 +33,20 @@ public class EquipmentIPProperties {
|
|||
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;
|
||||
}
|
||||
if (gatewayHeartbeat.length() > 80) {
|
||||
return false;
|
||||
}
|
||||
// 每分钟从数据库入库一次
|
||||
if (this.lastSyncEquipment.getAcquire() > System.currentTimeMillis() - 60_000) {
|
||||
this.jdbcTemplate.queryForList("select gateway_sn from device").forEach(map -> {
|
||||
String gatewaySn = StringUtils.trim(MapUtil.getStr(map, "gateway_sn", ""));
|
||||
if (StringUtils.isNotBlank(gatewaySn)) {
|
||||
this.identifiers.put(HexUtil.bytesToHexString(gatewaySn.getBytes()).toUpperCase(), gatewaySn);
|
||||
}
|
||||
});
|
||||
this.lastSyncEquipment.setRelease(System.currentTimeMillis());
|
||||
}
|
||||
return this.identifiers.containsKey(gatewayHeartbeat);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.iot.modbus_rtcp;
|
||||
|
||||
import com.iot.modbus_rtcp.utils.CRCUtil;
|
||||
import com.iot.modbus_rtcp.utils.HexUtil;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -15,26 +16,7 @@ public class GatewayTest {
|
|||
String s2 = "KENENG1400000358";
|
||||
System.out.println(new String(HexUtil.hexStringToBytes(s1)));
|
||||
System.out.println(HexUtil.bytesToHexString(s2.getBytes()));
|
||||
|
||||
|
||||
String hex = "333538";
|
||||
int decimal = 0;
|
||||
int power = 0;
|
||||
for (int i = hex.length() - 1; i >= 0; i--) {
|
||||
char digit = hex.charAt(i);
|
||||
if (digit >= '0' && digit <= '9') {
|
||||
decimal += (digit - '0') * Math.pow(16, power);
|
||||
} else if (digit >= 'A' && digit <= 'F') {
|
||||
decimal += (digit - 'A' + 10) * Math.pow(16, power);
|
||||
} else if (digit >= 'a' && digit <= 'f') {
|
||||
decimal += (digit - 'a' + 10) * Math.pow(16, power);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Invalid hex digit: " + digit);
|
||||
System.out.println(CRCUtil.verifyCRC(HexUtil.hexStringToBytes(s1)));
|
||||
}
|
||||
power++;
|
||||
}
|
||||
System.out.println(decimal);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue