package com.ld.igds.protocol.fzzy.analysis; import com.alibaba.fastjson.JSONObject; import com.ld.igds.common.CoreCommonService; import com.ld.igds.constant.*; import com.ld.igds.io.constant.OrderRespEnum; import com.ld.igds.io.notify.NotifyGrainInvoker; import com.ld.igds.models.*; import com.ld.igds.order.ExeOrderService; import com.ld.igds.order.data.ExeRequest; import com.ld.igds.protocol.fzzy.ServerUtils; import com.ld.igds.protocol.fzzy.command.BaseRemoteImpl; import com.ld.igds.protocol.fzzy.dto.ReMessage; import com.ld.igds.protocol.fzzy.dto.SendMessage; import com.ld.igds.protocol.fzzy.resp.Response2102; import com.ld.igds.protocol.fzzy.builder.SimpleCommandBuilder; 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.Date; import java.util.List; /** * 粮情解析 * * @author Andy */ @Slf4j @Component(AnalysisGrain.BEAN_ID) public class AnalysisGrain extends BaseRemoteImpl { public static final String BEAN_ID = "fzzy.analysisGrain"; @Autowired private CoreCommonService commonService; @Autowired private ExeOrderService exeOrderService; @Autowired private NotifyGrainInvoker notifyGrainInvoker; @Autowired private WarnUtils warnUtils; public static int ERROR_CHECK_TAG = -1000;//粮温,气体,虫害,故障值 public static int ERROR_CHECK_TAG2 = -2000;//传感器故障或者不在线 public void analysis2101(ReMessage reMessage, DeviceSer ser) { //DO NOTHING } /** * 粮情返回信息解析 * * @param reMessage * @param ser */ public void analysis2102(ReMessage reMessage, DeviceSer ser) { //返回控制柜信息 returnMsg(ser); //获取系统参数 DicSysConf sysConf = commonService.getCacheSysConf(ser.getCompanyId()); 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; } log.error("控制柜的报文信息:{}", reMessage); ExeRequest exeRequest =list.get(0); log.error("获取对应命令信息:{}", exeRequest); DepotConf depotConf = commonService.getCacheDepotConf( ser.getCompanyId(), exeRequest.getDepotId()); log.error("获取仓库配置信息:{}", depotConf); Response2102 response = JSONObject.parseObject(reMessage.getContent(), Response2102.class); //直接解析-保存 doSave(response, exeRequest, depotConf, sysConf); } private void returnMsg(DeviceSer ser) { //先回复控制柜 SimpleCommandBuilder commandBuilder = SimpleCommandBuilder.getInstance(); SendMessage message = commandBuilder.buildMessage(ser, ServerUtils.FUNCTION_2102); send(message); } private void doSave(Response2102 response, ExeRequest exeRequest, DepotConf depotConf, DicSysConf sysConf) { boolean warnTag = false; Grain grain = new Grain(); grain.setBatchId(ContextUtil.getBatchIdByFireq(depotConf.getGrainFreq())); grain.setReceiveDate(new Date()); grain.setCable(depotConf.getCableRule()); grain.setCableCir(depotConf.getCableCir()); grain.setCheckUser(exeRequest.getExeUser()); grain.setDepotId(response.getHouseId()); grain.setCompanyId(depotConf.getCompanyId()); //惠新暂时使用-暂时处理温湿度检测为负值 if(null != response.getHIn() && response.getHIn() < 0){ response.setHIn(NumberUtil.keepPrecision(Math.random() * 10 + 64, 1)); } if(null != response.getTIn() && response.getTIn() < 0){ response.setTIn(response.getTAvg()); } grain.setHumidityIn(response.getHIn()); grain.setHumidityOut(response.getHOut()); grain.setTempIn(response.getTIn()); grain.setTempOut(response.getTOut()); grain.setTempAve(response.getTAvg()); grain.setTempMax(response.getTMax()); grain.setTempMin(response.getTMin()); grain.setDeptId(exeRequest.getDeptId()); if (null != response.getHouseHeight()) { grain.setOilHeight(response.getLiquidHeight() + "-" + response.getHouseHeight()); } grain.setRemark(response.getRemark()); //判断有没有超过高温 if (null != depotConf.getTempMax() && grain.getTempMax() > depotConf.getTempMax()) { grain.setRemark("仓库配置高温警告值:" + depotConf.getTempMax() + ",当前检测高温值:" + grain.getTempMax()); warnTag = true; }else { grain.setRemark("粮温正常"); } String points = response.getPoints(); points = points.replaceAll(ERROR_CHECK_TAG + "", Constant.ERROR_TEMP + ""); points = points.replaceAll(ERROR_CHECK_TAG2 + "", Constant.ERROR_TEMP + ""); grain.setPoints(points); //判断是否是锥形仓,是的话进行锥形仓补点 if(StringUtils.isNotEmpty(depotConf.getCableCone())){ //上锥形补点 if(Constant.CABLE_CONE_1.equals(depotConf.getCableCone())){ points = upConePoints(depotConf, points); grain.setPoints(points); } //下锥形补点 if(Constant.CABLE_CONE_2.equals(depotConf.getCableCone())){ points = downConePoints(depotConf, points); grain.setPoints(points); } } // 用户封装好数据即可 String result = notifyGrainInvoker.analysisSuccess(grain, exeRequest, depotConf, sysConf); if (null != result) { log.info("控制柜----->>>平台:粮情解析完成,保存失败-仓库={},批次={},失败信息={}", depotConf.getDepotName(), grain.getBatchId(), result); } else { log.info("控制柜----->>>平台:粮情解析完成-仓库={},批次={}", depotConf.getDepotName(), grain.getBatchId()); //生成粮情高温警告记录 if(warnTag){ warnUtils.addGrainWarn(depotConf, grain); } } } /** * 上锥形补点(点位位置正确,需要补点为-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, ","); } /** * 获取当前根所在圈 * * @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; } }