jiazx0107@163.com
2023-05-17 620eab6cca2bc9ef9ea6d3067a0a5ba1deadbd1c
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
package com.ld.igds.protocol.fzzy.analysis;
 
import com.alibaba.fastjson.JSONObject;
import com.ld.igds.common.CoreCommonService;
import com.ld.igds.constant.*;
import com.ld.igds.io.constant.OrderRespEnum;
import com.ld.igds.io.notify.NotifyGrainInvoker;
import com.ld.igds.models.*;
import com.ld.igds.order.ExeOrderService;
import com.ld.igds.order.data.ExeRequest;
import com.ld.igds.protocol.fzzy.ServerUtils;
import com.ld.igds.protocol.fzzy.command.BaseRemoteImpl;
import com.ld.igds.protocol.fzzy.dto.ReMessage;
import com.ld.igds.protocol.fzzy.dto.SendMessage;
import com.ld.igds.protocol.fzzy.resp.Response2102;
import com.ld.igds.protocol.fzzy.builder.SimpleCommandBuilder;
import com.ld.igds.util.ContextUtil;
import com.ld.igds.warn.WarnUtils;
import com.ld.igds.warn.service.CoreWarnService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * 粮情解析
 *
 * @author Andy
 */
@Slf4j
@Component(AnalysisGrain.BEAN_ID)
public class AnalysisGrain extends BaseRemoteImpl {
 
    public static final String BEAN_ID = "fzzy.analysisGrain";
 
    @Autowired
    private CoreCommonService commonService;
    @Autowired
    private ExeOrderService exeOrderService;
    @Autowired
    private NotifyGrainInvoker notifyGrainInvoker;
    @Autowired
    private WarnUtils warnUtils;
 
    public static int ERROR_CHECK_TAG = -1000;//粮温,气体,虫害,故障值
 
    public static int ERROR_CHECK_TAG2 = -2000;//传感器故障或者不在线
 
    public void analysis2101(ReMessage reMessage, DeviceSer ser) {
        //DO NOTHING
    }
 
    /**
     * 粮情返回信息解析
     *
     * @param reMessage
     * @param ser
     */
    public void analysis2102(ReMessage reMessage, DeviceSer ser) {
 
        //返回控制柜信息
        returnMsg(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;
        }
        log.error("控制柜的报文信息:{}", reMessage);
        ExeRequest exeRequest =list.get(0);
        log.error("获取对应命令信息:{}", exeRequest);
 
        DepotConf depotConf = commonService.getCacheDepotConf(
                ser.getCompanyId(), exeRequest.getDepotId());
        log.error("获取仓库配置信息:{}", depotConf);
        Response2102 response = JSONObject.parseObject(reMessage.getContent(), Response2102.class);
 
 
        //直接解析-保存
        doSave(response, exeRequest, depotConf, sysConf);
    }
 
    private void returnMsg(DeviceSer ser) {
        //先回复控制柜
        SimpleCommandBuilder commandBuilder = SimpleCommandBuilder.getInstance();
        SendMessage message = commandBuilder.buildMessage(ser, ServerUtils.FUNCTION_2102);
        send(message);
    }
 
    private void doSave(Response2102 response, ExeRequest exeRequest, DepotConf depotConf, DicSysConf sysConf) {
        boolean warnTag = false;
        Grain grain = new Grain();
        grain.setBatchId(ContextUtil.getBatchIdByFireq(depotConf.getGrainFreq()));
        grain.setReceiveDate(new Date());
        grain.setCable(depotConf.getCableRule());
        grain.setCableCir(depotConf.getCableCir());
        grain.setCheckUser(exeRequest.getExeUser());
        grain.setDepotId(response.getHouseId());
        grain.setCompanyId(depotConf.getCompanyId());
        grain.setHumidityIn(response.getHIn());
        grain.setHumidityOut(response.getHOut());
        grain.setTempIn(response.getTIn());
        grain.setTempOut(response.getTOut());
        grain.setTempAve(response.getTAvg());
        grain.setTempMax(response.getTMax());
        grain.setTempMin(response.getTMin());
        grain.setDeptId(exeRequest.getDeptId());
        if (null != response.getHouseHeight()) {
            grain.setOilHeight(response.getLiquidHeight() + "-" + response.getHouseHeight());
        }
        grain.setRemark(response.getRemark());
 
        //判断有没有超过高温
        if (null != depotConf.getTempMax() && grain.getTempMax() > depotConf.getTempMax()) {
            grain.setRemark("仓库配置高温警告值:" + depotConf.getTempMax() + ",当前检测高温值:"
                    + grain.getTempMax());
            warnTag = true;
        }else {
            grain.setRemark("粮温正常");
        }
 
        String points = response.getPoints();
        points = points.replaceAll(ERROR_CHECK_TAG + "", Constant.ERROR_TEMP + "");
        points = points.replaceAll(ERROR_CHECK_TAG2 + "", Constant.ERROR_TEMP + "");
        grain.setPoints(points);
        //判断是否是锥形仓,是的话进行锥形仓补点
        if(StringUtils.isNotEmpty(depotConf.getCableCone())){
            //上锥形补点
            if(Constant.CABLE_CONE_1.equals(depotConf.getCableCone())){
                points = upConePoints(depotConf, points);
                grain.setPoints(points);
            }
            //下锥形补点
            if(Constant.CABLE_CONE_2.equals(depotConf.getCableCone())){
                points = downConePoints(depotConf, points);
                grain.setPoints(points);
            }
        }
 
        // 用户封装好数据即可
        String result = notifyGrainInvoker.analysisSuccess(grain, exeRequest, depotConf, sysConf);
        if (null != result) {
            log.info("控制柜----->>>平台:粮情解析完成,保存失败-仓库={},批次={},失败信息={}", depotConf.getDepotName(), grain.getBatchId(), result);
        } else {
            log.info("控制柜----->>>平台:粮情解析完成-仓库={},批次={}", depotConf.getDepotName(), grain.getBatchId());
            //生成粮情高温警告记录
            if(warnTag){
                warnUtils.addGrainWarn(depotConf, grain);
            }
        }
    }
 
    /**
     * 上锥形补点(点位位置正确,需要补点为-102)
     *
     * @param depotConf
     * @param points
     * @return
     */
    private String upConePoints(DepotConf depotConf, String points){
        String[] cableRuleAtt = depotConf.getCableRule().split("-");
        String[] cableCirAtt = depotConf.getCableCir().split("-");
        //获取最大的层配置
        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]);
            }
        }
        //所在层从1开始
        int curLay = 1;
        //所在根
        int curRoot = 1;
        //所在圈
        int curCir = 1;
        //当前圈的层
        int cirLay = 1;
        String[] array = points.split(",");
 
        for (int i = 0; i < array.length; i++) {
            curLay = (i % layMax) + 1;
            curRoot = (i / layMax) + 1;
            curCir = getCurCir(curRoot, cableRuleAtt);
            cirLay = Integer.valueOf(cableCirAtt[curCir - 1]);
 
            //比如配置了5层但是当前是6层,说明当前点为补偿点
            if (curLay <= (layMax - cirLay)) {
                array[i] = String.valueOf(Constant.ADD_TEMP);
            }
        }
        return StringUtils.join(array, ",");
    }
 
    /**
     * 下锥形补点-点位位置正确,将锥形点补为-102
     */
    /**
     * 下锥形补点(点位位置正确,需要补点为-102)
     *
     * @param depotConf
     * @param points
     * @return
     */
    private String downConePoints(DepotConf depotConf, String points){
        String[] cableRuleAtt = depotConf.getCableRule().split("-");
        String[] cableCirAtt = depotConf.getCableCir().split("-");
        //获取最大的层配置
        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]);
            }
        }
        //所在层从1开始
        int curLay = 1;
        //所在根
        int curRoot = 1;
        //所在圈
        int curCir = 1;
        //当前圈的层
        int cirLay = 1;
        String[] array = points.split(",");
 
        for (int i = 0; i < array.length; i++) {
            curLay = (i % layMax) + 1;
            curRoot = (i / layMax) + 1;
            curCir = getCurCir(curRoot, cableRuleAtt);
            cirLay = Integer.valueOf(cableCirAtt[curCir - 1]);
 
            //比如配置了5层但是当前是6层,说明当前点为补偿点
            if (curLay > cirLay) {
                array[i] = String.valueOf(Constant.ADD_TEMP);
            }
        }
        return StringUtils.join(array, ",");
    }
 
    /**
     * 获取当前根所在圈
     *
     * @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;
    }
}