Jay
2024-11-01 03e8aca3ad6201c0d74e00d4c8d7367cdaaa54f9
提交 | 用户 | 时间
a6de49 1 package com.iailab.module.data.channel.opcua.collector;
H 2
3 import com.iailab.module.data.common.enums.CommonConstant;
4 import com.iailab.module.data.common.enums.DataSourceType;
5 import com.iailab.framework.common.util.object.ConvertUtils;
6 import com.iailab.module.data.common.utils.TagUtils;
7 import com.iailab.module.data.channel.opcua.entity.ChannelOPCUADeviceEntity;
8 import com.iailab.module.data.channel.opcua.service.ChannelOPCUADeviceService;
9 import com.iailab.module.data.channel.opcua.dto.ChannelOPCUADeviceDTO;
10 import lombok.extern.slf4j.Slf4j;
11 import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
12 import javax.annotation.Resource;
13 import org.springframework.stereotype.Component;
14 import org.springframework.util.CollectionUtils;
15
16 import java.math.BigDecimal;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.concurrent.ConcurrentHashMap;
21
22 /**
23  * @author PanZhibao
24  * @Description
25  * @createTime 2024年05月12日
26  */
27 @Slf4j
28 @Component
29 public class OpcUaCollector {
30
31     @Resource
32     private OpcUaUtils opcUaUtils;
33
34     @Resource
35     private ChannelOPCUADeviceService channelOPCUADeviceService;
36
37     private Map<String, OpcUaClient> clientMap = new ConcurrentHashMap<>();
38
39     private Map<String, ChannelOPCUADeviceEntity> deviceMap = new HashMap<>();
40
41
42     private OpcUaClient getClient(String sourceId) throws Exception {
43         try {
44             if (!clientMap.containsKey(sourceId)) {
45                 log.info("根据数据源获取Client,sourceId=" + sourceId);
46                 ChannelOPCUADeviceEntity opcuaDevice = channelOPCUADeviceService.info(sourceId);
47                 deviceMap.put(sourceId, opcuaDevice);
48                 ChannelOPCUADeviceDTO configDto = ConvertUtils.sourceToTarget(opcuaDevice, ChannelOPCUADeviceDTO.class);
49                 OpcUaClient opcUaClient = opcUaUtils.createClient(configDto);
50                 if (opcUaClient != null) {
51                     clientMap.put(sourceId, opcUaClient);
52                 }
53             }
54         } catch (Exception ex) {
55             log.info("=========getClient Exception============");
56             log.info("ex.message+" + ex.getMessage());
57             ex.printStackTrace();
58             throw new Exception(ex.getMessage());
59         }
60         OpcUaClient opcUaClient = clientMap.get(sourceId);
61         if (!opcUaClient.connect().isDone()) {
62             opcUaClient.connect().get();
63         }
64         return opcUaClient;
65     }
66
67     public String getTagValue(String sourceId, String tagNo) throws Exception {
68         OpcUaClient opcUaClient = this.getClient(sourceId);
69         String value = OpcUaUtils.readNode(opcUaClient, tagNo);
70         return value;
71     }
72
73     public void setTagData(String sourceId, String tagNo, String newValue, String dataType) {
74         try {
75             OpcUaClient opcUaClient = this.getClient(sourceId);
76             switch (dataType) {
77                 case "float":
78                     log.debug("Float,TagNo=" + tagNo + ",Value=" + Float.parseFloat(newValue));
79                     OpcUaUtils.writeFloatValue(opcUaClient, tagNo, Float.parseFloat(newValue));
80                     break;
81                 case "int":
82                     log.debug("Int,TagNo=" + tagNo + ",Value=" + new BigDecimal(newValue).intValue());
83                     OpcUaUtils.writeIntValue(opcUaClient, tagNo, new BigDecimal(newValue).intValue());
84                     break;
85                 case "boolean":
86                     log.debug("Boolean,TagNo=" + tagNo + ",Value=" + newValue);
87                     OpcUaUtils.writeBooleanValue(opcUaClient, tagNo, Boolean.parseBoolean(newValue));
88                     break;
89                 default:
90                     log.warn("##################### No DataType ####################");
91                     break;
92             }
93
94         } catch (Exception ex) {
95             ex.printStackTrace();
96         }
97     }
98
99     public Map<String, Object> getTagValues(List<String[]> params) {
100         if (CollectionUtils.isEmpty(params)) {
101             return new HashMap<>();
102         }
103         Map<String, Object> result = new HashMap<>(params.size());
104         params.forEach(item -> {
105
106             try {
107                 OpcUaClient opcUaClient = this.getClient(item[0]);
108                 String value = OpcUaUtils.readNode(opcUaClient, item[1]);
109                 result.put(TagUtils.genTagId(DataSourceType.OPCUA.getCode(), deviceMap.get(item[0]).getServerName(), item[1]), value);
110             } catch (Exception ex) {
111                 ex.printStackTrace();
112                 result.put(TagUtils.genTagId(DataSourceType.OPCUA.getCode(), deviceMap.get(item[0]).getServerName(), item[1]), CommonConstant.BAD_VALUE);
113             }
114         });
115         return result;
116     }
117 }