liurunyu
2023-11-29 f2df44d7b3865cafe86baa48e19d1df0c0723702
pipIrr-platform/pipIrr-mw/pipIrr-mwTest-client/src/main/java/com/dy/testClient/tcpClient/MyThreadJob.java
@@ -1,46 +1,126 @@
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.mina.core.future.CloseFuture;
import org.apache.mina.core.session.IoSession;
public class MyThreadJob  implements ThreadPool.Job {
    public String rtuAddr;
    public String serverIp;
    public Integer serverPort;
    public IoSession session ;
    public static final int connectTimeout = 3000 ;
    public int sendTimes = 0 ;//发送数据次数
    public int heartbeatTimes = 0 ;//上报心跳次数
    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(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() ;
            }
        }
    }
    private void sendDataOfP206V1_0_0(){
        try{
            if(heartbeatTimes >= ServerProperties.heartbeatTimes){
                heartbeatTimes = 0 ;
                this.sendReportData() ;
                sendTimes++ ;
            }else{
                this.sendHeartbeat() ;
                heartbeatTimes++ ;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    private void sendHeartbeat(){
        try{
            byte[] bs = this.createHeartbeat() ;
            this.session.write(bs) ;
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    private void sendReportData(){
        TcpClUnit.clientSendData();
    }
    private void jobOver(){
        CloseFuture closeFuture = 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 ;
    }
    @Override
    public void destroy() {