liurunyu
2024-12-06 8c4a723ff13e34152934b84c52267615c4e0fb8b
升级模拟器增加上报81数据,为上报阀门状态,用81数据代替心跳
4个文件已修改
1个文件已添加
294 ■■■■■ 已修改文件
pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/PipIrrMwSimulateRtuApplication.java 78 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/ServerProperties.java 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/tcpClient/TcpClUnit.java 69 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/tcpClient/Worker.java 96 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/tcpClient/upData/UpHeartBeat.java 50 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/PipIrrMwSimulateRtuApplication.java
@@ -2,7 +2,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@@ -36,25 +35,32 @@
    public static void main(String[] args) {
        if(args != null && args.length > 0){
            if(args.length == 1){
                getArg0(args) ;
                getArg0(args, 0) ;
            }else if(args.length == 2){
                getArg0(args) ;
                getArg1(args) ;
                getArg0(args, 0) ;
                getArg1(args, 1) ;
            }else if(args.length == 3){
                getArg0(args) ;
                getArg1(args) ;
                getArg2(args) ;
                getArg0(args, 0) ;
                getArg1(args, 1) ;
                getArg2(args, 2) ;
            }else if(args.length == 4){
                getArg0(args) ;
                getArg1(args) ;
                getArg2(args) ;
                getArg3(args) ;
                getArg0(args, 0) ;
                getArg1(args, 1) ;
                getArg2(args, 2) ;
                getArg3(args, 3) ;
            }else if(args.length == 5){
                getArg0(args) ;
                getArg1(args) ;
                getArg2(args) ;
                getArg3(args) ;
                getArg4(args) ;
                getArg0(args, 0) ;
                getArg1(args, 1) ;
                getArg2(args, 2) ;
                getArg3(args, 3) ;
                getArg4(args,4) ;
            }else if(args.length == 6){
                getArg0(args, 0) ;
                getArg1(args, 1) ;
                getArg2(args, 2) ;
                getArg3(args, 3) ;
                getArg4(args, 4) ;
                getArg5(args, 5) ;
            }
            if(ServerProperties.argOneDie && ServerProperties.argMultiDie){
@@ -65,37 +71,43 @@
                .web(WebApplicationType.NONE)//不启动web服务
                .run(args);
    }
    private static void getArg0(String[] args){
        if(args[0] instanceof String){
            if(args[0] != null && !args[0].trim().equals("")){
                String rtuAddr = (String)args[0] ;//本模拟器模拟RTU地址
    private static void getArg0(String[] args, int index){
        if(args[index] instanceof String){
            if(args[index] != null && !args[index].trim().equals("")){
                String rtuAddr = (String)args[index] ;//本模拟器模拟RTU地址
                ServerProperties.argRtuAddr = rtuAddr ;
            }
        }
    }
    private static void getArg1(String[] args){
        if(args[1] != null && !args[1].trim().equals("")){
            Integer oneDie = Integer.parseInt(args[1]) ;//一包死:1是,0否
    private static void getArg1(String[] args, int index){
        if(args[index] != null && !args[index].trim().equals("")){
            Integer conf = Integer.parseInt(args[index]) ;//心跳类型:1:功能码02,0:功能码81
            ServerProperties.argHeardBeatBy02True81False = conf == 1?true:false ;
        }
    }
    private static void getArg2(String[] args, int index){
        if(args[index] != null && !args[index].trim().equals("")){
            Integer oneDie = Integer.parseInt(args[index]) ;//一包死:1是,0否
            ServerProperties.argOneDie = oneDie == 1?true:false ;
        }
    }
    private static void getArg2(String[] args){
        if(args[2] != null && !args[2].trim().equals("")){
            Integer multiDie = Integer.parseInt(args[2]) ;//多包死:1是,0否
    private static void getArg3(String[] args, int index){
        if(args[index] != null && !args[index].trim().equals("")){
            Integer multiDie = Integer.parseInt(args[index]) ;//多包死:1是,0否
            ServerProperties.argMultiDie = multiDie == 1?true:false ;
        }
    }
    private static void getArg3(String[] args){
        if(args[3] instanceof String){
            if(args[3] != null && !args[3].trim().equals("")){
                String serverIp = (String)args[3] ;//服务端IP
    private static void getArg4(String[] args, int index){
        if(args[index] instanceof String){
            if(args[index] != null && !args[index].trim().equals("")){
                String serverIp = (String)args[index] ;//服务端IP
                ServerProperties.serverIp = serverIp ;
            }
        }
    }
    private static void getArg4(String[] args){
        if(args[4] != null && !args[4].trim().equals("")){
            Integer serverPort = Integer.parseInt(args[4]) ;//服务端port
    private static void getArg5(String[] args, int index){
        if(args[index] != null && !args[index].trim().equals("")){
            Integer serverPort = Integer.parseInt(args[index]) ;//服务端port
            ServerProperties.serverPort = serverPort ;
        }
    }
pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/ServerProperties.java
@@ -12,6 +12,7 @@
    //程序启动参数:
    public static String argRtuAddr ;
    public static Boolean argHeardBeatBy02True81False = true ;
    public static Boolean argOneDie = false ;
    public static Boolean argMultiDie = false ;
pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/tcpClient/TcpClUnit.java
@@ -14,14 +14,12 @@
public class TcpClUnit implements UnitInterface {
    private static final Logger log = LogManager.getLogger(TcpClUnit.class) ;
    private static TcpClUnit instance = new TcpClUnit() ;
    public static TcpClUnitAdapter adapter ;
    public static TcpClUnitConfigVo confVo ;
    public static IoSession session ;
    private static Worker worker ;
    private TcpClUnit(){} ;
@@ -39,12 +37,13 @@
        if(TcpClUnit.confVo == null){
            throw new Exception("Tcp Client模块配置对象不能为空!") ;
        }
        TcpClUnit.worker = Worker.getInstance(TcpClUnit.confVo) ;
    }
    @Override
    public void start(UnitCallbackInterface callback) throws Exception {
        System.out.println("Tcp Client模块成功启动");
        this.doStart();
        TcpClUnit.worker.doStart();
        callback.call(null) ;
    }
@@ -53,67 +52,5 @@
        callback.call(null);
    }
    private void doStart(){
        new Thread(() -> {
            Exception ex ;
            while(true){
                ex = null ;
                try {
                    new TcpConnect().createSession(
                            confVo.mwServerIp,
                            confVo.mwServerPort,
                            confVo.connectTimeout,
                            new TcpHandler(),
                            new Callback() {
                                @Override
                                public void call(Object obj) {
                                    if (obj == null) {
                                        log.error("创建网络会话返回为null");
                                    } else {
                                        TcpClUnit.session = (IoSession) obj;
                                        log.info("成功创建与通信中间件的网络连接");
                                        UpData.setSession(TcpClUnit.session);
                                        heartBeat() ;
                                    }
                                }
                                @Override
                                public void call(Object... objs) {
                                }
                                @Override
                                public void exception(Exception e) {
                                }
                            });
                }catch (Exception e){
                   ex = e ;
                }
                if(ex == null){
                    break ;
                }else{
                    try{
                        Thread.sleep(100);
                    }catch (Exception e){
                    }
                }
            }
        }).start();
    }
    private void heartBeat(){
        new Thread(new Runnable(){
            @Override
            public void run() {
                while(true){
                    try {
                        UpHeartBeat.upCd02Data(ServerProperties.rtuAddr);
                        Thread.sleep(30000L);
                        //Thread.sleep(30000000L);
                    }catch (Exception e){
                        continue;
                    }
                }
            }
        }).start();
    }
}
pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/tcpClient/Worker.java
New file
@@ -0,0 +1,96 @@
package com.dy.simRtu.tcpClient;
import com.dy.common.util.Callback;
import com.dy.simRtu.ServerProperties;
import com.dy.simRtu.tcpClient.upData.UpData;
import com.dy.simRtu.tcpClient.upData.UpHeartBeat;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.mina.core.session.IoSession;
/**
 * @Author: liurunyu
 * @Date: 2024/12/6 8:49
 * @Description
 */
public class Worker {
    private static final Logger log = LogManager.getLogger(TcpClUnit.class) ;
    private static Worker instance = new Worker() ;
    private static TcpClUnitConfigVo confVo ;
    public static IoSession session ;
    private Worker(){
    }
    public static Worker getInstance(TcpClUnitConfigVo confVo){
        Worker.confVo = confVo ;
        return instance ;
    }
    public void doStart(){
        new Thread(() -> {
            Exception ex ;
            while(true){
                ex = null ;
                try {
                    new TcpConnect().createSession(
                            confVo.mwServerIp,
                            confVo.mwServerPort,
                            confVo.connectTimeout,
                            new TcpHandler(),
                            new Callback() {
                                @Override
                                public void call(Object obj) {
                                    if (obj == null) {
                                        log.error("创建网络会话返回为null");
                                    } else {
                                        Worker.session = (IoSession) obj;
                                        log.info("成功创建与通信中间件的网络连接");
                                        UpData.setSession(Worker.session);
                                        heartBeat() ;
                                    }
                                }
                                @Override
                                public void call(Object... objs) {
                                }
                                @Override
                                public void exception(Exception e) {
                                }
                            });
                }catch (Exception e){
                    ex = e ;
                }
                if(ex == null){
                    break ;
                }else{
                    try{
                        Thread.sleep(100);
                    }catch (Exception e){
                    }
                }
            }
        }).start();
    }
    private void heartBeat(){
        new Thread(new Runnable(){
            @Override
            public void run() {
                while(true){
                    try {
                        UpHeartBeat.upCd02Data(ServerProperties.rtuAddr);
                        Thread.sleep(30000L);
                        //Thread.sleep(30000000L);
                    }catch (Exception e){
                        continue;
                    }
                }
            }
        }).start();
    }
}
pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/tcpClient/upData/UpHeartBeat.java
@@ -2,6 +2,8 @@
import com.dy.common.mw.protocol.p206V1.parse.global.GlCreate;
import com.dy.common.util.ByteUtil;
import com.dy.common.util.DateTime;
import com.dy.simRtu.ServerProperties;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -22,7 +24,12 @@
        if(upHeartBeat){
            try{
                if(UpData.session != null && UpData.session.isConnected()){
                    byte[] bs = createData(rtuAddr) ;
                    byte[] bs ;
                    if(ServerProperties.argHeardBeatBy02True81False){
                        bs = createCd02Data(rtuAddr) ;
                    }else{
                        bs = createCd81Data(rtuAddr) ;
                    }
                    UpData.upSend(bs) ;
                }else{
                    log.error("未连接通信中间件,不能发送数据");
@@ -39,7 +46,7 @@
     * @return 字节数组
     * @throws Exception 异常
     */
    private static byte[] createData(String rtuAddr) throws Exception {
    private static byte[] createCd02Data(String rtuAddr) throws Exception {
        byte[] bytes = creatHead(rtuAddr, "02", (byte)0xB0);
        byte[] bs = new byte[1] ;
@@ -60,4 +67,43 @@
        return bytes ;
    }
    /**
     * 构造心跳数据
     * @return 字节数组
     * @throws Exception 异常
     */
    private static byte[] createCd81Data(String rtuAddr) throws Exception {
        byte[] bytes = creatHead(rtuAddr, "81", (byte)0xB0);
        int index = 0 ;
        byte[] bs1 = new byte[4] ;
        bs1[index++] = (byte)0x22 ;
        bs1[index++] = (byte)0x6 ;
        bs1[index++] = (byte)0x44 ;
        bs1[index++] = (byte)0x00 ;
        bytes = ByteUtil.bytesMerge(bytes, bs1) ;
        int[] ymdhms = DateTime.yyyy_MM_dd_HH_MM_SS_2_ymdhmsGroup(DateTime.yyyy_MM_dd_HH_mm_ss()) ;
        byte[] bs2 = new byte[6] ;
        index = 0 ;
        int index1 = 5 ;
        bs2[index++] = ByteUtil.int2BCD_BE(ymdhms[index1--])[0] ;
        bs2[index++] = ByteUtil.int2BCD_BE(ymdhms[index1--])[0] ;
        bs2[index++] = ByteUtil.int2BCD_BE(ymdhms[index1--])[0] ;
        bs2[index++] = ByteUtil.int2BCD_BE(ymdhms[index1--])[0] ;
        bs2[index++] = ByteUtil.int2BCD_BE(ymdhms[index1--])[0] ;
        bs2[index++] = ByteUtil.int2BCD_BE(ymdhms[index1--] - 2000)[0] ;
        bytes = ByteUtil.bytesMerge(bytes, bs2) ;
        GlCreate.createLen(bytes);//长度放字节数组中
        byte[] bsTail = GlCreate.createCrcTail4P206(bytes) ;//CRC和尾叠加字节数组中
        bytes = ByteUtil.bytesMerge(bytes, bsTail) ;
        return bytes ;
    }
}