zhangnaisong
2024-07-27 6bab172dc8767e93c8889c15a146c76e4d38e9c0
ard-work/src/main/java/com/ruoyi/sy/service/impl/ArdTankLockServiceImpl.java
@@ -1,5 +1,6 @@
package com.ruoyi.sy.service.impl;
import java.io.IOException;
import java.util.*;
import com.alibaba.fastjson.JSON;
@@ -13,6 +14,9 @@
import com.ruoyi.system.mapper.SysConfigMapper;
import com.ruoyi.utils.forest.SYClient;
import com.ruoyi.utils.websocket.util.WebSocketUtils;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;
@@ -56,6 +60,8 @@
    @Resource
    private SYClient sYClient;
    private Map<String,Thread> alarmMap = new HashMap();//电磁锁历史报警线程容器
    /**
     * 查询电磁锁
@@ -529,13 +535,13 @@
                }else{
                    lockMap.put("onlineTime",ardTankLock.getOnlineTime());
                }
                Map<String,Object> ardTankLockState = new HashMap();
                //Map<String,Object> ardTankLockState = new HashMap();
                ArdTankLockState ardTankLockStateResult = ardTankLockStateMapper.selectArdTankLockStateByLockIdLimitOne(ardTankLock.getId());
                if(ardTankLockStateResult == null){
                    ardTankLockState.put("id","");
                    /*ardTankLockState.put("id","");
                    ardTankLockState.put("lockId","");
                    ardTankLockState.put("lockNum","");
                    ardTankLockState.put("lockState","");
                    ardTankLockState.put("lockNum","");*/
                    /*ardTankLockState.put("lockState","");
                    ardTankLockState.put("lockPositionState","");
                    ardTankLockState.put("lockShellState","");
                    ardTankLockState.put("restartState","");
@@ -546,12 +552,25 @@
                    ardTankLockState.put("uploadTime","");
                    ardTankLockState.put("uniqueMark","");
                    ardTankLockState.put("lockStateMark",false);
                    lockMap.put("ardTankLockState",ardTankLockState);
                    lockMap.put("ardTankLockState",ardTankLockState);*/
                    lockMap.put("lockState","");
                    lockMap.put("lockPositionState","");
                    lockMap.put("lockShellState","");
                    lockMap.put("restartState","");
                    lockMap.put("batterVoltage","");
                    lockMap.put("batterVoltageAlarm",false);
                    lockMap.put("powerVoltage","");
                    lockMap.put("powerVoltageAlarm",false);
                    lockMap.put("uploadTime","");
                    lockMap.put("uniqueMark","");
                    lockMap.put("lockStateMark",false);
                    lockMap.put("alarmMark",false);
                    lockMap.put("alarmInfo","");
                }else{
                    ardTankLockState.put("id",ardTankLockStateResult.getId());
                    /*ardTankLockState.put("id",ardTankLockStateResult.getId());
                    ardTankLockState.put("lockId",ardTankLockStateResult.getLockId());
                    ardTankLockState.put("lockNum",ardTankLockStateResult.getLockNum());
                    ardTankLockState.put("lockState",ardTankLockStateResult.getLockState());
                    ardTankLockState.put("lockNum",ardTankLockStateResult.getLockNum());*/
                    /*ardTankLockState.put("lockState",ardTankLockStateResult.getLockState());
                    ardTankLockState.put("lockPositionState",ardTankLockStateResult.getLockPositionState());
                    ardTankLockState.put("lockShellState",ardTankLockStateResult.getLockShellState());
                    ardTankLockState.put("restartState",ardTankLockStateResult.getRestartState());
@@ -570,7 +589,44 @@
                    ardTankLockState.put("uploadTime",ardTankLockStateResult.getUploadTime());
                    ardTankLockState.put("uniqueMark",ardTankLockStateResult.getUniqueMark());
                    ardTankLockState.put("lockStateMark",ardTankLockStateResult.getLockStateMark());
                    lockMap.put("ardTankLockState",ardTankLockState);
                    lockMap.put("ardTankLockState",ardTankLockState);*/
                    String alarmInfo = "";
                    lockMap.put("lockState",ardTankLockStateResult.getLockState());
                    if(ardTankLockStateResult.getLockState().equals("异常状态")){
                        alarmInfo = alarmInfo + "锁芯异常;";
                    }
                    lockMap.put("lockPositionState",ardTankLockStateResult.getLockPositionState());
                    if(ardTankLockStateResult.getLockPositionState().equals("异常")){
                        alarmInfo = alarmInfo + "位置异常;";
                    }
                    lockMap.put("lockShellState",ardTankLockStateResult.getLockShellState());
                    if(ardTankLockStateResult.getLockShellState().equals("异常")){
                        alarmInfo = alarmInfo + "外壳异常;";
                    }
                    lockMap.put("restartState",ardTankLockStateResult.getRestartState());
                    lockMap.put("batterVoltage",ardTankLockStateResult.getBatterVoltage());
                    if(Double.parseDouble(ardTankLockStateResult.getBatterVoltage()) <= lockBatteryVoltage){
                        lockMap.put("batterVoltageAlarm",true);
                        alarmInfo = alarmInfo + "电池电压低于" + lockBatteryVoltage + ";";
                    }else{
                        lockMap.put("batterVoltageAlarm",false);
                    }
                    lockMap.put("powerVoltage",ardTankLockStateResult.getPowerVoltage());
                    if(Double.parseDouble(ardTankLockStateResult.getPowerVoltage()) <= lockPowerVoltage){
                        lockMap.put("powerVoltageAlarm",true);
                        alarmInfo = alarmInfo + "电源电压低于" + lockBatteryVoltage + ";";
                    }else{
                        lockMap.put("powerVoltageAlarm",false);
                    }
                    lockMap.put("uploadTime",ardTankLockStateResult.getUploadTime());
                    lockMap.put("uniqueMark",ardTankLockStateResult.getUniqueMark());
                    lockMap.put("lockStateMark",ardTankLockStateResult.getLockStateMark());
                    if(alarmInfo.equals("")){
                        lockMap.put("alarmMark",false);
                    }else{
                        lockMap.put("alarmMark",true);
                    }
                    lockMap.put("alarmInfo",alarmInfo);
                }
                lockList.add(lockMap);
            }
@@ -581,4 +637,83 @@
            WebSocketUtils.sendMessage(ONLINE_USER_SESSIONS.get(userId), JSON.toJSONString(data));
        }
    }
    @Override
    public void getAlarm(String syUserId,String syPassword) {
        List<SysConfig> syURLResult = sysConfigMapper.selectByType("syCarPT");
        String syURL = "";
        if(syURLResult.size() != 0){
            syURL = syURLResult.get(0).getConfigValue();
        }else{
            return;
        }
        String finalSyURL = syURL;
        String passwordMd5 = DigestUtils.md5Hex(syPassword);
        Map<String, Object> LogInResult = sYClient.logIn(syURL, passwordMd5, syUserId);
        String sessionId = (String) LogInResult.get("sessionId");
        List<ArdTankLock> ardTankLockResult = ardTankLockMapper.getAll();
        for(ArdTankLock ardTankLock : ardTankLockResult){
            if(this.alarmMap.get(ardTankLock.getLockNum()) != null){
                continue;//当前锁已在获取报警,不在创建线程
            }
            Thread thread = new Thread(finalSyURL){
                @Override
                public void run() {
                    try{
                        String lockNum = ardTankLock.getLockNum();
                        String lockNumHead = lockNum.substring(0,2);
                        String lockNumTail = lockNum.substring(2,4);
                        String checkNum = Integer.toHexString(Integer.parseInt(lockNumHead, 16)//锁编号高8位
                                + Integer.parseInt(lockNumTail, 16)//锁编号低8位
                                + Integer.parseInt("06", 16));//设置锁芯状态功能码06
                        if(checkNum.length() == 1){
                            checkNum = "0" + checkNum;
                        }
                        if(checkNum.length() > 2){
                            checkNum = checkNum.substring(checkNum.length() - 2,checkNum.length());
                        }
                        String paramsStr = lockNum + "06" + checkNum;
                        paramsStr = "%7B%22type%22%3A%22ff%22%2C%22dataCnt%22%3A%22" + paramsStr + "%22%7D";
                        Map<String,Object> result = sendCmd(finalSyURL, syUserId, ardTankLock.getCarId(), 199, "DataDownTransfer", paramsStr, sessionId);
                        alarmMap.remove(ardTankLock.getLockNum());
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                }
            };
            thread.start();
            alarmMap.put(ardTankLock.getLockNum(),thread);
        }
    }
    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>) com.alibaba.fastjson2.JSON.parse(message);
            return map;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return new HashMap();
        }
    }
}