CZT
2023-09-06 71c4fa1e27f75ae4b765c95c67a3069c84dc72ba
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
package com.ld.igds.protocol.zldz.analysis;
 
import com.ld.igds.common.CoreCommonService;
import com.ld.igds.common.dto.THDto;
import com.ld.igds.constant.BizType;
import com.ld.igds.constant.Constant;
import com.ld.igds.constant.DepotType;
import com.ld.igds.grain.GrainUtil;
import com.ld.igds.grain.dto.GrainItemInfo;
import com.ld.igds.io.notify.NotifyGrainInvoker;
import com.ld.igds.io.constant.OrderRespEnum;
import com.ld.igds.io.request.BaseRequest;
import com.ld.igds.models.DepotConf;
import com.ld.igds.models.DeviceSer;
import com.ld.igds.models.DicSysConf;
import com.ld.igds.models.Grain;
import com.ld.igds.order.ExeOrderService;
import com.ld.igds.order.data.ExeRequest;
import com.ld.igds.protocol.zldz.analysis.message.ReMessage;
import com.ld.igds.protocol.zldz.command.RemoteGrainServiceImpl;
import com.ld.igds.protocol.zldz.task.CommandReSendService;
import com.ld.igds.protocol.zldz.util.ServerUtils;
import com.ld.igds.th.CoreThService;
import com.ld.igds.util.BytesUtil;
import com.ld.igds.util.ContextUtil;
import com.ld.igds.warn.WarnUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 粮情解析
 *
 * @author Andy
 */
@Slf4j
@Component(AnalysisGrain.BEAN_ID)
public class AnalysisGrain {
 
    public static final String BEAN_ID = "zldz.analysisGrain";
 
    @Autowired
    private CoreCommonService commonService;
    @Autowired
    private CoreThService thService;
    @Autowired
    private ExeOrderService exeOrderService;
    @Autowired
    private GrainUtil grainUtil;
    @Autowired
    private NotifyGrainInvoker notifyGrainInvoker;
    @Autowired
    private RemoteGrainServiceImpl remoteGrainService;
    @Autowired
    private CommandReSendService commandReSendService;
    @Autowired
    private WarnUtils warnUtils;
 
    public static Map<String, Map<String, String>> contextMap = new HashMap<>();
 
    public static double ERROR_CHECK_TAG = -100.0;
 
    public static double FAULT_CHECK_TAG = 85.0;
 
    public static double ERROR_CHECK_TAG2 = 50;
 
    public static double MAX_TEMP = -50.0;
    public static double MIN_TEMP = 50.0;
 
    /**
     * 后台发送请求后,终端的响应
     *
     * @param reMessage
     * @param ser
     */
    public void analysis8815(ReMessage reMessage, DeviceSer ser) {
        BaseRequest request = new BaseRequest();
        request.setSerId(ser.getId());
        request.setCompanyId(ser.getCompanyId());
        request.setIp(reMessage.getIp());
        request.setPort(reMessage.getPort());
        request.setSerName(ser.getName());
 
        remoteGrainService.reply8815(request);
    }
 
    /**
     * 接收到粮情的回复
     *
     * @param reMessage
     * @param ser
     */
    public void reply8817(ReMessage reMessage, DeviceSer ser) {
        String bodyMsg = reMessage.getBody().getContent();
 
        log.info("分机------>>>平台:收到的粮情信息-{}-{}-{}", ser.getCompanyId(),
                ser.getName(), reMessage.getStrMsg());
        // Step1根据需要简化需要的信息
        String ua = bodyMsg.substring(0 * 2, 1 * 2);
        log.debug("获取到的电压信息,系统不保留={}", ua);
 
        // 总包数
        // int sumPacket = BytesUtil.hexToInt(bodyMsg.substring(1 * 2, 1 * 2 + 1
        // * 2));
        // 当前包
        String hexCurPacket = bodyMsg.substring(2 * 2, 2 * 2 + 1 * 2);
        // int curPacket = BytesUtil.hexToInt(hexCurPacket);
 
        BaseRequest request = new BaseRequest();
        request.setSerId(ser.getId());
        request.setCompanyId(ser.getCompanyId());
        request.setIp(reMessage.getIp());
        request.setPort(reMessage.getPort());
        request.setSerName(ser.getName());
 
        remoteGrainService.reply8817(request, hexCurPacket);
    }
 
    @Transactional
    public void analysis8817(ReMessage msg, DeviceSer ser) {
        // 接到信息回复--主机
        reply8817(msg, ser);
 
        // 首先获取到系统参数,判断是否需要批次自动优化
        DicSysConf sysConf = commonService.getCacheSysConf(ser.getCompanyId());
 
        List<ExeRequest> list = exeOrderService.getInProgressOrderBySerId(BizType.GRAIN.getCode(), ser.getId());
 
        if (null == list || list.isEmpty()) {
            String info = "粮情解析失败:分机=" + ser.getName() + "没有获取到所属仓库信息。";
            log.error("分机------>>>平台:" + info);
            notifyGrainInvoker.notifyWeb(ser.getCompanyId(),
                    OrderRespEnum.MSG_ERROR, BizType.GRAIN, info);
            return;
        }
        ExeRequest exeRequest = list.get(0);
        log.info("获取粮情命令信息={}", exeRequest);
        DepotConf depotConf = commonService.getCacheDepotConf(
                exeRequest.getCompanyId(), exeRequest.getDepotId());
        if (null == depotConf) {
            String info = "粮情解析失败:分机=" + ser.getName() + "没有获取到粮情参数配置信息。";
            log.error("分机------>>>平台:" + info);
            notifyGrainInvoker.notifyWeb(ser.getCompanyId(),
                    OrderRespEnum.MSG_ERROR, BizType.GRAIN, info);
            return;
        }
 
        analysisStep(depotConf, msg, ser, exeRequest, sysConf);
    }
 
 
    /**
     * 开始解析
     *
     * @param ser
     * @param msg
     * @throws Exception
     */
    private void analysisStep(DepotConf depotConf, ReMessage msg,
                              DeviceSer ser, ExeRequest exeRequest, DicSysConf sysConf) {
        // 粮情的批次号重新根据频率调整
        msg.setBatchId(ContextUtil.getBatchIdByFireq(depotConf.getGrainFreq()));
 
        String bodyMsg = msg.getBody().getContent();
 
        log.debug("{}-{}=收到的粮情信息={}", ser.getCompanyId(), ser.getName(),
                msg.getStrMsg());
 
        // Step1根据需要简化需要的信息
        String ua = bodyMsg.substring(0 * 2, 1 * 2);
        log.debug("获取到的电压信息,系统不保留={}", ua);
 
        // 总包数
        int sumPacket = BytesUtil.hexToInt(bodyMsg.substring(1 * 2,
                1 * 2 + 1 * 2));
        // 当前包
        String hexCurPacket = bodyMsg.substring(2 * 2, 2 * 2 + 1 * 2);
        int curPacket = BytesUtil.hexToInt(hexCurPacket);
 
        log.debug("分机={},包总数={},当前包={}", ser.getName(), sumPacket, curPacket);
 
        // 粮食温度信息
        String grainStr = bodyMsg.substring(3 * 2);
 
        String contextKey = this.buildContextKey(msg, depotConf.getDepotId());
        String curKey = this.buildCurKey(msg, curPacket);
 
        Map<String, String> curMap = contextMap.get(contextKey);
        if (null == curMap) {
            curMap = new HashMap<>();
            contextMap.put(contextKey, curMap);
        }
        contextMap.get(contextKey).put(curKey, grainStr);
 
        // 数据准备好,开始执行解析
        if (curPacket == sumPacket) {
            analysisStep0(depotConf, msg, ser, exeRequest, contextKey,
                    sumPacket, sysConf);
 
            log.info("分机------>>>平台:命令类型=8817--粮情全部收到,开始解析-{}-{}",
                    ser.getCompanyId(), ser.getName());
            // 清除命令
            commandReSendService.cleanKey(msg.getCompanyId(), msg.getIp(),
                    msg.getPort(), ServerUtils.MSG_TYPE_8817);
        }
    }
 
    private void analysisStep0(DepotConf depotConf, ReMessage msg,
                               DeviceSer ser, ExeRequest exeRequest, String contextKey,
                               int sumPacket, DicSysConf sysConf) {
 
        Map<String, String> curMap = contextMap.get(contextKey);
 
        List<DepotConf> depotConfs = null;
 
        // 一分机多仓,把起始列放到最大进行采集
        if (Constant.YN_Y.equals(sysConf.getGrainMoreTag())) {
            depotConfs = commonService.getCacheDepotConfBySerId2(
                    depotConf.getCompanyId(), ser.getId());
        }
 
        if (null == depotConfs) {// 一个分机1个仓
            if (DepotType.TYPE_02.getCode().equals(depotConf.getDepotType())) {
                analysisStep2(depotConf, msg, ser, exeRequest, curMap, sumPacket, sysConf);
            } else if (DepotType.TYPE_04.getCode().equals(depotConf.getDepotType())) {
                analysisStep2(depotConf, msg, ser, exeRequest, curMap, sumPacket, sysConf);
            } else {
                analysisStep1(depotConf, msg, ser, exeRequest, curMap, sumPacket, sysConf);
            }
 
            return;
        }
 
        // 一分几多仓情况,考虑单仓采集和多仓采集
        // 单仓采集时候数据从0返回
        if (StringUtils.isEmpty(exeRequest.getDepotIds())) {
 
            depotConf.setCableEnd(depotConf.getCableEnd() - depotConf.getCableStart() + 1);
            depotConf.setCableStart(ser.getCableStart());
 
            if (DepotType.TYPE_02.getCode().equals(depotConf.getDepotType())) {
                analysisStep2(depotConf, msg, ser, exeRequest, curMap, sumPacket, sysConf);
            } else if (DepotType.TYPE_04.getCode().equals(depotConf.getDepotType())) {
                analysisStep2(depotConf, msg, ser, exeRequest, curMap, sumPacket, sysConf);
            } else {
                analysisStep1(depotConf, msg, ser, exeRequest, curMap, sumPacket, sysConf);
            }
 
            return;
 
        }
 
        // 批量采集所有关联一起采集,遍历执行
        for (DepotConf depotConfTemp : depotConfs) {
            if (DepotType.TYPE_02.getCode().equals(depotConfTemp.getDepotType())) {
                analysisStep2(depotConfTemp, msg, ser, exeRequest, curMap, sumPacket, sysConf);
            } else if (DepotType.TYPE_04.getCode().equals(depotConfTemp.getDepotType())) {
                analysisStep2(depotConfTemp, msg, ser, exeRequest, curMap, sumPacket, sysConf);
            } else {
                analysisStep1(depotConfTemp, msg, ser, exeRequest, curMap, sumPacket, sysConf);
            }
        }
    }
 
    /**
     * 圆筒仓的粮情解析
     *
     * @param depotConf
     * @param msg
     * @param ser
     * @param exeRequest
     * @param packetMap
     * @param sumPackets
     */
    private void analysisStep2(DepotConf depotConf, ReMessage msg,
                               DeviceSer ser, ExeRequest exeRequest,
                               Map<String, String> packetMap, int sumPackets, DicSysConf sysConf) {
        if (StringUtils.isEmpty(depotConf.getCableRule())
                || StringUtils.isEmpty(depotConf.getCableCir())) {
            log.error("分机------>>>平台:当前仓库:{}-{},没有没有配置布线规则,无法解析粮情信息……",
                    ser.getCompanyId(), depotConf.getDepotName());
            return;
        }
 
        String[] cableRuleAtt = depotConf.getCableRule().split("-");
        String[] cableCirAtt = depotConf.getCableCir().split("-");
 
        if (cableRuleAtt.length != cableCirAtt.length) {
            log.error("分机------>>>平台:当前仓库:{}-{},布线规则不正确,无法解析粮情信息……", ser.getCompanyId(), depotConf.getDepotName());
            return;
        }
 
 
        //避免空指针
        if (null == depotConf.getCableCone()) depotConf.setCableCone(Constant.CABLE_CONE_0);
 
        // 获取最大的层配置--默认每一圈都一样
        int layMax = Integer.valueOf(cableCirAtt[0]);
        for (int i = 0; i < cableCirAtt.length; i++) {
            if (Integer.valueOf(cableCirAtt[i]) >= layMax) layMax = Integer.valueOf(cableCirAtt[i]);
        }
 
        // 获取完整的粮情包信息
        String strPoints = "";
        for (int i = 1; i <= sumPackets; i++) {
            strPoints += packetMap.get(buildCurKey(msg, i));
        }
 
        log.info("------筒仓完整的粮情报文={}------", strPoints);
 
        int sumNum = 0, cableZ = 1;// sumNum 共多少根电缆;cableZ 层的最大值,锥形仓补齐最大层
        for (int i = 0; i < cableCirAtt.length; i++) {
            if (Integer.valueOf(cableCirAtt[i]) > cableZ) {
                cableZ = Integer.valueOf(cableCirAtt[i]);
            }
            sumNum += Integer.valueOf(cableRuleAtt[i]);
        }
 
        // 根据层行列获取指定长度
        int start = 4 * (depotConf.getCableStart() - ser.getCableStart()) * cableZ;
        int len = 4 * cableZ * sumNum;
 
        strPoints = strPoints.substring(start, start + len);
        log.info("分机------>>>平台:返回粮情完整信息,所属组织={},分机={}", ser.getCompanyId(), ser.getName());
 
        // 将粮情解析成数组
        List<Double> temps = new ArrayList<>();
        double tempValue;
        String temp;
        int curLay = 1;//所在层从1开始
        int curRoot = 1;//所在根
        int curCir = 1;//所在圈
        int cirLay = 1;//当前圈的层
        for (int i = 0; i < strPoints.length() / 4; i++) {
            temp = strPoints.substring(i * 4, i * 4 + 4);
            if (temp == null) {
                temp = "0000";
            }
            tempValue = BytesUtil.hexToInt(BytesUtil.tran_LH(temp)) / 10.0;
 
            //非正常值
            if (tempValue > ERROR_CHECK_TAG2) {
                tempValue = Constant.ERROR_TEMP;
            }
 
            // 故障值处理
            if (tempValue >= FAULT_CHECK_TAG) {
                tempValue = Constant.FAULT_TEMP;
            }
 
            // 备用值
            if (tempValue == ERROR_CHECK_TAG) {
                tempValue = Constant.ERROR_TEMP;
 
 
                //验证是不是锥形仓补偿值
                curLay = (i % layMax) + 1;
                curRoot = (i / layMax) + 1;
                curCir = getCurCir(curRoot, cableRuleAtt);
 
                cirLay = Integer.valueOf(cableCirAtt[curCir - 1]);
 
                //比如配置了5层但是当前是6层,说明当前点为补偿点
                if (curLay > cirLay) {
                    tempValue = Constant.ADD_TEMP;
 
                    //判断是不是上锥形,将补点转移到上方
                    if (Constant.CABLE_CONE_1.equals(depotConf.getCableCone())) {
                        int index = i - curLay - 1;
                        temps.add(index, tempValue);
                    }else {
                        temps.add(tempValue);
                    }
 
                } else {
                    temps.add(tempValue);
                }
 
            } else {
                temps.add(tempValue);
            }
        }
 
        log.debug("-------CheckGrainRequest--={}", exeRequest.toString());
        THDto thDto = thService.getCacheTH(exeRequest.getCompanyId(), exeRequest.getTempSerId(), exeRequest.getThConf());
        if (null != thDto) {
            log.debug("-------THDto--={}", thDto.toString());
        }
 
        // 将集合解析成坐标数据
        addPoint2(temps, msg, depotConf, exeRequest, thDto, sysConf);
    }
 
    /**
     * 获取当前跟所在圈
     *
     * @param curRoot
     * @param cableRuleAtt
     * @return
     */
    private int getCurCir(int curRoot, String[] cableRuleAtt) {
 
        int sum = 0;
        for (int i = 0; i < cableRuleAtt.length; i++) {
            sum += Integer.valueOf(cableRuleAtt[i]);
            if (curRoot <= sum) return i + 1;
        }
 
        return 1;
    }
 
    /**
     * 平方仓的解析,解析需要考虑当前是否启用的一分机多仓
     *
     * @param depotConf
     * @param msg
     * @param ser
     * @param exeRequest
     * @param packetMap
     * @param sumPackets
     */
    private void analysisStep1(DepotConf depotConf, ReMessage msg,
                               DeviceSer ser, ExeRequest exeRequest,
                               Map<String, String> packetMap, int sumPackets, DicSysConf sysConf) {
        // 获取完整的粮情包信息
        String strPoints = "";
        for (int i = 1; i <= sumPackets; i++) {
            strPoints += packetMap.get(buildCurKey(msg, i));
        }
        String[] attCable = depotConf.getCableRule().split("-");
        int cableZ = Integer.valueOf(attCable[0]);
        int cableY = Integer.valueOf(attCable[1]);
        int cableX = Integer.valueOf(attCable[2]);
 
        // 根据层行列获取指定长度
        int start = 4 * (depotConf.getCableStart() - ser.getCableStart())
                * cableZ * cableY;
        int len = 4 * cableZ * cableY * cableX;
 
        log.info("分机------>>>平台:返回粮情完整信息,所属组织={},分机={}", ser.getCompanyId(), ser.getName());
 
        strPoints = strPoints.substring(start, start + len);
 
        // 将粮情解析成数组
        List<Double> temps = new ArrayList<>();
        double tempValue;
        String temp;
        for (int i = 0; i < strPoints.length() / 4; i++) {
            temp = strPoints.substring(i * 4, i * 4 + 4);
            if (temp == null) {
                temp = "0000";
            }
            tempValue = BytesUtil.hexToInt(BytesUtil.tran_LH(temp)) / 10.0;
            // 说明解析的数据有问题
            if (tempValue == ERROR_CHECK_TAG || tempValue == ERROR_CHECK_TAG2) {
                tempValue = Constant.ERROR_TEMP;
            }
            // 故障值处理
            if (tempValue >= FAULT_CHECK_TAG) {
                tempValue = Constant.FAULT_TEMP;
            }
            temps.add(tempValue);
        }
 
        log.debug("-------CheckGrainRequest--={}", exeRequest.toString());
        THDto thDto = thService.getCacheTH(exeRequest.getCompanyId(),
                exeRequest.getTempSerId(), exeRequest.getThConf());
        if (null != thDto) {
            log.debug("-------THDto--={}", thDto.toString());
        }
        // 将集合解析成坐标数据
        addPoint1(temps, msg, depotConf, ser, exeRequest, thDto, sysConf);
    }
 
    private String buildCurKey(ReMessage msg, int curPacket) {
        return msg.getSourceId() + "_" + curPacket;
    }
 
    private String buildContextKey(ReMessage msg, String depotId) {
        return msg.getCompanyId() + "_" + msg.getSourceId() + "_" + depotId;
    }
 
    /**
     * 平房仓,解析第二步,解析到坐标数据
     *
     * @param temps
     * @throws Exception
     */
    private void addPoint1(List<Double> temps, ReMessage msg,
                           DepotConf depotConf, DeviceSer ser, ExeRequest exeRequest,
                           THDto thDto, DicSysConf sysConf) {
 
        //根据电缆起始方位和布线方向,对粮情数据进行调整
        if (null != depotConf.getStartOrientation()) {
            temps = reversalGrainPoint(temps, depotConf);
        }
 
        //若起点点位为底部,则将粮情数据进行翻转
        if (null != depotConf.getStartPoint()
                && Constant.GRAIN_START_POINT_BELOW.equals(depotConf.getStartPoint())) {
            temps = grainUtil.reversalUpAndDown(temps, depotConf.getCableRule());
        }
 
        //若配置层行转换,则将粮情数据进行层行转换
        if (StringUtils.isNotEmpty(depotConf.getStartConvert())) {
            temps = convertGrainPoint(temps, depotConf);
            String[] cableRule = depotConf.getCableRule().split("-");
            //转换层行列配置
            if(Constant.GRAIN_CONVERT_CLOCKWISE.equals(depotConf.getStartConvert())
                    || Constant.GRAIN_CONVERT_ANTICLOCKWISE.equals(depotConf.getStartConvert())){
                depotConf.setCableRule(Integer.valueOf(cableRule[1]) + "-" + Integer.valueOf(cableRule[0]) + "-" + Integer.valueOf(cableRule[2]));
            }
        }
 
        String[] attCable = depotConf.getCableRule().split("-");
        int cableZ = Integer.valueOf(attCable[0]);
        int cableY = Integer.valueOf(attCable[1]);
        int cableX = Integer.valueOf(attCable[2]);
 
        Grain grain = new Grain();
        grain.setDepotId(depotConf.getDepotId());
        grain.setCompanyId(depotConf.getCompanyId());
        grain.setCable(depotConf.getCableRule());
        grain.setBatchId(msg.getBatchId());
        grain.setTempIn(Constant.ERROR_TEMP);
        grain.setHumidityIn(Constant.ERROR_TEMP);
 
        if (null != thDto) {
            log.debug("粮情解析中获取到的温湿度信息={}", thDto.toString());
            grain.setHumidityIn(thDto.getHumidityIn());
            grain.setTempIn(thDto.getTempIn());
        }
        grain.setReceiveDate(msg.getReceiveDate());
        grain.setRemark("粮温正常");
 
        // 获取缓存中的命令信息
        grain.setCheckUser(exeRequest.getExeUser());
 
        double max = MAX_TEMP, min = MIN_TEMP, sumT = 0.0, sumNum = cableX
                * cableY * cableZ;
 
        // 校验和实现统计,生成采集点信息
        List<GrainItemInfo> listGrainItems = new ArrayList<>();
        int i = 1;
        for (Double temp : temps) {
            if (temp == Constant.ERROR_TEMP || temp == Constant.FAULT_TEMP || temp == Constant.ADD_TEMP) {
                sumNum--;
            } else {
                sumT += temp;
                if (temp > max) {
                    max = temp;
                }
                if (temp < min) {
                    min = temp;
                }
            }
            listGrainItems.add(new GrainItemInfo(i, temp));
            i++;
        }
 
        if (sumNum == 0) {
            sumNum = 1;
            grain.setRemark("当前粮情采集异常");
        }
        //过滤比较用的最大最小值
        if(max == MAX_TEMP){
            max = 0.0;
        }
        if(min == MIN_TEMP){
            min = 0.0;
        }
        if (null != depotConf.getTempMax() && max > depotConf.getTempMax()) {
            grain.setRemark("仓库配置高温警告值:" + depotConf.getTempMax() + ",当前检测高温值:"
                    + max);
            warnUtils.addGrainWarn(depotConf, grain);
        }
 
        grain.setTempAve(sumT / sumNum);
        grain.setTempMax(max);
        grain.setTempMin(min);
        grain.setPoints(StringUtils.join(temps, ","));
 
        String depotIds = exeRequest.getDepotIds();
 
        boolean notifyWeb = true;
        if (null != depotIds && depotIds.indexOf(depotConf.getDepotId()) == -1) {
            notifyWeb = false;
        }
        // 用户封装好数据即可
        notifyGrainInvoker.analysisSuccess(grain, listGrainItems, depotConf,
                sysConf, notifyWeb, exeRequest);
    }
 
    /**
     * 圆筒仓解析步骤
     *
     * @param temps
     * @param msg
     * @param depotConf
     * @param exeRequest
     * @param thDto
     */
    private void addPoint2(List<Double> temps, ReMessage msg,
                           DepotConf depotConf, ExeRequest exeRequest, THDto thDto,
                           DicSysConf sysConf) {
 
        Grain grain = new Grain();
        grain.setDepotId(depotConf.getDepotId());
        grain.setCompanyId(depotConf.getCompanyId());
        grain.setCable(depotConf.getCableRule());
        grain.setCableCir(depotConf.getCableCir());
        grain.setBatchId(msg.getBatchId());
        grain.setTempIn(Constant.ERROR_TEMP);
        grain.setHumidityIn(Constant.ERROR_TEMP);
        if (null != thDto) {
            log.debug("粮情解析中获取到的温湿度信息={}", thDto.toString());
            grain.setHumidityIn(thDto.getHumidityIn());
            grain.setTempIn(thDto.getTempIn());
        }
        grain.setReceiveDate(msg.getReceiveDate());
        grain.setRemark("粮温正常");
        grain.setCheckUser(exeRequest.getExeUser());
 
        double max = MAX_TEMP, min = MIN_TEMP, sumT = 0.0;
 
        int sumNum = temps.size();
        List<GrainItemInfo> listGrainItems = new ArrayList<>();
        int i = 1;
        for (Double temp : temps) {
            if (temp == Constant.ERROR_TEMP || temp == Constant.FAULT_TEMP || temp == Constant.ADD_TEMP) {
                sumNum--;
            } else {
                sumT += temp;
                if (temp > max) {
                    max = temp;
                }
                if (temp < min) {
                    min = temp;
                }
            }
            listGrainItems.add(new GrainItemInfo(i, temp));
            i++;
        }
 
        if (sumNum == 0) {
            sumNum = 1;
            grain.setRemark("当前粮情采集异常");
        }
        //过滤比较用的最大最小值
        if(max == MAX_TEMP){
            max = 0.0;
        }
        if(min == MIN_TEMP){
            min = 0.0;
        }
        if (null != depotConf.getTempMax() && max > depotConf.getTempMax()) {
            grain.setRemark("仓库配置高温警告值:" + depotConf.getTempMax() + ",当前检测高温值:"
                    + max);
            warnUtils.addGrainWarn(depotConf, grain);
        }
 
        grain.setTempAve(sumT / sumNum);
        grain.setTempMax(max);
        grain.setTempMin(min);
        grain.setPoints(StringUtils.join(temps, ","));
 
        String depotIds = exeRequest.getDepotIds();
        boolean notifyWeb = true;
        if (null != depotIds && depotIds.indexOf(depotConf.getDepotId()) == -1) {
            notifyWeb = false;
        }
 
        // 用户封装好数据即可
        notifyGrainInvoker.analysisSuccess(grain, listGrainItems, depotConf,
                sysConf, notifyWeb, exeRequest);
    }
 
    private List<Double> convertGrainPoint(List<Double> temps, DepotConf conf) {
        if (Constant.GRAIN_CONVERT_DEFAULT.equals(conf.getStartConvert())) {
            //若层行转换为默认,则直接返回
            return temps;
        }
 
        //顺时针转换
        if(Constant.GRAIN_CONVERT_CLOCKWISE.equals(conf.getStartConvert())){
            return grainUtil.convertRight(temps, conf.getCableRule());
        }
        //逆时针转换
        if(Constant.GRAIN_CONVERT_ANTICLOCKWISE.equals(conf.getStartConvert())){
            return grainUtil.convertLeft(temps, conf.getCableRule());
        }
 
        return temps;
    }
 
    private List<Double> reversalGrainPoint(List<Double> temps, DepotConf conf) {
        if (StringUtils.isEmpty(conf.getStartOrientation())) {
            //若起始方位为空,则默认起始方位和方向,直接返回
            return temps;
        }
        //起始方位为右边时
        if (Constant.GRAIN_START_ORIENTATION_RIGHT.equals(conf.getStartOrientation())) {
            if (StringUtils.isEmpty(conf.getStartDirection())) {
                //右边起始,默认纵向布线,直接返回
                return temps;
            }
            if (Constant.GRAIN_START_DIRECTION_TRANSVERSE.equals(conf.getStartDirection())) {
                //右边起始,横向布线
                return grainUtil.reversalRight1(temps, conf.getCableRule());
            }
            //右边起始,默认纵向布线,直接返回
            return temps;
        }
        //起始方位为右上时
        if (Constant.GRAIN_START_ORIENTATION_RIGHT_UP.equals(conf.getStartOrientation())) {
            if (StringUtils.isEmpty(conf.getStartDirection())) {
                //右上起始,默认纵向布线
                return grainUtil.reversalRightUp2(temps, conf.getCableRule());
            }
            if (Constant.GRAIN_START_DIRECTION_TRANSVERSE.equals(conf.getStartDirection())) {
                //右上起始,横向布线
                return grainUtil.reversalRightUp1(temps, conf.getCableRule());
            }
            //右上起始,默认纵向布线
            return grainUtil.reversalRightUp2(temps, conf.getCableRule());
        }
        //起始方位为左边时
        if (Constant.GRAIN_START_ORIENTATION_LEFT.equals(conf.getStartOrientation())) {
            if (StringUtils.isEmpty(conf.getStartDirection())) {
                //左边起始,默认纵向布线
                return grainUtil.reversalLeft2(temps, conf.getCableRule());
            }
            if (Constant.GRAIN_START_DIRECTION_TRANSVERSE.equals(conf.getStartDirection())) {
                //左边起始,横向布线
                return grainUtil.reversalLeft1(temps, conf.getCableRule());
            }
            //左边起始,默认纵向布线
            return grainUtil.reversalLeft2(temps, conf.getCableRule());
        }
        //起始方位为左上时
        if (Constant.GRAIN_START_ORIENTATION_LEFT_UP.equals(conf.getStartOrientation())) {
            if (StringUtils.isEmpty(conf.getStartDirection())) {
                //左上起始,默认纵向布线
                return grainUtil.reversalLeftUp2(temps, conf.getCableRule());
            }
            if (Constant.GRAIN_START_DIRECTION_TRANSVERSE.equals(conf.getStartDirection())) {
                //左上起始,横向布线
                return grainUtil.reversalLeftUp1(temps, conf.getCableRule());
            }
            //左上起始,默认纵向布线
            return grainUtil.reversalLeftUp2(temps, conf.getCableRule());
        }
 
        return temps;
    }
}