refactor(device): 优化设备发送告警信息功能

- 批量查询设备,减少数据库交互次数
- 优化设备状态检查逻辑,提高效率
- 封装单个设备发送告警信息的逻辑,提高代码可读性- 使用 Redis 和 MQTT 时增加异常处理,提高系统稳定性
- 优化日志记录和异常提示,便于问题排查
This commit is contained in:
2025-08-26 17:12:36 +08:00
parent f839883f82
commit a5b8cdffec
3 changed files with 116 additions and 62 deletions

View File

@ -114,13 +114,24 @@ public class XinghanBootLogoRule implements MqttMessageRule {
private static final int CHUNK_SIZE = 256;
/**
* 计算数据的CRC32校验值并将结果转换为整数列表
*
* @param data 需要计算CRC32校验值的字节数组
* @return 包含CRC32校验值的整数列表每个字节对应一个无符号整数
*/
private static ArrayList<Integer> crc32AsList(byte[] data) {
// 计算CRC32校验值
CRC32 crc = new CRC32();
crc.update(data);
// 将CRC32值转换为字节数组
byte[] crcBytes = ByteBuffer.allocate(4)
.order(ByteOrder.BIG_ENDIAN)
.putInt((int) crc.getValue())
.array();
// 将字节数组转换为无符号整数列表
ArrayList<Integer> list = new ArrayList<>(4);
for (byte b : crcBytes) {
list.add(Byte.toUnsignedInt(b));
@ -128,6 +139,7 @@ public class XinghanBootLogoRule implements MqttMessageRule {
return list;
}
/* ---------- DTO ---------- */
@Data

View File

@ -79,7 +79,7 @@ public class XinghanSendAlarmMessageRule implements MqttMessageRule {
ArrayList<Integer> intData = new ArrayList<>();
intData.add(blockIndex);
// 获取块原内容 转成GBK 再转成无符号十进制整数
String blockTxt = data.split(",")[blockIndex-1];
String blockTxt = data.split("\\|")[blockIndex-1];
// 再按 GBK 编码把字符串转成字节数组,并逐个转为无符号十进制整数
for (byte b : blockTxt.getBytes(GBK)) {
intData.add(b & 0xFF); // b & 0xFF 得到 0~255 的整数