zhubaomin
2025-04-07 1a2b07f01ba4616fd9e894dddf474b56d020158c
pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu202404/src/main/java/com/dy/simRtu202404/tcpClient/Worker.java
New file
@@ -0,0 +1,121 @@
package com.dy.simRtu202404.tcpClient;
import com.dy.simRtu202404.ServerProperties;
import com.dy.simRtu202404.tcpClient.upData.UpData;
import com.dy.simRtu202404.tcpClient.upData.UpHeartBeat;
import com.dy.simRtu202404.tcpClient.upData.UpOpenCloseValve;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.mina.core.session.IoSession;
/**
 * @Author: liurunyu
 * @Date: 2025/2/26 14:39
 * @Description
 */
public class Worker {
    private static final Logger log = LogManager.getLogger(TcpClUnit.class) ;
    protected static IoSession session ;
    private static Worker instance = new Worker() ;
    private static Integer UpHeartBeatTimes = 0 ;
    private static boolean isOpenValve = false ;
    private static boolean isCloseValve = false ;
    private static Integer OpenedCount = 0 ;
    private static Integer ClosedCount = 0 ;
    private Worker(){
    }
    public static Worker getInstance(IoSession session){
        Worker.session = session ;
        return instance ;
    }
    public void doWork(){
        UpData.setSession(Starter.session);
        new Thread(new Runnable(){
            @Override
            public void run() {
                while(true){
                    try {
                        reportData() ;
                        Thread.sleep(1000L);
                    }catch (Exception e){
                        continue;
                    }
                }
            }
        }).start();
    }
    private void reportData() throws Exception{
        UpHeartBeat.upCd02Data(ServerProperties.rtuAddr);
        UpHeartBeatTimes += 1 ;
        Thread.sleep(10000L);
        if(UpHeartBeatTimes == 2){
            UpHeartBeatTimes = 0 ;
            if(ServerProperties.type == 1){
                if(!isOpenValve){
                    UpOpenCloseValve.upCd84Data(ServerProperties.rtuAddr);
                    isOpenValve = true ;
                }
            }
            if(ServerProperties.type == 2){
                if(!isCloseValve){
                    UpOpenCloseValve.upCd85Data(ServerProperties.rtuAddr);
                    isCloseValve = true ;
                }
            }
            if(ServerProperties.type == 3){
                if(!isOpenValve){
                    UpOpenCloseValve.upCd84Data(ServerProperties.rtuAddr);
                    isOpenValve = true ;
                }
                OpenedCount += 1 ;
                if(OpenedCount == 10){
                    OpenedCount = 0 ;
                    if(!isCloseValve){
                        UpOpenCloseValve.upCd85Data(ServerProperties.rtuAddr);
                        isCloseValve = true ;
                    }
                }
            }
            if(ServerProperties.type == 4){
                if(!isOpenValve && !isCloseValve){
                    //初次
                    UpOpenCloseValve.upCd84Data(ServerProperties.rtuAddr);
                    isOpenValve = true ;
                    isCloseValve = false ;
                }else{
                    if(isOpenValve){
                        OpenedCount += 1 ;
                    }
                    if(OpenedCount == 10){
                        OpenedCount = 0 ;
                        UpOpenCloseValve.upCd85Data(ServerProperties.rtuAddr);
                        isOpenValve = false ;
                        isCloseValve = true ;
                    }
                    if(isCloseValve){
                        ClosedCount += 1 ;
                    }
                    if(ClosedCount == 10){
                        ClosedCount = 0 ;
                        UpOpenCloseValve.upCd84Data(ServerProperties.rtuAddr);
                        isOpenValve = true ;
                        isCloseValve = false ;
                    }
                }
            }
        }
    }
}