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.DevStatusDealer; 
 | 
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{ 
 | 
            dealOneline() ; 
 | 
        }catch(Exception e){ 
 | 
            log.error("更新RTU会话上报数据时刻时发生集合操作异常,此异常并不影响系统正常运行", e); 
 | 
        } 
 | 
        try{ 
 | 
            dealMqMsg() ; 
 | 
        }catch(Exception e){ 
 | 
            log.error(e); 
 | 
        } 
 | 
        return MqttSubMsgCache.size()>0?0:1 ; 
 | 
    } 
 | 
  
 | 
    private void dealOneline(){ 
 | 
        DevStatusDealer.updateOnLineState(); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 处理MQTT订阅的消息 
 | 
     */ 
 | 
    private 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); 
 | 
        } 
 | 
    } 
 | 
  
 | 
} 
 |