CZT
2023-10-07 876270f40c443d99729c5d98d629b74fa5a7a1c0
igds-protocol-beibo/src/main/java/com/ld/igds/protocol/beibo/grainv1/analysis/AnalysisService.java
@@ -26,6 +26,7 @@
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.*;
/**
@@ -54,21 +55,21 @@
    public static String result = "";
    public static Map<String, String> contextMap = new HashMap<>();
    public static double ERROR_CHECK_TAG = -100.0;
    public static double FAULT_CHECK_TAG = 85.0;
    public static double ERROR_CHECK_TAG2 = 50;
    public static String ERROR_HEX = "FFFF";
    public static double MAX_TEMP = -50.0;
    public static double MIN_TEMP = 50.0;
    public static void main(String[] args) {
        String str = "00000000FFEB90FEAA41E821EC21EA21EC21ED21EC21E921E221E921E921E921EC21E921ED21F021E521E521E221E421E321E321E121DE21D721E021DD21DC21DF21FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";
        System.out.println(str.substring(9*2, 10*2));
        System.out.println(str.substring(9 * 2, 10 * 2));
    }
    /**
     * 00000000FFEB90FEAA41E821EC21EA21EC21ED21EC21E921E221E921E921E921EC21E921ED21F021E521E521E221E421E321E321E121DE21D721E021DD21DC21DF21FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
     *
@@ -77,59 +78,66 @@
    public void analysis(String ip, int port, String hexStr) {
        result += hexStr;
        if(result.length() < 1066*2){
        if (result.length() < 1066 * 2) {
            log.info("分机------->>平台,报文长度不够,等待下一包数据");
            return;
        }
        //封装数据
        ReMessage reMessage = ReMessageBuilder.getInstance().buildMessage(result);
        reMessage.setIp(ip);
        reMessage.setPort(port);
        log.info("分机------->>平台:粮情完整报文信息={}", reMessage);
        result = "";
        if (!BeiboGrainServerUtils.MSG_START.startsWith(reMessage.getStartStr())) {
            log.error("分机------->>平台,解析粮情失败:报文起始符={}错误,不解析", reMessage.getStartStr());
            return;
        try {
            //封装数据
            ReMessage reMessage = ReMessageBuilder.getInstance().buildMessage(result);
            reMessage.setIp(ip);
            reMessage.setPort(port);
            log.info("分机------->>平台:粮情完整报文信息={}", reMessage);
            result = "";
            if (!BeiboGrainServerUtils.MSG_START.startsWith(reMessage.getStartStr())) {
                log.error("分机------->>平台,解析粮情失败:报文起始符={}错误,不解析", reMessage.getStartStr());
                return;
            }
            //根据分机地址获取分机信息
            DeviceSer ser = coreSerService.getCacheSer(ContextUtil.getDefaultCompanyId(), reMessage.getSerId());
            if (ser == null) {
                log.error("分机-------->>平台,解析粮情失败,未获取到系统粮情主机配置:" + reMessage.getSerId());
                return;
            }
            List<ExeRequest> list = exeOrderService.getInProgressOrderBySerId(BizType.GRAIN.getCode(), ser.getId());
            if (null == list || list.isEmpty()) {
                String info = "粮情解析失败:分机=" + ser.getName() + "没有获取到所属仓库信息。";
                log.error("分机------>>>平台:" + info);
                notifyGrainInvoker.notifyWeb(ser.getCompanyId(), OrderRespEnum.MSG_ERROR, BizType.GRAIN, info);
                return;
            }
            ExeRequest exeRequest = list.get(0);
            log.info("获取粮情命令信息={}", exeRequest);
            if (null == exeRequest) {
                String info = "粮情解析失败:分机=" + ser.getName() + "没有获取历史命令。";
                log.error("贝博分机------>>>平台:" + info);
                return;
            }
            DepotConf depotConf = commonService.getCacheDepotConf(exeRequest.getCompanyId(), exeRequest.getDepotId());
            if (null == depotConf) {
                String info = "粮情解析失败:分机=" + ser.getName() + "没有获取到粮情参数配置信息。";
                log.error("分机------>>>平台:" + info);
                notifyGrainInvoker.notifyWeb(ser.getCompanyId(),
                        OrderRespEnum.MSG_ERROR, BizType.GRAIN, info);
                return;
            }
            // 首先获取到系统参数,判断是否需要批次自动优化
            DicSysConf sysConf = commonService.getCacheSysConf(ser.getCompanyId());
            reMessage.setCompanyId(ser.getCompanyId());
            analysisGrain(depotConf, reMessage, ser, exeRequest, sysConf);
        } catch (Exception e) {
            result = "";
            log.error("分机------->>平台,解析粮情异常:原因={}", e.toString());
        }
        //根据分机地址获取分机信息
        DeviceSer ser = coreSerService.getCacheSer(ContextUtil.getDefaultCompanyId(), reMessage.getSerId());
        if (ser == null) {
            log.error("分机-------->>平台,解析粮情失败,未获取到系统粮情主机配置:" + reMessage.getSerId());
            return;
        }
        List<ExeRequest> list = exeOrderService.getInProgressOrderBySerId(BizType.GRAIN.getCode(), ser.getId());
        if (null == list || list.isEmpty()) {
            String info = "粮情解析失败:分机=" + ser.getName() + "没有获取到所属仓库信息。";
            log.error("分机------>>>平台:" + info);
            notifyGrainInvoker.notifyWeb(ser.getCompanyId(), OrderRespEnum.MSG_ERROR, BizType.GRAIN, info);
            return;
        }
        ExeRequest exeRequest = list.get(0);
        log.info("获取粮情命令信息={}", exeRequest);
        if (null == exeRequest) {
            String info = "粮情解析失败:分机=" + ser.getName() + "没有获取历史命令。";
            log.error("贝博分机------>>>平台:" + info);
            return;
        }
        DepotConf depotConf = commonService.getCacheDepotConf(exeRequest.getCompanyId(), exeRequest.getDepotId());
        if (null == depotConf) {
            String info = "粮情解析失败:分机=" + ser.getName() + "没有获取到粮情参数配置信息。";
            log.error("分机------>>>平台:" + info);
            notifyGrainInvoker.notifyWeb(ser.getCompanyId(),
                    OrderRespEnum.MSG_ERROR, BizType.GRAIN, info);
            return;
        }
        // 首先获取到系统参数,判断是否需要批次自动优化
        DicSysConf sysConf = commonService.getCacheSysConf(ser.getCompanyId());
        reMessage.setCompanyId(ser.getCompanyId());
        analysisGrain(depotConf, reMessage, ser, exeRequest, sysConf);
    }
@@ -255,15 +263,22 @@
                temp = "0000";
            }
            //高低位转换后转为16位二进制字符串
            temp = BytesUtil.toBinary8StringSame(BytesUtil.hexToInt(BytesUtil.tran_LH(temp)), 16);
            //符号位
            symbol = Integer.valueOf(temp.substring(0, 1));
            //获取温度值
            tempValue = BytesUtil.hexToInt(BytesUtil.binToHex(temp.substring(6))) * 0.0625;
            if(symbol == 1){
                tempValue = (1- BytesUtil.hexToInt(BytesUtil.binToHex(temp.substring(6))))*0.0625;
            temp = BytesUtil.tran_LH(temp);
            if(ERROR_HEX.equals(temp)){
                tempValue = Constant.ERROR_TEMP;
            }else {
                //10进制转16位的2进制
                temp = BytesUtil.hexString2binaryString(temp, 16);
                //符号位
                symbol = Integer.valueOf(temp.substring(0, 1));
                //获取温度值
                tempValue = BytesUtil.biannary2Decimal(temp.substring(6)) * 0.0625;
                //若为负,则补码:取反加1
                if (symbol == 1) {
                    tempValue = ((~BytesUtil.biannary2Decimal(temp.substring(6))) + 1) * 0.0625;
                }
                tempValue = NumberUtil.keepPrecision(tempValue, 1);
            }
            tempValue = NumberUtil.keepPrecision(tempValue, 1);
            //非正常值
            if (tempValue > ERROR_CHECK_TAG2) {
@@ -370,16 +385,22 @@
                temp = "0000";
            }
            //高低位转换后转为16位二进制字符串
            temp = BytesUtil.toBinary8StringSame(BytesUtil.hexToInt(BytesUtil.tran_LH(temp)), 16);
            //符号位
            symbol = Integer.valueOf(temp.substring(0, 1));
            //获取温度值
            tempValue = BytesUtil.hexToInt(BytesUtil.binToHex(temp.substring(6))) * 0.0625;
            if(symbol == 1){
                tempValue = (1- BytesUtil.hexToInt(BytesUtil.binToHex(temp.substring(6))))*0.0625;
            if(ERROR_HEX.equals(temp)){
                tempValue = Constant.ERROR_TEMP;
            }else {
                //10进制转16位的2进制
                temp = BytesUtil.hexString2binaryString(temp, 16);
                //符号位
                symbol = Integer.valueOf(temp.substring(0, 1));
                //获取温度值
                tempValue = BytesUtil.biannary2Decimal(temp.substring(6)) * 0.0625;
                //若为负,则补码:取反加1
                if (symbol == 1) {
                    tempValue = ((~BytesUtil.biannary2Decimal(temp.substring(6))) + 1) * 0.0625;
                }
                tempValue = NumberUtil.keepPrecision(tempValue, 1);
            }
            tempValue = NumberUtil.keepPrecision(tempValue, 1);
            // 说明解析的数据有问题
            if (tempValue == ERROR_CHECK_TAG || tempValue == ERROR_CHECK_TAG2) {
                tempValue = Constant.ERROR_TEMP;
@@ -442,7 +463,7 @@
        grain.setTempIn(Constant.ERROR_TEMP);
        grain.setHumidityIn(Constant.ERROR_TEMP);
        grain.setReceiveDate(msg.getReceiveDate());
        grain.setReceiveDate(new Date());
        grain.setRemark("粮温正常");
        // 获取缓存中的命令信息
@@ -524,7 +545,7 @@
        grain.setTempIn(Constant.ERROR_TEMP);
        grain.setHumidityIn(Constant.ERROR_TEMP);
        grain.setReceiveDate(msg.getReceiveDate());
        grain.setReceiveDate(new Date());
        grain.setRemark("粮温正常");
        grain.setCheckUser(exeRequest.getExeUser());