增量开发MQTT协议、功能模块,上下行命令(消息)等
| | |
| | | <version>2.2.2</version> |
| | | </dependency> |
| | | |
| | | <!-- MQTTæ¶æ¯ä¸é´ä»¶,è¿æ¥æ± å客æ·ç«¯ --> |
| | | <dependency> |
| | | <groupId>org.apache.commons</groupId> |
| | | <artifactId>commons-pool2</artifactId> |
| | | <version>2.9.0</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>org.eclipse.paho</groupId> |
| | | <artifactId>org.eclipse.paho.client.mqttv3</artifactId> |
| | | <version>1.2.5</version> |
| | | </dependency> |
| | | |
| | | <!-- quartz --> |
| | | <dependency> |
| | | <groupId>org.quartz-scheduler</groupId> |
| | |
| | | <version>2.17.2</version> |
| | | </dependency> |
| | | |
| | | |
| | | </dependencies> |
| | | |
| | | <build> |
New file |
| | |
| | | package com.dy.common.mw.channel.mqtt; |
| | | |
| | | import org.apache.commons.pool2.impl.GenericObjectPool; |
| | | import org.apache.commons.pool2.impl.GenericObjectPoolConfig; |
| | | import org.eclipse.paho.client.mqttv3.MqttClient; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/6/4 11:35 |
| | | * @Description |
| | | */ |
| | | public class MqttClientPool { |
| | | |
| | | private final GenericObjectPool<MqttClient> pool; |
| | | |
| | | public MqttClientPool(String broker, String username, String password, int maxConnections) { |
| | | MqttClientPooledObjectFactory factory = new MqttClientPooledObjectFactory(broker, username, password); |
| | | GenericObjectPoolConfig<MqttClient> config = new GenericObjectPoolConfig<>(); |
| | | config.setMaxTotal(maxConnections); |
| | | config.setMaxIdle(maxConnections); |
| | | config.setMinIdle(1); |
| | | config.setTestOnBorrow(true); |
| | | config.setTestOnReturn(true); |
| | | config.setTestWhileIdle(true); |
| | | this.pool = new GenericObjectPool<>(factory, config); |
| | | } |
| | | |
| | | public MqttClient popClient() throws Exception { |
| | | return pool.borrowObject(); |
| | | } |
| | | |
| | | public void pushClient(MqttClient client) { |
| | | if (client != null) { |
| | | pool.returnObject(client); |
| | | } |
| | | } |
| | | |
| | | public boolean isClose(){ |
| | | return pool.isClosed(); |
| | | } |
| | | |
| | | public void close() { |
| | | pool.close(); |
| | | } |
| | | } |
| | | |
New file |
| | |
| | | package com.dy.common.mw.channel.mqtt; |
| | | |
| | | import org.apache.commons.pool2.BasePooledObjectFactory; |
| | | import org.apache.commons.pool2.PooledObject; |
| | | import org.apache.commons.pool2.impl.DefaultPooledObject; |
| | | import org.eclipse.paho.client.mqttv3.MqttClient; |
| | | import org.eclipse.paho.client.mqttv3.MqttConnectOptions; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/6/4 11:16 |
| | | * @Description |
| | | */ |
| | | public class MqttClientPooledObjectFactory extends BasePooledObjectFactory<MqttClient> { |
| | | |
| | | private final String broker; |
| | | private final String username; |
| | | private final String password; |
| | | |
| | | public MqttClientPooledObjectFactory(String broker, String username, String password) { |
| | | this.broker = broker; |
| | | this.username = username; |
| | | this.password = password; |
| | | } |
| | | |
| | | @Override |
| | | public MqttClient create() throws Exception { |
| | | String clientId = MqttClient.generateClientId(); |
| | | MqttClient client = new MqttClient(broker, clientId); |
| | | |
| | | MqttConnectOptions options = new MqttConnectOptions(); |
| | | options.setUserName(username); |
| | | options.setPassword(password.toCharArray()); |
| | | options.setAutomaticReconnect(true); |
| | | options.setCleanSession(true); |
| | | |
| | | client.connect(options); |
| | | return client; |
| | | } |
| | | |
| | | @Override |
| | | public PooledObject<MqttClient> wrap(MqttClient client) { |
| | | return new DefaultPooledObject<>(client); |
| | | } |
| | | |
| | | @Override |
| | | public void destroyObject(PooledObject<MqttClient> p) throws Exception { |
| | | MqttClient client = p.getObject(); |
| | | if (client.isConnected()) { |
| | | client.disconnect(); |
| | | } |
| | | client.close(); |
| | | } |
| | | |
| | | @Override |
| | | public boolean validateObject(PooledObject<MqttClient> p) { |
| | | MqttClient client = p.getObject(); |
| | | return client.isConnected(); |
| | | } |
| | | } |
New file |
| | |
| | | package com.dy.common.mw.channel.mqtt; |
| | | |
| | | import com.dy.common.util.Callback; |
| | | import com.dy.common.util.ThreadJob; |
| | | import org.eclipse.paho.client.mqttv3.MqttClient; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/6/4 11:35 |
| | | * @Description |
| | | */ |
| | | public class Test { |
| | | |
| | | private static String mqSvIp = "121.199.41.121"; |
| | | private static Integer mqSvPort = 1883; |
| | | private static String mqSvUserName = "dyyjy"; |
| | | private static String mqSvUserPassword = "Dyyjy2025,;.abc!@#"; |
| | | |
| | | private static String topic1 = "test/topic1" ; |
| | | private static String topic2 = "test/topic2" ; |
| | | |
| | | private static int maxConnections = 10 ; |
| | | |
| | | private static MqttClientPool pool; |
| | | /** |
| | | * QoS |
| | | * ç级 åç§° æ¶æ¯ä¼ éç¹æ§ |
| | | * 0 è³å¤ä¸æ¬¡ï¼At most onceï¼ æ¶æ¯åéåä¸ä¿è¯å°è¾¾ï¼å¯è½ä¸¢å¤±æéå¤ï¼å¼éæå°ï¼å¯é æ§æä½ã |
| | | * 1 è³å°ä¸æ¬¡ï¼At least onceï¼ æ¶æ¯è³å°ä¼å°è¾¾ä¸æ¬¡ï¼å¯è½éå¤ï¼ä½ä¸ä¼ä¸¢å¤±ï¼å¯é æ§ä¸çï¼éç¨äºå¤æ°åºæ¯ã |
| | | * 2 æ°å¥½ä¸æ¬¡ï¼Exactly onceï¼ æ¶æ¯ä»
ä¼å°è¾¾ä¸æ¬¡ï¼ä¸éå¤ä¸ä¸ä¸¢å¤±ï¼å¯é æ§æé«ï¼ä½å¼éæå¤§ï¼å®ç°æå¤æã |
| | | */ |
| | | private static int QoS = 1 ; |
| | | |
| | | public static void main(String[] args) { |
| | | try{ |
| | | // åå§åè¿æ¥æ± |
| | | pool = new MqttClientPool("tcp://" + mqSvIp + ":" + mqSvPort, mqSvUserName, mqSvUserPassword, maxConnections); |
| | | MqttClient clientSub = pool.popClient() ; |
| | | testSubscribe(clientSub, topic1); |
| | | testSubscribe(clientSub, topic2); |
| | | MqttClient clientPub = pool.popClient() ; |
| | | testPublish(clientPub, topic1, "hello world", 1000); |
| | | testPublish(clientPub, topic2, "hello China", 1500); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | // å
³éè¿æ¥æ± |
| | | //pool.close(); |
| | | } |
| | | } |
| | | private static void testSubscribe(MqttClient clientSub, String topic) throws Exception { |
| | | ThreadJob tjob = new ThreadJob() { |
| | | @Override |
| | | public Object execute() throws Exception { |
| | | clientSub.subscribe(topic, QoS, (topic, msg) -> { |
| | | System.out.println("ä»ä¸»é¢" + topic + "æ¶å°æ¶æ¯: " + new String(msg.getPayload())); |
| | | }); |
| | | while (true) { |
| | | Thread.sleep(1000L); |
| | | } |
| | | } |
| | | }; |
| | | tjob.start(new Callback() { |
| | | @Override |
| | | public void call(Object obj) { |
| | | System.out.println("æ§è¡æå"); |
| | | } |
| | | @Override |
| | | public void call(Object... objs) { |
| | | System.out.println("æ§è¡æå"); |
| | | } |
| | | |
| | | @Override |
| | | public void exception(Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | private static void testPublish(MqttClient clientPub, String topic, String message, long sleep) throws Exception { |
| | | ThreadJob tjob = new ThreadJob() { |
| | | @Override |
| | | public Object execute() throws Exception { |
| | | int count = 0 ; |
| | | while (true) { |
| | | // å叿¶æ¯ |
| | | byte[] bs = (message + " " + count++).getBytes() ; |
| | | clientPub.publish(topic, bs, QoS, false); |
| | | Thread.sleep(sleep); |
| | | } |
| | | } |
| | | }; |
| | | tjob.start(new Callback() { |
| | | @Override |
| | | public void call(Object obj) { |
| | | System.out.println("æ§è¡æå"); |
| | | } |
| | | @Override |
| | | public void call(Object... objs) { |
| | | System.out.println("æ§è¡æå"); |
| | | } |
| | | |
| | | @Override |
| | | public void exception(Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | }); |
| | | } |
| | | } |
| | |
| | | */ |
| | | public static final String outerTransCommand = "outerTransCommand" ; |
| | | |
| | | |
| | | /** |
| | | * é对Mqttå¤é¨å½ä»¤ |
| | | * åªè½æ¯å¼æ¥ï¼å½ä»¤ç»æéè¿ç¸å
³çä¿¡æ¯åå¸ééåå¸åºå» |
| | | */ |
| | | public static final String mqttCommand = "mqttCommand" ; |
| | | |
| | | |
| | | /** |
| | | * æ¬å½ä»¤æ¯ä¸ä¸ªå«çå½ä»¤çç»æï¼ç»æä»¥å½ä»¤çæ¹å¼è¡¨ç¤ºï¼ |
| | | */ |
New file |
| | |
| | | package com.dy.common.mw.protocol4Mqtt; |
| | | |
| | | import lombok.Data; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/6/4 17:38 |
| | | * @Description å½ä»¤å¼å¯¹è±¡ |
| | | */ |
| | | @Data |
| | | public class Com4Mqtt { |
| | | public String commandId ;//å½ä»¤ID |
| | | |
| | | public String deviceId ;//设å¤ID |
| | | |
| | | public String protocol;//åè®®åç§°ï¼å®ç°åå®¶ç¼ç ï¼ |
| | | |
| | | public Short protocolVersion;//åè®®çæ¬å· |
| | | |
| | | public String code ;//åè½ç |
| | | public String value ;//å¼ï¼å¯è½æ¯æ´æ°ãæµ®ç¹æ°ãå¸å°æ°ï¼trueæfalseï¼ï¼ |
| | | |
| | | } |
New file |
| | |
| | | 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 MqttSubMsg parseSubMsg(String topic, MqttMessage mqttMsg) 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æ¶æ¯ä¸»é¢ä¸åè®®ï¼åå®¶åçæ¬ï¼ä¸å¯è¯å«") ; |
| | | } |
| | | } |
| | | }else{ |
| | | throw new Exception("æ¥æ¶çmqttæ¶æ¯ä¸»é¢ä¸ºç©º") ; |
| | | } |
| | | } |
| | | |
| | | public 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); |
| | | } |
| | | } |
| | | } |
New file |
| | |
| | | package com.dy.common.mw.protocol4Mqtt; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/6/5 11:44 |
| | | * @Description |
| | | */ |
| | | public abstract class MqttPubMsg { |
| | | public String commandId ;//å½ä»¤ID |
| | | |
| | | public String deviceId ;//设å¤ID |
| | | |
| | | public String mqttResultSendWebUrl ;//Mqttè¿åå½ä»¤ç»æ ååç®çå°web URL |
| | | |
| | | public String topic ;//æ¶æ¯ä¸»é¢ |
| | | public String msg ;//æ¶æ¯ |
| | | |
| | | public boolean isCacheForOffLine ;//ä¸è¡å½ä»¤æ§å¶ï¼æ¶æ¯ä¸é´ä»¶ä¸å¨çº¿æ¯å¦ç¼åå½ä»¤ |
| | | public boolean hasResponse ;//ä¸è¡å½ä»¤æ§å¶ï¼å½ä»¤æ¯å¦æåºç |
| | | |
| | | public abstract boolean valid(); |
| | | |
| | | } |
New file |
| | |
| | | package com.dy.common.mw.protocol4Mqtt; |
| | | |
| | | import com.dy.common.util.Callback; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/6/5 11:44 |
| | | * @Description |
| | | */ |
| | | |
| | | public abstract class MqttSubMsg { |
| | | public String commandId ;//å½ä»¤ID |
| | | |
| | | public String deviceId ;//设å¤ID |
| | | |
| | | public String mqttResultSendWebUrl ;//Mttè¿åå½ä»¤ç»æ ååç®çå°web URL |
| | | |
| | | public String topic ;//æ¶æ¯ä¸»é¢ |
| | | public String msg ;//æ¶æ¯ |
| | | |
| | | public abstract boolean valid(); |
| | | |
| | | public abstract boolean subMsgMatchPubMsg(MqttPubMsg pubMsg); |
| | | |
| | | public abstract void action(Callback callback); |
| | | } |
New file |
| | |
| | | package com.dy.common.mw.protocol4Mqtt.pSdV1; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/6/4 17:41 |
| | | * @Description |
| | | */ |
| | | public class CodeSdV1 { |
| | | public static final String cd_Fault = "00" ;//æ
éè§£é¤å½ä»¤ |
| | | public static final String cd_Stir = "01" ;//æ
æå¯åå½ä»¤ |
| | | public static final String cd_Inject = "02" ;//注è¥å¯åå½ä»¤ |
| | | public static final String cd_Irr = "03" ;//çæºå¯åå½ä»¤ |
| | | } |
New file |
| | |
| | | package com.dy.common.mw.protocol4Mqtt.pSdV1; |
| | | |
| | | import com.dy.common.mw.protocol4Mqtt.MqttPubMsg; |
| | | import lombok.Data; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/6/4 18:00 |
| | | * @Description ä¸åçå叿¶æ¯ï¼å³ä¸è¡å½ä»¤ï¼ |
| | | */ |
| | | @Data |
| | | public class MqttPubMsgSdV1 extends MqttPubMsg { |
| | | |
| | | public Integer address ;//å¯åå¨å°å |
| | | public String value ;//å¯åå¨å¼ |
| | | |
| | | @Override |
| | | public boolean valid() { |
| | | if (topic == null || topic.isEmpty()) { |
| | | return false; |
| | | } |
| | | if (msg == null || msg.isEmpty()) { |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | } |
New file |
| | |
| | | package com.dy.common.mw.protocol4Mqtt.pSdV1; |
| | | |
| | | import com.dy.common.mw.protocol4Mqtt.MqttPubMsg; |
| | | import com.dy.common.mw.protocol4Mqtt.MqttSubMsg; |
| | | import com.dy.common.util.Callback; |
| | | import lombok.Data; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/6/4 16:58 |
| | | * @Description æ¶å°ç订é
æ¶æ¯ |
| | | */ |
| | | @Data |
| | | public class MqttSubMsgSdV1 extends MqttSubMsg { |
| | | public Integer address ;//å¯åå¨å°å |
| | | public String value ;//å¯åå¨å¼ |
| | | |
| | | public MqttSubMsgSdV1(){} |
| | | |
| | | public MqttSubMsgSdV1(String deviceId, String topic, String msg) { |
| | | this.deviceId = deviceId ; |
| | | this.topic = topic ; |
| | | this.msg = msg ; |
| | | } |
| | | public String toString(){ |
| | | StringBuilder sb = new StringBuilder(); |
| | | if(commandId != null){ |
| | | sb.append("commandId:") |
| | | .append(commandId) |
| | | .append("\n") ; |
| | | } |
| | | sb.append("主é¢:") |
| | | .append(topic) |
| | | .append("\n") ; |
| | | sb.append("æ¶æ¯:") |
| | | .append(msg) |
| | | .append("\n") ; |
| | | |
| | | return sb.toString() ; |
| | | } |
| | | |
| | | public boolean subMsgMatchPubMsg(MqttPubMsg pubMsg){ |
| | | if (pubMsg instanceof MqttPubMsgSdV1) { |
| | | MqttPubMsgSdV1 pubMsgSdV1 = (MqttPubMsgSdV1) pubMsg; |
| | | if(this.address.intValue() == pubMsgSdV1.getAddress().intValue()){ |
| | | return true ; |
| | | } |
| | | } |
| | | return false ; |
| | | } |
| | | |
| | | @Override |
| | | public boolean valid() { |
| | | if (topic == null || topic.isEmpty()) { |
| | | return false; |
| | | } |
| | | if (msg == null || msg.isEmpty()) { |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | | public void action(Callback callback){ |
| | | callback.call(this) ; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.dy.common.mw.protocol4Mqtt.pSdV1; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/6/5 10:52 |
| | | * @Description |
| | | */ |
| | | public class ProtocolConstantSdV1 { |
| | | public static final String protocolName = "sd" ; |
| | | public static final short protocolVer = 1 ; |
| | | } |
New file |
| | |
| | | package com.dy.common.mw.protocol4Mqtt.pSdV1; |
| | | |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.dy.common.mw.protocol.Command; |
| | | import com.dy.common.mw.protocol4Mqtt.pSdV1.downVos.ComCtrlVo; |
| | | import org.eclipse.paho.client.mqttv3.MqttMessage; |
| | | |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/6/5 11:41 |
| | | * @Description |
| | | */ |
| | | public class ProtocolParserSdV1 { |
| | | public MqttSubMsgSdV1 parseSubMsg(String deviceId, String topic, MqttMessage mqttMsg) throws Exception { |
| | | MqttSubMsgSdV1 ms = new MqttSubMsgSdV1(deviceId, topic, new String(mqttMsg.getPayload(), "UTF-8")); |
| | | return ms; |
| | | } |
| | | |
| | | public MqttPubMsgSdV1 createPubMsg(String orgTag, Command com) throws Exception { |
| | | MqttPubMsgSdV1 msg = null ; |
| | | switch (com.code){ |
| | | case CodeSdV1.cd_Fault:{ |
| | | //æ
éè§£é¤å½ä»¤ |
| | | this.checkParam(com); |
| | | this.checkRtnWebUrl(com); |
| | | msg = this.createPubMsgOfFault(orgTag, com) ; |
| | | break ; |
| | | } |
| | | case CodeSdV1.cd_Stir:{ |
| | | //æ
æå¯åå½ä»¤ |
| | | this.checkParam(com); |
| | | this.checkRtnWebUrl(com); |
| | | msg = this.createPubMsgOfStir(orgTag, com) ; |
| | | break ; |
| | | } |
| | | case CodeSdV1.cd_Inject:{ |
| | | //注è¥å¯åå½ä»¤ |
| | | this.checkParam(com); |
| | | this.checkRtnWebUrl(com); |
| | | msg = this.createPubMsgOfInject(orgTag, com) ; |
| | | break ; |
| | | } |
| | | case CodeSdV1.cd_Irr:{ |
| | | //çæºå¯åå½ä»¤ |
| | | this.checkParam(com); |
| | | this.checkRtnWebUrl(com); |
| | | msg = this.createPubMsgOfIrr(orgTag, com) ; |
| | | break ; |
| | | } |
| | | default:{ |
| | | throw new Exception("æ¥æ¶å°MQTTå½ä»¤ï¼åè®®" + com.protocol + "çæ¬" + com.protocolVersion + "åè½ç " + com.code + "æé 卿ªå®ç°") ; |
| | | } |
| | | } |
| | | return msg ; |
| | | } |
| | | private void checkParam(Command com)throws Exception { |
| | | if(com.param == null){ |
| | | throw new Exception("æ¥æ¶å°MQTTå½ä»¤ï¼åè®®" + com.protocol + "çæ¬" + com.protocolVersion + "åè½ç " + com.code + "å½ä»¤æ°æ®ä¸ºç©º") ; |
| | | } |
| | | } |
| | | private void checkRtnWebUrl(Command com)throws Exception { |
| | | if(com.rtuResultSendWebUrl == null || com.rtuResultSendWebUrl.trim().equals("")){ |
| | | throw new Exception("æ¥æ¶å°MQTTå½ä»¤ï¼åè®®" + com.protocol + "çæ¬" + com.protocolVersion + "åè½ç " + com.code + "å½ä»¤ç»æåæ¶URL为空") ; |
| | | } |
| | | } |
| | | private MqttPubMsgSdV1 createPubMsgOfFault(String orgTag, Command com) throws Exception { |
| | | JSONObject obj = (JSONObject) com.param; |
| | | String json = obj.toJSONString(); |
| | | ComCtrlVo cvo = JSON.parseObject(json, ComCtrlVo.class); |
| | | if(cvo == null){ |
| | | throw new Exception("json转ComCtrlVo为null") ; |
| | | } |
| | | MqttPubMsgSdV1 msg = new MqttPubMsgSdV1() ; |
| | | this.setPubMsgBase(com, msg); |
| | | msg.isCacheForOffLine = false ; |
| | | msg.hasResponse = true ; |
| | | msg.address = 123 ; |
| | | msg.value = "" + (cvo.isDo?1:0); |
| | | msg.topic = createTopic(orgTag, com) ; |
| | | msg.msg = "" ; |
| | | return msg ; |
| | | } |
| | | private MqttPubMsgSdV1 createPubMsgOfStir(String orgTag, Command com) throws Exception { |
| | | JSONObject obj = (JSONObject) com.param; |
| | | String json = obj.toJSONString(); |
| | | ComCtrlVo cvo = JSON.parseObject(json, ComCtrlVo.class); |
| | | if(cvo == null){ |
| | | throw new Exception("json转ComCtrlVo为null") ; |
| | | } |
| | | MqttPubMsgSdV1 msg = new MqttPubMsgSdV1() ; |
| | | this.setPubMsgBase(com, msg); |
| | | msg.isCacheForOffLine = false ; |
| | | msg.hasResponse = true ; |
| | | msg.address = 123 ; |
| | | msg.value = "" + (cvo.isDo?1:0); |
| | | msg.topic = createTopic(orgTag, com) ; |
| | | msg.msg = "" ; |
| | | return msg ; |
| | | } |
| | | private MqttPubMsgSdV1 createPubMsgOfInject(String orgTag, Command com) throws Exception { |
| | | JSONObject obj = (JSONObject) com.param; |
| | | String json = obj.toJSONString(); |
| | | ComCtrlVo cvo = JSON.parseObject(json, ComCtrlVo.class); |
| | | if(cvo == null){ |
| | | throw new Exception("json转ComCtrlVo为null") ; |
| | | } |
| | | MqttPubMsgSdV1 msg = new MqttPubMsgSdV1() ; |
| | | this.setPubMsgBase(com, msg); |
| | | msg.isCacheForOffLine = false ; |
| | | msg.hasResponse = true ; |
| | | msg.address = 123 ; |
| | | msg.value = "" + (cvo.isDo?1:0); |
| | | msg.topic = createTopic(orgTag, com) ; |
| | | msg.msg = "" ; |
| | | return msg ; |
| | | } |
| | | private MqttPubMsgSdV1 createPubMsgOfIrr(String orgTag, Command com) throws Exception { |
| | | JSONObject obj = (JSONObject) com.param; |
| | | String json = obj.toJSONString(); |
| | | ComCtrlVo cvo = JSON.parseObject(json, ComCtrlVo.class); |
| | | if(cvo == null){ |
| | | throw new Exception("json转ComCtrlVo为null") ; |
| | | } |
| | | MqttPubMsgSdV1 msg = new MqttPubMsgSdV1() ; |
| | | this.setPubMsgBase(com, msg); |
| | | msg.isCacheForOffLine = false ; |
| | | msg.hasResponse = true ; |
| | | msg.address = 123 ; |
| | | msg.value = "" + (cvo.isDo?1:0); |
| | | msg.topic = createTopic(orgTag, com) ; |
| | | msg.msg = "" ; |
| | | return msg ; |
| | | } |
| | | |
| | | private void setPubMsgBase(Command com, MqttPubMsgSdV1 msg){ |
| | | msg.commandId = com.id ; |
| | | msg.deviceId = com.rtuAddr ; |
| | | msg.mqttResultSendWebUrl = com.rtuResultSendWebUrl ; |
| | | } |
| | | |
| | | private String createTopic(String orgTag, Command com){ |
| | | String topic = null ; |
| | | switch (com.code){ |
| | | case CodeSdV1.cd_Fault:{ |
| | | topic = orgTag + "/" + com.protocol + com.protocolVersion + "/" + com.rtuAddr + "/control/m4" ; |
| | | break ; |
| | | } |
| | | case CodeSdV1.cd_Stir:{ |
| | | topic = orgTag + "/" + com.protocol + com.protocolVersion + "/" + com.rtuAddr + "/control/m80" ; |
| | | break ; |
| | | } |
| | | case CodeSdV1.cd_Inject:{ |
| | | topic = orgTag + "/" + com.protocol + com.protocolVersion + "/" + com.rtuAddr + "/control/m1" ; |
| | | break ; |
| | | } |
| | | case CodeSdV1.cd_Irr:{ |
| | | topic = orgTag + "/" + com.protocol + com.protocolVersion + "/" + com.rtuAddr + "/control/m2" ; |
| | | break ; |
| | | } |
| | | default:{ |
| | | topic = null ; |
| | | break; |
| | | } |
| | | } |
| | | return topic ; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.dy.common.mw.protocol4Mqtt.pSdV1.downVos; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/6/5 15:57 |
| | | * @Description |
| | | */ |
| | | public class ComCtrlVo { |
| | | //æ¯å¦æ§å¶å¨ä½ï¼trueæ¯ï¼falseå¦ |
| | | //å¯ä»¥æ§è¡åè½ç 00ï¼01ï¼02ï¼03çå¨ä½ |
| | | public boolean isDo;// |
| | | } |
New file |
| | |
| | | 山䏿³°å®å
¬å¸æä¾æ°´è¥æºãå壤墿
ç«ãæ°è±¡ç«ãFBoxç³»ç»åè®® |
| | | |
| | | å»ºè®®æ¶æ¯ä¸»é¢è§åï¼ |
| | | åç³»ç»ï¼æºæï¼/åè®®åç§°ï¼åå®¶ï¼+çæ¬å·/设å¤ç¼å·/åè½ç»/å°å |
| | | ä¾å¦ï¼ |
| | | ym/sd1/10000/control/m4 (å
è°/å±±ä¸+çæ¬1/设å¤ç¼å·/è®¾å¤æ§å¶/å°å) |
| | |
| | | <version>2.0.7</version> |
| | | </dependency> |
| | | |
| | | <!-- MQTTæ¶æ¯ä¸é´ä»¶,è¿æ¥æ± å客æ·ç«¯ --> |
| | | <dependency> |
| | | <groupId>org.apache.commons</groupId> |
| | | <artifactId>commons-pool2</artifactId> |
| | | <version>2.9.0</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>org.eclipse.paho</groupId> |
| | | <artifactId>org.eclipse.paho.client.mqttv3</artifactId> |
| | | <version>1.2.5</version> |
| | | </dependency> |
| | | |
| | | <!--ééæ¶æ¯æ¨é--> |
| | | <dependency> |
| | | <groupId>com.aliyun</groupId> |
| | |
| | | import java.util.List; |
| | | |
| | | import com.dy.common.util.ConfigProperties; |
| | | import com.dy.common.util.IPUtils; |
| | | import com.dy.rtuMw.server.*; |
| | | import com.dy.rtuMw.server.mqtt.MqttUnit; |
| | | import com.dy.rtuMw.server.mqtt.MqttUnitConfigVo; |
| | | import com.dy.rtuMw.server.msCenter.MsCenterConfigVo; |
| | | import com.dy.rtuMw.server.msCenter.MsCenterUnit; |
| | | import com.dy.rtuMw.server.rtuData.RtuDataUnit; |
| | | import com.dy.rtuMw.server.rtuData.RtuDataUnitConfigVo; |
| | | import com.dy.rtuMw.server.tasks.FromRtuComResultConstantTask; |
| | | import com.dy.rtuMw.server.tasks.FromRtuDataConstantTask; |
| | | import com.dy.rtuMw.server.tasks.*; |
| | | import com.dy.common.mw.UnitInterface; |
| | | import com.dy.common.mw.channel.tcp.TcpConfigVo; |
| | | import com.dy.common.mw.channel.tcp.TcpUnit; |
| | |
| | | import com.dy.common.mw.protocol.ProtocolUnit; |
| | | import com.dy.common.mw.support.SupportUnit; |
| | | import com.dy.common.mw.support.SupportUnitConfigVo; |
| | | import com.dy.rtuMw.server.tasks.SendMsConstantTask; |
| | | import com.dy.rtuMw.server.tasks.RtuDownConstantTask; |
| | | import com.dy.rtuMw.resource.ResourceUnit; |
| | | import com.dy.rtuMw.resource.ResourceUnitConfigVo; |
| | | import com.dy.common.springUtil.SpringContextUtil; |
| | |
| | | //RTUè¿ç¨å级模å |
| | | UpgradeUnitConfigVo ugVo = new UpgradeUnitConfigVo(); |
| | | ugVo.enable = conf.getSetAttrBoolean(doc, "config.upgrade", "enable", null, null) ; |
| | | ugVo.openNoUpgrade = conf.getSetAttrBoolean(doc, "config.upgrade", "openNoUpgrade", null, null) ; |
| | | ugVo.lastOpenMaxGoOn = conf.getSetAttrPlusInt(doc, "config.upgrade", "lastOpenMaxGoOn", null, 5, 360000, null); |
| | | ugVo.lastOpenMaxGoOn = ugVo.lastOpenMaxGoOn * 1000 ;//åææ¯«ç§ |
| | | ugVo.noOneRtuUpgradeMaxDuration = conf.getSetAttrPlusInt(doc, "config.upgrade", "noOneRtuUpgradeMaxDuration", null, 5, 360000, null); |
| | | ugVo.noOneRtuUpgradeMaxDuration = ugVo.noOneRtuUpgradeMaxDuration * 1000 ;//åææ¯«ç§ |
| | | ugVo.runningAndIdleDuration = conf.getSetAttrPlusInt(doc, "config.upgrade", "runningAndIdleDuration", null, 5, 360000, null); |
| | | ugVo.runningAndIdleDuration = ugVo.runningAndIdleDuration * 1000 ;//åææ¯«ç§ |
| | | ugVo.failTryTimes = conf.getSetAttrPlusInt(doc, "config.upgrade", "failTryTimes", null, 0, 100, null); |
| | | ugVo.ugMaxRtuAtOnce = conf.getSetAttrPlusInt(doc, "config.upgrade", "ugMaxRtuAtOnce", null, 0, 1000000, null); |
| | | ugVo.rtuOffLineWaitDuration = conf.getSetAttrPlusInt(doc, "config.upgrade", "rtuOffLineWaitDuration", null, 1, 3600000, null); |
| | | ugVo.rtuOffLineWaitDuration = ugVo.rtuOffLineWaitDuration * 1000;//åææ¯«ç§ |
| | | ugVo.notifyStateInterval = conf.getSetAttrPlusInt(doc, "config.upgrade", "notifyStateInterval", null, 1, 300, null); |
| | | ugVo.notifyStateInterval = ugVo.notifyStateInterval * 1000;//åææ¯«ç§ |
| | | ugVo.notifyTimesAfterOver = conf.getSetAttrPlusInt(doc, "config.upgrade", "notifyTimesAfterOver", null, 0, null, null); |
| | | ugVo.showStartInfo = showStartInfo ; |
| | | AdapterImp_UpgradeUnit ugAdap = new AdapterImp_UpgradeUnit(); |
| | | ugAdap.setConfig(ugVo); |
| | | UpgradeUnit ugUnit = UpgradeUnit.getInstance(); |
| | | ugUnit.setAdapter(ugAdap); |
| | | ugUnit.start(obj -> { |
| | | }); |
| | | units.add(ugUnit) ; |
| | | if(ugVo.enable){ |
| | | ugVo.openNoUpgrade = conf.getSetAttrBoolean(doc, "config.upgrade", "openNoUpgrade", null, null) ; |
| | | ugVo.lastOpenMaxGoOn = conf.getSetAttrPlusInt(doc, "config.upgrade", "lastOpenMaxGoOn", null, 5, 360000, null); |
| | | ugVo.lastOpenMaxGoOn = ugVo.lastOpenMaxGoOn * 1000 ;//åææ¯«ç§ |
| | | ugVo.noOneRtuUpgradeMaxDuration = conf.getSetAttrPlusInt(doc, "config.upgrade", "noOneRtuUpgradeMaxDuration", null, 5, 360000, null); |
| | | ugVo.noOneRtuUpgradeMaxDuration = ugVo.noOneRtuUpgradeMaxDuration * 1000 ;//åææ¯«ç§ |
| | | ugVo.runningAndIdleDuration = conf.getSetAttrPlusInt(doc, "config.upgrade", "runningAndIdleDuration", null, 5, 360000, null); |
| | | ugVo.runningAndIdleDuration = ugVo.runningAndIdleDuration * 1000 ;//åææ¯«ç§ |
| | | ugVo.failTryTimes = conf.getSetAttrPlusInt(doc, "config.upgrade", "failTryTimes", null, 0, 100, null); |
| | | ugVo.ugMaxRtuAtOnce = conf.getSetAttrPlusInt(doc, "config.upgrade", "ugMaxRtuAtOnce", null, 0, 1000000, null); |
| | | ugVo.rtuOffLineWaitDuration = conf.getSetAttrPlusInt(doc, "config.upgrade", "rtuOffLineWaitDuration", null, 1, 3600000, null); |
| | | ugVo.rtuOffLineWaitDuration = ugVo.rtuOffLineWaitDuration * 1000;//åææ¯«ç§ |
| | | ugVo.notifyStateInterval = conf.getSetAttrPlusInt(doc, "config.upgrade", "notifyStateInterval", null, 1, 300, null); |
| | | ugVo.notifyStateInterval = ugVo.notifyStateInterval * 1000;//åææ¯«ç§ |
| | | ugVo.notifyTimesAfterOver = conf.getSetAttrPlusInt(doc, "config.upgrade", "notifyTimesAfterOver", null, 0, null, null); |
| | | ugVo.showStartInfo = showStartInfo ; |
| | | AdapterImp_UpgradeUnit ugAdap = new AdapterImp_UpgradeUnit(); |
| | | ugAdap.setConfig(ugVo); |
| | | UpgradeUnit ugUnit = UpgradeUnit.getInstance(); |
| | | ugUnit.setAdapter(ugAdap); |
| | | ugUnit.start(obj -> { |
| | | }); |
| | | units.add(ugUnit) ; |
| | | } |
| | | |
| | | |
| | | ///////////////// |
| | | //RTUä¸è¡æ°æ®å¤ç模åï¼ä»»å¡æ ï¼ |
| | |
| | | CoreUnit.addConstantTask(new RtuDownConstantTask()); |
| | | CoreUnit.addConstantTask(new FromRtuDataConstantTask()); |
| | | CoreUnit.addConstantTask(new FromRtuComResultConstantTask()); |
| | | Boolean enableMq = conf.getSetAttrBoolean(doc, "config.mqtt", "enable", null, null) ; |
| | | if(enableMq != null && enableMq.booleanValue()){ |
| | | CoreUnit.addConstantTask(new MqttSubMessageConstantTask()); |
| | | CoreUnit.addConstantTask(new MqttPubMessageConstantTask()); |
| | | CoreUnit.addConstantTask(new MqttComResultConstantTask()); |
| | | } |
| | | CoreUnit.addConstantTask(new SendMsConstantTask()); |
| | | coreUnit.start(obj -> { |
| | | }); |
| | |
| | | }); |
| | | TcpSvUrl = "[ip]:" + tcpVo.port ; |
| | | units.add(tcpUnit) ; |
| | | } |
| | | } |
| | | |
| | | ///////////////// |
| | | //MQTT模å |
| | | MqttUnitConfigVo mqVo = new MqttUnitConfigVo(); |
| | | mqVo.enable = conf.getSetAttrBoolean(doc, "config.mqtt", "enable", null, null) ; |
| | | ServerProperties.mqttUnitEnable = mqVo.enable ; |
| | | if(mqVo.enable){ |
| | | mqVo.svIp = conf.getSetAttrTxt(doc, "config.mqtt", "svIp", null, true, null) ; |
| | | if(!IPUtils.ipValid(mqVo.svIp)){ |
| | | throw new Exception("config.mqtt.svIpé
ç½®çIPä¸åæ³") ; |
| | | } |
| | | mqVo.svPort = conf.getSetAttrPlusInt(doc, "config.mqtt", "svPort", null, 5, 360000, null); |
| | | if(mqVo.svPort < 0 || mqVo.svPort > 65535){ |
| | | throw new Exception("config.mqtt.svPorté
ç½®ç端å£ä¸åæ³") ; |
| | | } |
| | | mqVo.svUserName = conf.getSetAttrTxt(doc, "config.mqtt", "svUserName", null, true, null) ; |
| | | if(mqVo.svUserName == null || mqVo.svUserName.trim().equals("")){ |
| | | throw new Exception("config.mqtt.svUserNameé
ç½®çç¨æ·åä¸åæ³") ; |
| | | }else{ |
| | | mqVo.svUserName = mqVo.svUserName.trim() ; |
| | | } |
| | | mqVo.svUserPassword = conf.getSetAttrTxt(doc, "config.mqtt", "svUserPassword", null, true, null) ; |
| | | if(mqVo.svUserPassword == null || mqVo.svUserPassword.trim().equals("")){ |
| | | throw new Exception("config.mqtt.svUserNameé
ç½®çç¨æ·å¯ç ä¸åæ³") ; |
| | | }else{ |
| | | mqVo.svUserPassword = mqVo.svUserPassword.trim() ; |
| | | } |
| | | mqVo.poolMaxSize = conf.getSetAttrPlusInt(doc, "config.mqtt", "poolMaxSize", null, 5, 360000, null); |
| | | if(mqVo.poolMaxSize <= 1 || mqVo.poolMaxSize > 1000){ |
| | | throw new Exception("config.mqtt.poolMaxSizeé
ç½®çè¿æ¥æ± è¿æ¥æå¤§æ°éä¸åæ³") ; |
| | | } |
| | | String topicAndQos = conf.getSetAttrTxt(doc, "config.mqtt", "topicAndQos", null, true, null) ; |
| | | if(topicAndQos == null || topicAndQos.trim().equals("")){ |
| | | throw new Exception("config.mqtt.topicAndQosé
ç½®ç主é¢åQosä¸åæ³") ; |
| | | }else{ |
| | | topicAndQos = topicAndQos.trim() ; |
| | | topicAndQos = topicAndQos.replaceAll("ï¼", ","); |
| | | topicAndQos = topicAndQos.replaceAll("ï¼", ";"); |
| | | String[] topicAndQosArr = topicAndQos.split(";") ; |
| | | mqVo.subTopics = new String[topicAndQosArr.length] ; |
| | | mqVo.topicsQos = new int[topicAndQosArr.length] ; |
| | | int index = 0 ; |
| | | for(String topicAndQosStr : topicAndQosArr){ |
| | | String[] tq = topicAndQosStr.split(",") ; |
| | | mqVo.subTopics[index] = tq[0].trim() ; |
| | | mqVo.topicsQos[index] = Integer.parseInt(tq[1].trim()) ; |
| | | index++ ; |
| | | } |
| | | } |
| | | mqVo.publishQos = conf.getSetAttrPlusInt(doc, "config.mqtt", "publishQos", null, 0, 3, null); |
| | | if(mqVo.publishQos < 0 || mqVo.publishQos > 3){ |
| | | throw new Exception("config.mqtt.publishQosé
ç½®ä¸åæ³") ; |
| | | } |
| | | mqVo.showStartInfo = showStartInfo ; |
| | | AdapterImp_MqttUnit mqAdapt = new AdapterImp_MqttUnit(); |
| | | mqAdapt.setConfig(mqVo); |
| | | MqttUnit mqUnit = MqttUnit.getInstance(); |
| | | mqUnit.setAdapter(mqAdapt); |
| | | mqUnit.start(obj -> { |
| | | }); |
| | | units.add(mqUnit) ; |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | |
| | | try{ |
| | | // ç¡®ä¿è¿æ®µä»£ç å°½å¯è½å¿«éæ§è¡ï¼é¿å
å½±åJVMçå
³é |
| | | log.info("ç¨åºï¼æ§å¶å°ï¼å
³éé©åç±»æ§è¡"); |
| | | Command com = new Command() ; |
| | | com.id = Command.defaultId ; |
| | | com.code = CodeLocal.stopTcpSv ; |
| | | com.type = CommandType.innerCommand ; |
| | | new CommandInnerDeaLer().deal(com) ; |
| | | Command com1 = new Command() ; |
| | | com1.id = Command.defaultId ; |
| | | com1.code = CodeLocal.stopTcpSv ; |
| | | com1.type = CommandType.innerCommand ; |
| | | new CommandInnerDeaLer().deal(com1) ; |
| | | //Thread.sleep(100L);//宿µä¸æ§è¡ |
| | | log.info("å
³éç¨åºåï¼å
³éäºTCPæå¡"); |
| | | Command com2 = new Command() ; |
| | | com2.id = Command.defaultId ; |
| | | com2.code = CodeLocal.stopMqttSv ; |
| | | com2.type = CommandType.innerCommand ; |
| | | new CommandInnerDeaLer().deal(com2) ; |
| | | |
| | | }catch (Exception e){ |
| | | log.error("ç¨åºï¼æ§å¶å°ï¼å
³éé©ååçå¼å¸¸", e); |
| | | } |
New file |
| | |
| | | package com.dy.rtuMw.server; |
| | | |
| | | |
| | | import com.dy.rtuMw.server.mqtt.MqttUnitAdapter; |
| | | import com.dy.rtuMw.server.mqtt.MqttUnitConfigVo; |
| | | |
| | | public class AdapterImp_MqttUnit implements MqttUnitAdapter { |
| | | |
| | | private MqttUnitConfigVo configVo ; |
| | | |
| | | public MqttUnitConfigVo getConfig() { |
| | | return configVo; |
| | | } |
| | | |
| | | public void setConfig(MqttUnitConfigVo configVo){ |
| | | this.configVo = configVo ; |
| | | } |
| | | |
| | | } |
| | |
| | | //ææ¥è¦åçæ¶ï¼åééåéæ¶æ¯çé´éæ¶é¿ï¼åéï¼ |
| | | public static Integer sendDingDingAlarmMsInterval = 60 ; |
| | | |
| | | //Mqttæ¨¡åæ¯å¦å¯å¨ |
| | | public static Boolean mqttUnitEnable = false ; |
| | | |
| | | } |
| | |
| | | import com.dy.common.mw.protocol.Command; |
| | | import com.dy.common.mw.protocol.rtuState.RtuStatus; |
| | | import com.dy.rtuMw.server.local.localProtocol.*; |
| | | import com.dy.rtuMw.server.mqtt.MqttUnit; |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | |
| | | return this.stopTcpSv(com) ; |
| | | }else if(code.equals(CodeLocal.recoverTcpSv)){ |
| | | return this.recoverTcpSv(com) ; |
| | | }else if(code.equals(CodeLocal.recoverMqttSv)){ |
| | | return this.stopMqttSv(com) ; |
| | | }else if(code.equals(CodeLocal.mwState)){ |
| | | return this.mwInfo(com) ; |
| | | } |
| | |
| | | return ReturnCommand.successed("å·²ç»å¯å¨æ¢å¤TCPæå¡", command.getId(), command.getCode(), null) ; |
| | | } |
| | | |
| | | /** |
| | | * 忢TCPæå¡ï¼ä¸åæ¥å
¥æ°çTCPè¿æ¥ï¼å·²ç»TCPè¿æ¥çå
¨é¨æè¿æ¥ |
| | | * @throws Exception |
| | | */ |
| | | private Command stopMqttSv(Command command) throws Exception{ |
| | | MqttUnit.getInstance().stop(new UnitCallbackInterface(){ |
| | | public void call(Object obj) throws Exception { |
| | | } |
| | | }); |
| | | return ReturnCommand.successed("å·²ç»å¯å¨åæ¢Mqttæå¡", command.getId(), command.getCode(), null) ; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * æ¢å¤TCPæå¡ï¼æ¥å
¥æ°çTCPè¿æ¥ |
| | | * @throws Exception |
| | | */ |
| | | private Command recoverMqttSv(Command command) throws Exception{ |
| | | MqttUnit.getInstance().recover(new UnitCallbackInterface(){ |
| | | public void call(Object obj) throws Exception { |
| | | } |
| | | }); |
| | | return ReturnCommand.successed("å·²ç»å¯å¨æ¢å¤Mqttæå¡", command.getId(), command.getCode(), null) ; |
| | | } |
| | | |
| | | |
| | | /** |
| | |
| | | |
| | | public static final String recoverTcpSv = "LCD0112" ;//éå¯TCPæå¡ï¼æ¥å
¥æ°çTCPè¿æ¥ |
| | | |
| | | public static final String stopMqttSv = "LCD0114" ;//忢Mqttæå¡ |
| | | |
| | | public static final String recoverMqttSv = "LCD0116" ;//éå¯Mqttæå¡ |
| | | |
| | | public static final String mwState = "LCD0200" ;//å¾å°éä¿¡ä¸é´ä»¶è¿è¡ä¿¡æ¯ |
| | | |
| | | } |
New file |
| | |
| | | package com.dy.rtuMw.server.mqtt; |
| | | |
| | | import com.dy.common.queue.Node; |
| | | import com.dy.common.queue.Queue; |
| | | import com.dy.rtuMw.server.ServerProperties; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/6/5 15:05 |
| | | * @Description |
| | | */ |
| | | public class MqttComResultCache { |
| | | |
| | | //TCPä¸è¡å½ä»¤ç¼åéå |
| | | private static Queue cacheQueue = new Queue("MqttComResultCache") ; |
| | | |
| | | private static MqttComResultCache instance = new MqttComResultCache() ; |
| | | |
| | | private MqttComResultCache(){ |
| | | cacheQueue.setLimit(ServerProperties.cacheUpDownDataWarnCount, ServerProperties.cacheUpDownDataMaxCount); |
| | | } |
| | | |
| | | public static MqttComResultCache getInstance(){ |
| | | return instance ; |
| | | } |
| | | |
| | | /** |
| | | * ç¼åèç¹ |
| | | * @param node node |
| | | * @throws Exception å¼å¸¸ |
| | | */ |
| | | public static void cacheMqttComResult(MqttComResultNode node) throws Exception{ |
| | | if(node != null && node.obj != null){ |
| | | cacheQueue.pushHead(node); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å¾å°ç¬¬ä¸ä¸ªèç¹ |
| | | * @return Node |
| | | */ |
| | | public static Node getFirstQueueNode(){ |
| | | return cacheQueue.getFirstNode() ; |
| | | } |
| | | |
| | | /** |
| | | * å¾å°æåä¸ä¸ªèç¹ |
| | | * @return Node |
| | | */ |
| | | public static Node getLastQueueNode(){ |
| | | return cacheQueue.getLastNode() ; |
| | | } |
| | | |
| | | /** |
| | | * ç§»é¤èç¹ |
| | | * @param node |
| | | */ |
| | | public static void removeNode(Node node){ |
| | | cacheQueue.remove(node); |
| | | } |
| | | |
| | | /** |
| | | * ç¼åçèç¹æ° |
| | | * @Return ç¼åèç¹æ° |
| | | */ |
| | | public static Integer size(){ |
| | | return cacheQueue.size() ; |
| | | } |
| | | } |
New file |
| | |
| | | package com.dy.rtuMw.server.mqtt; |
| | | |
| | | import com.dy.common.mw.protocol4Mqtt.MqttSubMsg; |
| | | import com.dy.common.queue.NodeObj; |
| | | import com.dy.common.springUtil.SpringContextUtil; |
| | | import com.dy.common.threadPool.ThreadPool; |
| | | import com.dy.common.threadPool.TreadPoolFactory; |
| | | import com.dy.rtuMw.web.comResult.CommandResultDeal; |
| | | import org.apache.logging.log4j.LogManager; |
| | | import org.apache.logging.log4j.Logger; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/6/5 15:06 |
| | | * @Description |
| | | */ |
| | | public class MqttComResultNode implements NodeObj { |
| | | |
| | | private static final Logger log = LogManager.getLogger(MqttComResultNode.class.getName()); |
| | | |
| | | public Object obj ;//æ°æ® |
| | | |
| | | public MqttComResultNode(Object obj){ |
| | | this.obj = obj ; |
| | | } |
| | | /** |
| | | * èªå·±å¤çèªå·± |
| | | * @return |
| | | */ |
| | | public boolean dealSelf(){ |
| | | try { |
| | | ThreadPool.Pool pool = TreadPoolFactory.getThreadPoolLong() ; |
| | | pool.putJob(new ThreadPool.Job() { |
| | | public void execute() { |
| | | if(obj != null){ |
| | | if(obj instanceof MqttSubMsg){ |
| | | CommandResultDeal deal = SpringContextUtil.getBean(CommandResultDeal.class) ; |
| | | deal.deal((MqttSubMsg)obj); |
| | | } |
| | | } |
| | | } |
| | | @Override |
| | | public void destroy(){ |
| | | } |
| | | @Override |
| | | public boolean isDestroy(){ |
| | | return false ; |
| | | } |
| | | |
| | | }); |
| | | } catch (Exception e) { |
| | | log.error("å¨RtuComResultNodeå
åçå¼å¸¸", e); |
| | | } |
| | | return true ; |
| | | } |
| | | |
| | | |
| | | } |
New file |
| | |
| | | package com.dy.rtuMw.server.mqtt; |
| | | |
| | | import com.dy.common.mw.channel.mqtt.MqttClientPool; |
| | | import org.apache.logging.log4j.LogManager; |
| | | import org.apache.logging.log4j.Logger; |
| | | import org.eclipse.paho.client.mqttv3.MqttClient; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/6/4 14:54 |
| | | * @Description |
| | | */ |
| | | public class MqttManager { |
| | | |
| | | private static final Logger log = LogManager.getLogger(MqttManager.class.getName()); |
| | | |
| | | private static final MqttManager INSTANCE = new MqttManager(); |
| | | |
| | | private MqttUnitConfigVo configVo ; |
| | | |
| | | private MqttClientPool pool; |
| | | |
| | | private MqttManager(){ |
| | | } |
| | | |
| | | public static MqttManager getInstance() { |
| | | return MqttManager.INSTANCE; |
| | | } |
| | | /** |
| | | * åå§åé
ç½®ä¿¡æ¯ |
| | | */ |
| | | public void initOption(MqttUnitConfigVo configVo) { |
| | | this.configVo = configVo; |
| | | } |
| | | |
| | | public void start()throws Exception{ |
| | | String URL = "tcp://" + this.configVo.svIp + ":" + this.configVo.svPort; |
| | | this.pool = new MqttClientPool(URL, this.configVo.svUserName, this.configVo.svUserPassword, this.configVo.poolMaxSize); |
| | | if(this.pool.isClose()){ |
| | | throw new Exception("Mqttè¿æ¥æ± åå§å失败"); |
| | | } |
| | | MqttClient clientSub = null ; |
| | | try { |
| | | clientSub = pool.popClient();//æ°å建ä¸ä¸ªClientæ¶ï¼æ¤Clientå®é
å»è¿æ¥MQTTæå¡å¨ï¼å¦æè¿æ¥ä¸ä¸ï¼å°±ä¼æåºå¼å¸¸ |
| | | }catch (Exception e){ |
| | | throw new Exception("Mqttè¿æ¥æ± è·å¾è¿æ¥å¼å¸¸", e); |
| | | } |
| | | if(clientSub == null || !clientSub.isConnected()){ |
| | | throw new Exception("Mqttè¿æ¥æ± è·å¾è®¢é
è¿æ¥ä¸å¯ç¨"); |
| | | } |
| | | for(int i = 0; i < this.configVo.subTopics.length; i++){ |
| | | clientSub.subscribe(this.configVo.subTopics[i], this.configVo.topicsQos[i], new MqttMessageListener()); |
| | | } |
| | | } |
| | | |
| | | public void stop()throws Exception{ |
| | | if(this.pool != null){ |
| | | // å
³éè¿æ¥æ± |
| | | this.pool.close(); |
| | | } |
| | | } |
| | | |
| | | public MqttClient popMqttClient() throws Exception{ |
| | | return this.pool.popClient(); |
| | | } |
| | | |
| | | public void pushMqttClient(MqttClient client) { |
| | | this.pool.pushClient(client); |
| | | } |
| | | |
| | | public void publishMsg(MqttClient client, String topic, byte[] msg) throws Exception{ |
| | | client.publish(topic, msg, this.configVo.publishQos, false); |
| | | } |
| | | |
| | | public void publishMsg(MqttClient client, String topic, String msg) throws Exception{ |
| | | byte[] bs = msg.getBytes("UTF-8") ; |
| | | client.publish(topic, bs, this.configVo.publishQos, false); |
| | | } |
| | | |
| | | public boolean poolIsClose(){ |
| | | if(this.pool == null){ |
| | | return true; |
| | | } |
| | | return this.pool.isClose(); |
| | | } |
| | | } |
New file |
| | |
| | | package com.dy.rtuMw.server.mqtt; |
| | | |
| | | import com.dy.common.mw.protocol4Mqtt.MqttMsgParser; |
| | | import com.dy.common.mw.protocol4Mqtt.MqttPubMsg; |
| | | import com.dy.common.mw.protocol4Mqtt.MqttSubMsg; |
| | | import com.dy.common.util.Callback; |
| | | import org.eclipse.paho.client.mqttv3.* ; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/6/4 15:52 |
| | | * @Description |
| | | */ |
| | | public class MqttMessageListener implements IMqttMessageListener{ |
| | | @Override |
| | | public void messageArrived(String topic, MqttMessage msg) throws Exception { |
| | | MqttMsgParser parser = new MqttMsgParser() ; |
| | | MqttSubMsg subMsg = parser.parseSubMsg(topic, msg) ; |
| | | this.nextDeal(subMsg); |
| | | } |
| | | private void nextDeal(MqttSubMsg subMsg)throws Exception { |
| | | subMsg.action(new Callback() { |
| | | @Override |
| | | public void call(Object obj) { |
| | | MqttSubMsg subMs = (MqttSubMsg) obj ; |
| | | MqttPubMsg pubMs = MqttPubMsgCache.matchFromTail(subMs) ; |
| | | if(pubMs != null){ |
| | | subMs.mqttResultSendWebUrl = pubMs.mqttResultSendWebUrl ; |
| | | subMs.commandId = pubMs.commandId ; |
| | | try { |
| | | MqttComResultCache.getInstance().cacheMqttComResult(new MqttComResultNode(subMs)); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | try{ |
| | | MqttSubMsgCache.getInstance().cacheMsg(new MqttSubMsgNode(subMsg)); |
| | | }catch (Exception e){ |
| | | } |
| | | } |
| | | @Override |
| | | public void call(Object... objs) { |
| | | } |
| | | @Override |
| | | public void exception(Exception e) { |
| | | } |
| | | }); |
| | | } |
| | | } |
New file |
| | |
| | | package com.dy.rtuMw.server.mqtt; |
| | | |
| | | import com.dy.common.mw.protocol4Mqtt.MqttPubMsg; |
| | | import com.dy.common.mw.protocol4Mqtt.MqttSubMsg; |
| | | import com.dy.common.queue.Node; |
| | | import com.dy.common.queue.Queue; |
| | | import com.dy.rtuMw.server.ServerProperties; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/6/4 17:24 |
| | | * @Description |
| | | */ |
| | | public class MqttPubMsgCache { |
| | | |
| | | //TCPä¸è¡å½ä»¤ç¼åéå |
| | | private static Queue cacheQueue = new Queue("mqttPubMsgCache") ; |
| | | |
| | | private static MqttPubMsgCache instance = new MqttPubMsgCache() ; |
| | | |
| | | private MqttPubMsgCache(){ |
| | | cacheQueue.setLimit(ServerProperties.cacheUpDownDataWarnCount, ServerProperties.cacheUpDownDataMaxCount); |
| | | } |
| | | |
| | | public static MqttPubMsgCache getInstance(){ |
| | | return instance ; |
| | | } |
| | | |
| | | |
| | | public static Integer info(){ |
| | | Integer comTotalDown = 0 ;//ç¼åçä¸è¡å½ä»¤æ»æ° |
| | | MqttPubMsgNode obj ; |
| | | Node node = cacheQueue.getFirstNode() ; |
| | | while(node != null && node.obj != null){ |
| | | obj = (MqttPubMsgNode)node.obj; |
| | | if(!obj.onceReceivedResult){ |
| | | comTotalDown ++ ; |
| | | } |
| | | } |
| | | return comTotalDown ; |
| | | } |
| | | |
| | | /** |
| | | * ç¼åå½ä»¤ |
| | | * @param result |
| | | * @throws Exception |
| | | */ |
| | | public static void cacheCommand(MqttPubMsg result) throws Exception{ |
| | | if(result != null){ |
| | | cacheQueue.pushHead(new MqttPubMsgNode(result)); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å¹é
å½ä»¤ç»æ |
| | | * @param subMsg |
| | | * @return |
| | | */ |
| | | public static MqttPubMsg matchFromHead(MqttSubMsg subMsg){ |
| | | MqttPubMsg pubMsg = null ; |
| | | MqttPubMsgNode obj = null ; |
| | | Node node = cacheQueue.getFirstNode() ; |
| | | while(node != null && node.obj != null){ |
| | | obj = (MqttPubMsgNode)node.obj; |
| | | pubMsg = obj.result ; |
| | | if(pubMsg != null |
| | | && subMsg.subMsgMatchPubMsg(pubMsg)){ |
| | | obj.onceReceivedResult = true ;//æ è¯å·²ç»æ¶å°å½ä»¤ç»æ |
| | | return pubMsg; |
| | | }else{ |
| | | node = node.next ; |
| | | } |
| | | } |
| | | return null ; |
| | | } |
| | | |
| | | /** |
| | | * å¹é
å½ä»¤ç»æ |
| | | * @param subMsg |
| | | * @return |
| | | */ |
| | | public static MqttPubMsg matchFromTail(MqttSubMsg subMsg){ |
| | | MqttPubMsg pubMsg = null ; |
| | | MqttPubMsgNode obj = null ; |
| | | Node node = cacheQueue.getLastNode() ; |
| | | while(node != null && node.obj != null){ |
| | | obj = (MqttPubMsgNode)node.obj; |
| | | pubMsg = obj.result ; |
| | | if(pubMsg != null |
| | | && subMsg.subMsgMatchPubMsg(pubMsg)){ |
| | | obj.onceReceivedResult = true ;//æ è¯å·²ç»æ¶å°å½ä»¤ç»æ |
| | | return pubMsg; |
| | | }else{ |
| | | node = node.pre ; |
| | | } |
| | | } |
| | | return null ; |
| | | } |
| | | |
| | | /** |
| | | * å¾å°ç¬¬ä¸ä¸ªèç¹ |
| | | * @return |
| | | */ |
| | | public static Node getFirstQueueNode(){ |
| | | return cacheQueue.getFirstNode() ; |
| | | } |
| | | |
| | | /** |
| | | * å¾å°æåä¸ä¸ªèç¹ |
| | | * @return |
| | | */ |
| | | public static Node getLastQueueNode(){ |
| | | return cacheQueue.getLastNode() ; |
| | | } |
| | | |
| | | /** |
| | | * ç§»é¤èç¹ |
| | | * @param node |
| | | */ |
| | | public static void removeNode(Node node){ |
| | | cacheQueue.remove(node); |
| | | } |
| | | |
| | | /** |
| | | * ç¼åçèç¹æ° |
| | | * @Return ç¼åèç¹æ° |
| | | */ |
| | | public static Integer size(){ |
| | | return cacheQueue.size() ; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.dy.rtuMw.server.mqtt; |
| | | |
| | | import com.dy.common.mw.protocol4Mqtt.MqttPubMsg; |
| | | import com.dy.common.queue.NodeObj; |
| | | import com.dy.rtuMw.server.ServerProperties; |
| | | import org.apache.logging.log4j.LogManager; |
| | | import org.apache.logging.log4j.Logger; |
| | | import org.eclipse.paho.client.mqttv3.MqttClient; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/6/4 17:25 |
| | | * @Description |
| | | */ |
| | | public class MqttPubMsgNode implements NodeObj { |
| | | |
| | | private static Logger log = LogManager.getLogger(MqttPubMsgNode.class.getName()); |
| | | |
| | | public MqttPubMsg result ;//ä¸è¡å½ä»¤ |
| | | public Long cachTime ;//ç¼åæ¶å» |
| | | public boolean onceReceivedResult ;//å·²ç»æ¶å°å½ä»¤åºç |
| | | |
| | | |
| | | public MqttPubMsgNode(MqttPubMsg result){ |
| | | this.result = result ; |
| | | this.cachTime = System.currentTimeMillis() ; |
| | | this.onceReceivedResult = false ; |
| | | } |
| | | |
| | | /** |
| | | * èªå·±å¤çèªå·± |
| | | * @param now |
| | | * @return |
| | | */ |
| | | public boolean dealSelf(Long now){ |
| | | if(this.onceReceivedResult){ |
| | | //å·²ç»æ¶å°å½ä»¤ç»æ |
| | | //è®°å½ç¶æ |
| | | //RtuStatusDealer.commandSuccess(this.result.rtuAddr, this.result.downCode, this.result.downCodeName); |
| | | return true ; |
| | | } |
| | | boolean noConnect2MqSv = false ; |
| | | MqttManager mqttManager = MqttManager.getInstance() ; |
| | | MqttClient mqttClient = null ; |
| | | |
| | | noConnect2MqSv = mqttManager.poolIsClose() ; |
| | | if(noConnect2MqSv){ |
| | | //æªæ¾è¿æ¥MQTTæå¡å¨ |
| | | return this.decideRemoveNodeFromCach(now) ; |
| | | }else{ |
| | | try { |
| | | //妿ç½ç»ä¸å¥½ææç½ï¼æ¤å¤ç¨æ¶è¾é¿ |
| | | mqttClient = mqttManager.popMqttClient() ; |
| | | if(mqttClient == null || !mqttClient.isConnected()){ |
| | | noConnect2MqSv = false ; |
| | | } |
| | | }catch (Exception e){ |
| | | log.error("è·åMQTT客æ·ç«¯å¤±è´¥", e); |
| | | } |
| | | } |
| | | if(noConnect2MqSv){ |
| | | //æªæ¾è¿æ¥MQTTæå¡å¨ |
| | | return this.decideRemoveNodeFromCach(now) ; |
| | | }else{ |
| | | if(mqttClient != null && mqttClient.isConnected()){ |
| | | try { |
| | | mqttManager.publishMsg(mqttClient, this.result.topic, this.result.msg); |
| | | log.info("åå¸MQTTæ¶æ¯ï¼ä¸»é¢=" + this.result.topic + "ï¼" + this.result.msg); |
| | | }catch (Exception e){ |
| | | log.error("MQTTå叿¶æ¯å¤±è´¥ï¼ä¸»é¢=" + this.result.topic + "ï¼" , e); |
| | | }finally { |
| | | mqttManager.pushMqttClient(mqttClient); |
| | | } |
| | | return false ; |
| | | }else{ |
| | | //æªæ¾è¿æ¥MQTTæå¡å¨ |
| | | return this.decideRemoveNodeFromCach(now) ; |
| | | } |
| | | } |
| | | } |
| | | |
| | | private boolean decideRemoveNodeFromCach(Long now){ |
| | | if(!this.result.isCacheForOffLine){ |
| | | //ä¸å¨çº¿å½ä»¤ä¸ç¼å |
| | | return true ; |
| | | }else{ |
| | | //ä¸å¨çº¿å½ä»¤ç¼å |
| | | if(now - this.cachTime >= ServerProperties.offLineCacheTimeout){ |
| | | //ç¼åè¶
æ¶ |
| | | return true ; |
| | | } |
| | | } |
| | | return false ; |
| | | } |
| | | } |
New file |
| | |
| | | package com.dy.rtuMw.server.mqtt; |
| | | |
| | | import com.dy.common.mw.protocol4Mqtt.MqttSubMsg; |
| | | import com.dy.common.queue.Node; |
| | | import com.dy.common.queue.Queue; |
| | | import com.dy.rtuMw.server.ServerProperties; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/6/4 16:13 |
| | | * @Description |
| | | */ |
| | | public class MqttSubMsgCache { |
| | | |
| | | //TCPä¸è¡å½ä»¤ç¼åéå |
| | | private static Queue cacheQueue = new Queue("mqttSubMsgCache") ; |
| | | |
| | | private static MqttSubMsgCache instance = new MqttSubMsgCache() ; |
| | | |
| | | private MqttSubMsgCache(){ |
| | | cacheQueue.setLimit(ServerProperties.cacheUpDownDataWarnCount, ServerProperties.cacheUpDownDataMaxCount); |
| | | } |
| | | |
| | | public static MqttSubMsgCache getInstance(){ |
| | | return instance ; |
| | | } |
| | | |
| | | /** |
| | | * ç¼å订é
çæ¶æ¯ |
| | | * @param node æ¶å°çæ¶æ¯ |
| | | * @throws Exception |
| | | */ |
| | | public static void cacheMsg(MqttSubMsgNode node) throws Exception{ |
| | | cacheQueue.pushHead(node); |
| | | } |
| | | /** |
| | | * å¾å°ç¬¬ä¸ä¸ªèç¹ |
| | | * @return |
| | | */ |
| | | public static Node getFirstQueueNode(){ |
| | | return cacheQueue.getFirstNode() ; |
| | | } |
| | | |
| | | /** |
| | | * å¾å°æåä¸ä¸ªèç¹ |
| | | * @return |
| | | */ |
| | | public static Node getLastQueueNode(){ |
| | | // è°ç¨cacheQueueçgetLastNodeæ¹æ³ï¼è¿åæåä¸ä¸ªèç¹ |
| | | return cacheQueue.getLastNode() ; |
| | | } |
| | | |
| | | /** |
| | | * ç§»é¤èç¹ |
| | | * @param node |
| | | */ |
| | | public static void removeNode(Node node){ |
| | | cacheQueue.remove(node); |
| | | } |
| | | |
| | | /** |
| | | * ç¼åçèç¹æ° |
| | | * @Return ç¼åèç¹æ° |
| | | */ |
| | | public static Integer size(){ |
| | | return cacheQueue.size() ; |
| | | } |
| | | } |
New file |
| | |
| | | package com.dy.rtuMw.server.mqtt; |
| | | |
| | | import com.dy.common.mw.protocol4Mqtt.MqttSubMsg; |
| | | import com.dy.common.queue.NodeObj; |
| | | import com.dy.common.threadPool.ThreadPool; |
| | | import com.dy.common.threadPool.TreadPoolFactory; |
| | | import com.dy.rtuMw.server.rtuData.TaskPool; |
| | | import com.dy.rtuMw.server.rtuData.TaskSurpport; |
| | | import org.apache.logging.log4j.LogManager; |
| | | import org.apache.logging.log4j.Logger; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/6/4 16:14 |
| | | * @Description |
| | | */ |
| | | public class MqttSubMsgNode implements NodeObj { |
| | | |
| | | private static Logger log = LogManager.getLogger(MqttSubMsgNode.class.getName()); |
| | | |
| | | protected MqttSubMsg msg; |
| | | |
| | | public MqttSubMsgNode(MqttSubMsg obj){ |
| | | this.msg = obj ; |
| | | } |
| | | |
| | | /** |
| | | * èªå·±å¤çèªå·± |
| | | * @return |
| | | */ |
| | | public boolean dealSelf(){ |
| | | try { |
| | | ThreadPool.Pool pool = TreadPoolFactory.getThreadPoolLong() ; |
| | | pool.putJob(new ThreadPool.Job() { |
| | | public void execute() { |
| | | if(msg != null && msg.valid()){ |
| | | TaskSurpport task = null ; |
| | | try{ |
| | | task = TaskPool.popTask() ; |
| | | if(task != null){ |
| | | task.execute(msg); |
| | | }else{ |
| | | log.error("æªå¾å°Mq订é
æ¶æ¯å¤çä»»å¡ï¼"); |
| | | } |
| | | }catch(Exception e){ |
| | | log.error("Mq订é
æ¶æ¯ä»»å¡æ± å¤çæ°æ®æ¶åçå¼å¸¸", e); |
| | | }finally { |
| | | if(task != null){ |
| | | TaskPool.freeAndCleanTask(task); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | @Override |
| | | public void destroy(){ |
| | | } |
| | | @Override |
| | | public boolean isDestroy(){ |
| | | return false ; |
| | | } |
| | | |
| | | }); |
| | | } catch (Exception e) { |
| | | log.error("å¨MqttSubMsgObjå
åçå¼å¸¸", e); |
| | | } |
| | | return true ; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.dy.rtuMw.server.mqtt; |
| | | |
| | | import com.dy.common.mw.UnitAdapterInterface; |
| | | import com.dy.common.mw.UnitCallbackInterface; |
| | | import com.dy.common.mw.UnitInterface; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/6/4 14:46 |
| | | * @Description |
| | | */ |
| | | public class MqttUnit implements UnitInterface { |
| | | |
| | | private static MqttUnit instance = new MqttUnit() ; |
| | | |
| | | public static MqttUnitAdapter adapter ; |
| | | public static MqttUnitConfigVo confVo ; |
| | | |
| | | private static MqttManager manager ; |
| | | private MqttUnit(){} ; |
| | | |
| | | public static MqttUnit getInstance(){ |
| | | return instance ; |
| | | } |
| | | |
| | | @Override |
| | | public void setAdapter(UnitAdapterInterface adapter) throws Exception { |
| | | if(adapter == null){ |
| | | throw new Exception("Mqttæ¶æ¯æ¨¡åéé
å¨å¯¹è±¡ä¸è½ä¸ºç©ºï¼") ; |
| | | } |
| | | MqttUnit.adapter = (MqttUnitAdapter)adapter ; |
| | | MqttUnit.confVo = MqttUnit.adapter.getConfig() ; |
| | | if(MqttUnit.confVo == null){ |
| | | throw new Exception("Mqttæ¶æ¯æ¨¡åé
置对象ä¸è½ä¸ºç©ºï¼") ; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * åå§å |
| | | */ |
| | | @Override |
| | | public void start(UnitCallbackInterface callback) throws Exception { |
| | | if(confVo.enable){ |
| | | manager = MqttManager.getInstance() ; |
| | | manager.initOption(confVo); |
| | | manager.start(); |
| | | callback.call(null) ; |
| | | System.out.println("Mqttæ¶æ¯æ¨¡åæåå¯å¨"); |
| | | }else{ |
| | | System.out.println("Mqttæ¶æ¯æ¨¡åé
ç½®ä¸å¯å¨"); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void stop(UnitCallbackInterface callback) throws Exception { |
| | | if(manager != null){ |
| | | manager.stop(); |
| | | } |
| | | callback.call(null) ; |
| | | } |
| | | |
| | | public void recover(UnitCallbackInterface callback) throws Exception { |
| | | this.start(callback) ; |
| | | callback.call(null) ; |
| | | } |
| | | |
| | | } |
| | | |
New file |
| | |
| | | package com.dy.rtuMw.server.mqtt; |
| | | |
| | | import com.dy.common.mw.UnitAdapterInterface; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/6/4 14:47 |
| | | * @Description |
| | | */ |
| | | public interface MqttUnitAdapter extends UnitAdapterInterface { |
| | | |
| | | MqttUnitConfigVo getConfig(); |
| | | |
| | | } |
New file |
| | |
| | | package com.dy.rtuMw.server.mqtt; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/6/4 14:46 |
| | | * @Description |
| | | */ |
| | | public class MqttUnitConfigVo { |
| | | public Boolean showStartInfo ; |
| | | public Boolean enable ;//æ¨¡åæ¯å¦å¯å¨ |
| | | public String svIp ;// |
| | | public Integer svPort ;// |
| | | public String svUserName ;// |
| | | public String svUserPassword ;// |
| | | public Integer poolMaxSize ;// |
| | | public String[] subTopics ;//订é
çä¸»é¢ |
| | | public int[] topicsQos ;////订é
主é¢çQos |
| | | public int publishQos ;////å叿¶æ¯çQos |
| | | |
| | | public MqttUnitConfigVo(){ |
| | | this.enable = false ; |
| | | this.svIp = "127.0.0.1" ; |
| | | this.svPort = 1883 ; |
| | | this.svUserName = "dyyjy" ; |
| | | this.svUserPassword = "Dyyjy2025,;.abc!@#" ; |
| | | this.poolMaxSize = 10 ; |
| | | this.publishQos = 1 ; |
| | | } |
| | | } |
New file |
| | |
| | | package com.dy.rtuMw.server.rtuData; |
| | | |
| | | import com.dy.common.mw.protocol4Mqtt.pSdV1.MqttSubMsgSdV1; |
| | | import com.dy.rtuMw.server.rtuData.pSdV1.TkFindPSdV1; |
| | | import org.apache.logging.log4j.LogManager; |
| | | import org.apache.logging.log4j.Logger; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/6/4 17:05 |
| | | * @Description |
| | | */ |
| | | public class TkMqttData extends TaskSurpport { |
| | | |
| | | private static Logger log = LogManager.getLogger(TkMqttData.class.getName()) ; |
| | | |
| | | //ç±»IDï¼ä¸å®ä¸Tree.xmlé
ç½®æä»¶ä¸é
ç½®ä¸è´ |
| | | public static final String taskId = "TkMqttData" ; |
| | | |
| | | /** |
| | | * æ§è¡èç¹ä»»å¡ |
| | | * @param data éè¦å¤ççæ°æ® |
| | | */ |
| | | @Override |
| | | public void execute(Object data) { |
| | | if(data == null){ |
| | | log.error("严éé误ï¼Mqtt订é
æ¶æ¯æ°æ®ä¸ºç©ºï¼" ); |
| | | }else{ |
| | | if(data instanceof MqttSubMsgSdV1){ |
| | | this.toNextOneTask(data, TkFindPSdV1.taskId); |
| | | }else{ |
| | | log.error("严éé误ï¼è¯¥æ°æ®ç±»åï¼" + data.getClass().getName() + "ï¼ï¼æ¥æ¶æ°æ®ä»»å¡è¿æªå®ç°ï¼" ); |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | |
| | | package com.dy.rtuMw.server.rtuData; |
| | | |
| | | import com.dy.common.mw.protocol4Mqtt.MqttSubMsg; |
| | | import com.dy.common.mw.protocol.Data; |
| | | import org.apache.logging.log4j.LogManager; |
| | | import org.apache.logging.log4j.Logger; |
| | |
| | | log.error("严éé误ï¼RTUä¸è¡æ°æ®ä¸ºç©ºï¼" ); |
| | | }else{ |
| | | if(data instanceof Data){ |
| | | this.toNextTasks(data); |
| | | this.toNextOneTask(data, TkRtuData.taskId); |
| | | }else if(data instanceof MqttSubMsg){ |
| | | this.toNextOneTask(data, TkMqttData.taskId); |
| | | }else{ |
| | | log.error("严éé误ï¼è¯¥æ°æ®ç±»åï¼" + data.getClass().getName() + "ï¼ï¼æ¥æ¶æ°æ®ä»»å¡è¿æªå®ç°ï¼" ); |
| | | } |
New file |
| | |
| | | package com.dy.rtuMw.server.rtuData.pSdV1; |
| | | |
| | | import com.dy.common.mw.protocol4Mqtt.pSdV1.MqttSubMsgSdV1; |
| | | import com.dy.rtuMw.server.rtuData.TaskSurpport; |
| | | import org.apache.logging.log4j.LogManager; |
| | | import org.apache.logging.log4j.Logger; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/6/5 13:43 |
| | | * @Description |
| | | */ |
| | | public class TkFindPSdV1 extends TaskSurpport { |
| | | |
| | | private static Logger log = LogManager.getLogger(TkFindPSdV1.class.getName()) ; |
| | | |
| | | //ç±»IDï¼ä¸å®ä¸Tree.xmlé
ç½®æä»¶ä¸é
ç½®ä¸è´ |
| | | public static final String taskId = "TkFindPSdV1" ; |
| | | |
| | | /** |
| | | * æ§è¡èç¹ä»»å¡ |
| | | * @param data éè¦å¤ççæ°æ® |
| | | */ |
| | | @Override |
| | | public void execute(Object data) { |
| | | //åé¢çä»»å¡å·²ç»å¤æäºdataä¸ä¸ºç©º |
| | | MqttSubMsgSdV1 msg = (MqttSubMsgSdV1)data ; |
| | | log.info(msg.toString()); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.dy.rtuMw.server.tasks; |
| | | |
| | | import com.dy.common.mw.core.CoreTask; |
| | | import com.dy.common.queue.Node; |
| | | import com.dy.rtuMw.server.mqtt.MqttComResultCache; |
| | | import com.dy.rtuMw.server.mqtt.MqttComResultNode; |
| | | import org.apache.logging.log4j.LogManager; |
| | | import org.apache.logging.log4j.Logger; |
| | | |
| | | /** |
| | | * 对RTUä¸è¡æ°æ®è¿è¡ä¸å¡å¤ç |
| | | */ |
| | | public class MqttComResultConstantTask extends CoreTask { |
| | | private static final Logger log = LogManager.getLogger(MqttComResultConstantTask.class.getName()); |
| | | |
| | | /** |
| | | * å¨å线ç¨ç¯å¢ä¸è¿è¡ |
| | | */ |
| | | @Override |
| | | public Integer execute() { |
| | | try{ |
| | | dealRtuComResult() ; |
| | | }catch(Exception e){ |
| | | log.error(e); |
| | | } |
| | | return MqttComResultCache.size()>0?0:1 ; |
| | | } |
| | | /** |
| | | * å¤çä¸è¡å½ä»¤ç»æ |
| | | */ |
| | | public void dealRtuComResult() { |
| | | Node first = MqttComResultCache.getFirstQueueNode() ; |
| | | if(first != null){ |
| | | Node last = MqttComResultCache.getLastQueueNode() ; |
| | | while (last != null){ |
| | | last = this.doDealRtuComResult(first, last); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å¤çç¼åçä¸è¡æ°æ®èç¹ |
| | | * @param first 第ä¸ä¸ªèç¹ |
| | | * @param last æåä¸ä¸ªèç¹ |
| | | */ |
| | | private Node doDealRtuComResult(Node first, Node last){ |
| | | if(last != null){ |
| | | //å¨dealNodeæ¹æ³ä¸ï¼å¯è½è¦ælastä»éåä¸ç§»é¤ï¼è¿æ¶last.preä¸ºç©ºï¼æä»¥æåælast.preååºæ¥ |
| | | Node pre = last.pre ; |
| | | dealNode(last) ; |
| | | if(first != last){ |
| | | return pre ; |
| | | }else{ |
| | | //忢 |
| | | return null ; |
| | | } |
| | | }else{ |
| | | return null ; |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * å¤çä¸ä¸ªèç¹ |
| | | * @param node èç¹ |
| | | */ |
| | | private void dealNode(Node node){ |
| | | if(node != null && node.obj != null){ |
| | | MqttComResultNode obj = (MqttComResultNode)node.obj ; |
| | | obj.dealSelf() ; |
| | | MqttComResultCache.removeNode(node); |
| | | } |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.dy.rtuMw.server.tasks; |
| | | |
| | | import com.dy.common.mw.core.CoreTask; |
| | | import com.dy.common.queue.Node; |
| | | import com.dy.rtuMw.server.mqtt.MqttPubMsgCache; |
| | | import com.dy.rtuMw.server.mqtt.MqttPubMsgNode; |
| | | import org.apache.logging.log4j.LogManager; |
| | | import org.apache.logging.log4j.Logger; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/6/4 16:25 |
| | | * @Description |
| | | */ |
| | | public class MqttPubMessageConstantTask extends CoreTask { |
| | | private static final Logger log = LogManager.getLogger(MqttPubMessageConstantTask.class.getName()); |
| | | |
| | | /** |
| | | * å¨å线ç¨ç¯å¢ä¸è¿è¡ |
| | | */ |
| | | @Override |
| | | public Integer execute() { |
| | | try{ |
| | | dealMqMsg() ; |
| | | }catch(Exception e){ |
| | | log.error(e); |
| | | } |
| | | return MqttPubMsgCache.size()>0?0:1 ; |
| | | } |
| | | /** |
| | | * å¤çMQTT订é
çæ¶æ¯ |
| | | */ |
| | | public void dealMqMsg() { |
| | | Node first = MqttPubMsgCache.getFirstQueueNode() ; |
| | | if(first != null){ |
| | | Node last = MqttPubMsgCache.getLastQueueNode() ; |
| | | while (last != null){ |
| | | last = this.doDealMqMsg(System.currentTimeMillis(), first, last); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å¤çç¼åçä¸è¡æ°æ®èç¹ |
| | | * @param now å½åæ¶å» |
| | | * @param first 第ä¸ä¸ªèç¹ |
| | | * @param last æåä¸ä¸ªèç¹ |
| | | */ |
| | | private Node doDealMqMsg(Long now, Node first, Node last){ |
| | | if(last != null){ |
| | | //å¨dealNodeæ¹æ³ä¸ï¼å¯è½è¦ælastä»éåä¸ç§»é¤ï¼è¿æ¶last.preä¸ºç©ºï¼æä»¥æåælast.preååºæ¥ |
| | | Node pre = last.pre ; |
| | | dealNode(now, last) ; |
| | | if(first != last){ |
| | | return pre ; |
| | | }else{ |
| | | //忢 |
| | | return null ; |
| | | } |
| | | }else{ |
| | | return null ; |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * å¤çä¸ä¸ªèç¹ |
| | | * @param now ç°å¨æ¶å» |
| | | * @param node èç¹ |
| | | */ |
| | | private void dealNode(Long now, Node node){ |
| | | if(node != null && node.obj != null){ |
| | | MqttPubMsgNode obj = (MqttPubMsgNode)node.obj ; |
| | | boolean removeNode = obj.dealSelf(now) ; |
| | | if(removeNode){ |
| | | MqttPubMsgCache.removeNode(node); |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.dy.rtuMw.server.tasks; |
| | | |
| | | import com.dy.common.mw.core.CoreTask; |
| | | import com.dy.common.queue.Node; |
| | | import com.dy.rtuMw.server.mqtt.MqttSubMsgCache; |
| | | import com.dy.rtuMw.server.mqtt.MqttSubMsgNode; |
| | | import org.apache.logging.log4j.LogManager; |
| | | import org.apache.logging.log4j.Logger; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/6/4 16:25 |
| | | * @Description |
| | | */ |
| | | public class MqttSubMessageConstantTask extends CoreTask { |
| | | private static final Logger log = LogManager.getLogger(MqttSubMessageConstantTask.class.getName()); |
| | | |
| | | /** |
| | | * å¨å线ç¨ç¯å¢ä¸è¿è¡ |
| | | */ |
| | | @Override |
| | | public Integer execute() { |
| | | try{ |
| | | dealMqMsg() ; |
| | | }catch(Exception e){ |
| | | log.error(e); |
| | | } |
| | | return MqttSubMsgCache.size()>0?0:1 ; |
| | | } |
| | | /** |
| | | * å¤çMQTT订é
çæ¶æ¯ |
| | | */ |
| | | public void dealMqMsg() { |
| | | Node first = MqttSubMsgCache.getFirstQueueNode() ; |
| | | if(first != null){ |
| | | Node last = MqttSubMsgCache.getLastQueueNode() ; |
| | | while (last != null){ |
| | | last = this.doDealMqMsg(first, last); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å¤çç¼åçä¸è¡æ°æ®èç¹ |
| | | * @param first 第ä¸ä¸ªèç¹ |
| | | * @param last æåä¸ä¸ªèç¹ |
| | | */ |
| | | private Node doDealMqMsg(Node first, Node last){ |
| | | if(last != null){ |
| | | //å¨dealNodeæ¹æ³ä¸ï¼å¯è½è¦ælastä»éåä¸ç§»é¤ï¼è¿æ¶last.preä¸ºç©ºï¼æä»¥æåælast.preååºæ¥ |
| | | Node pre = last.pre ; |
| | | dealNode(last) ; |
| | | if(first != last){ |
| | | return pre ; |
| | | }else{ |
| | | //忢 |
| | | return null ; |
| | | } |
| | | }else{ |
| | | return null ; |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * å¤çä¸ä¸ªèç¹ |
| | | * @param node èç¹ |
| | | */ |
| | | private void dealNode(Node node){ |
| | | if(node != null && node.obj != null){ |
| | | MqttSubMsgNode obj = (MqttSubMsgNode)node.obj ; |
| | | obj.dealSelf() ; |
| | | MqttSubMsgCache.removeNode(node); |
| | | } |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.dy.rtuMw.server.tasks; |
| | | |
| | | import com.dy.common.mw.core.CoreTask; |
| | | import com.dy.common.mw.protocol.Command; |
| | | import com.dy.common.mw.protocol.Driver; |
| | | import com.dy.common.mw.protocol.MidResult; |
| | | import com.dy.common.mw.protocol.ProtocolCache; |
| | | import com.dy.common.mw.protocol4Mqtt.MqttMsgParser; |
| | | import com.dy.common.mw.protocol4Mqtt.MqttPubMsg; |
| | | import com.dy.rtuMw.server.ServerProperties; |
| | | import com.dy.rtuMw.server.forTcp.TcpSessionCache; |
| | | import com.dy.rtuMw.server.mqtt.MqttPubMsgCache; |
| | | import org.apache.logging.log4j.LogManager; |
| | | import org.apache.logging.log4j.Logger; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/6/5 15:42 |
| | | * @Description |
| | | */ |
| | | |
| | | public class WebDownCom4MqttTask extends CoreTask { |
| | | |
| | | private static Logger log = LogManager.getLogger(WebDownCom4MqttTask.class.getName()); |
| | | |
| | | @Override |
| | | public Integer execute() { |
| | | Command com = (Command)this.data ; |
| | | try { |
| | | log.info("ä¸åMQTTå½ä»¤" + com.getCode() + "çæ ¸å¿ä»»å¡å¼å§æ§è¡"); |
| | | this.deal(com); |
| | | } catch (Exception e) { |
| | | log.error("å¤çä¸è¡MQTTå½ä»¤åºé" + (e.getMessage()==null?"!":("ï¼" + e.getMessage())) ,e); |
| | | } |
| | | return null ; |
| | | } |
| | | |
| | | /** |
| | | * å¤çå½ä»¤ |
| | | * @param com å½ä»¤ |
| | | * @throws Exception |
| | | */ |
| | | private void deal(Command com) throws Exception{ |
| | | MqttMsgParser parser = new MqttMsgParser() ; |
| | | MqttPubMsg pubMsg = parser.createPubMsg(ServerProperties.orgTag, com) ; |
| | | if(pubMsg != null){ |
| | | MqttPubMsgCache.cacheCommand(pubMsg); |
| | | } |
| | | } |
| | | |
| | | } |
| | |
| | | import com.dy.rtuMw.server.forTcp.TcpSessionCache; |
| | | import com.dy.rtuMw.server.local.CommandInnerDeaLer; |
| | | import com.dy.rtuMw.server.local.ReturnCommand; |
| | | import com.dy.rtuMw.server.mqtt.MqttManager; |
| | | import com.dy.rtuMw.server.msCenter.MsCenterUnit; |
| | | import com.dy.rtuMw.server.tasks.WebDownCom4MqttTask; |
| | | import com.dy.rtuMw.server.tasks.WebDownComTask; |
| | | import com.dy.common.mw.core.CoreUnit; |
| | | import com.dy.common.mw.protocol.Command; |
| | |
| | | }catch(Exception e){ |
| | | return BaseResponseUtils.buildError(ReturnCommand.errored("å¤çååRTUçå¤é¨éä¼ å½ä»¤åºé" + (e.getMessage() == null?"":("ï¼" + e.getMessage())), com.getId(), com.getCode()) ); |
| | | } |
| | | }else if(commandType.equals(CommandType.mqttCommand)){ |
| | | //ååMQTTçå¤é¨å½ä»¤ |
| | | try{ |
| | | return this.dealMqttCommand(com) ; |
| | | }catch(Exception e){ |
| | | return BaseResponseUtils.buildError(ReturnCommand.errored("å¤çååRTUçå¤é¨å½ä»¤åºé" + (e.getMessage() == null?"":("ï¼" + e.getMessage())), com.getId(), com.getCode()) ); |
| | | } |
| | | }else if(commandType.equals(CommandType.resultCommand)){ |
| | | return BaseResponseUtils.buildError(ReturnCommand.errored("åºéï¼éä¿¡ä¸é´ä»¶ä¸æ¥ç»æç±»åçå½ä»¤ï¼", com.getId(), com.getCode())); |
| | | }else{ |
| | |
| | | return BaseResponseUtils.buildSuccess(ReturnCommand.successed("éä¼ å½ä»¤å·²æ¥åï¼å³å°æé å¹¶ä¸åå½ä»¤ã", command.getId(), command.getCode())); |
| | | } |
| | | |
| | | /** |
| | | * å¤çååRTUçå¤é¨å½ä»¤ |
| | | * @return ç»æ |
| | | */ |
| | | private BaseResponse<Command> dealMqttCommand(Command command){ |
| | | String rtuAddr = command.getRtuAddr() ; |
| | | if(rtuAddr == null || rtuAddr.trim().equals("")){ |
| | | return BaseResponseUtils.buildError(ReturnCommand.errored("åºéï¼è®¾å¤ID为空ï¼", command.getId(), command.getCode())) ; |
| | | } |
| | | if(!ServerProperties.mqttUnitEnable.booleanValue()){ |
| | | return BaseResponseUtils.buildError(ReturnCommand.errored("åºéï¼MQTTè¿æ¥æ¨¡åé
ç½®æªå¯å¨ï¼", command.getId(), command.getCode())) ; |
| | | } |
| | | if(MqttManager.getInstance().poolIsClose()){ |
| | | return BaseResponseUtils.buildError(ReturnCommand.errored("åºéï¼MQTTè¿æ¥æ± æ°´å建æåï¼", command.getId(), command.getCode())) ; |
| | | } |
| | | |
| | | //çæå¼æ¥ä»»å¡ |
| | | WebDownCom4MqttTask task = new WebDownCom4MqttTask() ; |
| | | task.data = command ; |
| | | try{ |
| | | log.info("æé ä¸åMQTTå½ä»¤" + command.getCode() + "çæ ¸å¿ä»»å¡ï¼å¹¶æ¾å
¥ä»»å¡éåä¸"); |
| | | CoreUnit.getInstance().pushCoreTask(task); |
| | | }catch(Exception e){ |
| | | log.error(e.getMessage(), e); |
| | | return BaseResponseUtils.buildError(ReturnCommand.successed("MQTTå½ä»¤å¤ç失败" + e.getMessage(), command.getId(), command.getCode())) ; |
| | | } |
| | | return BaseResponseUtils.buildSuccess(ReturnCommand.successed("MQTTå½ä»¤å·²æ¥åï¼å³å°æé å¹¶ä¸åå½ä»¤ã", command.getId(), command.getCode())); |
| | | } |
| | | |
| | | |
| | | |
| | | } |
| | |
| | | import com.dy.common.contant.Constant; |
| | | import com.dy.common.mw.protocol.Command; |
| | | import com.dy.common.mw.protocol.Data; |
| | | import com.dy.common.mw.protocol4Mqtt.MqttSubMsg; |
| | | import com.dy.rtuMw.server.ServerProperties; |
| | | import org.apache.logging.log4j.LogManager; |
| | | import org.apache.logging.log4j.Logger; |
| | |
| | | this.restTemplate = restTemplate ; |
| | | } |
| | | |
| | | /** |
| | | * RTUè®¾å¤æ°æ® |
| | | * @param data |
| | | */ |
| | | public void deal(Data data) { |
| | | if (data.rtuResultSendWebUrl != null |
| | | && !data.rtuResultSendWebUrl.trim().equals("") |
| | |
| | | log.error("严éé误ï¼å¨com.dy.aceMw.web.comResult.CommandResultDealéï¼å¤ççæ¯RTUå½ä»¤ç»æNodeï¼ä½æ°æ®ä¸rtuResultSendWebUrl为空"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Mqttæ¶æ¯æ°æ® |
| | | * @param subMsg |
| | | */ |
| | | public void deal(MqttSubMsg subMsg) { |
| | | if (subMsg.mqttResultSendWebUrl != null |
| | | && !subMsg.mqttResultSendWebUrl.trim().equals("") |
| | | && !subMsg.mqttResultSendWebUrl.trim().equals(Command.ignoreRtuResultSendWebUrl)) { |
| | | String url = UriComponentsBuilder.fromUriString(subMsg.mqttResultSendWebUrl) |
| | | .build() |
| | | .toUriString(); |
| | | restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8)); |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8")); |
| | | headers.set(Constant.UserTokenKeyInHeader, ServerProperties.orgTag); |
| | | HttpEntity<?> httpEntity = new HttpEntity<>(subMsg, headers); |
| | | ResponseEntity<WebResponseVo> response = null; |
| | | try { |
| | | // éè¿Postæ¹å¼è°ç¨æ¥å£ |
| | | response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, WebResponseVo.class); |
| | | } catch (Exception e) { |
| | | log.error("å½ä»¤ç»æåè°åçå¼å¸¸", e); |
| | | e.printStackTrace(); |
| | | } |
| | | //assert response != null; |
| | | } else { |
| | | log.error("严éé误ï¼å¨com.dy.aceMw.web.comResult.CommandResultDealéï¼å¤ççæ¯RTUå½ä»¤ç»æNodeï¼ä½æ°æ®ä¸rtuResultSendWebUrl为空"); |
| | | } |
| | | } |
| | | } |
| | |
| | | </task> |
| | | </task> |
| | | </task> |
| | | <!-- Mqttæ¶æ¯ä¸é´ä»¶è®¢é
çæ¶æ¯ --> |
| | | <task id="TkMqttData" name="æ¥æ¶Mqttæ¶æ¯" enable="true" class="com.dy.rtuMw.server.rtuData.TkMqttData"> |
| | | <task id="TkFindPSdV1" name="è¯å«å±±ä¸V1æ°æ®" enable="true" class="com.dy.rtuMw.server.rtuData.pSdV1.TkFindPSdV1"> |
| | | </task> |
| | | </task> |
| | | </project> |
| | |
| | | #RTUä¸è¡æ°æ®æå°é´éï¼å¤§äºè¿ä¸ªé´é认为设å¤ç¦»çº¿äºï¼æµæ§ä¸ä½éæ¯3ï¼è¡¨éä¸ä½æºæ¯6ï¼é»è®¤éç¨æ¶é´æé¿ç6 |
| | | base.upData.min.interval=6 |
| | | |
| | | # MQTTæå¡é
ç½® |
| | | # 233æå¡å¨ï¼ |
| | | # å
è°ï¼ mqtt.enable=false |
| | | # æ²çï¼ mqtt.enable=false |
| | | # æµè¯ï¼ mqtt.enable=false |
| | | # æ¢
æ±ï¼ mqtt.enable=false |
| | | # 121æå¡å¨ï¼ |
| | | # æ°å¤ï¼ mqtt.enable=true |
| | | # å»¶åºï¼ mqtt.enable=false |
| | | # é»é¾æ±ï¼ mqtt.enable=false |
| | | # çå·ï¼ mqtt.enable=false |
| | | # åå·ï¼ mqtt.enable=false |
| | | # éå·ï¼ mqtt.enable=true |
| | | mqtt.enable=true |
| | | mqtt.topicAndQos=ym/sd1/10000/control/m1,1;ym/sd1/10000/control/m2,1;ym/sd1/control/m4,1;ym/sd1/10000/control/m80,1 |
| | | |
| | |
| | | idle="10" |
| | | /> |
| | | |
| | | |
| | | <!-- |
| | | topicAndQos: 主é¢ä¸Qosï¼ä¸»é¢åä¸å
¶Qosç¨éå·éå¼ï¼å¤ä¸ªä¸»é¢åQosç¨åå·éå¼ï¼ä¾å¦ï¼ym/topic1,1;ym/topic2,1;ym/topic3,1ï¼å¦ææå¤ä¸ªOrgTagï¼ä¸»é¢åç¼ç¨å
¶OrgTag |
| | | publishQos: å叿¶æ¯çQosï¼åå¼èå´ï¼ |
| | | 0 è³å¤ä¸æ¬¡ï¼At most onceï¼ æ¶æ¯åéåä¸ä¿è¯å°è¾¾ï¼å¯è½ä¸¢å¤±æéå¤ï¼å¼éæå°ï¼å¯é æ§æä½ã |
| | | 1 è³å°ä¸æ¬¡ï¼At least onceï¼ æ¶æ¯è³å°ä¼å°è¾¾ä¸æ¬¡ï¼å¯è½éå¤ï¼ä½ä¸ä¼ä¸¢å¤±ï¼å¯é æ§ä¸çï¼éç¨äºå¤æ°åºæ¯ã |
| | | 2 æ°å¥½ä¸æ¬¡ï¼Exactly onceï¼ æ¶æ¯ä»
ä¼å°è¾¾ä¸æ¬¡ï¼ä¸éå¤ä¸ä¸ä¸¢å¤±ï¼å¯é æ§æé«ï¼ä½å¼éæå¤§ï¼å®ç°æå¤æã |
| | | --> |
| | | <mqtt enable="${mqtt.enable}" |
| | | svIp="121.199.41.121" |
| | | svPort="1883" |
| | | svUserName="dyyjy" |
| | | svUserPassword="Dyyjy2025,;.abc!@#" |
| | | poolMaxSize="10" |
| | | topicAndQos="${mqtt.topicAndQos}" |
| | | publishQos="1" |
| | | /> |
| | | |
| | | </config> |