jiazx0107@163.com
2024-01-04 50da1f165cc96d49d72694606920b77c74e1747c
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
362
363
364
365
366
367
368
369
370
371
package com.fzzy.gateway.hx2023.service;
 
import com.alibaba.fastjson2.JSONObject;
import com.fzzy.api.data.GatewayDeviceType;
import com.fzzy.api.data.PushProtocol;
import com.fzzy.api.utils.DateUtil;
import com.fzzy.api.utils.NumberUtil;
import com.fzzy.async.fzzy40.Fzzy40CommonService;
import com.fzzy.async.fzzy40.entity.Fz40Grain;
import com.fzzy.gateway.GatewayUtils;
import com.fzzy.gateway.api.GatewayDeviceTestService;
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 lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.util.*;
 
@Slf4j
@Component
public class ScGatewayTestServiceImpl implements GatewayDeviceTestService {
 
    @Resource
    private Fzzy40CommonService fzzy40CommonService;
 
    @Override
    public String getProtocol() {
        return PushProtocol.GATEWAY_SC_2023.getCode();
    }
 
    @Override
    public BaseResp testGrain(BaseReqData reqData) {
 
        GatewayDevice device = reqData.getDevice();
 
        if (StringUtils.isEmpty(device.getCableCir())) {
            return getGrainTest1(reqData, device);
        }
 
        BaseResp resp = new BaseResp();
        resp.setCode(500);
        resp.setMsg("没有匹配到规则");
        return resp;
    }
 
    @Override
    public BaseResp testGrainKafka(BaseReqData reqData) {
 
        Date start = DateUtil.getCurZero(reqData.getDayTime());
        Date end = DateUtil.getNextZero(reqData.getDayTime());
 
        return pushByV40(reqData, start, end);
    }
 
    private BaseResp pushByV40(BaseReqData reqData, Date start, Date end) {
        GatewayDevice device = reqData.getDevice();
        String depotIdSys = device.getDepotIdSys();
 
        if (StringUtils.isEmpty(depotIdSys)) {
            log.error("--------设备--{}-未配置系统相关仓库编码,无法执行当前操作", device.getDeviceName());
            return new BaseResp(BaseResp.CODE_50, "未配置系统相关仓库编码,无法执行当前操作");
        }
 
        List<Fz40Grain> listGrain = fzzy40CommonService.listGrain(depotIdSys, start, end);
        if (null == listGrain || listGrain.isEmpty()) {
            log.error("---------设备-{}-系统仓库编码-{}-未同步到粮情信息,请确认当前条件下是否有数据", device.getDeviceName(), device.getDepotIdSys());
            return new BaseResp(BaseResp.CODE_50, "未同步到粮情信息,请确认当前条件下是否有数据");
        }
 
        //获取第一条数据执行推送
        Fz40Grain lastData = listGrain.get(0);
        KafaGrainData pushData = this.lastData2PushData(lastData, device);
 
        return new BaseResp(JSONObject.toJSONString(pushData));
    }
 
    /**
     * 将粮情数据转换为推送需要的数据格式
     *
     * @param grainData
     * @return
     */
    private KafaGrainData lastData2PushData(Fz40Grain grainData, GatewayDevice device) {
        KafaGrainData result = new KafaGrainData();
 
        result.setMessageId(ScConstant.getMessageId());
        result.setMessgeId(result.getMessageId());
        result.setDeviceID(device.getDeviceId());
        result.setAvgTemperature(grainData.getTempAve() + "");
        result.setMinTemperature(grainData.getTempMin() + "");
        result.setMaxTemperature(grainData.getTempMax() + "");
        result.setCollectTime(DateFormatUtils.format(grainData.getReceiveDate(), "yyyy-MM-dd HH:mm:ss"));
 
        //层-行-列
        String[] attrCable = grainData.getCable().split("-");
        if (StringUtils.isNotEmpty(grainData.getCableCir())) {
            return lastData2PushData2(grainData, device);
        }
 
        //层行列
        int cableZ = Integer.valueOf(attrCable[0]);
        int cableY = Integer.valueOf(attrCable[1]);
        int cableX = Integer.valueOf(attrCable[2]);
 
        //温度集合
        String[] attr = grainData.getPoints().split(",");
 
        //根号
        int cableNum = 1, position = 0;
        String curTemp;
        List<KafkaGrainDataDetail1> temperature = new ArrayList<>();
 
        int x = 0, y = 0, z = 0;
        for (int i = 0; i < attr.length; i++) {
            position = i;
            z = i % cableZ + 1;
            x = i / (cableZ * cableY);
            y = x * (cableZ * cableY);
            y = (i - y) / cableZ;
            // 倒转X轴
            x = cableX - 1 - x;
            //根号
            cableNum = (i / cableZ) + 1;
            curTemp = attr[i];
 
 
            //如果是异常值,执行调整数据 TODO
            if (Double.valueOf(curTemp) < -99.9) {
 
                curTemp = grainData.getTempAve() + "";
 
            } else {
                //判断最大
                if (curTemp.equals(result.getMaxTemperature())) {
                    result.setMaxX(x + "");
                    result.setMaxY(y + "");
                    result.setMaxZ(position + "");
                }
                //判断最小
                if (curTemp.equals(result.getMinTemperature())) {
                    result.setMinX(x + "");
                    result.setMinY(y + "");
                    result.setMinZ(position + "");
                }
            }
 
            temperature.add(new KafkaGrainDataDetail1(cableNum + "", z + "", curTemp, position + "", x + "", y + ""));
        }
 
        //粮温信息
        JSONObject trhInfo = new JSONObject();
        // TRHInfo trhInfo = new TRHInfo();
        trhInfo.put("temperature", temperature);
 
 
        //仓温度信息
        KafkaGrainTH grainTH = new KafkaGrainTH();
        grainTH.setHumidity(grainData.getHumidityIn() + "");
        grainTH.setTemperature(grainData.getTempIn() + "");
        grainTH.setAirHumidity(grainData.getHumidityOut() + "");
        grainTH.setAirTemperature(grainData.getTempOut() + "");
 
        List<KafkaGrainTH> temperatureAndhumidity = new ArrayList<>();
        temperatureAndhumidity.add(grainTH);
 
        trhInfo.put("temperatureAndhumidity", temperatureAndhumidity);
        //trhInfo.put("temperatureAndhumidity",grainTH);
 
        JSONObject params = new JSONObject();
        params.put("TRHInfo", trhInfo);
 
        result.setParams(params);
 
        return result;
    }
 
    /**
     * 针对筒仓  TODO -----
     *
     * @param lastData
     * @param device
     * @return
     */
    private KafaGrainData lastData2PushData2(Fz40Grain lastData, GatewayDevice device) {
 
        return null;
    }
 
 
    @Override
    public BaseResp testWeight(BaseReqData reqData) {
 
        GatewayDevice device = reqData.getDevice();
        WebSocketPacket packet = new WebSocketPacket();
 
        WebSocketPacketHeader header = new WebSocketPacketHeader();
        header.setDeviceName(device.getDeviceName());
        header.setProductId(device.getProductId());
        packet.setHeaders(header);
        packet.setMessageType(ScConstant.MESSAGE_TYPE_REPORT_PROPERTY);
        packet.setDeviceId(device.getDeviceId());
 
        //设置信息主体
        WeightInfo weightInfo = new WeightInfo();
        weightInfo.setGrossWeight(reqData.getWeight());
        weightInfo.setNetWeight(reqData.getWeight());
        weightInfo.setNetWeight(reqData.getWeight());
        weightInfo.setWeightUnit("KG");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("weightInfo", JSONObject.toJSONString(weightInfo));
 
        packet.setProperties(jsonObject);
 
        packet.setTimestamp(System.currentTimeMillis());
 
        return new BaseResp(JSONObject.toJSONString(packet));
    }
 
    @Override
    public BaseResp testLpr(BaseReqData reqData) {
 
 
        GatewayDevice device = reqData.getDevice();
 
        WebSocketPacket packet = new WebSocketPacket();
        WebSocketPacketHeader header = new WebSocketPacketHeader();
        header.setDeviceName(reqData.getDeviceName());
        header.setProductId(reqData.getProductId());
 
        packet.setHeaders(header);
        packet.setMessageType(ScConstant.MESSAGE_TYPE_REPORT_PROPERTY);
        packet.setDeviceId(reqData.getDeviceId());
        packet.setMessageId(System.currentTimeMillis() + "");
        //设置信息主体
        LprData lpr = new LprData();
        lpr.setDeviceId(reqData.getDeviceId());
        lpr.setCarNumber(reqData.getCarNumber());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("carNumber", reqData.getCarNumber());
        jsonObject.put("position", device.getPosition());
        packet.setProperties(jsonObject);
 
        packet.setTimestamp(System.currentTimeMillis());
 
        return new BaseResp(JSONObject.toJSONString(packet));
    }
 
    private BaseResp getGrainTest1(BaseReqData reqData, GatewayDevice device) {
 
        String[] cableRule = device.getCableRule().split("-");
 
        int cableZ = Integer.valueOf(cableRule[0]);
        int cableY = Integer.valueOf(cableRule[1]);
        int cableX = Integer.valueOf(cableRule[2]);
        int sumNum = cableZ * cableY * cableX;
 
 
        WeatherWebDto weather = WeatherWebDto.contextMap.get("default");
        double tMIn = 10, tMax = 15;
        if (null != weather) {
            double tOut = Double.valueOf(weather.getTem());
            tMIn = tOut - 5;
            tMax = tOut;
        }
        if (tMIn < 10) tMIn = 10;
        if (tMIn > 20) tMIn = 20;
        if (tMax < 15) tMax = 15;
        if (tMax > 25) tMax = 25;
 
        //数据封装
        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(NumberUtil.keepPrecision((tMax + tMIn) / 2, 1) + "");
        outPut.setMinTemperature(tMax + "");
        outPut.setMaxTemperature(tMIn + "");
 
 
        List<GrainTemp> temperature = new ArrayList<>();
        //根号
        int cableNum = 1, position = 0;
 
        double curTemp = tMIn;
        double randomNumber = tMIn;
        int x = 0, y = 0, z = 0;
        for (int i = 0; i < sumNum; i++) {
            log.info("i=:" + i);
            if (i % cableZ == 0) {
                randomNumber = RandomUtils.nextDouble(tMIn, tMax + 1.5);
                log.info("第1层温度:" + randomNumber);
            } else if (i % cableZ == 1) {
                randomNumber = RandomUtils.nextDouble(tMIn - 1, tMax - 0);
                log.info("第2层温度:" + randomNumber);
            } else if (i % cableZ == 2) {
                randomNumber = RandomUtils.nextDouble(tMIn - 2, tMax - 1.5);
                log.info("第3层温度:" + randomNumber);
            } else if (i % cableZ == 3) {
                randomNumber = RandomUtils.nextDouble(tMIn - 3, tMax - 3);
                log.info("第4层温度:" + randomNumber);
            } else if (i % cableZ == 4) {
                randomNumber = RandomUtils.nextDouble(tMIn - 4, tMax - 4.5);
                log.info("第5层温度:" + randomNumber);
            }
            // randomNumber = Math.random() * (tMax - tMIn + 1) + tMIn;
            curTemp = NumberUtil.keepPrecision(randomNumber, 1);
            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);
        List<GrainTH> ths = new ArrayList<>();
 
        ths.add(new GrainTH(weather.getTem() != null ? weather.getTem() + "" : "", weather.getHumidity() != null ? weather.getHumidity() + "" : "", "1"));
        outPut.setTemperatureAndhumidity(ths);
 
        grain.setOutput(JSONObject.toJSONString(outPut));
 
 
        GatewayDevice gatewayDeviceWeather = GatewayUtils.getCacheByDeviceTypeOne(GatewayDeviceType.TYPE_09.getCode());
 
        //气象信息
        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));
 
        return new BaseResp(JSONObject.toJSONString(grain));
    }
}