vince
2025-07-09 6495040bbda5308c86e852ad1b080097bfa916a9
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
package com.fzzy.protocol.fzzydatabase;
 
import com.alibaba.fastjson.JSONObject;
import com.fzzy.api.data.GatewayDeviceProtocol;
import com.fzzy.api.data.GatewayDeviceType;
import com.fzzy.api.utils.BytesUtil;
import com.fzzy.api.utils.DateUtil;
import com.fzzy.api.utils.NumberUtil;
import com.fzzy.async.fzzy40.Fzzy40CommonService;
import com.fzzy.data.ConfigData;
import com.fzzy.gateway.GatewayUtils;
import com.fzzy.gateway.api.GatewayDeviceReportService;
import com.fzzy.gateway.api.GatewayRemoteManager;
import com.fzzy.gateway.api.GatewaySyncGranService;
import com.fzzy.gateway.data.BaseReqData;
import com.fzzy.gateway.data.BaseResp;
import com.fzzy.gateway.data.GrainCableData;
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.gateway.service.GatewayConfService;
import com.fzzy.gateway.service.GatewayDeviceTestPR;
import com.fzzy.protocol.ProtocolUtils;
import com.fzzy.protocol.bhzn.cmd.ReMessageBuilder;
import com.fzzy.protocol.data.THDto;
import com.fzzy.protocol.youxian1.client.ClientEngine;
import com.ld.io.api.InvokeResult;
import io.netty.channel.Channel;
import com.fzzy.async.fzzy40.entity.Fz40Grain;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * 游仙分库协议-粮情采集入口
 */
@Slf4j
@Component
public class FzzyDatabaseGatewayGrainService implements GatewaySyncGranService {
    @Resource
    private GatewayConfService confService;
    @Resource
    private GatewayRemoteManager gatewayRemoteManager;
    @Resource
    private ConfigData configData;
    @Resource
    private Fzzy40CommonService fzzy40CommonService;
    @Resource
    private GatewayDeviceTestPR gatewayDeviceTestPR;
 
    @Override
    public String getGrainProtocol() {
        return GatewayDeviceProtocol.GRAIN_FZZY_DATABASE.getCode();
    }
 
    /**
     * @param reqData
     * @return
     */
    @Override
    public synchronized BaseResp syncGrain(BaseReqData reqData) {
 
        BaseResp resp = new BaseResp();
 
        GatewayDevice device = reqData.getDevice();
 
        if (null == device) {
            resp.setCode(500);
            resp.setMsg("系统未获取到下行连接设备信息,无法执行");
            log.error("----------------系统未获取到下行连接设备信息,无法执行---------");
            return resp;
        }
        try {
         //从数据库查询最新的数据,通过mqtt推送上去
            //reqData = new BaseReqData();
            reqData.setProductId(device.getProductId());
            reqData.setDeviceName(device.getDeviceName());
            reqData.setDeviceId(device.getDeviceId());
            reqData.setDayTime(new Date());
            reqData.setDevice(device);
            Date start = DateUtil.getCurZero(reqData.getDayTime());
            Date end = DateUtil.getNextZero(reqData.getDayTime());
            //获取到数据
            String depotIdSys = device.getDepotIdSys();
            if (StringUtils.isEmpty(depotIdSys)) {
                log.error("--------设备--{}-未配置系统相关仓库编码,无法执行当前操作", device.getDeviceName());
                return new BaseResp(BaseResp.CODE_500, "未配置系统相关仓库编码,无法执行当前操作");
            }
 
            List<Fz40Grain> listGrain = fzzy40CommonService.listGrain(depotIdSys, start, end);
            if (null == listGrain || listGrain.isEmpty()) {
                log.error("---------设备-{}-系统仓库编码-{}-未同步到粮情信息,请确认当前条件下是否有数据", device.getDeviceName(), device.getDepotIdSys());
                return new BaseResp(BaseResp.CODE_500, "未同步到粮情信息,请确认当前条件下是否有数据");
            }
 
            //获取第一条数据执行推送
            Fz40Grain lastData = listGrain.get(0);
            log.info(lastData.toString());
            lastData.setReceiveDate(new Date());
            GrainData grain = fz40Grain2GrainData(reqData, lastData, device);
            //推送
            Thread.sleep(3000);
            doPushGrain(reqData,grain);
        } catch (Exception e) {
            log.error("粮情检测异常:{}", e);
            resp.setCode(500);
            resp.setMsg("平台------>>>>控制柜:发送粮情检测命令:" + e.getMessage());
            return resp;
        }
        return resp;
    }
 
 
    private GrainData fz40Grain2GrainData(BaseReqData reqData ,Fz40Grain grainData, GatewayDevice device) {
        GrainCableData cableData = GatewayUtils.getCableData(device);
        GrainData grain = new GrainData();
        //层行列
        int cableZ = cableData.getCableZ();
        int cableY = cableData.getCableY();
        int cableX = cableData.getCableX();
 
        //温度集合
        String[] attr = grainData.getPoints().split(",");
        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 = 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 < attr.length; i++) {
            curTemp = Double.valueOf(attr[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 > max  && curTemp < 40) {
                max = curTemp;
            }
            if (curTemp < min && curTemp > 3) {
                min = curTemp;
            }
            if(curTemp > 3 &&  curTemp < 40){
                sumT += curTemp;
                sumNum++;
            }
        }
 
        if (sumNum == 0) {
            sumNum = 1;
            log.warn("---当前粮情采集异常--");
        }
        //过滤比较用的最大最小值
        if (max == ReMessageBuilder.MAX_TEMP) {
            max = 0.0;
        }
        if (min == ReMessageBuilder.MIN_TEMP) {
            min = 0.0;
        }
        //遍历温度点位,异常温度点使用平均温
        for( GrainTemp temp : temperature){
            Double curTemp2 = Double.valueOf(temp.getTemperature());
            if(!(curTemp2 > 3 &&  curTemp2 < 40)){
                temp.setTemperature(outPut.getAvgTemperature());
            }
        }
        outPut.setTemperature(temperature);
        outPut.setAvgTemperature(NumberUtil.keepPrecision((sumT / sumNum), 1) + "");
        outPut.setMinTemperature(min + "");
        outPut.setMaxTemperature(max + "");
        List<GrainTH> ths = new ArrayList<>();
 
 
        //获取温湿度
        ths.add(new GrainTH(grainData.getTempIn() != null ? grainData.getTempIn() + "" : "", grainData.getHumidityIn() != null ? grainData.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));
        return 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);
    }
    @Override
    public BaseResp syncGrainTh(BaseReqData reqData) {
        return new BaseResp();
    }
 
    @Override
    public BaseResp syncConf(BaseReqData reqData) {
        return new BaseResp();
    }
 
    @Override
    public BaseResp writeConf(BaseReqData reqData) {
        return new BaseResp();
    }
 
    @Override
    public BaseResp initCable(BaseReqData reqData) {
        return new BaseResp();
    }
 
    @Override
    public BaseResp disconnect(BaseReqData reqData) {
        return new BaseResp();
    }
 
    @Override
    public BaseResp transparent(BaseReqData reqData) {
        return new BaseResp();
    }
}