sgj
2026-03-06 bd3c4b90882fc29711a5e73f4929db2da3e22cb6
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.fzzy.igds.iot.analysis;
 
import com.fzzy.igds.constant.Constant;
import com.fzzy.igds.constant.OrderRespEnum;
import com.fzzy.igds.constant.RedisConst;
import com.fzzy.igds.data.GrainData;
import com.fzzy.igds.domain.*;
import com.fzzy.igds.io.notify.NotifyGrainInvoker;
import com.fzzy.igds.iot.analysis.builder.ReMessageBuilder;
import com.fzzy.igds.iot.analysis.message.DeviceAttr;
import com.fzzy.igds.iot.analysis.message.DeviceAttrInfo;
import com.fzzy.igds.io.notify.NotifyWebInvoker;
import com.fzzy.igds.service.*;
import com.fzzy.igds.utils.ContextUtil;
import com.ruoyi.common.core.redis.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
 
/**
 * @Description 解析入口
 * @Author CZT
 * @Date 2025/11/11 18:20
 */
@Slf4j
@Component(AnalysisService.BEAN_ID)
public class AnalysisService {
 
    public static final String BEAN_ID = "iot.analysisService";
 
    @Resource
    private DeviceSerService deviceSerService;
    @Resource
    private DeviceIotService deviceIotService;
    @Autowired
    private DepotService depotService;
    @Autowired
    private NotifyWebInvoker notifyWebInvoker;
    @Autowired
    private DepotConfService depotConfService;
    @Autowired
    private NotifyGrainInvoker notifyGrainInvoker;
    @Resource
    private QuantityService quantityService;
 
    @Resource
    private RedisCache redisCache;
 
    /**
     * @param sessionKey ip:port
     * @param msg
     */
    public void analysis(String sessionKey, String msg) throws Exception {
        String[] attr = sessionKey.split(":");
        String ip = attr[0];
        Integer port = Integer.valueOf(attr[1]);
 
        DeviceAttr reMessage = ReMessageBuilder.getInstance().buildMessage( ip, port, msg);
 
        if (null == reMessage || null == reMessage.getOrgId()) {
            log.error("IOT网关----->>>平台:返回信息没有获取组织编码信息,无法继续解析----{}", sessionKey);
            return;
        }
 
        DeviceSer ser = deviceSerService.getCacheSerBySn(reMessage.getOrgId(),reMessage.getSn());
        if (null == ser) {
            String info = "IOT网关=" + reMessage.getDeviceId() + "返回信息没有匹配到分机,忽略消息。";
            log.error("IOT网关----->>>平台:" + info);
            notifyWebInvoker.notifyWeb(reMessage.getOrgId(), OrderRespEnum.MSG_ERROR,"SYS", info);
            // 清除缓存并销毁链接
            return;
        }
 
        //更新分机状态
        ser.setIp(ip);
        ser.setPort(port);
        ser.setStatus(Constant.YN_Y);
        deviceSerService.updateByData(ser);
        log.debug("IOT网关----->>>平台:所属组织-{},分机-{} 返回的命令信息={}", ser.getCompanyId(), ser.getName(), reMessage.toString());
        List<DeviceIot> deviceIots = deviceIotService.getCacheDeviceIotBySerId(ser.getCompanyId(),ser.getId());
        if(deviceIots == null || deviceIots.size() <= 0){
            log.error("IOT网关----->>>平台:" + "没有获取到分机下面有设备!");
            return ;
        }
 
        DeviceIot deviceIot = deviceIots.stream().filter(item -> (item.getPassCode()+"").equals(reMessage.getDeviceId())).findAny().orElse(null);
        if(deviceIot == null ){
            log.error("IOT网关----->>>平台:" + "没有获取到设备单个!");
            return ;
        }
 
        log.info("IOT网关----->>>平台:"+ "开始解析数据" + reMessage.toString());
        if(StringUtils.isEmpty(reMessage.getType())){
            //液位解析
            analysisHeight(reMessage, ser,deviceIot);
        }
        if("2".equals(reMessage.getType()) || "02".equals(reMessage.getType())){
            //油温解析
            analysisOilGrain(reMessage, ser,deviceIot);
        }else {
            //液位解析
            analysisHeight(reMessage, ser,deviceIot);
        }
    }
 
    /**
     * 网管油情解析
     * @param deviceAttr
     * @param ser
     */
    public void analysisOilGrain(DeviceAttr deviceAttr, DeviceSer ser, DeviceIot deviceIot) {
        try {
            log.info("IOT网关----->>>平台:"+ "开始解析温度数据" + deviceAttr.toString());
            List<DeviceAttrInfo> list = deviceAttr.getTerminalAttrInfoList();
            if(null ==  list || list.isEmpty()){
                log.error("IOT网关----->>>平台:" + "油情数据为空,不解析!");
                return;
            }
 
            Integer ceng = 0;
            String points = "";
            Double max = null, min = null, sumT = 0.0;
            Double temp = 0.0;
            for (DeviceAttrInfo iotInfo : list) {
                if("1".equals(iotInfo.getPasscode()) || "2".equals(iotInfo.getPasscode())){
                    //通道号为1或者2时,是通信状态和告警状态,不解析
                    continue;
                }
                points += iotInfo.getValue() + ",";
                ceng ++;
 
                temp = Double.valueOf(iotInfo.getValue());
                if(null == max){
                    max = temp;
                }
                if(null == min){
                    min = temp;
                }
                if (temp > max) {
                    max = temp;
                }
                if (temp < min) {
                    min = temp;
                }
                sumT += temp;
            }
 
            DepotConf depotConf = depotConfService.getCacheDepotConfByDepotId(ser.getCompanyId(), deviceIot.getDepotId());
            Depot depot = depotService.getCacheDepot(ser.getCompanyId(), deviceIot.getDepotId());
            Grain grain = new Grain();
            grain.setBatchId(ContextUtil.getBatchIdByFireq(depotConf.getGrainFreq()));
            grain.setReceiveDate(new Date());
            grain.setCable("1");
            grain.setCableCir(ceng + "");
            grain.setPoints(points);
            grain.setCheckUser("系统");
            grain.setDepotId(deviceIot.getDepotId());
            grain.setCompanyId(depotConf.getCompanyId());
 
            grain.setHumidityIn(-100.0);
            grain.setTempIn(-100.0);
 
            grain.setTempAve(sumT/ceng);
            grain.setTempMax(max);
            grain.setTempMin(min);
            grain.setDeptId(depot.getDeptId());
 
            //油罐 液位高度,有就赋值
            GrainData data = (GrainData) redisCache.getCacheObject(RedisConst.buildKey(ser.getCompanyId(),RedisConst.KEY_DEPOT_HEIGHT,grain.getDepotId()));
            if(data!= null && StringUtils.isNotEmpty(data.getOilHeight())){
                grain.setOilHeight(data.getOilHeight());
            }
 
            // 用户封装好数据即可
            notifyGrainInvoker.analysisSuccess(grain, depot);
            log.info("IOT网关----->>>平台:油温解析完成:" + data);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
 
    /**
     * 网管液位高度解析
     * @param deviceAttr
     * @param ser
     */
    public void analysisHeight(DeviceAttr deviceAttr, DeviceSer ser, DeviceIot deviceIot) {
        try {
            Depot depot = depotService.getCacheDepot(ser.getCompanyId(), deviceIot.getDepotId());
            if(null == depot){
                log.error("IOT网关----->>>平台:" + "未获取仓库信息,不解析!");
                return;
            }
 
            List<DeviceAttrInfo> list = deviceAttr.getTerminalAttrInfoList();
            if(null ==  list || list.isEmpty()){
                log.error("IOT网关----->>>平台:" + "油情数据为空,不解析!");
                return;
            }
 
            Double diameter = 0.0;   //直径
            Double volume = 0.0;     //体积
            Double bulkWeight = 0.0; //容重
            Double realHeight = 0.0; //页面高度
            Double maxHeight = 0.0;  //最大高度
            Double storage = 0.0;    //计算储量
            Double heightPer = 0.0;    //高度百分比
 
            if(null != depot.getHeight()){
                maxHeight = depot.getHeight();
            }
            if(null != depot.getLength()){
                diameter = depot.getLength();
            }
            if(null != depot.getBulkWeight()){
                bulkWeight = depot.getBulkWeight();
            }
            //解析真实高度
            realHeight = Double.valueOf(list.get(2).getValue());
 
            //计算体积
            volume = 3.14159 * Math.pow(diameter / 2, 2) * realHeight;
            //判断体积是否为负,为负值则不计算,重置为0
            if (volume < 0) {
                volume = 0.0;
            }
 
            //计算重量
            storage = volume * bulkWeight;
            storage = Double.valueOf(String.format("%.2f", storage));
 
            //高度百分比
            if(maxHeight > 0){
                heightPer = Double.valueOf(String.format("%.2f", realHeight/maxHeight * 100));
            }
 
            Quantity data = new Quantity();
 
            data.setBatchId(depot.getId() +"_"+ ContextUtil.generateId());
            data.setCompanyId(ser.getCompanyId());
            data.setDeptId(depot.getDeptId());
            data.setDepotId(deviceIot.getDepotId());
 
            data.setDiameter(depot.getLength());
            data.setHeightPer(heightPer);
            data.setHeight(realHeight);
            data.setBulk(volume);
            data.setWeight(storage);
 
            data.setReceiveDate(new Date());
            data.setCheckUser("系统");
 
            quantityService.saveData( data);
 
            log.info("高度解析完成:" + data);
 
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}