jiazx0107@163.com
2023-12-14 a69402c8b67d8ce4b698d0c394d15ff43b5d99d0
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
package com.fzzy.protocol.fzzy.analysis;
 
import com.alibaba.fastjson.JSONObject;
import com.fzzy.api.data.ApiCommonDevice;
import com.fzzy.api.data.GatewayDeviceType;
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.BaseResp;
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.fzzy.builder.SimpleCommandBuilder;
import com.fzzy.protocol.fzzy.cmd.BaseRemoteImpl;
import com.fzzy.protocol.fzzy.data.ReMessage;
import com.fzzy.protocol.fzzy.data.SendMessage;
import com.fzzy.protocol.fzzy.resp.Response2102;
import com.fzzy.protocol.fzzy.server.ServerUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
 
/**
 * 粮情解析
 *
 * @author Andy
 */
@Slf4j
@Component(AnalysisGrain.BEAN_ID)
public class AnalysisGrain extends BaseRemoteImpl {
 
    @Resource
    private GatewayRemoteManager gatewayRemoteManager;
 
    public static final String BEAN_ID = "fzzy.analysisGrain";
 
 
    public static int ERROR_CHECK_TAG = -1000;//粮温,气体,虫害,故障值
 
    public static int ERROR_CHECK_TAG2 = -2000;//传感器故障或者不在线
 
 
    public void analysis2101(ReMessage reMessage, ApiCommonDevice ser) {
        //DO NOTHING
    }
 
    /**
     * 粮情返回信息解析
     *
     * @param reMessage
     * @param ser
     */
    public void analysis2102(ReMessage reMessage, ApiCommonDevice ser) {
 
        try {
 
            //返回控制柜信息
            returnMsg(ser);
 
            Response2102 response = JSONObject.parseObject(reMessage.getContent(), Response2102.class);
 
 
            //粮情解析
            log.info("控制柜--->平台,控制柜返回粮情检测结果--{}", response);
 
            //粮情封装和处理
            if (StringUtils.isEmpty(response.getLayerPerCircle())) {
                buildBizInfo1(response);
            } else {
                buildBizInfo2(response);
            }
 
 
        } catch (Exception e) {
 
            log.error("控制柜--->平台粮情信息异常-{}", e);
 
        }
    }
 
    private void buildBizInfo2(Response2102 response) {
        //TODO 筒仓的封装解析待处理
    }
 
    //信息调整封装
    private void buildBizInfo1(Response2102 response) {
 
        //获取请求信息
        BaseReqData reqData = ProtocolUtils.getSyncReq(response.getHouseId());
 
        if (null == reqData) {
            log.error("--粮情封装解析,未获取到请求相关参数----");
            return;
        }
 
 
        int cableZ = Integer.valueOf(response.getLay());
        int cableY = Integer.valueOf(response.getRow());
        int cableX = Integer.valueOf(response.getCol());
 
        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();
 
        outPut.setAvgTemperature(response.getTAvg() + "");
        outPut.setMinTemperature(response.getTMin() + "");
        outPut.setMaxTemperature(response.getTMax() + "");
 
 
        List<GrainTemp> temperature = new ArrayList<>();
        //根号
        int cableNum = 1, position = 0;
 
        double curTemp;
        String[] attr = response.getPoints().split(",");
        int x = 0, y = 0, z = 0;
        for (int i = 0; i < attr.length; i++) {
            curTemp = Double.valueOf(attr[i]);
 
            if (curTemp < -900) {
                curTemp = ERROR_CHECK_TAG;
            }
 
            //数据优化
            if (curTemp > 35) {
                curTemp = response.getTAvg();
            }
 
            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 + ""));
        }
 
        outPut.setTemperature(temperature);
 
        grain.setOutput(com.alibaba.fastjson2.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);
    }
 
    private void returnMsg(ApiCommonDevice ser) {
        //先回复控制柜
        SimpleCommandBuilder commandBuilder = SimpleCommandBuilder.getInstance();
        SendMessage message = commandBuilder.buildMessage(ser, ServerUtils.FUNCTION_2102);
        send(message);
    }
}