CZT
2023-10-07 2583d630205582822e2af3026b75c5f915352bfc
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
package com.ld.igds.io.impl;
 
import com.ld.igds.common.CoreCommonService;
import com.ld.igds.constant.DepotType;
import com.ld.igds.io.RemoteGrainService;
import com.ld.igds.io.constant.OrderRespEnum;
import com.ld.igds.io.constant.ProtocolEnum;
import com.ld.igds.io.request.CheckGrainRequest;
import com.ld.igds.io.response.GrainResponse;
import com.ld.igds.models.DepotConf;
import com.ld.igds.models.DicSysConf;
import com.ld.igds.models.Grain;
import com.ld.igds.models.WeatherInfo;
import com.ld.igds.util.ContextUtil;
import com.ld.igds.util.NumberUtil;
import com.ld.igds.warn.WarnUtils;
import com.ld.igds.weather.CoreWeatherService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
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;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
 
/**
 * 默认实现类,直接返回
 */
@Slf4j
@Component
public class DefaultRemoteGrainImpl implements RemoteGrainService {
 
    @Autowired
    private CoreCommonService commonService;
    @Autowired
    private CoreWeatherService weatherService;
    @Autowired
    private WarnUtils warnUtils;
 
    @Override
    public String getProtocol() {
        return ProtocolEnum.TCP_DEFAULT.getCode();
    }
 
    /**
     * 演示实现,根据当前库区的气象信息生成,一套演示数据。
     *
     * @param request
     * @return
     */
    @Override
    public GrainResponse checkGrain(CheckGrainRequest request) {
        WeatherInfo weatherInfo = weatherService.getCacheWeather(request.getCompanyId());
 
        if (null == weatherInfo) {
            log.error("平台------>>>>控制柜:粮情检测-失败{}", "系统没有获取到当前库区气象信息。");
 
            return new GrainResponse(OrderRespEnum.ORDER_ERROR.getCode(),
                    "命令发送异常:系统没有获取到当前库区气象信息。");
        }
        DepotConf depotConf = commonService.getCacheDepotConf(
                request.getCompanyId(), request.getDepotId());
 
        // 首先获取到系统参数,判断是否需要批次自动优化
        DicSysConf sysConf = commonService.getCacheSysConf(request.getCompanyId());
 
        List<Double> listPoint = new ArrayList<>();
        String[] cableRule = depotConf.getCableRule().split("-");
        int lay = 0, row = 0, col = 0, sumNum = 0;
        if (DepotType.TYPE_01.getCode().equals(depotConf.getDepotType())) {
            lay = Integer.valueOf(cableRule[0]);
            row = Integer.valueOf(cableRule[1]);
            col = Integer.valueOf(cableRule[2]);
            sumNum = lay * row * col;
        } else {
            String[] cableCir = depotConf.getCableCir().split("-");
            for (int i = 0; i < cableRule.length; i++) {
                sumNum += Integer.valueOf(cableRule[i]) * Integer.valueOf(cableCir[i]);
            }
        }
 
        //模拟数据,规则在温湿度的基础上下浮动2度随机取值。
        double tOut = Double.valueOf(weatherInfo.getTemp());
        double tMIn = tOut - 2;
        double tMax = tOut + 2;
        double sumTemp = 0.0;
        double temp = tOut;
        for (int i = 0; i < sumNum; i++) {
            temp = NumberUtil.keepPrecision(RandomUtils.nextDouble(tMIn, tMax), 1);
            listPoint.add(temp);
            sumTemp += temp;
        }
 
        double hOut = Integer.valueOf(weatherInfo.getHumidity().replace("%", ""));
 
        Grain grain = new Grain();
        grain.setBatchId(ContextUtil.getBatchIdByFireq(depotConf.getGrainFreq()));
        grain.setReceiveDate(new Date());
        grain.setCable(request.getCableRule());
        grain.setCableCir(depotConf.getCableCir());
        grain.setCheckUser(ContextUtil.getLoginUserCName());
        grain.setDepotId(request.getDepotId());
        grain.setCompanyId(depotConf.getCompanyId());
        grain.setHumidityIn(NumberUtil.keepPrecision(RandomUtils.nextDouble(hOut - 2, hOut + 2), 1));
        grain.setHumidityOut(hOut);
        grain.setTempIn(NumberUtil.keepPrecision(RandomUtils.nextDouble(tOut - 2, tOut + 2), 1));
        grain.setTempOut(tOut);
        grain.setTempAve(NumberUtil.keepPrecision(sumTemp / sumNum, 1));
        grain.setTempMax(tMax);
        grain.setTempMin(tMIn);
        grain.setDeptId(request.getDeptId());
        grain.setRemark("粮情正常");
 
        grain.setPoints(StringUtils.join(listPoint, ","));
        if (null != depotConf.getTempMax() && tMax > depotConf.getTempMax()) {
            grain.setRemark("仓库配置高温警告值:" + depotConf.getTempMax() + ",当前检测高温值:"
                    + tMax);
            warnUtils.addGrainWarn(depotConf, grain);
        }
 
        // 独立启动一个子现在,延迟之后自动返回
        FutureTask<String> futureTask = new FutureTask<>(new DefaultAnalysisGrainTask(grain, depotConf, sysConf,request.getOrderId()));
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.submit(futureTask);
        executorService.shutdown();
 
        return new GrainResponse(OrderRespEnum.ORDER_SUCCESS.getCode(),
                "粮情粮情发送成功!");
    }
 
}