package com.ld.igds.protocol.beibo.grainv1.analysis; import com.ld.igds.common.CoreCommonService; import com.ld.igds.common.CoreSerService; import com.ld.igds.constant.BizType; import com.ld.igds.constant.Constant; import com.ld.igds.constant.DepotType; import com.ld.igds.grain.GrainUtil; import com.ld.igds.grain.dto.GrainItemInfo; import com.ld.igds.io.constant.OrderRespEnum; import com.ld.igds.io.notify.NotifyGrainInvoker; import com.ld.igds.models.DepotConf; import com.ld.igds.models.DeviceSer; import com.ld.igds.models.DicSysConf; import com.ld.igds.models.Grain; import com.ld.igds.order.ExeOrderService; import com.ld.igds.order.data.ExeRequest; import com.ld.igds.protocol.beibo.grainv1.builder.ReMessage; import com.ld.igds.protocol.beibo.grainv1.builder.ReMessageBuilder; import com.ld.igds.protocol.beibo.grainv1.util.BeiboGrainServerUtils; import com.ld.igds.util.BytesUtil; import com.ld.igds.util.ContextUtil; import com.ld.igds.util.NumberUtil; import com.ld.igds.warn.WarnUtils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.*; /** * 协议解析 * * @author czt */ @Slf4j @Component(AnalysisService.BEAN_ID) public class AnalysisService { public static final String BEAN_ID = "beiboGrain.analysisService"; @Autowired private CoreSerService coreSerService; @Autowired private CoreCommonService commonService; @Autowired private NotifyGrainInvoker notifyGrainInvoker; @Autowired private GrainUtil grainUtil; @Autowired private WarnUtils warnUtils; @Autowired private ExeOrderService exeOrderService; public static String result = ""; 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; /* * 00000000FFEB90FEAA41D821DD21DB21DE21DE21DA21DD21DE21DC21DC21DC21DC21DB21D821D721CE21D121DD21DC21DF21DE21DE21DD21DA21DD21D921DD21DC21DA21D921D921D721D521D321DD21DE21DF21DC21DF21DC21DA21DD21DD21DD21DC21DC21DC21DB21D821D721D121DC21DF21DF21DD21DD21DA21DE21DD21DC21DE21DB21D821DA21DB21D821D721D321DE21E021DD21DF21DF21DE21DB21DC21DF21DF21DD21DF21DB21DC21DB21FFFFFFFFDA21DC21D721DB21DB21DA21DB21D421DB21D921DC21D821D921D621D321FFFFFFFFDF21E021DE21DE21DB21DD21DC21E021DF21DD21DD21DC21DC21DC21DA21FFFFFFFFD921DD21DD21DE21DB21DD21DA21DB21DF21DD21DC21DB21DB21DB21D921FFFFFFFFDD21DC21DE21DD21DE21DC21DB21DC21DE21DE21DC21DC21DB21D921D921FFFFFFFFE021DD21DB21E021DF21DB21DB21D821DE21DE21DD21DD21DB21DB21DB21FFFFFFFFE021E021E021E221DC21DE21DF21DD21DD21DF21DF21DB21DC21DD21DC21FFFFFFFFE221E321E321E521E421DF21E321E321E321E521E321E121D921DE21FFFFFFFFFFFFE321E421E321E621E521E321E621E021E521E321E221E321DF21DE21FFFFFFFFFFFFE421E621E521E521E621E321E521E121E621E121E321E421E121DE21FFFFFFFFFFFFE621E421E521E521E521E321E521E421E121E321E521E321DF21E021FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF40013D10101039014110101010101010101000000000000000E5113500031A4C * * 00000000FFEB90FEAA * 41 * D821DD21DB21DE21DE21DA21DD21DE21DC21DC21DC21DC21DB21D821D721CE21D121 * DD21DC21DF21DE21DE21DD21DA21DD21D921DD21DC21DA21D921D921D721D521D321 * DD21DE21DF21DC21DF21DC21DA21DD21DD21DD21DC21DC21DC21DB21D821D721D121 * DC21DF21DF21DD21DD21DA21DE21DD21DC21DE21DB21D821DA21DB21D821D721D321 * DE21E021DD21DF21DF21DE21DB21DC21DF21DF21DD21DF21DB21DC21DB21FFFFFFFF * DA21DC21D721DB21DB21DA21DB21D421DB21D921DC21D821D921D621D321FFFFFFFF * DF21E021DE21DE21DB21DD21DC21E021DF21DD21DD21DC21DC21DC21DA21FFFFFFFF * D921DD21DD21DE21DB21DD21DA21DB21DF21DD21DC21DB21DB21DB21D921FFFFFFFF * DD21DC21DE21DD21DE21DC21DB21DC21DE21DE21DC21DC21DB21D921D921FFFFFFFF * E021DD21DB21E021DF21DB21DB21D821DE21DE21DD21DD21DB21DB21DB21FFFFFFFF * E021E021E021E221DC21DE21DF21DD21DD21DF21DF21DB21DC21DD21DC21FFFFFFFF * * E221E321E321E521E421DF21E321E321E321E521E321E121D921DE21FFFFFFFFFFFF * E321E421E321E621E521E321E621E021E521E321E221E321DF21DE21FFFFFFFFFFFF * E421E621E521E521E621E321E521E121E621E121E321E421E121DE21FFFFFFFFFFFF * E621E421E521E521E521E321E521E421E121E321E521E321DF21E021FFFFFFFFFFFF * FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF * 40013D 101010 390141 101010101010101010 * 00000000000000E51135 * 00031A4C * * * * * * */ /** * * * @param hexStr */ public void analysis(String ip, int port, String hexStr) { result += hexStr; if (result.length() < 1066 * 2) { log.info("分机------->>平台,报文长度不够,等待下一包数据"); 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 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()); } } private void analysisGrain(DepotConf depotConf, ReMessage reMessage, DeviceSer ser, ExeRequest exeRequest, DicSysConf sysConf) { // 粮情的批次号重新根据频率调整 reMessage.setBatchId(ContextUtil.getBatchIdByFireq(depotConf.getGrainFreq())); String grainStr = reMessage.getGrainStr(); log.debug("{}-{}=收到的粮情信息={}", ser.getCompanyId(), ser.getName(), grainStr); analysisStep0(depotConf, reMessage, ser, exeRequest, sysConf); log.info("分机------>>>平台:命令类型=8817--粮情全部收到,开始解析-{}-{}", ser.getCompanyId(), ser.getName()); } private void analysisStep0(DepotConf depotConf, ReMessage reMessage, DeviceSer ser, ExeRequest exeRequest, DicSysConf sysConf) { List depotConfs = null; // 一分机多仓,把起始列放到最大进行采集 if (Constant.YN_Y.equals(sysConf.getGrainMoreTag())) { depotConfs = commonService.getCacheDepotConfBySerId2( depotConf.getCompanyId(), ser.getId()); } if (null == depotConfs) {// 一个分机1个仓 if (DepotType.TYPE_02.getCode().equals(depotConf.getDepotType())) { analysisStep2(depotConf, ser, exeRequest, reMessage, sysConf); } else if (DepotType.TYPE_04.getCode().equals(depotConf.getDepotType())) { analysisStep2(depotConf, ser, exeRequest, reMessage, sysConf); } else { analysisStep1(depotConf, ser, exeRequest, reMessage, sysConf); } return; } // 一分几多仓情况,考虑单仓采集和多仓采集 // 批量采集所有关联一起采集,遍历执行 for (DepotConf depotConfTemp : depotConfs) { if (DepotType.TYPE_02.getCode().equals(depotConfTemp.getDepotType())) { analysisStep2(depotConfTemp, ser, exeRequest, reMessage, sysConf); } else if (DepotType.TYPE_04.getCode().equals(depotConfTemp.getDepotType())) { analysisStep2(depotConfTemp, ser, exeRequest, reMessage, sysConf); } else { analysisStep1(depotConfTemp, ser, exeRequest, reMessage, sysConf); } } } /** * 圆筒仓的粮情解析 * * @param depotConf * @param ser * @param exeRequest * @param reMessage */ private void analysisStep2(DepotConf depotConf, DeviceSer ser, ExeRequest exeRequest, ReMessage reMessage, DicSysConf sysConf) { if (StringUtils.isEmpty(depotConf.getCableRule()) || StringUtils.isEmpty(depotConf.getCableCir())) { log.error("分机------>>>平台:当前仓库:{}-{},没有没有配置布线规则,无法解析粮情信息……", ser.getCompanyId(), depotConf.getDepotName()); return; } String[] cableRuleAtt = depotConf.getCableRule().split("-"); String[] cableCirAtt = depotConf.getCableCir().split("-"); if (cableRuleAtt.length != cableCirAtt.length) { log.error("分机------>>>平台:当前仓库:{}-{},布线规则不正确,无法解析粮情信息……", ser.getCompanyId(), depotConf.getDepotName()); return; } //避免空指针 if (null == depotConf.getCableCone()) depotConf.setCableCone(Constant.CABLE_CONE_0); // 获取最大的层配置--默认每一圈都一样 int layMax = Integer.valueOf(cableCirAtt[0]); for (int i = 0; i < cableCirAtt.length; i++) { if (Integer.valueOf(cableCirAtt[i]) >= layMax) layMax = Integer.valueOf(cableCirAtt[i]); } //粮情信息 String strPoints = reMessage.getGrainStr(); log.info("------筒仓粮情报文={}------", strPoints); int sumNum = 0, cableZ = 1; // sumNum 共多少根电缆;cableZ 层的最大值,锥形仓补齐最大层 for (int i = 0; i < cableCirAtt.length; i++) { if (Integer.valueOf(cableCirAtt[i]) > cableZ) { cableZ = Integer.valueOf(cableCirAtt[i]); } sumNum += Integer.valueOf(cableRuleAtt[i]); } int cableZTemp = 1;// 当前仓的最大层 cableZTemp = cableZ; //判断最大层跟分机的层数是否一致 if(cableZ < ser.getCableZ()){ cableZ = ser.getCableZ(); } // 根据层行列获取指定长度 int start = 4 * (depotConf.getCableStart() - ser.getCableStart()) * cableZ; int len = 4 * cableZ * sumNum; strPoints = strPoints.substring(start, start + len); log.info("分机------>>>平台:返回粮情完整信息,所属组织={},分机={}", ser.getCompanyId(), ser.getName()); // 将粮情解析成数组 List temps = new ArrayList<>(); double tempValue; String temp; int symbol = 0; //符号位 int num = 0; //标记解析的层数 for (int i = 0; i < strPoints.length() / 4; i++) { if(num == cableZ){ num = 0; } if(num < cableZTemp){ temp = strPoints.substring(i * 4, i * 4 + 4); if (temp == null) { temp = "0000"; } //高低位转换后转为16位二进制字符串 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); } //非正常值 if (tempValue > ERROR_CHECK_TAG2) { tempValue = Constant.ERROR_TEMP; } // 故障值处理 if (tempValue >= FAULT_CHECK_TAG) { tempValue = Constant.FAULT_TEMP; } temps.add(tempValue); } num ++; } log.debug("-------CheckGrainRequest--={}", exeRequest.toString()); // 将集合解析成坐标数据 addPoint2(temps, reMessage, depotConf, exeRequest, sysConf); } /** * 获取当前跟所在圈 * * @param curRoot * @param cableRuleAtt * @return */ private int getCurCir(int curRoot, String[] cableRuleAtt) { int sum = 0; for (int i = 0; i < cableRuleAtt.length; i++) { sum += Integer.valueOf(cableRuleAtt[i]); if (curRoot <= sum) return i + 1; } return 1; } /** * 平方仓的解析,解析需要考虑当前是否启用的一分机多仓 * * @param depotConf * @param ser * @param exeRequest * @param reMessage */ private void analysisStep1(DepotConf depotConf, DeviceSer ser, ExeRequest exeRequest, ReMessage reMessage, DicSysConf sysConf) { // 粮情信息 String strPoints = reMessage.getGrainStr(); String[] attCable = depotConf.getCableRule().split("-"); int cableZ = Integer.valueOf(attCable[0]); int cableY = Integer.valueOf(attCable[1]); int cableX = Integer.valueOf(attCable[2]); // 根据层行列获取指定长度 int start = 4 * (depotConf.getCableStart() - ser.getCableStart()) * cableZ * cableY; int len = 4 * cableZ * cableY * cableX; log.info("分机------>>>平台:返回粮情完整信息,所属组织={},分机={}", ser.getCompanyId(), ser.getName()); strPoints = strPoints.substring(start, start + len); // 将粮情解析成数组 List temps = new ArrayList<>(); double tempValue; String temp; int symbol = 0; for (int i = 0; i < strPoints.length() / 4; i++) { temp = strPoints.substring(i * 4, i * 4 + 4); //高低位转换后转为16位二进制字符串 temp = BytesUtil.tran_LH(temp); if (temp == null) { temp = "0000"; } 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); } // 说明解析的数据有问题 if (tempValue == ERROR_CHECK_TAG || tempValue == ERROR_CHECK_TAG2) { tempValue = Constant.ERROR_TEMP; } // 故障值处理 if (tempValue >= FAULT_CHECK_TAG) { tempValue = Constant.FAULT_TEMP; } temps.add(tempValue); } log.debug("-------CheckGrainRequest--={}", exeRequest.toString()); // 将集合解析成坐标数据 addPoint1(temps, reMessage, depotConf, ser, exeRequest, sysConf); } /** * 平房仓,解析第二步,解析到坐标数据 * * @param temps * @throws Exception */ private void addPoint1(List temps, ReMessage msg, DepotConf depotConf, DeviceSer ser, ExeRequest exeRequest, DicSysConf sysConf) { //根据电缆起始方位和布线方向,对粮情数据进行调整 if (null != depotConf.getStartOrientation()) { temps = reversalGrainPoint(temps, depotConf); } //若起点点位为底部,则将粮情数据进行翻转 if (null != depotConf.getStartPoint() && Constant.GRAIN_START_POINT_BELOW.equals(depotConf.getStartPoint())) { temps = grainUtil.reversalUpAndDown(temps, depotConf.getCableRule()); } //若配置层行转换,则将粮情数据进行层行转换 if (StringUtils.isNotEmpty(depotConf.getStartConvert())) { temps = convertGrainPoint(temps, depotConf); String[] cableRule = depotConf.getCableRule().split("-"); //转换层行列配置 if (Constant.GRAIN_CONVERT_CLOCKWISE.equals(depotConf.getStartConvert()) || Constant.GRAIN_CONVERT_ANTICLOCKWISE.equals(depotConf.getStartConvert())) { depotConf.setCableRule(Integer.valueOf(cableRule[1]) + "-" + Integer.valueOf(cableRule[0]) + "-" + Integer.valueOf(cableRule[2])); } } String[] attCable = depotConf.getCableRule().split("-"); int cableZ = Integer.valueOf(attCable[0]); int cableY = Integer.valueOf(attCable[1]); int cableX = Integer.valueOf(attCable[2]); Grain grain = new Grain(); grain.setDepotId(depotConf.getDepotId()); grain.setCompanyId(depotConf.getCompanyId()); grain.setCable(depotConf.getCableRule()); grain.setBatchId(msg.getBatchId()); int humidity = -100; int tem = -100; String substring = ""; //解析仓内温仓内湿 int thConf = Integer.valueOf(depotConf.getThConf()); int start = 0, end = 6; if(thConf > 1){ start += 6*thConf; //12-18 end += start; } String thStr = msg.getThStr().substring(start, end); humidity = BytesUtil.hexToInt(thStr.substring(0, 2)); grain.setHumidityIn(humidity * 1.0); substring = thStr.substring(2); tem = BytesUtil.hexToInt(substring); grain.setTempIn(tem * 0.1); if(substring.startsWith("1")){ grain.setTempIn(tem * -0.1); } grain.setReceiveDate(new Date()); grain.setRemark("粮温正常"); // 获取缓存中的命令信息 grain.setCheckUser(exeRequest.getExeUser()); double max = MAX_TEMP, min = MIN_TEMP, sumT = 0.0, sumNum = cableX * cableY * cableZ; // 校验和实现统计,生成采集点信息 List listGrainItems = new ArrayList<>(); int i = 1; for (Double temp : temps) { if (temp == Constant.ERROR_TEMP || temp == Constant.FAULT_TEMP || temp == Constant.ADD_TEMP) { sumNum--; } else { sumT += temp; if (temp > max) { max = temp; } if (temp < min) { min = temp; } } listGrainItems.add(new GrainItemInfo(i, temp)); i++; } if (sumNum == 0) { sumNum = 1; grain.setRemark("当前粮情采集异常"); } //过滤比较用的最大最小值 if (max == MAX_TEMP) { max = 0.0; } if (min == MIN_TEMP) { min = 0.0; } if (null != depotConf.getTempMax() && max > depotConf.getTempMax()) { grain.setRemark("仓库配置高温警告值:" + depotConf.getTempMax() + ",当前检测高温值:" + max); warnUtils.addGrainWarn(depotConf, grain); } grain.setTempAve(sumT / sumNum); grain.setTempMax(max); grain.setTempMin(min); grain.setPoints(StringUtils.join(temps, ",")); String depotIds = exeRequest.getDepotIds(); boolean notifyWeb = true; if (null != depotIds && depotIds.indexOf(depotConf.getDepotId()) == -1) { notifyWeb = false; } // 用户封装好数据即可 notifyGrainInvoker.analysisSuccess(grain, listGrainItems, depotConf, sysConf, notifyWeb, exeRequest); } /** * 圆筒仓解析步骤 * * @param temps * @param msg * @param depotConf * @param exeRequest */ private void addPoint2(List temps, ReMessage msg, DepotConf depotConf, ExeRequest exeRequest, DicSysConf sysConf) { Grain grain = new Grain(); grain.setDepotId(depotConf.getDepotId()); grain.setCompanyId(depotConf.getCompanyId()); grain.setCable(depotConf.getCableRule()); grain.setCableCir(depotConf.getCableCir()); grain.setBatchId(msg.getBatchId()); int humidity = -100; int tem = -100; String substring = ""; //解析仓内温仓内湿 int thConf = Integer.valueOf(depotConf.getThConf()); int start = 0, end = 6; if(thConf > 1){ start += 6*thConf; //12-18 end += start; } String thStr = msg.getThStr().substring(start, end); humidity = BytesUtil.hexToInt(thStr.substring(0, 2)); grain.setHumidityIn(humidity * 1.0); substring = thStr.substring(2); tem = BytesUtil.hexToInt(substring); grain.setTempIn(tem * 0.1); if(substring.startsWith("1")){ grain.setTempIn(tem * -0.1); } grain.setReceiveDate(new Date()); grain.setRemark("粮温正常"); grain.setCheckUser(exeRequest.getExeUser()); double max = MAX_TEMP, min = MIN_TEMP, sumT = 0.0; int sumNum = temps.size(); List listGrainItems = new ArrayList<>(); int i = 1; for (Double temp : temps) { if (temp == Constant.ERROR_TEMP || temp == Constant.FAULT_TEMP || temp == Constant.ADD_TEMP) { sumNum--; } else { sumT += temp; if (temp > max) { max = temp; } if (temp < min) { min = temp; } } listGrainItems.add(new GrainItemInfo(i, temp)); i++; } if (sumNum == 0) { sumNum = 1; grain.setRemark("当前粮情采集异常"); } //过滤比较用的最大最小值 if (max == MAX_TEMP) { max = 0.0; } if (min == MIN_TEMP) { min = 0.0; } if (null != depotConf.getTempMax() && max > depotConf.getTempMax()) { grain.setRemark("仓库配置高温警告值:" + depotConf.getTempMax() + ",当前检测高温值:" + max); warnUtils.addGrainWarn(depotConf, grain); } grain.setTempAve(sumT / sumNum); grain.setTempMax(max); grain.setTempMin(min); grain.setPoints(StringUtils.join(temps, ",")); //判断是否是锥形仓,是的话进行锥形仓补点 if(StringUtils.isNotEmpty(depotConf.getCableCone())){ //上锥形补点 if(Constant.CABLE_CONE_1.equals(depotConf.getCableCone())){ grain.setPoints(upConePoints(depotConf, grain.getPoints())); } //下锥形补点 if(Constant.CABLE_CONE_2.equals(depotConf.getCableCone())){ grain.setPoints(downConePoints(depotConf, grain.getPoints())); } } String depotIds = exeRequest.getDepotIds(); boolean notifyWeb = true; if (null != depotIds && depotIds.indexOf(depotConf.getDepotId()) == -1) { notifyWeb = false; } // 用户封装好数据即可 notifyGrainInvoker.analysisSuccess(grain, listGrainItems, depotConf, sysConf, notifyWeb, exeRequest); } private List convertGrainPoint(List temps, DepotConf conf) { if (Constant.GRAIN_CONVERT_DEFAULT.equals(conf.getStartConvert())) { //若层行转换为默认,则直接返回 return temps; } //顺时针转换 if (Constant.GRAIN_CONVERT_CLOCKWISE.equals(conf.getStartConvert())) { return grainUtil.convertRight(temps, conf.getCableRule()); } //逆时针转换 if (Constant.GRAIN_CONVERT_ANTICLOCKWISE.equals(conf.getStartConvert())) { return grainUtil.convertLeft(temps, conf.getCableRule()); } return temps; } private List reversalGrainPoint(List temps, DepotConf conf) { if (StringUtils.isEmpty(conf.getStartOrientation())) { //若起始方位为空,则默认起始方位和方向,直接返回 return temps; } //起始方位为右边时 if (Constant.GRAIN_START_ORIENTATION_RIGHT.equals(conf.getStartOrientation())) { if (StringUtils.isEmpty(conf.getStartDirection())) { //右边起始,默认纵向布线,直接返回 return temps; } if (Constant.GRAIN_START_DIRECTION_TRANSVERSE.equals(conf.getStartDirection())) { //右边起始,横向布线 return grainUtil.reversalRight1(temps, conf.getCableRule()); } //右边起始,默认纵向布线,直接返回 return temps; } //起始方位为右上时 if (Constant.GRAIN_START_ORIENTATION_RIGHT_UP.equals(conf.getStartOrientation())) { if (StringUtils.isEmpty(conf.getStartDirection())) { //右上起始,默认纵向布线 return grainUtil.reversalRightUp2(temps, conf.getCableRule()); } if (Constant.GRAIN_START_DIRECTION_TRANSVERSE.equals(conf.getStartDirection())) { //右上起始,横向布线 return grainUtil.reversalRightUp1(temps, conf.getCableRule()); } //右上起始,默认纵向布线 return grainUtil.reversalRightUp2(temps, conf.getCableRule()); } //起始方位为左边时 if (Constant.GRAIN_START_ORIENTATION_LEFT.equals(conf.getStartOrientation())) { if (StringUtils.isEmpty(conf.getStartDirection())) { //左边起始,默认纵向布线 return grainUtil.reversalLeft2(temps, conf.getCableRule()); } if (Constant.GRAIN_START_DIRECTION_TRANSVERSE.equals(conf.getStartDirection())) { //左边起始,横向布线 return grainUtil.reversalLeft1(temps, conf.getCableRule()); } //左边起始,默认纵向布线 return grainUtil.reversalLeft2(temps, conf.getCableRule()); } //起始方位为左上时 if (Constant.GRAIN_START_ORIENTATION_LEFT_UP.equals(conf.getStartOrientation())) { if (StringUtils.isEmpty(conf.getStartDirection())) { //左上起始,默认纵向布线 return grainUtil.reversalLeftUp2(temps, conf.getCableRule()); } if (Constant.GRAIN_START_DIRECTION_TRANSVERSE.equals(conf.getStartDirection())) { //左上起始,横向布线 return grainUtil.reversalLeftUp1(temps, conf.getCableRule()); } //左上起始,默认纵向布线 return grainUtil.reversalLeftUp2(temps, conf.getCableRule()); } return temps; } /** * 上锥形补点(点位位置正确,需要补点为-102) * * @param depotConf * @param points * @return */ private String upConePoints(DepotConf depotConf, String points){ String[] cableRuleAtt = depotConf.getCableRule().split("-"); String[] cableCirAtt = depotConf.getCableCir().split("-"); //获取最大的层配置 int layMax = Integer.valueOf(cableCirAtt[0]); for (int i = 0; i < cableCirAtt.length; i++) { if (Integer.valueOf(cableCirAtt[i]) >= layMax) { layMax = Integer.valueOf(cableCirAtt[i]); } } //所在层从1开始 int curLay = 1; //所在根 int curRoot = 1; //所在圈 int curCir = 1; //当前圈的层 int cirLay = 1; String[] array = points.split(","); for (int i = 0; i < array.length; i++) { curLay = (i % layMax) + 1; curRoot = (i / layMax) + 1; curCir = getCurCir(curRoot, cableRuleAtt); cirLay = Integer.valueOf(cableCirAtt[curCir - 1]); //比如配置了5层但是当前是6层,说明当前点为补偿点 if (curLay <= (layMax - cirLay)) { array[i] = String.valueOf(Constant.ADD_TEMP); } } return StringUtils.join(array, ","); } /** * 下锥形补点(点位位置正确,需要补点为-102) * * @param depotConf * @param points * @return */ private String downConePoints(DepotConf depotConf, String points){ String[] cableRuleAtt = depotConf.getCableRule().split("-"); String[] cableCirAtt = depotConf.getCableCir().split("-"); //获取最大的层配置 int layMax = Integer.valueOf(cableCirAtt[0]); for (int i = 0; i < cableCirAtt.length; i++) { if (Integer.valueOf(cableCirAtt[i]) >= layMax) { layMax = Integer.valueOf(cableCirAtt[i]); } } //所在层从1开始 int curLay = 1; //所在根 int curRoot = 1; //所在圈 int curCir = 1; //当前圈的层 int cirLay = 1; String[] array = points.split(","); for (int i = 0; i < array.length; i++) { curLay = (i % layMax) + 1; curRoot = (i / layMax) + 1; curCir = getCurCir(curRoot, cableRuleAtt); cirLay = Integer.valueOf(cableCirAtt[curCir - 1]); //比如配置了5层但是当前是6层,说明当前点为补偿点 if (curLay > cirLay) { array[i] = String.valueOf(Constant.ADD_TEMP); } } return StringUtils.join(array, ","); } }