package com.ld.igds.protocol.es.dlt645.analysis; import com.ld.igds.common.CoreSerService; import com.ld.igds.constant.RedisConst; import com.ld.igds.es.dto.EsData; import com.ld.igds.es.service.CoreEsService; import com.ld.igds.models.DeviceSer; import com.ld.igds.protocol.es.dlt645.util.Dlt645Utils; import com.ld.igds.util.ContextUtil; import com.ld.igds.util.RedisUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Date; /** * DLT645电表协议解析 * * @author czt */ @Slf4j @Component(AnalysisService.BEAN_ID) public class AnalysisService { public static final String BEAN_ID = "dlt645.analysisService"; @Autowired private RedisUtil redisUtil; @Autowired private CoreSerService coreSerService; @Autowired private CoreEsService esService; /** * * @param result */ public void analysis(String result){ log.info("能耗分机------->>平台:信息报文={}", result); if(!result.startsWith(Dlt645Utils.MSG_START)){ log.error("能耗分机------->>平台,解析能耗失败:报文起始符错误,不解析"); return; } //去除起始符 result = result.substring(8); //获取校验位 String oldCheck = result.substring(result.length() - 4, result.length() - 2); String check = Dlt645Utils.makeCheck(result.substring(0, result.length() - 4)); if(!check.equals(oldCheck)){ log.error("能耗分机------->>平台,解析能耗失败:校验位不对,不解析"); return; } analysisGrain(result); } private void analysisGrain(String result) { try { //获取能耗分机地址 String serId = Dlt645Utils.addrToString(result.substring(2, 14)); //根据分机地址获取分机信息 DeviceSer ser = coreSerService.getCacheSer(ContextUtil.getDefaultCompanyId(), serId); if (ser == null) { log.error("能耗分机-------->>平台,解析能耗失败,未获取到系统能耗主机配置:" + serId); return; } String key = RedisConst.buildKey(ser.getCompanyId(), "ES-DLT645", ser.getId()); String depotId = (String) redisUtil.get(key); if(null == depotId){ log.error("能耗分机-------->>平台,解析能耗失败,未获取到仓库信息:" + depotId); return; } //解析总能耗数据 String dataStr = result.substring(28, 36); Double esNum = Dlt645Utils.parseData(dataStr); EsData esData = new EsData(); esData.setDeviceName("电表"); esData.setCompanyId(ser.getCompanyId()); esData.setDepotId(depotId); esData.setUpdateTime(new Date()); esData.setEp(esNum); esData.setEs(esNum); log.info("DLT645电表----->>>平台:能耗数据解析完成-仓库={}", esData.getDepotId()); esService.saveAndUpdateInc(esData); } catch (Exception e) { log.error(e.getMessage(), e); } } }