zhubaomin
2 天以前 080c76ddb23b9f199ed2f59f3871b0058347d43e
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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 ;
    }
}