20260905:微信小程序-蓝牙采集网关-数据消费
This commit is contained in:
parent
73bea92544
commit
545923add1
@ -24,7 +24,7 @@ public class DeviceReportMsgConsumer {
|
|||||||
@Async(FastBeeConstant.TASK.DEVICE_UP_MESSAGE_TASK)
|
@Async(FastBeeConstant.TASK.DEVICE_UP_MESSAGE_TASK)
|
||||||
public void consume(DeviceReportBo bo) {
|
public void consume(DeviceReportBo bo) {
|
||||||
try {
|
try {
|
||||||
//处理数据解析
|
// 处理数据解析
|
||||||
reportMessageService.parseReportMsg(bo);
|
reportMessageService.parseReportMsg(bo);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("设备主动上报队列监听异常", e);
|
log.error("设备主动上报队列监听异常", e);
|
||||||
|
|||||||
@ -21,7 +21,7 @@ import java.util.Objects;
|
|||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
public class subscribeCallback implements IMqttMessageListener {
|
public class SubscribeCallback implements IMqttMessageListener {
|
||||||
@Resource
|
@Resource
|
||||||
private RuleProcess ruleProcess;
|
private RuleProcess ruleProcess;
|
||||||
@Resource
|
@Resource
|
||||||
@ -31,7 +31,26 @@ public class subscribeCallback implements IMqttMessageListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
|
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
|
||||||
String message = new String(mqttMessage.getPayload());
|
// 获取原始字节数据(可能是 Modbus 二进制数据或十六进制字符串)
|
||||||
|
byte[] payload = mqttMessage.getPayload();
|
||||||
|
|
||||||
|
// 判断数据格式:如果数据包含非十六进制字符,说明是原始二进制数据
|
||||||
|
boolean isHexString = isHexString(payload);
|
||||||
|
|
||||||
|
byte[] data;
|
||||||
|
String message;
|
||||||
|
|
||||||
|
if (isHexString) {
|
||||||
|
// 十六进制字符串格式(Modbus 小程序蓝牙采集)
|
||||||
|
message = new String(payload, StandardCharsets.UTF_8);
|
||||||
|
data = message.getBytes(StandardCharsets.UTF_8);
|
||||||
|
log.info("接收消息格式: 十六进制字符串");
|
||||||
|
} else {
|
||||||
|
// 二进制格式(兼容旧版本)
|
||||||
|
message = bytesToHex(payload);
|
||||||
|
data = payload;
|
||||||
|
log.info("接收消息格式: 二进制数据");
|
||||||
|
}
|
||||||
log.info("接收消息主题 : {}", topic);
|
log.info("接收消息主题 : {}", topic);
|
||||||
log.info("接收消息Qos : {}", mqttMessage.getQos());
|
log.info("接收消息Qos : {}", mqttMessage.getQos());
|
||||||
log.info("接收消息内容 : {}", message);
|
log.info("接收消息内容 : {}", message);
|
||||||
@ -45,6 +64,7 @@ public class subscribeCallback implements IMqttMessageListener {
|
|||||||
&& StringUtils.isNotEmpty(context.getTopic())) {
|
&& StringUtils.isNotEmpty(context.getTopic())) {
|
||||||
topic = context.getTopic();
|
topic = context.getTopic();
|
||||||
message = context.getPayload();
|
message = context.getPayload();
|
||||||
|
data = message.getBytes(StandardCharsets.UTF_8);
|
||||||
}
|
}
|
||||||
String serialNumber = topicsUtils.parseSerialNumber(topic);
|
String serialNumber = topicsUtils.parseSerialNumber(topic);
|
||||||
Long productId;
|
Long productId;
|
||||||
@ -53,12 +73,11 @@ public class subscribeCallback implements IMqttMessageListener {
|
|||||||
} else {
|
} else {
|
||||||
productId = topicsUtils.parseProductId(topic);
|
productId = topicsUtils.parseProductId(topic);
|
||||||
}
|
}
|
||||||
// Long productId = topicsUtils.parseProductId(topic);
|
|
||||||
String name = topicsUtils.parseTopicName(topic);
|
String name = topicsUtils.parseTopicName(topic);
|
||||||
DeviceReportBo reportBo = DeviceReportBo.builder()
|
DeviceReportBo reportBo = DeviceReportBo.builder()
|
||||||
.serialNumber(serialNumber)
|
.serialNumber(serialNumber)
|
||||||
.productId(productId)
|
.productId(productId)
|
||||||
.data(message.getBytes(StandardCharsets.UTF_8))
|
.data(data)
|
||||||
.platformDate(DateUtils.getNowDate())
|
.platformDate(DateUtils.getNowDate())
|
||||||
.topicName(topic)
|
.topicName(topic)
|
||||||
.serverType(ServerType.MQTT)
|
.serverType(ServerType.MQTT)
|
||||||
@ -66,4 +85,32 @@ public class subscribeCallback implements IMqttMessageListener {
|
|||||||
/*将mqtt的消息发送至MQ队列处理消息 ,减轻mqtt客户端消息压力*/
|
/*将mqtt的消息发送至MQ队列处理消息 ,减轻mqtt客户端消息压力*/
|
||||||
emqxMessageSerice.sendEmqxMessage(name, reportBo);
|
emqxMessageSerice.sendEmqxMessage(name, reportBo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断字节数组是否为十六进制字符串
|
||||||
|
* 十六进制字符串只包含 0-9, A-F, a-f 字符
|
||||||
|
*/
|
||||||
|
private boolean isHexString(byte[] bytes) {
|
||||||
|
if (bytes == null || bytes.length == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (byte b : bytes) {
|
||||||
|
// 判断是否为十六进制字符(0-9, A-F, a-f)
|
||||||
|
if (!((b >= '0' && b <= '9') || (b >= 'A' && b <= 'F') || (b >= 'a' && b <= 'f'))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字节数组转十六进制字符串
|
||||||
|
*/
|
||||||
|
private String bytesToHex(byte[] bytes) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for (byte b : bytes) {
|
||||||
|
sb.append(String.format("%02X", b));
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -74,7 +74,7 @@ public class ModbusProtocol implements IProtocol {
|
|||||||
DeviceReport report = new DeviceReport();
|
DeviceReport report = new DeviceReport();
|
||||||
report.setProductId(deviceData.getProductId());
|
report.setProductId(deviceData.getProductId());
|
||||||
report.setSerialNumber(deviceData.getSerialNumber());
|
report.setSerialNumber(deviceData.getSerialNumber());
|
||||||
//原始报文字符串
|
// 原始报文字符串
|
||||||
String hexDump = ByteBufUtil.hexDump(deviceData.getBuf());
|
String hexDump = ByteBufUtil.hexDump(deviceData.getBuf());
|
||||||
ModbusRtu message = messageDecoder.decode(deviceData);
|
ModbusRtu message = messageDecoder.decode(deviceData);
|
||||||
int slaveId = message.getSlaveId();
|
int slaveId = message.getSlaveId();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user