提交 | 用户 | 时间
|
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 |
|
08b6a5
|
67 |
public String getTagValue(String sourceId, String tagNo) { |
H |
68 |
String value = CommonConstant.BAD_VALUE.toString(); |
|
69 |
try{ |
|
70 |
OpcUaClient opcUaClient = this.getClient(sourceId); |
|
71 |
value = OpcUaUtils.readNode(opcUaClient, tagNo); |
|
72 |
}catch (Exception ex){ |
|
73 |
ex.printStackTrace(); |
|
74 |
return value; |
|
75 |
} |
a6de49
|
76 |
return value; |
H |
77 |
} |
|
78 |
|
|
79 |
public void setTagData(String sourceId, String tagNo, String newValue, String dataType) { |
|
80 |
try { |
|
81 |
OpcUaClient opcUaClient = this.getClient(sourceId); |
|
82 |
switch (dataType) { |
|
83 |
case "float": |
|
84 |
log.debug("Float,TagNo=" + tagNo + ",Value=" + Float.parseFloat(newValue)); |
|
85 |
OpcUaUtils.writeFloatValue(opcUaClient, tagNo, Float.parseFloat(newValue)); |
|
86 |
break; |
|
87 |
case "int": |
|
88 |
log.debug("Int,TagNo=" + tagNo + ",Value=" + new BigDecimal(newValue).intValue()); |
|
89 |
OpcUaUtils.writeIntValue(opcUaClient, tagNo, new BigDecimal(newValue).intValue()); |
|
90 |
break; |
|
91 |
case "boolean": |
|
92 |
log.debug("Boolean,TagNo=" + tagNo + ",Value=" + newValue); |
|
93 |
OpcUaUtils.writeBooleanValue(opcUaClient, tagNo, Boolean.parseBoolean(newValue)); |
|
94 |
break; |
|
95 |
default: |
|
96 |
log.warn("##################### No DataType ####################"); |
|
97 |
break; |
|
98 |
} |
|
99 |
|
|
100 |
} catch (Exception ex) { |
|
101 |
ex.printStackTrace(); |
|
102 |
} |
|
103 |
} |
|
104 |
|
|
105 |
public Map<String, Object> getTagValues(List<String[]> params) { |
|
106 |
if (CollectionUtils.isEmpty(params)) { |
|
107 |
return new HashMap<>(); |
|
108 |
} |
|
109 |
Map<String, Object> result = new HashMap<>(params.size()); |
|
110 |
params.forEach(item -> { |
|
111 |
|
|
112 |
try { |
|
113 |
OpcUaClient opcUaClient = this.getClient(item[0]); |
|
114 |
String value = OpcUaUtils.readNode(opcUaClient, item[1]); |
|
115 |
result.put(TagUtils.genTagId(DataSourceType.OPCUA.getCode(), deviceMap.get(item[0]).getServerName(), item[1]), value); |
|
116 |
} catch (Exception ex) { |
|
117 |
ex.printStackTrace(); |
|
118 |
result.put(TagUtils.genTagId(DataSourceType.OPCUA.getCode(), deviceMap.get(item[0]).getServerName(), item[1]), CommonConstant.BAD_VALUE); |
|
119 |
} |
|
120 |
}); |
|
121 |
return result; |
|
122 |
} |
|
123 |
} |