package com.dy.rtuMw.server.tasks; import com.dy.rtuMw.server.rtuData.RtuComResultNode; import com.dy.rtuMw.server.rtuData.RtuComResultCache; import com.dy.common.mw.core.CoreTask; import com.dy.common.queue.Node; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; /** * 对RTU上行数据进行业务处理 */ public class FromRtuComResultConstantTask extends CoreTask { private static final Logger log = LogManager.getLogger(FromRtuComResultConstantTask.class.getName()); /** * 在单线程环境中运行 */ @Override public Integer execute() { try{ dealRtuComResult() ; }catch(Exception e){ log.error(e); } return RtuComResultCache.size()>0?0:1 ; } /** * 处理上行命令结果 */ public void dealRtuComResult() { Node first = RtuComResultCache.getFirstQueueNode() ; if(first != null){ Node last = RtuComResultCache.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 ; } } //////////////////////////////////////////////// // //以下实现,采用了递归调用,当队列缓存结点很多时,会产生栈溢出异常 // //////////////////////////////////////////////// /** * 处理上行数据 */ public Integer dealRtuComResult_() { Node first = RtuComResultCache.getFirstQueueNode() ; if(first != null){ Integer count = RtuComResultCache.size() ; Node last = RtuComResultCache.getLastQueueNode() ; this.doDealRtuComResult_(first, last); return count ; } return null ; } /** * 处理缓存的上行数据节点 * @param first 第一个节点 * @param last 最后一个节点 */ private void doDealRtuComResult_(Node first, Node last){ if(last != null){ //在dealNode方法中,可能要把last从队列中移除,这时last.pre为空,所以提前把last.pre取出来 Node pre = last.pre ; dealNode(last) ; if(first != last){ doDealRtuComResult_(first, pre) ; }else{ //停止 } } } /** * 处理一个节点 * @param node 节点 */ private void dealNode(Node node){ if(node != null && node.obj != null){ RtuComResultNode obj = (RtuComResultNode)node.obj ; obj.dealSelf() ; RtuComResultCache.removeNode(node); } } }