|
...
|
...
|
@@ -10,6 +10,7 @@ import com.zhonglai.luhui.mqtt.comm.factory.Topic; |
|
|
|
import com.zhonglai.luhui.mqtt.comm.util.ByteUtil;
|
|
|
|
import com.zhonglai.luhui.mqtt.service.db.DeviceService;
|
|
|
|
import lombok.SneakyThrows;
|
|
|
|
import org.apache.commons.beanutils.BeanUtils;
|
|
|
|
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
|
|
|
|
import org.eclipse.paho.client.mqttv3.MqttCallbackExtended;
|
|
|
|
import org.eclipse.paho.client.mqttv3.MqttException;
|
|
...
|
...
|
@@ -53,6 +54,9 @@ public class MqttCallback implements MqttCallbackExtended { |
|
|
|
|
|
|
|
@Override
|
|
|
|
public void messageArrived(String s, MqttMessage mqttMessage) {
|
|
|
|
Topic desttopic = new Topic(s);
|
|
|
|
desttopic.setPayloadtype("POST_REQ");
|
|
|
|
|
|
|
|
//接收到消息
|
|
|
|
StringBuffer buffer = new StringBuffer();
|
|
|
|
buffer.append("topic:");
|
|
...
|
...
|
@@ -65,30 +69,33 @@ public class MqttCallback implements MqttCallbackExtended { |
|
|
|
buffer.append(ByteUtil.hexStringToSpace(ByteUtil.toHexString(mqttMessage.getPayload())));
|
|
|
|
buffer.append("\r\n");
|
|
|
|
buffer.append("\r\n");
|
|
|
|
Topic topic = new Topic(s);
|
|
|
|
if(null == topic)
|
|
|
|
{
|
|
|
|
log.error("消息{},topic为空,不做解析");
|
|
|
|
log.error("消息《"+s+"》解析为空 》》》内容:\r\n"+buffer.toString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//日志记录
|
|
|
|
log.info(buffer.toString());
|
|
|
|
|
|
|
|
//准备数据
|
|
|
|
byte[] data = mqttMessage.getPayload();
|
|
|
|
IotDevice iotDevice = deviceService.getDeviceById(topic.getClientid());
|
|
|
|
if(null == iotDevice)
|
|
|
|
{
|
|
|
|
log.info("设备{}不存在",topic.getClientid());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if("ONLINE".equals(topic.getTopicType().toUpperCase()))
|
|
|
|
{
|
|
|
|
topic.setPayloadtype("String");
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
Topic topic = new Topic(s);
|
|
|
|
if(null == topic)
|
|
|
|
{
|
|
|
|
log.error("消息{},topic为空,不做解析");
|
|
|
|
log.error("消息《"+s+"》解析为空 》》》内容:\r\n"+buffer.toString());
|
|
|
|
terminalService.publish(desttopic.generateSendMessageTopic(),"0");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//日志记录
|
|
|
|
log.info(buffer.toString());
|
|
|
|
|
|
|
|
//准备数据
|
|
|
|
byte[] data = mqttMessage.getPayload();
|
|
|
|
IotDevice iotDevice = deviceService.getDeviceById(topic.getClientid());
|
|
|
|
if(null == iotDevice)
|
|
|
|
{
|
|
|
|
log.info("设备{}不存在",topic.getClientid());
|
|
|
|
terminalService.publish(desttopic.generateSendMessageTopic(),"1");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if("ONLINE".equals(topic.getTopicType().toUpperCase()))
|
|
|
|
{
|
|
|
|
topic.setPayloadtype("String");
|
|
|
|
}
|
|
|
|
//转化为协议对象
|
|
|
|
BusinessDto businessDto = BusinessDtoClassNew.newBean(topic.getPayloadtype(),data).analyticalModel(iotDevice.getThings_model_value());
|
|
|
|
|
|
...
|
...
|
@@ -106,8 +113,17 @@ public class MqttCallback implements MqttCallbackExtended { |
|
|
|
|
|
|
|
//数据持久化
|
|
|
|
dataPersistenceService.persistence(topic,dto);
|
|
|
|
|
|
|
|
terminalService.publish(desttopic.generateSendMessageTopic(),"2");
|
|
|
|
log.info("{}payload解析完成",s);
|
|
|
|
} catch (Exception e) {
|
|
|
|
log.error(s+"消息解析异常",e);
|
|
|
|
try {
|
|
|
|
terminalService.publish(desttopic.generateSendMessageTopic(),"3");
|
|
|
|
} catch (MqttException ex) {
|
|
|
|
log.error(s+"消息解析异常时返回的执行结果消息异常",ex);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
...
|
...
|
|