From c70b92da60c0d00625da5fc2369b475e69e0b88e Mon Sep 17 00:00:00 2001 From: liurunyu <lry9898@163.com> Date: 星期四, 22 八月 2024 11:36:41 +0800 Subject: [PATCH] 1、完善代码和application.yml配置文件,去掉不用的引入文件; 2、init-config.xml配置中增加属性 enable,相应解析类也增加了解析与判断; 3、凡是有Server的应用,加载资源方式改为用springboot的工具类加载; 4、通信协议相关的扫描注解功能实现改为采用reflections-0.10.2,以实现可以在jar包情况下能够扫描类(.class)注解。 --- pipIrr-platform/pipIrr-mw/pipIrr-mwTest-client/src/main/java/com/dy/testClient/tcpClient/MyThreadJob.java | 129 ++++++++++++++++++++++++++++++++++-------- 1 files changed, 104 insertions(+), 25 deletions(-) diff --git a/pipIrr-platform/pipIrr-mw/pipIrr-mwTest-client/src/main/java/com/dy/testClient/tcpClient/MyThreadJob.java b/pipIrr-platform/pipIrr-mw/pipIrr-mwTest-client/src/main/java/com/dy/testClient/tcpClient/MyThreadJob.java index eee927c..fe1eae5 100644 --- a/pipIrr-platform/pipIrr-mw/pipIrr-mwTest-client/src/main/java/com/dy/testClient/tcpClient/MyThreadJob.java +++ b/pipIrr-platform/pipIrr-mw/pipIrr-mwTest-client/src/main/java/com/dy/testClient/tcpClient/MyThreadJob.java @@ -1,46 +1,125 @@ package com.dy.testClient.tcpClient; +import com.dy.common.mw.protocol.p206V1_0_0.CommonV1_0_1; +import com.dy.common.mw.protocol.p206V1_0_0.ProtocolConstantV206V1_0_0; +import com.dy.common.mw.protocol.p206V1_0_0.parse.global.GlCreate; import com.dy.common.threadPool.ThreadPool; -import com.dy.testClient.httpCl.*; - -import java.util.Base64; +import com.dy.common.util.ByteUtil; +import com.dy.testClient.ServerProperties; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.mina.core.session.IoSession; public class MyThreadJob implements ThreadPool.Job { + private static final Logger log = LogManager.getLogger(MyThreadJob.class) ; + + public static String icCardAddr = "3B7D1E1A" ; + public static String icCardNo = "61181622830147821" ; + public static String openValveDt = "240711163700" ; + public static String cdC0 = "C0" ; + public static String cd83Open = "83Open" ; + public static String cd83Close = "83Close" ; + + + public static String curCd = cdC0 ;//閫氳繃淇敼杩欎釜鍊硷紝鏉ユ敼鍙樹笂鎶ユ暟鎹被鍨� + public String rtuAddr; + public String serverIp; + public Integer serverPort; + + public IoSession session ; + public boolean exceptionOnConnect = false ; + + public static final int connectTimeout = 3000 ; + + public int sendTimes = 1 ;//鍙戦�佹暟鎹鏁� + public int heartbeatTimes = 1 ;//涓婃姤蹇冭烦娆℃暟 + + public long overStart = 0L; + public boolean isOver = false ; public MyThreadJob(){ } - public MyThreadJob(String rtuAddr){ + public MyThreadJob(String rtuAddr, String serverIp, Integer serverPort){ this.rtuAddr = rtuAddr ; + this.serverIp = serverIp ; + this.serverPort = serverPort ; } @Override public void execute() throws Exception { - testP206V1_0_0() ; - } - - private void testP206V1_0_0(){ - try{ - RtuData4MeterV1_0 rd = new RtuData4MeterV1_0() ; - byte[] bs = rd.create(rtuAddr) ; - bs = Base64.getEncoder().encode(bs) ; - - UpDataPayloadVo payloadVo = new UpDataPayloadVo() ; - payloadVo.APPdata = new String(bs, "UTF-8") ; - - UpDataVo vo = new UpDataVo(rtuAddr, "8086" + rtuAddr) ; - vo.payload = payloadVo ; - String json = vo.toJson() ; - byte[] bytes = json.getBytes() ; - HttpPost.post(bytes); - }catch(Exception e){ - e.printStackTrace(); - }finally{ - HttpClUnit.clientOver() ; + if(!this.isOver){ + if(session != null){ + if(sendTimes <= ServerProperties.sendTimes){ + log.info("RTU" + rtuAddr + "寮�濮嬩换鍔�"); + log.info("RTU" + rtuAddr + "灏嗚鎵ц" + ServerProperties.sendTimes + "杞浠诲姟锛屽綋鍓嶈疆娆℃槸" + sendTimes); + sendDataOfP206V1_0_0(curCd) ; + }else{ + if(ServerProperties.sendOverThenCloseConnect == 1){ + log.info("RTU" + rtuAddr + "绛夊緟涓�浼氾紝浠ユ帴鏀堕�氫俊涓棿浠朵笅琛屾暟鎹�"); + if(overStart == 0){ + overStart = System.currentTimeMillis() ; + }else{ + long now = System.currentTimeMillis() ; + if(now - overStart >= 30 * 1000){ + this.jobOver() ; + } + } + }else{ + this.jobOver() ; + } + } + } } } + private void sendDataOfP206V1_0_0(String cd){ + try{ + if(heartbeatTimes > ServerProperties.heartbeatTimes){ + heartbeatTimes = 1 ; + byte[] bs = null ; + if(cd.equals(cdC0)){ + bs = RtuUpData.createCdC0(this.rtuAddr) ; + }else if(cd.equals(cd83Open)){ + bs = RtuUpData.createCd83Open(this.rtuAddr, icCardAddr, icCardNo, openValveDt) ; + }else if(cd.equals(cd83Close)){ + bs = RtuUpData.createCd83Close(this.rtuAddr, icCardAddr, icCardNo, openValveDt) ; + } + this.session.write(bs) ; + log.info("RTU" + rtuAddr + "鍙戦�佷簡涓婅鏁版嵁锛�" + ByteUtil.bytes2Hex(bs, false)); + TcpClUnit.clientSendData(); + sendTimes++ ; + }else{ + this.sendHeartbeat() ; + TcpClUnit.clientSendData(); + heartbeatTimes++ ; + } + }catch(Exception e){ + e.printStackTrace(); + } + } + private void sendHeartbeat(){ + try{ + byte[] bs = RtuUpData.createHeartbeat(this.rtuAddr) ; + this.session.write(bs) ; + log.info("RTU" + rtuAddr + "鍙戦�佷簡蹇冭烦鏁版嵁锛�" + ByteUtil.bytes2Hex(bs, false)); + }catch (Exception e){ + e.printStackTrace(); + } + } + + + private void jobOver(){ + if(ServerProperties.sendOverThenCloseConnect == 1){ + session.closeOnFlush() ; + } + this.isOver = true ; + TcpClUnit.clientOver() ; + } + + + @Override public void destroy() { -- Gitblit v1.8.0