vince
2025-06-04 0d9a7c8974add02c61d2471642e4e5a04013ab7b
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package com.fzzy.protocol.xsyg.analysis;
 
import com.alibaba.fastjson.JSONObject;
import com.fzzy.api.data.GatewayDeviceType;
import com.fzzy.api.utils.BytesUtil;
import com.fzzy.api.utils.NumberUtil;
import com.fzzy.gateway.GatewayUtils;
import com.fzzy.gateway.api.GatewayDeviceReportService;
import com.fzzy.gateway.api.GatewayRemoteManager;
import com.fzzy.gateway.data.BaseReqData;
import com.fzzy.gateway.data.WeatherWebDto;
import com.fzzy.gateway.entity.GatewayDevice;
import com.fzzy.gateway.hx2023.ScConstant;
import com.fzzy.gateway.hx2023.data.*;
import com.fzzy.protocol.ProtocolUtils;
import com.fzzy.protocol.bhzn.v0.cmd.ReMessageBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
 
@Slf4j
@Component(AnalysisService.BEAN_ID)
public class AnalysisService {
 
    public static final String BEAN_ID = "xsyg.analysisService";
 
 
    @Resource
    private GatewayRemoteManager gatewayRemoteManager;
 
 
    /**
     * 粮情接线
     *
     * @param address
     * @param port
     * @param strMsg
     */
    public void analysis(InetAddress address, int port, String strMsg) {
 
            this.analysisGrainStep1(address, strMsg);
    }
 
    /**
     *byte(i)  01 7C   01 67   01 66   01 81 。。。   02 89  AA BB CC
     *   byte(i)--------包头
     * AA BB CC-----包尾
     * 蓝色数据每两个字节表示一个测温点的值(转换为十进制后*0.0625).共有cwcs[1]*2个字节。
     * 02 89 两个字节表示该油罐的空高值(高位在前)单位毫米。(FF FF或00 00表示故障,请在软件上做出处理,把该空高值赋值成传感器安装高度,表示该罐是空罐)。用H值减去该数据,得到油位高度。
     *
     * @param strMsg
     */
    private void analysisGrainStep1(InetAddress address, String strMsg) {
        strMsg = strMsg.substring(0,strMsg.length()-6);
        log.info("完整的数据体:"+strMsg);
        int start = 0;
        int len = 1 * 2;
        String tag = strMsg.substring(start, start + 2);
        int depotIdSys = BytesUtil.hexToInt(tag);
 
        BaseReqData reqData = ProtocolUtils.getSyncReq(depotIdSys + "");
 
        if (null == reqData) {
            log.error("---------没有获取到请求信息,不执行解析------{}", address);
            return;
        }
 
        //仓内湿度
 
 
        GatewayDevice device = reqData.getDevice();
 
        String[] attCable = device.getCableRule().split("-");
        int cableZ = Integer.valueOf(attCable[0]);
        int cableY = Integer.valueOf(attCable[1]);
        int cableX = Integer.valueOf(attCable[2]);
        log.info("z={},x={},y={}", cableZ, cableX, cableY);
 
        int sumNum = cableZ * cableY * cableX;
        //粮温信息
        start = 2;
        len = 2 * 2;
        List<Double> points = new ArrayList<>();
        double temp = 0.0;
        for (int i = 0; i < sumNum; i++) {
            start = i * 2 * 2 +2;
            tag = strMsg.substring(start, start + len);
            if("FFFF".equals(tag)){
                points.add(-100.00);
                continue;
            }
            temp = BytesUtil.hexToInt(tag) * 0.0625;
            temp = NumberUtil.keepPrecision(temp, 1);
            points.add(temp);
        }
        double height = 0.0;
        tag = strMsg.substring(strMsg.length()-4);
        height = NumberUtil.keepPrecision((double) (BytesUtil.hexToInt(tag) /1000), 2);
 
        //执行封装解析
        analysisGrainStep2(reqData, cableZ, cableY, cableX, points, -100, -100,height);
    }
 
    private void analysisGrainStep2(BaseReqData reqData, int cableZ, int cableY, int cableX, List<Double> points, double hIn, double tIn,double height) {
        GatewayDevice device = reqData.getDevice();
        int sumNum = points.size();
 
        //数据封装
        GrainData grain = new GrainData();
        grain.setMessageId(ScConstant.getMessageId());
        grain.setDeviceId(device.getDeviceId());
        grain.setTimestamp(System.currentTimeMillis() + "");
 
        ClientHeaders headers = new ClientHeaders();
        headers.setDeviceName(device.getDeviceName());
        headers.setProductId(device.getProductId());
        headers.setOrgId(device.getOrgId());
        headers.setMsgId(reqData.getMessageId());
        grain.setHeaders(headers);
 
 
        GrainOutPut outPut = new GrainOutPut();
 
 
        double max = com.fzzy.protocol.bhzn.v0.cmd.ReMessageBuilder.MAX_TEMP, min = com.fzzy.protocol.bhzn.v0.cmd.ReMessageBuilder.MIN_TEMP, sumT = 0.0;
 
        List<GrainTemp> temperature = new ArrayList<>();
        //根号
        int cableNum = 1, position = 0;
 
        double curTemp;
        int x = 0, y = 0, z = 0;
        for (int i = 0; i < sumNum; i++) {
            curTemp = points.get(i);
            position = i;
 
            z = i % cableZ + 1;
            x = i / (cableZ * cableY);
            y = x * (cableZ * cableY);
            y = (i - y) / cableZ;
            //根号
            cableNum = (i / cableZ) + 1;
 
            temperature.add(new GrainTemp(cableNum + "", z + "", curTemp + "", position + ""));
 
            //求最大最小值
            if (curTemp < -900) {
                sumNum--;
            } else {
                sumT += curTemp;
                if (curTemp > max) {
                    max = curTemp;
                }
                if (curTemp < min) {
                    min = curTemp;
                }
            }
        }
 
        if (sumNum == 0) {
            sumNum = 1;
            log.warn("---当前粮情采集异常--");
        }
        //过滤比较用的最大最小值
        if (max == com.fzzy.protocol.bhzn.v0.cmd.ReMessageBuilder.MAX_TEMP) {
            max = 0.0;
        }
        if (min == ReMessageBuilder.MIN_TEMP) {
            min = 0.0;
        }
 
        outPut.setTemperature(temperature);
        outPut.setAvgTemperature(NumberUtil.keepPrecision((sumT / sumNum), 1) + "");
        outPut.setMinTemperature(min + "");
        outPut.setMaxTemperature(max + "");
 
 
        JSONObject properties = new JSONObject();
 
        properties.put("timestamp", grain.getTimestamp());
        outPut.setDetectTime( grain.getTimestamp());
        if (StringUtils.isEmpty(height+"")) height = 0.0;
        properties.put("liquidHeight", height);
 
        outPut.setLiquidHeight(height+"");
        grain.setOutput(JSONObject.toJSONString(outPut));
        properties.put("output", outPut);
        grain.setProperties(properties);
 
        //封装好的数据
        log.info("---浅圆仓封装完成----开始执行推送");
 
        reqData.setData(JSONObject.toJSONString(grain));
 
        doPushGrain(reqData,grain);
    }
 
    private void doPushGrain(BaseReqData reqData,GrainData grainData) {
 
        GatewayDeviceReportService reportService = gatewayRemoteManager.getDeviceReportService(reqData.getDevice().getPushProtocol());
        if (null == reportService) {
            log.error("------------粮情推送失败,系统不存在当前协议执行类----{}", reqData.getDevice().getDeviceName());
            return;
        }
        reportService.reportGrainData(reqData);
        reqData.setData(reportService.grainData2GatewayApiInfoKafka(grainData,reqData.getDevice()).getData());
        reportService.reportGrainDataByKafka(reqData);
    }
}