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