From 9ad6b0179be4d351105f1507779d4f41a3a41953 Mon Sep 17 00:00:00 2001 From: liurunyu <lry9898@163.com> Date: 星期六, 07 九月 2024 16:57:40 +0800 Subject: [PATCH] 1、完善代码; 2、江海协议时标(tp)实现中的bug修改。 --- pipIrr-platform/pipIrr-mw/pipIrr-mwTest-client/src/main/java/com/dy/testClient/tcpClient/MyThreadJob.java | 119 +++++++++++++++++++++++++++++------------------------------ 1 files changed, 59 insertions(+), 60 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 0a07ff7..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 @@ -6,22 +6,37 @@ import com.dy.common.threadPool.ThreadPool; import com.dy.common.util.ByteUtil; import com.dy.testClient.ServerProperties; -import org.apache.mina.core.future.CloseFuture; +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 = 0 ;//鍙戦�佹暟鎹鏁� - public int heartbeatTimes = 0 ;//涓婃姤蹇冭烦娆℃暟 + public int sendTimes = 1 ;//鍙戦�佹暟鎹鏁� + public int heartbeatTimes = 1 ;//涓婃姤蹇冭烦娆℃暟 + public long overStart = 0L; public boolean isOver = false ; public MyThreadJob(){ @@ -34,28 +49,50 @@ @Override public void execute() throws Exception { - if(session == null){ - IoSession se = new TcpConnect().createSession(this.rtuAddr, this, this.serverIp, this.serverPort, connectTimeout, new TcpHandler()) ; - if(se != null){ - this.session = se ; - } - }else{ - if(sendTimes <= ServerProperties.sendTimes){ - sendDataOfP206V1_0_0() ; - }else{ - this.jobOver() ; + 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(){ + private void sendDataOfP206V1_0_0(String cd){ try{ - if(heartbeatTimes >= ServerProperties.heartbeatTimes){ - heartbeatTimes = 0 ; - this.sendReportData() ; + 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){ @@ -64,61 +101,23 @@ } private void sendHeartbeat(){ try{ - byte[] bs = this.createHeartbeat() ; + 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 sendReportData(){ - TcpClUnit.clientSendData(); - } private void jobOver(){ - CloseFuture closeFuture = session.closeOnFlush() ; + if(ServerProperties.sendOverThenCloseConnect == 1){ + session.closeOnFlush() ; + } this.isOver = true ; TcpClUnit.clientOver() ; } - /** - * 鏋勯�犱笅琛屾暟鎹� - * @return 瀛楄妭鏁扮粍 - * @throws Exception 寮傚父 - */ - public byte[] createHeartbeat( ) throws Exception { - CommonV1_0_1 commonV1_0_1 = new CommonV1_0_1() ; - byte[] bytes ; - byte[] bsHead = new byte[ProtocolConstantV206V1_0_0.lenHead2Code] ; - byte index = 0 ; - bsHead[index] = ProtocolConstantV206V1_0_0.P_Head_Byte ; - - index++ ; - bsHead[index] = 0 ;//甯ч暱搴� - - index++ ; - bsHead[index] = ProtocolConstantV206V1_0_0.P_Head_Byte ; - - index++ ; - bsHead[index] = commonV1_0_1.createCtrl((byte)0) ; - - index++ ; - GlCreate.createRtuAddr(this.rtuAddr, bsHead, index); - index += 5 ; - - ByteUtil.hex2Bytes("02", bsHead, index) ; - - byte[] bs = new byte[1] ; - bs[0] = (byte)0xF2 ;//鏁版嵁鍩燂細 1 涓瓧鑺傦紝F0 鐧诲綍锛� F1 閫�鍑虹櫥褰曪紝F2 鍦ㄧ嚎淇濇寔銆� - - bytes = ByteUtil.bytesMerge(bsHead, bs) ; - - GlCreate.createLen(bytes);//闀垮害鏀惧瓧鑺傛暟缁勪腑 - - bytes = GlCreate.createCrcTail(bytes) ;//CRC鍜屽熬鍙犲姞瀛楄妭鏁扮粍涓� - - return bytes ; - } -- Gitblit v1.8.0