提交 | 用户 | 时间
|
9d7e02
|
1 |
package com.iailab.module.data.channel.opcda.collector; |
潘 |
2 |
|
|
3 |
import com.iailab.module.data.channel.opcda.entity.ChannelOPCDADeviceEntity; |
|
4 |
import com.iailab.module.data.channel.opcda.service.ChannelOPCDADeviceService; |
|
5 |
import com.iailab.module.data.common.enums.CommonConstant; |
|
6 |
import com.iailab.module.data.common.enums.DataSourceType; |
|
7 |
import com.iailab.module.data.common.utils.TagUtils; |
|
8 |
import lombok.extern.slf4j.Slf4j; |
|
9 |
import org.jinterop.dcom.common.JIException; |
|
10 |
import org.jinterop.dcom.core.JIVariant; |
|
11 |
import org.openscada.opc.lib.da.Group; |
|
12 |
import org.openscada.opc.lib.da.Item; |
|
13 |
import org.openscada.opc.lib.da.ItemState; |
|
14 |
import org.openscada.opc.lib.da.Server; |
|
15 |
import org.springframework.beans.factory.annotation.Autowired; |
|
16 |
import org.springframework.stereotype.Component; |
|
17 |
import org.springframework.util.CollectionUtils; |
|
18 |
|
|
19 |
import java.util.HashMap; |
|
20 |
import java.util.List; |
|
21 |
import java.util.Map; |
|
22 |
import java.util.concurrent.ConcurrentHashMap; |
|
23 |
import java.util.stream.Collectors; |
|
24 |
|
|
25 |
@Slf4j |
|
26 |
@Component |
|
27 |
public class OpcDACollector { |
|
28 |
|
|
29 |
@Autowired |
|
30 |
private OpcDAUtils opcDAUtils; |
|
31 |
|
|
32 |
@Autowired |
|
33 |
private ChannelOPCDADeviceService channelOPCDADeviceService; |
|
34 |
|
|
35 |
private Map<String, Server> serverMap = new ConcurrentHashMap<>(); |
|
36 |
private Map<String, Group> groupMap = new ConcurrentHashMap<>(); |
|
37 |
|
|
38 |
private Map<String, ChannelOPCDADeviceEntity> deviceMap = new HashMap<>(); |
|
39 |
|
|
40 |
private Server getServer(String sourceId) throws Exception { |
|
41 |
try { |
|
42 |
if (!serverMap.containsKey(sourceId)) { |
|
43 |
log.info("根据数据源获取opcDAServer,sourceId=" + sourceId); |
|
44 |
ChannelOPCDADeviceEntity OPCDADevice = channelOPCDADeviceService.info(sourceId); |
|
45 |
deviceMap.put(sourceId, OPCDADevice); |
|
46 |
Server server = opcDAUtils.createServer(OPCDADevice); |
|
47 |
if (server != null) { |
|
48 |
serverMap.put(sourceId, server); |
|
49 |
} |
|
50 |
} |
|
51 |
} catch (Exception ex) { |
|
52 |
log.info("=========getOPCDAServer Exception============"); |
|
53 |
log.info("ex.message+" + ex.getMessage()); |
|
54 |
ex.printStackTrace(); |
|
55 |
throw new Exception(ex.getMessage()); |
|
56 |
} |
|
57 |
return serverMap.get(sourceId); |
|
58 |
} |
|
59 |
|
|
60 |
public Map<String, Object> getTagValues(List<String[]> tags) { |
|
61 |
if (CollectionUtils.isEmpty(tags)) { |
|
62 |
return new HashMap<>(); |
|
63 |
} |
|
64 |
Map<String, Object> result = new HashMap<>(tags.size()); |
|
65 |
// 按照sourceId分组 |
|
66 |
Map<String, List<String[]>> sourceIdTagMap = tags.stream().collect(Collectors.groupingBy(t -> t[0])); |
|
67 |
|
|
68 |
for (Map.Entry<String, List<String[]>> entry : sourceIdTagMap.entrySet()) { |
|
69 |
try { |
|
70 |
Server server = this.getServer(entry.getKey()); |
|
71 |
Group group = this.getGroup(server, entry.getKey()); |
|
72 |
Map<Item, ItemState> read = OpcDAUtils.readA(group, entry.getValue()); |
|
73 |
|
|
74 |
for (Map.Entry<Item, ItemState> itemStateEntry : read.entrySet()) { |
|
75 |
try { |
|
76 |
result.put(TagUtils.genTagId(DataSourceType.OPCDA.getCode(), deviceMap.get(entry.getKey()).getServerName(), itemStateEntry.getKey().getId()), OpcDAUtils.getObjectValue(itemStateEntry.getValue())); |
|
77 |
} catch (JIException e) { |
|
78 |
result.put(TagUtils.genTagId(DataSourceType.OPCDA.getCode(), deviceMap.get(entry.getKey()).getServerName(), itemStateEntry.getKey().getId()), CommonConstant.BAD_VALUE); |
|
79 |
} |
|
80 |
} |
|
81 |
} catch (Exception ex) { |
|
82 |
ex.printStackTrace(); |
|
83 |
for (String[] tag : entry.getValue()) { |
|
84 |
result.put(TagUtils.genTagId(DataSourceType.OPCDA.getCode(), deviceMap.get(entry.getKey()).getServerName(), tag[1]), CommonConstant.BAD_VALUE); |
|
85 |
} |
|
86 |
} |
|
87 |
} |
|
88 |
return result; |
|
89 |
} |
|
90 |
|
|
91 |
private Group getGroup(Server server, String sourceId) throws Exception { |
|
92 |
try { |
|
93 |
if (!groupMap.containsKey(sourceId)) { |
|
94 |
log.info("根据数据源获取opcDAGroup,sourceId=" + sourceId); |
|
95 |
Group group = server.addGroup(sourceId); |
|
96 |
if (group != null) { |
|
97 |
groupMap.put(sourceId, group); |
|
98 |
} |
|
99 |
} |
|
100 |
} catch (Exception ex) { |
|
101 |
log.info("=========getOPCDAGroup Exception============"); |
|
102 |
log.info("ex.message+" + ex.getMessage()); |
|
103 |
ex.printStackTrace(); |
|
104 |
throw new Exception(ex.getMessage()); |
|
105 |
} |
|
106 |
return groupMap.get(sourceId); |
|
107 |
} |
|
108 |
|
|
109 |
public void write(String serverId) throws Exception { |
|
110 |
Server server = this.getServer(serverId); |
|
111 |
Group group = server.addGroup(); |
|
112 |
Item item = group.addItem("通道 1.设备 1.item001"); |
|
113 |
OpcDAUtils.write(item, new JIVariant("999")); |
|
114 |
} |
|
115 |
} |