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
package com.ld.igds.protocol.iot.height.analysis;
 
import com.ld.igds.common.CoreDeviceIotService;
import com.ld.igds.common.CoreSerService;
import com.ld.igds.constant.BizType;
import com.ld.igds.constant.Constant;
import com.ld.igds.io.constant.OrderRespEnum;
import com.ld.igds.io.notify.NotifyWebInvoker;
import com.ld.igds.models.DeviceIot;
import com.ld.igds.models.DeviceSer;
import com.ld.igds.protocol.iot.height.analysis.builder.ReMessageBuilder;
import com.ld.igds.protocol.iot.height.analysis.message.DeviceAttr;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.List;
 
/**
 * 协议解析入口
 *
 * @author vince
 */
@Slf4j
@Component(AnalysisService.BEAN_ID)
public class AnalysisService {
 
    public static final String BEAN_ID = "iot.analysisService";
 
    @Autowired
    private CoreSerService coreSerService;
    @Autowired
    private CoreDeviceIotService coreDeviceIotService;
    @Autowired
    private NotifyWebInvoker notifyWebInvoker;
    @Autowired
    private AnalysisHeightService analysisHeightService;
 
    /**
     * @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("分机----->>>平台:返回信息没有获取组织编码信息,无法继续解析----{}", sessionKey);
            return;
        }
 
        DeviceSer ser = coreSerService.getCacheSerBySn(reMessage.getOrgId(),reMessage.getSn());
        if (null == ser) {
            String info = "分机=" + reMessage.getDeviceId() + "返回信息没有匹配到分机,忽略消息。";
            log.error("分机----->>>平台:" + info);
            notifyWebInvoker.notifyWeb(reMessage.getOrgId(), OrderRespEnum.MSG_ERROR, BizType.SYS, info);
            // 清除缓存并销毁链接
            return;
        }
        //更新分机状态
        ser.setIp(ip);
        ser.setPort(port);
        ser.setStatus(Constant.YN_Y);
        coreSerService.updateByData(ser);
        log.debug("分机----->>>平台:所属组织-{},分机-{} 返回的命令信息={}", ser.getCompanyId(), ser.getName(), reMessage.toString());
        List<DeviceIot> deviceIots = coreDeviceIotService.getCacheDeviceIotBySerId(ser.getCompanyId(),ser.getId());
        if(deviceIots == null || deviceIots.size() <= 0){
            log.error("分机----->>>平台:" + "没有获取到分机下面有设备!");
            return ;
        }
 
        DeviceIot deviceIot = deviceIots.stream().filter(item -> (item.getPassCode()+"").equals(reMessage.getDeviceId())).findAny().orElse(null);
        if(deviceIot == null ){
            log.error("分机----->>>平台:" + "没有获取到设备单个!");
            return ;
        }
 
        if(deviceIot.getType().equals(deviceIot.getType())){
            log.info("分机----->>>平台:"+ "开始液位计解析");
            analysisHeightService.analysis(reMessage, ser,deviceIot);
        }else {
            log.error("分机----->>>平台:" + "没有找到该设备类型的解析!");
        }
    }
 
}