持久化网关名称解析问题
This commit is contained in:
parent
e19a2c77c3
commit
f075d7780e
|
@ -4,6 +4,7 @@ import cn.hutool.core.map.MapUtil;
|
|||
import cn.hutool.json.JSONUtil;
|
||||
import com.isu.gaswellwatch.entity.Response;
|
||||
import com.isu.gaswellwatch.modbus.data.listener.Queues;
|
||||
import com.isu.gaswellwatch.utils.HexUtil;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
|
@ -122,8 +123,12 @@ public class Redis2DBPersistenceService {
|
|||
}
|
||||
List<Map<String, Object>> idGatewayMappingList = this.jdbcTemplate.queryForList("select id, gateway_sn"
|
||||
+ " from device where gateway_sn in ("
|
||||
+ response.getBody().getData().stream().map(value -> "'"
|
||||
+ value + "'").collect(Collectors.joining(",")) + ")");
|
||||
+ response.getBody()
|
||||
.getData()
|
||||
.stream()
|
||||
.map(value -> new String(HexUtil.hexStringToBytes(value)))
|
||||
.map(value -> "'" + value + "'")
|
||||
.collect(Collectors.joining(",")) + ")");
|
||||
if (ObjectUtils.isEmpty(idGatewayMappingList)) {
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
package com.isu.gaswellwatch.utils;
|
||||
|
||||
public class HexUtil {
|
||||
|
||||
public static byte[] hexStringToBytes(String src) {
|
||||
int l = src.length() / 2;
|
||||
byte[] ret = new byte[l];
|
||||
for (int i = 0; i < l; i++) {
|
||||
ret[i] = (byte) Integer.valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static String bytesToHexString(byte[] src) {
|
||||
String strHex = "";
|
||||
StringBuilder sb = new StringBuilder("");
|
||||
for (int n = 0; n < src.length; n++) {
|
||||
strHex = Integer.toHexString(src[n] & 0xFF);
|
||||
// 每个字节由两个字符表示,位数不够,高位补0
|
||||
sb.append((strHex.length() == 1) ? "0" + strHex : strHex);
|
||||
}
|
||||
return sb.toString().trim().toUpperCase();
|
||||
}
|
||||
|
||||
public static int hashPartition(String key, int partition) {
|
||||
return Math.abs(key.hashCode() % partition);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(hexStringToBytes("0D"));
|
||||
System.out.println(new byte[]{(byte) Integer.parseInt("0D", 16)});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue