houzhongjian
2024-07-23 a6de490948278991e47952e90671ddba4555e9a2
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
package com.iailab.module.data.channel.opcua.collector;
 
import com.iailab.module.data.common.enums.CommonConstant;
import com.iailab.module.data.common.enums.DataSourceType;
import com.iailab.framework.common.util.object.ConvertUtils;
import com.iailab.module.data.common.utils.TagUtils;
import com.iailab.module.data.channel.opcua.entity.ChannelOPCUADeviceEntity;
import com.iailab.module.data.channel.opcua.service.ChannelOPCUADeviceService;
import com.iailab.module.data.channel.opcua.dto.ChannelOPCUADeviceDTO;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
 
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * @author PanZhibao
 * @Description
 * @createTime 2024年05月12日
 */
@Slf4j
@Component
public class OpcUaCollector {
 
    @Resource
    private OpcUaUtils opcUaUtils;
 
    @Resource
    private ChannelOPCUADeviceService channelOPCUADeviceService;
 
    private Map<String, OpcUaClient> clientMap = new ConcurrentHashMap<>();
 
    private Map<String, ChannelOPCUADeviceEntity> deviceMap = new HashMap<>();
 
 
    private OpcUaClient getClient(String sourceId) throws Exception {
        try {
            if (!clientMap.containsKey(sourceId)) {
                log.info("根据数据源获取Client,sourceId=" + sourceId);
                ChannelOPCUADeviceEntity opcuaDevice = channelOPCUADeviceService.info(sourceId);
                deviceMap.put(sourceId, opcuaDevice);
                ChannelOPCUADeviceDTO configDto = ConvertUtils.sourceToTarget(opcuaDevice, ChannelOPCUADeviceDTO.class);
                OpcUaClient opcUaClient = opcUaUtils.createClient(configDto);
                if (opcUaClient != null) {
                    clientMap.put(sourceId, opcUaClient);
                }
            }
        } catch (Exception ex) {
            log.info("=========getClient Exception============");
            log.info("ex.message+" + ex.getMessage());
            ex.printStackTrace();
            throw new Exception(ex.getMessage());
        }
        OpcUaClient opcUaClient = clientMap.get(sourceId);
        if (!opcUaClient.connect().isDone()) {
            opcUaClient.connect().get();
        }
        return opcUaClient;
    }
 
    public String getTagValue(String sourceId, String tagNo) throws Exception {
        OpcUaClient opcUaClient = this.getClient(sourceId);
        String value = OpcUaUtils.readNode(opcUaClient, tagNo);
        return value;
    }
 
    public void setTagData(String sourceId, String tagNo, String newValue, String dataType) {
        try {
            OpcUaClient opcUaClient = this.getClient(sourceId);
            switch (dataType) {
                case "float":
                    log.debug("Float,TagNo=" + tagNo + ",Value=" + Float.parseFloat(newValue));
                    OpcUaUtils.writeFloatValue(opcUaClient, tagNo, Float.parseFloat(newValue));
                    break;
                case "int":
                    log.debug("Int,TagNo=" + tagNo + ",Value=" + new BigDecimal(newValue).intValue());
                    OpcUaUtils.writeIntValue(opcUaClient, tagNo, new BigDecimal(newValue).intValue());
                    break;
                case "boolean":
                    log.debug("Boolean,TagNo=" + tagNo + ",Value=" + newValue);
                    OpcUaUtils.writeBooleanValue(opcUaClient, tagNo, Boolean.parseBoolean(newValue));
                    break;
                default:
                    log.warn("##################### No DataType ####################");
                    break;
            }
 
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
 
    public Map<String, Object> getTagValues(List<String[]> params) {
        if (CollectionUtils.isEmpty(params)) {
            return new HashMap<>();
        }
        Map<String, Object> result = new HashMap<>(params.size());
        params.forEach(item -> {
 
            try {
                OpcUaClient opcUaClient = this.getClient(item[0]);
                String value = OpcUaUtils.readNode(opcUaClient, item[1]);
                result.put(TagUtils.genTagId(DataSourceType.OPCUA.getCode(), deviceMap.get(item[0]).getServerName(), item[1]), value);
            } catch (Exception ex) {
                ex.printStackTrace();
                result.put(TagUtils.genTagId(DataSourceType.OPCUA.getCode(), deviceMap.get(item[0]).getServerName(), item[1]), CommonConstant.BAD_VALUE);
            }
        });
        return result;
    }
}