zhangnaisong
2024-08-01 93cecfaf0fae080ebd0212adea714726290d0d4a
ard-work/src/main/java/com/ruoyi/sy/controller/ArdSyCarController.java
@@ -1,5 +1,6 @@
package com.ruoyi.sy.controller;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.*;
@@ -12,8 +13,14 @@
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.sy.domain.ArdTankLock;
import com.ruoyi.sy.gps31.PushClientImplSerialPort;
import com.ruoyi.sy.param.ArdSyCarParam;
import com.ruoyi.sy.service.IArdTankAbnormalParkAlarmService;
import com.ruoyi.sy.service.IArdTankLockService;
import com.ruoyi.sy.service.impl.ArdTankLockServiceImpl;
import com.ruoyi.sy.vo.ArdSyCarVo;
import com.ruoyi.sy.domain.ArdSyUser;
import com.ruoyi.sy.gps31.PositionContainer;
@@ -24,10 +31,14 @@
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.impl.SysConfigServiceImpl;
import com.ruoyi.utils.forest.SYClient;
import com.ruoyi.utils.result.Results;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
@@ -41,6 +52,8 @@
import com.ruoyi.sy.service.IArdSyCarService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;
import static com.ruoyi.utils.websocket.util.WebSocketUtils.ONLINE_USER_SESSIONS;
/**
 * 三一车辆Controller
@@ -70,6 +83,12 @@
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private IArdTankAbnormalParkAlarmService ardTankAbnormalParkAlarmService;
    @Autowired
    private IArdTankLockService ardTankLockService;
    @Resource
    private SYClient sYClient;
@@ -126,6 +145,356 @@
        PushClientImplAlarm pushClientImplAlarm = new PushClientImplAlarm(ip,userId,password);
        Thread pushClientImplAlarmThread = new Thread(pushClientImplAlarm);
        pushClientImplAlarmThread.start();
        //串口线程
        PushClientImplSerialPort pushClientImplSerialPort = new PushClientImplSerialPort(ip,userId,password);
        Thread pushClientImplSerialPortThread = new Thread(pushClientImplSerialPort);
        pushClientImplSerialPortThread.start();
        //删除结束时间为null的记录
        int result = ardTankAbnormalParkAlarmService.deleteArdTankAbnormalParkAlarmByEndTime();
        List<ArdSyCar> ardSyCarAndLockList = ardSyCarService.selectArdSyCarAndTankLock();//查询全部罐车及下挂电磁锁
        for(ArdSyCar ardSyCar : ardSyCarAndLockList){//构造容器
            Map<String,Map<String,Thread>> carMap = new HashMap();//车辆容器
            for(ArdTankLock ardTankLock : ardSyCar.getArdTankLockList()){
                Map<String,Thread> lockMap = new HashMap();//锁容器
                carMap.put(ardTankLock.getLockNum(),lockMap);
            }
            PositionContainer.getCarLockProcessThreadMap().put(ardSyCar.getCarId(),carMap);
        }
        Timer lockStateTimer = new Timer();//定时获取各个锁的状态
        TimerTask lockStateTask =new TimerTask(){
            @Override
            public void run(){
                Thread thread = new Thread(){
                    @Override
                    public void run() {
                        SysConfig config = new SysConfig();
                        config.setConfigKey("syCarPT");
                        List<SysConfig> sysConfigResult = sysConfigService.selectConfigList(config);
                        String syURL = "";
                        if(sysConfigResult.size() == 0){
                            return;
                        }else{
                            syURL = sysConfigResult.get(0).getConfigValue();
                        }
                        String passwordMd5 = DigestUtils.md5Hex(password);
                        Map<String, Object> LogInResult = sYClient.logIn(syURL, passwordMd5, userId);
                        String sessionId = (String) LogInResult.get("sessionId");
                        String finalSyURL = syURL;
                        List<ArdTankLock> result = ardTankLockService.getArdTankLockAll();//查询全部电磁锁
                        for(ArdTankLock ardTankLock : result){
                            Thread lockThread = new Thread(finalSyURL){
                                @Override
                                public void run() {
                                    String lockNumHead = ardTankLock.getLockNum().substring(0,2);
                                    String lockNumTail = ardTankLock.getLockNum().substring(2,4);
                                    //计算校验
                                    String checkNum = Integer.toHexString(Integer.parseInt(lockNumHead, 16) + Integer.parseInt(lockNumTail, 16) + Integer.parseInt("01", 16));
                                    if(checkNum.length() == 1){
                                        checkNum = "0" + checkNum;
                                    }else if(checkNum.length() > 2){
                                        checkNum = checkNum.substring(checkNum.length() - 2,checkNum.length());
                                    }
                                    String paramsStr = "%7B%22type%22%3A%22ff%22%2C%22dataCnt%22%3A%22" + ardTankLock.getLockNum() + "01" + checkNum + "%22%7D";
                                    try{
                                        sendCmd(finalSyURL,userId,ardTankLock.getCarId(),199,"DataDownTransfer", paramsStr,sessionId);
                                        PositionContainer.getCarLockProcessThreadMap().get(ardTankLock.getCarId()).get(ardTankLock.getLockNum()).remove("lockState");
                                    }catch(Exception e){
                                        PositionContainer.getCarLockProcessThreadMap().get(ardTankLock.getCarId()).get(ardTankLock.getLockNum()).remove("lockState");
                                    }
                                }
                            };
                            //lockThread.start();
                            //将状态线程放入容器
                            PositionContainer.getCarLockProcessThreadMap().get(ardTankLock.getCarId()).get(ardTankLock.getLockNum()).put("lockState",lockThread);
                        }
                    }
                };
                thread.start();
            }
        };
        //lockStateTimer.scheduleAtFixedRate(lockStateTask,date,3000);
        lockStateTimer.scheduleAtFixedRate(lockStateTask,date,5000);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Timer lockCheckTimeTimer = new Timer();//定时校准时间
        TimerTask lockCheckTimeTask = new TimerTask(){
            @Override
            public void run() {
                SysConfig config = new SysConfig();
                config.setConfigKey("syCarPT");
                List<SysConfig> sysConfigResult = sysConfigService.selectConfigList(config);
                String syURL = "";
                if(sysConfigResult.size() == 0){
                    return;
                }else{
                    syURL = sysConfigResult.get(0).getConfigValue();
                }
                String passwordMd5 = DigestUtils.md5Hex(password);
                Map<String, Object> LogInResult = sYClient.logIn(syURL, passwordMd5, userId);
                String sessionId = (String) LogInResult.get("sessionId");
                String finalSyURL = syURL;
                List<ArdTankLock> result = ardTankLockService.getArdTankLockAll();//查询全部电磁锁
                for(ArdTankLock ardTankLock : result){
                    Thread lockThread = new Thread(finalSyURL){
                        @Override
                        public void run() {
                            //获取时间并拆分年月日时分秒
                            String nowTime = sdf.format(new Date());
                            String nowYear = nowTime.substring(2,4);
                            String nowMonth = nowTime.substring(5,7);
                            String nowDay = nowTime.substring(8,10);
                            String nowHour = nowTime.substring(11,13);
                            String nowMinute = nowTime.substring(14,16);
                            String nowSecond = nowTime.substring(17,19);
                            String nowYearHex = Integer.toHexString(Integer.parseInt(nowYear));
                            if(nowYearHex.length() == 1){
                                nowYearHex = "0" + nowYearHex;
                            }
                            String nowMonthHex = Integer.toHexString(Integer.parseInt(nowMonth));
                            if(nowMonthHex.length() == 1){
                                nowMonthHex = "0" + nowMonthHex;
                            }
                            String nowDayHex = Integer.toHexString(Integer.parseInt(nowDay));
                            if(nowDayHex.length() == 1){
                                nowDayHex = "0" + nowDayHex;
                            }
                            String nowHourHex = Integer.toHexString(Integer.parseInt(nowHour));
                            if(nowHourHex.length() == 1){
                                nowHourHex = "0" + nowHourHex;
                            }
                            String nowMinuteHex = Integer.toHexString(Integer.parseInt(nowMinute));
                            if(nowMinuteHex.length() == 1){
                                nowMinuteHex = "0" + nowMinuteHex;
                            }
                            String nowSecondHex = Integer.toHexString(Integer.parseInt(nowSecond));
                            if(nowSecondHex.length() == 1){
                                nowSecondHex = "0" + nowSecondHex;
                            }
                            //16进制时间
                            String timeHex = nowYearHex + nowMonthHex + nowDayHex + nowHourHex + nowMinuteHex + nowSecondHex;
                            String lockNumHead = ardTankLock.getLockNum().substring(0,2);
                            String lockNumTail = ardTankLock.getLockNum().substring(2,4);
                            //计算校验
                            String checkNum = Integer.toHexString(Integer.parseInt(lockNumHead, 16) + Integer.parseInt(lockNumTail, 16) + Integer.parseInt("08", 16) +
                                    Integer.parseInt(nowYearHex, 16) + Integer.parseInt(nowMonthHex, 16) + Integer.parseInt(nowDayHex, 16) +
                                    Integer.parseInt(nowHourHex, 16) + Integer.parseInt(nowMinuteHex, 16) + Integer.parseInt(nowSecondHex, 16));
                            if(checkNum.length() == 1){
                                checkNum = "0" + checkNum;
                            }else if(checkNum.length() > 2){
                                checkNum = checkNum.substring(checkNum.length() - 2,checkNum.length());
                            }
                            String paramsStr = "%7B%22type%22%3A%22ff%22%2C%22dataCnt%22%3A%22" + ardTankLock.getLockNum() + "08" + timeHex + checkNum + "%22%7D";
                            try{
                                sendCmd(finalSyURL,userId,ardTankLock.getCarId(),199,"DataDownTransfer", paramsStr,sessionId);
                                PositionContainer.getCarLockProcessThreadMap().get(ardTankLock.getCarId()).get(ardTankLock.getLockNum()).remove("lockTime");
                            }catch(Exception e){
                                PositionContainer.getCarLockProcessThreadMap().get(ardTankLock.getCarId()).get(ardTankLock.getLockNum()).remove("lockTime");
                            }
                        }
                    };
                    //lockThread.start();
                    PositionContainer.getCarLockProcessThreadMap().get(ardTankLock.getCarId()).get(ardTankLock.getLockNum()).put("lockTime",lockThread);
                }
            }
        };
        //lockCheckTimeTimer.scheduleAtFixedRate(lockCheckTimeTask,date,7000);
        lockCheckTimeTimer.scheduleAtFixedRate(lockCheckTimeTask,date,67000);
        Timer sendTimer = new Timer();//定时发送锁状态
        TimerTask sendLockStateTask = new TimerTask(){
            @Override
            public void run() {
                for (String key : ONLINE_USER_SESSIONS.keySet()){
                    Thread lockThread = new Thread() {
                        @Override
                        public void run() {
                            ardTankLockService.sendLockState(key,userId,password);
                        }
                    };
                    lockThread.start();
                }
            }
        };
        sendTimer.scheduleAtFixedRate(sendLockStateTask,date,3000);
        Timer passwordTimer = new Timer();//定时查看电磁锁密码状态
        TimerTask passwordTask = new TimerTask(){
            @Override
            public void run() {
                ardTankLockService.readPassword(userId,password);
            }
        };
        //passwordTimer.scheduleAtFixedRate(passwordTask,date,11000);
        passwordTimer.scheduleAtFixedRate(passwordTask,date,13000);
        Timer lockRestartStateTimer = new Timer();//定时各个锁的复位状态
        TimerTask lockRestartStateTask = new TimerTask(){
            @Override
            public void run(){
                Thread thread = new Thread(){
                    @Override
                    public void run() {
                        SysConfig config = new SysConfig();
                        config.setConfigKey("syCarPT");
                        List<SysConfig> sysConfigResult = sysConfigService.selectConfigList(config);
                        String syURL = "";
                        if(sysConfigResult.size() == 0){
                            return;
                        }else{
                            syURL = sysConfigResult.get(0).getConfigValue();
                        }
                        String passwordMd5 = DigestUtils.md5Hex(password);
                        Map<String, Object> LogInResult = sYClient.logIn(syURL, passwordMd5, userId);
                        String sessionId = (String) LogInResult.get("sessionId");
                        String finalSyURL = syURL;
                        List<ArdTankLock> result = ardTankLockService.getArdTankLockAllWithState();//查询全部电磁锁及最终状态
                        for(ArdTankLock ardTankLock : result){
                            String restartState = ardTankLock.getRestartState();
                            String sRestartState = "";
                            if(ardTankLock.getArdTankLockState() != null){
                                sRestartState = ardTankLock.getArdTankLockState().getRestartState();
                            }
                            if(sRestartState.equals("")){
                                continue;
                            }else{
                                sRestartState = sRestartState.substring(0,2);
                                if(!restartState.equals(sRestartState)){
                                    Thread lockThread = new Thread(finalSyURL){
                                        @Override
                                        public void run() {
                                            String process = "";
                                            if(restartState.equals("开锁")){
                                                process = "01";
                                            }else if(restartState.equals("关锁")){
                                                process = "02";
                                            }else{
                                                return;
                                            }
                                            String lockNumHead = ardTankLock.getLockNum().substring(0,2);
                                            String lockNumTail = ardTankLock.getLockNum().substring(2,4);
                                            //计算校验
                                            String checkNum = Integer.toHexString(Integer.parseInt(lockNumHead, 16) + Integer.parseInt(lockNumTail, 16) + Integer.parseInt("03", 16) + Integer.parseInt(process, 16));
                                            if(checkNum.length() == 1){
                                                checkNum = "0" + checkNum;
                                            }else if(checkNum.length() > 2){
                                                checkNum = checkNum.substring(checkNum.length() - 2,checkNum.length());
                                            }
                                            String paramsStr = "%7B%22type%22%3A%22ff%22%2C%22dataCnt%22%3A%22" + ardTankLock.getLockNum() + "03" + process + checkNum + "%22%7D";
                                            try{
                                                sendCmd(finalSyURL,userId,ardTankLock.getCarId(),199,"DataDownTransfer", paramsStr,sessionId);
                                                PositionContainer.getCarLockProcessThreadMap().get(ardTankLock.getCarId()).get(ardTankLock.getLockNum()).remove("lockRestart");
                                            }catch (Exception e){
                                                PositionContainer.getCarLockProcessThreadMap().get(ardTankLock.getCarId()).get(ardTankLock.getLockNum()).remove("lockRestart");
                                            }
                                        }
                                    };
                                    //lockThread.start();
                                    PositionContainer.getCarLockProcessThreadMap().get(ardTankLock.getCarId()).get(ardTankLock.getLockNum()).put("lockRestart",lockThread);
                                }
                            }
                        }
                    }
                };
                thread.start();
            }
        };
        lockRestartStateTimer.scheduleAtFixedRate(lockRestartStateTask,date,17000);
        Set<String> lockNumSet = new HashSet();//存放发送过的锁编号
        Timer mainLockTimer = new Timer();//主定时器
        TimerTask mainLockTask = new TimerTask(){
            @Override
            public void run() {
                Long count = ardTankLockService.getArdTankLockCount();
                if(lockNumSet.size() >= count){//防止锁被删除,采用大于等于
                    lockNumSet.clear();//全部执行过,清空容器
                }
                for(String carId : PositionContainer.getCarLockProcessThreadMap().keySet()){
                    Thread thread = new Thread(){
                        @Override
                        public void run() {
                            Map<String, Map<String,Thread>> lockMap = PositionContainer.getCarLockProcessThreadMap().get(carId);
                            for(String lockNum : lockMap.keySet()){
                                if(lockNumSet.contains(lockNum)){//若执行过,本次忽略
                                    continue;
                                }
                                if(lockMap.get(lockNum).get("lockProcess") != null){
                                    break;//锁操作级别最高
                                }else{
                                    if(lockMap.get(lockNum).get("lockTime") != null){
                                        if(lockMap.get(lockNum).get("lockTime").isAlive()){
                                            lockNumSet.add(lockNum);
                                            break;//锁校时次之
                                        }else{
                                            lockMap.get(lockNum).get("lockTime").start();
                                            lockNumSet.add(lockNum);
                                            break;//锁校时次之
                                        }
                                    }else{
                                        if(lockMap.get(lockNum).get("lockPassword") != null){
                                            if(lockMap.get(lockNum).get("lockPassword").isAlive()){
                                                lockNumSet.add(lockNum);
                                                break;//锁密码次之
                                            }else{
                                                lockMap.get(lockNum).get("lockPassword").start();
                                                lockNumSet.add(lockNum);
                                                break;//锁密码次之
                                            }
                                        }else{
                                            if(lockMap.get(lockNum).get("lockRestart") != null) {
                                                if(lockMap.get(lockNum).get("lockRestart").isAlive()){
                                                    lockNumSet.add(lockNum);
                                                }else{
                                                    lockMap.get(lockNum).get("lockRestart").start();
                                                    lockNumSet.add(lockNum);
                                                    break;//锁复位次之
                                                }
                                            }else{
                                                if(lockMap.get(lockNum).get("lockState") != null) {
                                                    if(lockMap.get(lockNum).get("lockState").isAlive()){
                                                        lockNumSet.add(lockNum);
                                                    }else{
                                                        lockMap.get(lockNum).get("lockState").start();
                                                        lockNumSet.add(lockNum);
                                                        break;//锁状态次之
                                                    }
                                                }else{
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    };
                    thread.start();
                }
            }
        };
        mainLockTimer.scheduleAtFixedRate(mainLockTask,date,2000);
    }
    /**
@@ -499,4 +868,32 @@
        }
    }
    public Map<String,Object> sendCmd(String syURL,String userId,String carId,Integer cmdId,String cmd,String paramsStr,String sessionId){
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(syURL + "/gps-web/api/send_cmd.jsp?params="+paramsStr+"&userId="+userId+"&carId="+carId+"&cmdId="+cmdId+"&cmd="+cmd+"&sessionId="+sessionId)
                .build();
        //System.out.println("url = " + syURL + "/gps-web/api/send_cmd.jsp?params="+paramsStr+"&userId="+userId+"&carId="+carId+"&cmdId="+cmdId+"&cmd="+cmd+"&sessionId="+sessionId);
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        okhttp3.ResponseBody responseBody = response.body();
        try {
            String message = responseBody.string();// 响应体
            Map<String,Object> map = (Map<String, Object>) JSON.parse(message);
            return map;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return new HashMap();
        }
    }
}