提交 | 用户 | 时间
|
a6de49
|
1 |
package com.iailab.module.data.channel.kio.collector; |
H |
2 |
|
aecc49
|
3 |
import com.iailab.module.data.channel.kio.entity.ChannelKioDeviceEntity; |
L |
4 |
import com.iailab.module.data.channel.kio.service.ChannelKioDeviceService; |
08b6a5
|
5 |
import com.iailab.module.data.channel.opcua.collector.OpcUaUtils; |
H |
6 |
import com.iailab.module.data.common.enums.CommonConstant; |
a6de49
|
7 |
import com.iailab.module.data.common.enums.DataSourceType; |
H |
8 |
import com.iailab.module.data.common.utils.TagUtils; |
|
9 |
import lombok.extern.slf4j.Slf4j; |
08b6a5
|
10 |
import org.eclipse.milo.opcua.sdk.client.OpcUaClient; |
a6de49
|
11 |
import org.springframework.stereotype.Component; |
H |
12 |
import org.springframework.util.CollectionUtils; |
|
13 |
|
aecc49
|
14 |
import javax.annotation.Resource; |
a6de49
|
15 |
import java.math.BigDecimal; |
H |
16 |
import java.util.ArrayList; |
|
17 |
import java.util.HashMap; |
|
18 |
import java.util.List; |
|
19 |
import java.util.Map; |
|
20 |
import java.util.concurrent.ConcurrentHashMap; |
|
21 |
import java.util.stream.Collectors; |
|
22 |
|
|
23 |
/** |
|
24 |
* @author PanZhibao |
|
25 |
* @Description |
|
26 |
* @createTime 2024年06月04日 |
|
27 |
*/ |
|
28 |
@Slf4j |
|
29 |
@Component |
|
30 |
public class KingIOCollector { |
|
31 |
|
|
32 |
@Resource |
|
33 |
private ChannelKioDeviceService channelKioDeviceService; |
|
34 |
|
|
35 |
private Map<String, KingIOClient> clientMap = new ConcurrentHashMap<>(); |
|
36 |
|
aecc49
|
37 |
private Map<String, ChannelKioDeviceEntity> deviceMap = new HashMap<>(); |
a6de49
|
38 |
|
H |
39 |
public synchronized KingIOClient getClient(String sourceId) throws Exception { |
|
40 |
if (!clientMap.containsKey(sourceId)) { |
aecc49
|
41 |
ChannelKioDeviceEntity deviceEntity = channelKioDeviceService.info(sourceId); |
L |
42 |
deviceMap.put(sourceId, deviceEntity); |
|
43 |
KingIOClient kingIOClient = new KingIOClient(deviceEntity.getInstanceName()); |
a6de49
|
44 |
clientMap.put(sourceId, kingIOClient); |
aecc49
|
45 |
if (!kingIOClient.login(deviceEntity.getAddress(), deviceEntity.getPort(), deviceEntity.getUsername(), deviceEntity.getPassword())) { |
a6de49
|
46 |
throw new Exception("登录异常"); |
H |
47 |
} |
|
48 |
} |
|
49 |
KingIOClient kingIOClient = clientMap.get(sourceId); |
|
50 |
if (!kingIOClient.isConnect()) { |
|
51 |
kingIOClient.reLogin(); |
|
52 |
} |
|
53 |
return kingIOClient; |
|
54 |
} |
|
55 |
|
08b6a5
|
56 |
public String getTagValue(String sourceId, String tagName) { |
H |
57 |
String value = CommonConstant.BAD_VALUE.toString(); |
|
58 |
try{ |
|
59 |
KingIOClient client = this.getClient(sourceId); |
|
60 |
value = client.getTagValue(tagName); |
|
61 |
}catch (Exception ex){ |
|
62 |
ex.printStackTrace(); |
|
63 |
return value; |
|
64 |
} |
a6de49
|
65 |
return value; |
H |
66 |
} |
|
67 |
|
|
68 |
public void setTagValue(String sourceId, String tagName, String newValue, String dataType) throws Exception { |
|
69 |
try { |
|
70 |
KingIOClient client = this.getClient(sourceId); |
|
71 |
switch (dataType) { |
|
72 |
case "float": |
|
73 |
log.debug("Float,tagName=" + tagName + ",Value=" + Float.parseFloat(newValue)); |
|
74 |
client.writeFloatValue(tagName, Float.parseFloat(newValue)); |
|
75 |
break; |
|
76 |
case "int": |
|
77 |
log.debug("Int,tagName=" + tagName + ",Value=" + new BigDecimal(newValue).intValue()); |
|
78 |
client.writeIntValue(tagName, new BigDecimal(newValue).intValue()); |
|
79 |
break; |
|
80 |
case "boolean": |
|
81 |
log.debug("Boolean,tagName=" + tagName + ",Value=" + newValue); |
|
82 |
client.writeBooleanValue(tagName, Boolean.parseBoolean(newValue)); |
|
83 |
break; |
|
84 |
default: |
|
85 |
log.warn("##################### No DataType ####################"); |
|
86 |
break; |
|
87 |
} |
|
88 |
} catch (Exception ex) { |
|
89 |
ex.printStackTrace(); |
|
90 |
throw ex; |
|
91 |
} |
|
92 |
} |
|
93 |
|
|
94 |
public Map<String, Object> getTagValues(List<String[]> params) { |
|
95 |
if (CollectionUtils.isEmpty(params)) { |
|
96 |
return new HashMap<>(); |
|
97 |
} |
|
98 |
Map<String, Object> result = new HashMap<>(params.size()); |
|
99 |
|
|
100 |
/*params.forEach(item -> { |
|
101 |
try { |
|
102 |
KingIOClient client = this.getClient(item[0]); |
|
103 |
String value = client.getTagValue(item[1]); |
|
104 |
result.put(TagUtils.genTagId(DataSourceType.KIO.getCode(), deviceMap.get(item[0]).getInstanceName(), item[1]), value); |
|
105 |
} catch (Exception ex) { |
|
106 |
ex.printStackTrace(); |
|
107 |
result.put(TagUtils.genTagId(DataSourceType.KIO.getCode(), deviceMap.get(item[0]).getInstanceName(), item[1]), CommonConstant.BAD_VALUE); |
|
108 |
} |
|
109 |
});*/ |
|
110 |
|
|
111 |
Map<String, List<String[]>> sourceGroup = new HashMap<>(); |
|
112 |
params.forEach(item -> { |
|
113 |
if (sourceGroup.containsKey(item[0])) { |
|
114 |
sourceGroup.get(item[0]).add(item); |
|
115 |
} else { |
|
116 |
List<String[]> list = new ArrayList<>(); |
|
117 |
list.add(item); |
|
118 |
sourceGroup.put(item[0], list); |
|
119 |
} |
|
120 |
}); |
|
121 |
sourceGroup.forEach((k, v) -> { |
|
122 |
try { |
|
123 |
KingIOClient client = this.getClient(k); |
|
124 |
List<String> tagNames = v.stream().map(t -> { |
|
125 |
return t[1]; |
|
126 |
}).collect(Collectors.toList()); |
|
127 |
Map<String, String> tagsValue = client.getTagsValue(tagNames); |
|
128 |
for (Map.Entry<String, String> tagValue : tagsValue.entrySet()) { |
|
129 |
result.put(TagUtils.genTagId(DataSourceType.KIO.getCode(), deviceMap.get(k).getInstanceName(), tagValue.getKey()), tagValue.getValue()); |
|
130 |
} |
|
131 |
} catch (Exception ex) { |
|
132 |
ex.printStackTrace(); |
|
133 |
} |
|
134 |
|
|
135 |
}); |
|
136 |
return result; |
|
137 |
} |
|
138 |
} |