CZT
2023-08-29 4cfaac76490c1391237483329719bc6abd4392af
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
package com.ld.igds.protocol.modbus.command;
 
import com.ld.igds.common.CoreDeviceService;
import com.ld.igds.constant.BizType;
import com.ld.igds.gas.CoreGasService;
import com.ld.igds.io.constant.OrderRespEnum;
import com.ld.igds.io.notify.NotifyWebInvoker;
import com.ld.igds.io.request.CheckGasRequest;
import com.ld.igds.io.request.DeviceControlRequest;
import com.ld.igds.models.Gas;
import com.ld.igds.protocol.modbus.ServerUtil;
import com.ld.igds.protocol.modbus.data.ModbusGasResult;
import com.ld.igds.util.ContextUtil;
import com.ld.igds.util.NumberUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
 
/**
 * 解析
 */
@Slf4j
@Component
public class AnalysisService {
 
    @Resource
    private CoreDeviceService coreDeviceService;
    @Resource
    private NotifyWebInvoker notifyInvoker;
    @Autowired
    private CoreGasService gasService;
 
 
    /**
     * 异步更新设备状态,
     *
     * @param request
     * @param sleepTime 延迟执行时间=毫秒值,如果==0 ,表示不延迟
     */
    @Async
    public void analysisDevice(DeviceControlRequest request, long sleepTime) {
 
        try {
            if (sleepTime > 0) {
                Thread.sleep(sleepTime);
            }
 
            coreDeviceService.updateStatus(request.getCompanyId(), request.getSerId(), ServerUtil.getStatusMap());
 
            notifyInvoker.notifyAnalysisStatusSuccess(request.getCompanyId(), request.getSerId(), OrderRespEnum.MSG_SUCCESS, "设备状态查询成功并完成解析!");
 
        } catch (Exception e) {
            log.error("---MODBUS-TCP-状态解析异常{}", e);
        }
    }
 
 
    /**
     * 解析气体检测结果
     *
     * @param request
     * @param results
     */
    public void analysisGas(CheckGasRequest request, List<ModbusGasResult> results) {
 
        log.debug("----------开始执行气体结果解析----{}", request.getDepotId());
 
        String batchId = ContextUtil.getDefaultBatchId();
        //主体信息
        Gas gas = new Gas(batchId, request.getCompanyId(), request.getDepotId(), new Date());
        gas.setCheckNum(results.size());
        gas.setGasEnd(results.size());
        gas.setGasStart(1);
        gas.setReceiveDate(new Date());
 
        int sumNum = results.size();
        double co2, o2, ph3, n2;
        DecimalFormat df = new DecimalFormat("#0.00");
        double sumO2 = 0.0, sumCo2 = 0.0, sumPh3 = 0.0, sumN2 = 0.0;
        StringBuffer sb = new StringBuffer();
        for (ModbusGasResult gasResult : results) {
            //二氧化碳,单位:PPM,直接使用
            co2 = gasResult.getCo2().doubleValue();
            //氧气,除以10,单位:百分比
            o2 = Double.valueOf(df.format(gasResult.getO2().doubleValue()*0.1));
            //磷化氢,单位:PPM,直接使用
            ph3 = gasResult.getPh3().doubleValue();
            n2 = Double.valueOf(df.format(99.9 - o2));;
 
            if (gas.getPerCo2Max() < co2) {
                gas.setPerCo2Max(co2);
            }
            if (gas.getPerCo2Min() > co2) {
                gas.setPerCo2Min(co2);
            }
            if (gas.getPerO2Max() < o2) {
                gas.setPerO2Max(o2);
            }
            if (gas.getPerO2Min() > o2) {
                gas.setPerO2Min(o2);
            }
            if (gas.getPerPh3Max() < ph3) {
                gas.setPerPh3Max(ph3);
            }
            if (gas.getPerPh3Min() > ph3) {
                gas.setPerPh3Min(ph3);
            }
            if (gas.getPerN2Max() < n2) {
                gas.setPerN2Max(n2);
            }
            if (gas.getPerN2Min() > n2) {
                gas.setPerN2Min(n2);
            }
 
            //固定为:passCode,co2,o2,ph3,n2;passCode,co2,o2,ph3,n2;"
            sb.append(gasResult.getPasscode());
            sb.append(",");
            sb.append(co2);
            sb.append(",");
            sb.append(o2);
            sb.append(",");
            sb.append(ph3);
            sb.append(",");
            sb.append(n2);
            sb.append(";");
 
            sumCo2 += co2;
            sumO2 += o2;
            sumPh3 += ph3;
            sumN2 += n2;
 
        }
        gas.setPoints(sb.toString());
        gas.setPerCo2(NumberUtil.keepPrecision(sumCo2 / sumNum, 2));
        gas.setPerO2(NumberUtil.keepPrecision(sumO2 / sumNum, 2));
        gas.setPerN2(NumberUtil.keepPrecision(sumN2 / sumNum, 2));
        gas.setPerPh3(NumberUtil.keepPrecision(sumPh3 / sumNum, 2));
 
        gasService.saveOrUpdateData(gas);
 
        // 调用通知前端
        notifyInvoker.notifyWeb(gas.getCompanyId(), OrderRespEnum.MSG_SUCCESS, BizType.GAS, request.getDepotId() + " 气体检测:结果返回成功.");
        log.info("控制柜----->>>平台:气体解析完成-仓库={}", request.getDepotId());
    }
}