| | |
| | | * @Description |
| | | */ |
| | | public class MqttMsgParser { |
| | | public MqttSubMsg parseSubMsg(String topic, MqttMessage mqttMsg) throws Exception { |
| | | 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{ |
| | | if(topicGrp[1].equals("sd1")){ |
| | | //山东设备(协议),且版本号为1 |
| | | return new ProtocolParserSdV1().parseSubMsg(topicGrp[2], topic, mqttMsg); |
| | | }else{ |
| | | throw new Exception("接收的mqtt消息主题中协议(厂家及版本)不可识别") ; |
| | | } |
| | | 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消息主题为空") ; |
| | | throw new Exception("接收的mqtt消息主题不合法") ; |
| | | } |
| | | } |
| | | |
| | | public MqttPubMsg createPubMsg(String orgTag, Command com) throws Exception { |
| | | 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命令,但未提供协议") ; |
| | | } |