vince
2024-07-03 69e8acc5dd1f760eb60e914472c151bfa8126a52
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package com.fzzy.protocol.youxian1.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.cmd.ReMessageBuilder;
import com.fzzy.protocol.data.THDto;
import com.fzzy.protocol.youxian0.ServiceUtils;
import com.fzzy.protocol.youxian0.data.GrainRoot;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 游仙分库,返回报文解析
 */
@Slf4j
@Component(AnalysisService.BEAN_ID)
public class AnalysisService {
 
    public static final String BEAN_ID = "youxian1.analysisService";
 
    @Resource
    private GatewayRemoteManager gatewayRemoteManager;
 
    /**
     * 用于存放返回的仓温仓湿信息
     */
    public static Map<String, THDto> contextMapTH = new HashMap<>();
 
 
    /**
     * 协议解析
     * <p>
     * 80 80 80 80 80 90 41   --90代表分机编码
     * 39 39 39 35 39 39 39 39 39 39 39 39  --四组湿度
     * B4 30 30 31 30 30 B4 30 30 B4 30 30 --四组温度
     * ---以下为粮温,3个值转换为一个点
     * 31 30 35 31 37 32 32 31 34 32 30 33 31 30 38 31 36 34 32 32 35 32 31 38 31 30 34 31 36 36 32 32 33 32 31 34 31 30 32 31 35 39 32 31 37 32 30 34 31 30 37 31 33 37 32 32 34 32 31 37 31 31 30
     *
     * @param address
     * @param port
     * @param strMsg
     */
    public void analysis(InetAddress address, int port, String strMsg) {
 
        //分机ID
        int start = 5 * 2;
        int end = start + 1 * 2;
        String deviceSn = strMsg.substring(start, end);
        // deviceSn =  调整转换规则 ---TODO
 
        GatewayDevice device = GatewayUtils.getCacheByDeviceSn(deviceSn);
 
 
        //解析温湿度
        this.analysisGrainTh(device, strMsg);
 
 
        //解析粮情
        this.analysisGrainStep1(device, strMsg);
 
    }
 
    private void analysisGrainTh(GatewayDevice device, String strMsg) {
        //湿度信息,四组,获取有效的一组数据
        int start = 7 * 2;
        int end = start + 12 * 2;
        String hHex = strMsg.substring(start, end);
 
        THDto thDto = new THDto();
        double h = getGrainTemp(hHex.substring(0, 6));
        if (h < 99.9) {
            thDto.setHumidityIn(h);
        }
        h = getGrainTemp(hHex.substring(6, 12));
        if (h < 99.9) {
            thDto.setHumidityIn(h);
        }
        h = getGrainTemp(hHex.substring(12, 18));
        if (h < 99.9) {
            thDto.setHumidityIn(h);
        }
        h = getGrainTemp(hHex.substring(18, 24));
        if (h < 99.9) {
            thDto.setHumidityIn(h);
        }
 
        //湿度信息
        start = 19 * 2;
        end = start + 12 * 2;
        String tHex = strMsg.substring(start, end);
        double t = getGrainTemp(tHex.substring(0, 6));
        if (t > -40.0) {
            thDto.setTempIn(t);
        }
        t = getGrainTemp(tHex.substring(6, 12));
        if (t > -40.0) {
            thDto.setTempIn(t);
        }
        t = getGrainTemp(tHex.substring(12, 18));
        if (t > -40.0) {
            thDto.setTempIn(t);
        }
        t = getGrainTemp(tHex.substring(18, 24));
        if (t > -40.0) {
            thDto.setTempIn(t);
        }
 
        this.add2Map(device.getDeviceSn(), thDto);
    }
 
 
    /**
     * 协议解析
     * <p>
     * 80 80 80 80 80 90 41   --90代表分机编码
     * 39 39 39 35 39 39 39 39 39 39 39 39  --四组湿度
     * B4 30 30 31 30 30 B4 30 30 B4 30 30 --四组温度
     * ---以下为粮温,3个值转换为一个点
     * 31 30 35 31 37 32 32 31 34 32 30 33 31 30 38 31 36 34 32 32 35 32 31 38 31 30 34 31 36 36 32 32 33 32 31 34 31 30 32 31 35 39 32 31 37 32 30 34 31 30 37 31 33 37 32 32 34 32 31 37 31 31 30
     *
     * @param device
     * @Param strMsg
     */
    private void analysisGrainStep1(GatewayDevice device, String strMsg) {
 
        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);
 
        //获取请求信息
        BaseReqData reqData = ProtocolUtils.getSyncReq(device.getDepotIdSys());
        if (null == reqData) {
            log.error("---------没有获取到请求信息,不执行解析------{}", device.getDeviceName());
            return;
        }
 
        //只保留粮情信息
        int start = 31 * 2;
        strMsg = strMsg.substring(start);
        int num = cableZ * cableY * cableX;
        String tempStr = "";
        List<Double> points = new ArrayList<>();
        double point = 0;
        for (int i = 0; i < num; i++) {
            tempStr = strMsg.substring(i * 3 * 2, i * 3 * 2 + 3 * 2);
            point = this.getGrainTemp(tempStr);
 
            //处理无效的数据
            if (point > 50) {
                point = -100.0;
            }
 
            points.add(point);
        }
 
 
        analysisGrainStep2(reqData, cableZ, cableY, cableX, points);
    }
 
 
    /**
     * 数据转换
     *
     * @param reqData
     * @param cableZ
     * @param cableY
     * @param cableX
     * @param points
     */
    private void analysisGrainStep2(BaseReqData reqData, int cableZ, int cableY, int cableX, List<Double> points) {
 
        GatewayDevice device = reqData.getDevice();
        //数据封装
        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 = ReMessageBuilder.MAX_TEMP, min = ReMessageBuilder.MIN_TEMP, sumT = 0.0, sumNum = cableX * cableY * cableZ;
 
        List<GrainTemp> temperature = new ArrayList<>();
        //根号
        int cableNum = 1, position = 0;
 
        double curTemp;
        int x = 0, y = 0, z = 0;
        for (int i = 0; i < points.size(); 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 + ""));
 
            sumT += curTemp;
            if (curTemp > max) {
                max = curTemp;
            }
            if (curTemp < min) {
                min = curTemp;
            }
        }
 
        if (sumNum == 0) {
            sumNum = 1;
            log.warn("---当前粮情采集异常--");
        }
        //过滤比较用的最大最小值
        if (max == 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(min + "");
        List<GrainTH> ths = new ArrayList<>();
 
 
        //获取温湿度
        THDto thDto = this.getTH(device.getDeviceSn());
 
        ths.add(new GrainTH(thDto.getTempIn() != null ? thDto.getTempIn() + "" : "", thDto.getHumidityIn() != null ? thDto.getHumidityIn() + "" : "", "1"));
        outPut.setTemperatureAndhumidity(ths);
        grain.setOutput(JSONObject.toJSONString(outPut));
 
        GatewayDevice gatewayDeviceWeather = GatewayUtils.getCacheByDeviceTypeOne(GatewayDeviceType.TYPE_09.getCode());
 
        //系统气象站信息
        WeatherWebDto weather = WeatherWebDto.contextMap.get("default");
 
        //气象信息
        GrainWeather weatherStation = new GrainWeather();
        weatherStation.setMessageId(ScConstant.getMessageId());
        weatherStation.setMessgeId(weatherStation.getMessageId());
 
        if (null != gatewayDeviceWeather) {
            weatherStation.setId(gatewayDeviceWeather.getDeviceId());
        } else {
            weatherStation.setId(device.getDeviceId());
        }
        weatherStation.setAirPressure(weather.getPressure());
        weatherStation.setHumidity(weather.getHumidity().replaceAll("%",""));
        weatherStation.setPm(weather.getAir_pm25());
        weatherStation.setRadiation("0");
        weatherStation.setRainfallAmount(weather.getWea());
        weatherStation.setTemperature(weather.getTem());
        weatherStation.setWindDirection(weather.getWin());
        weatherStation.setWindPower(weather.getWin_meter());
        weatherStation.setWindSpeed(weather.getWin_speed());
 
        grain.setWeatherStation(JSONObject.toJSONString(weatherStation));
 
        //封装好的数据
        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);
    }
 
 
    /**
     * 计算粮情温度 3组温度
     *
     * @param tempHex
     * @return
     */
    public static double getGrainTemp(String tempHex) {
        String strAscii = "";
        strAscii += toAscii(tempHex.substring(0, 2));
        strAscii += toAscii(tempHex.substring(2, 4));
        strAscii += toAscii(tempHex.substring(4, 6));
 
        Integer value = Integer.valueOf(strAscii);
 
        return value / 10.0;
    }
 
    private void add2Map(String deviceSn, THDto thDto) {
        contextMapTH.put("TH_" + deviceSn, thDto);
    }
 
    private THDto getTH(String deviceSn) {
        return contextMapTH.get("TH_" + deviceSn);
    }
 
 
    public static void main(String[] args) {
        String strMsg = "31 30 35 31 37 32 32 31 34 32 30 33 31 30 38 31 36 34 32 32 35 32 31 38 31 30 34 31 36 36 32 32 33 32 31 34 31 30 32 31 35 39 32 31 37 32 30 34 31 30 37 31 33 37 32 32 34 32 31 37 31 31 30";
        strMsg = strMsg.replaceAll(" ", "");
        System.out.println("---" + strMsg);
        int num = 20;
        String tempStr = "";
        double tem;
        for (int i = 0; i < num; i++) {
            tempStr = strMsg.substring(i * 3 * 2, i * 3 * 2 + 3 * 2);
            System.out.println("---" + tempStr);
            tem = getGrainTemp(tempStr);
            System.out.println("-tem--" + tem);
        }
    }
 
    public static char toAscii(String hexString) {
        int decimal = Integer.parseInt(hexString, 16);
        return (char) decimal;
    }
 
}