czt
2025-06-12 930d29f39d115fe76c305af4320c2acbcb30c445
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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);
        }
    }
}