zhubaomin
2 天以前 10a0b0ca34824307aa7d23b0ad6679b36bd57842
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.dy.common.mw.protocol4Mqtt;
 
import com.dy.common.mw.protocol.Command;
import com.dy.common.mw.protocol4Mqtt.pSdV1.ProtocolConstantSdV1;
import com.dy.common.mw.protocol4Mqtt.pSdV1.ProtocolParserSdV1;
import org.eclipse.paho.client.mqttv3.MqttMessage;
 
/**
 * @Author: liurunyu
 * @Date: 2025/6/5 10:00
 * @Description
 */
public class MqttMsgParser {
    public static MqttTopic parseSubTopic(String topic) throws Exception {
        if(topic != null && topic.trim().length() != 0){
            String[] topicGrp = topic.split("/") ;
            if(topicGrp.length != 4){
                throw new Exception("接收的mqtt消息主题不可识别") ;
            }else{
                MqttTopic vo = new MqttTopic() ;
                vo.orgTag = topicGrp[0] ;
                vo.protocol = topicGrp[1] ;
                vo.devId = topicGrp[2] ;
                vo.topic = topicGrp[3] ;
                return vo ;
            }
        }else{
            throw new Exception("接收的mqtt消息主题不合法") ;
        }
    }
 
    public static String createPubTopic(MqttTopic tp) throws Exception {
        return tp.orgTag + "/" + tp.protocol + "/" + tp.devId + "/" + tp.topic ;
    }
 
    public static MqttSubMsg parseSubMsg(MqttTopic subTopic, MqttMessage mqttMsg, MqttCallback callback) throws Exception {
        if(subTopic.protocol.equals(ProtocolConstantSdV1.protocolName + ProtocolConstantSdV1.protocolVer)){
            //山东设备(协议),且版本号为1
            return new ProtocolParserSdV1().parseSubMsg(subTopic, mqttMsg, callback);
        }else{
            throw new Exception("接收的mqtt消息主题中协议(厂家及版本)不可识别") ;
        }
    }
 
    public static MqttPubMsg createPubMsg(String orgTag, Command com) throws Exception {
        if(com.protocol == null && com.protocol.trim().length() != 0){
            throw new Exception("接收到MQTT命令,但未提供协议") ;
        }
        if(com.protocolVersion == null){
            throw new Exception("接收到MQTT命令,但未提供协议版本号") ;
        }
        if(com.code != null && com.code.trim().length() != 0){
            throw new Exception("接收到MQTT命令,但未提供功能码") ;
        }
        if(com.protocol.equals(ProtocolConstantSdV1.protocolName)){
            if(com.protocolVersion.shortValue() == ProtocolConstantSdV1.protocolVer){
                return new ProtocolParserSdV1().createPubMsg(orgTag, com) ;
            }else{
                throw new Exception("接收到MQTT命令,协议" + com.protocol + "版本" + com.protocolVersion + "构造器未实现") ;
            }
        }else{
            throw new Exception("接收到MQTT命令,协议" + com.protocol + "构造器未实现") ;
        }
    }
 
 
    public static void main(String[] args) {
        String s = "ym/sd1/10000/control/m1" ;
        String[] ss = s.split("/") ;
        for (String s1 : ss) {
            System.out.println(s1);
        }
    }
}