vince
2024-01-16 5f92b2034f84caeab9cb6efc61ecbe22407935b4
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
package com.fzzy.protocol.wujia.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 lombok.extern.slf4j.Slf4j;
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 = "wujia.analysisService";
 
 
    @Resource
    private GatewayRemoteManager gatewayRemoteManager;
 
 
    /**
     * 粮情接线
     *
     * @param address
     * @param port
     * @param strMsg
     */
    public void analysis(InetAddress address, int port, String strMsg) {
 
        if (strMsg.startsWith("FE")) {
 
            //DO NOTHING
        } else {
            this.analysisGrainStep1(address, strMsg);
        }
    }
 
    /**
     * 粮情解析
     * X1 data1L data1H data2L data2H data3L data3H………datanL datanH DATA1H DATA1L DATA2H DATA2L AA BB CC
     * X1---发送的序号  AA BB CC----结束标志
     * DATA1H DATA1L----仓内湿度
     * DATA2H DATA2L----仓内温度
     * data1L data1H-----18B20返回温度
     * 注意:粮温返回数据高位在后,低位在前,转换为十进制后乘以0.0625,得到实际温度值
     * 仓温湿度高位在前,低位在后,转换为十进制后乘以0.1得到实际温湿度值
     *
     * @param strMsg
     */
    private void analysisGrainStep1(InetAddress address, String 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;
        }
 
        //仓内湿度
        start = 1 * 2;
        len = 2 * 2;
        tag = strMsg.substring(start, start + len);
        double hIn = BytesUtil.hexToInt(tag) * 0.1;
 
        //仓内温度
        start = 3 * 2;
        len = 2 * 2;
        tag = strMsg.substring(start, start + len);
        double tIn = BytesUtil.hexToInt(tag) * 0.1;
 
        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 = 5 * 2;
        len = 2 * 2;
        List<Double> points = new ArrayList<>();
        double temp = 0.0;
        for (int i = 0; i < sumNum; i++) {
            start = start + i * 2 * 2;
            tag = strMsg.substring(start, start + len);
            temp = BytesUtil.hexToInt(BytesUtil.tran_LH(tag)) * 0.0625;
            temp = NumberUtil.keepPrecision(temp, 1);
            points.add(temp);
        }
 
        //执行封装解析
        analysisGrainStep2(reqData, cableZ, cableY, cableX, points, hIn, tIn);
    }
 
    private void analysisGrainStep2(BaseReqData reqData, int cableZ, int cableY, int cableX, List<Double> points, double hIn, double tIn) {
        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<>();
 
 
        ths.add(new GrainTH(tIn + "", hIn + "", "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());
        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);
    }
 
    private void doPushGrain(BaseReqData reqData) {
 
        GatewayDeviceReportService reportService = gatewayRemoteManager.getDeviceReportService(reqData.getDevice().getPushProtocol());
        if (null == reportService) {
            log.error("------------粮情推送失败,系统不存在当前协议执行类----{}", reqData.getDevice().getDeviceName());
            return;
        }
 
        reportService.reportGrainData(reqData);
    }
}