提交 | 用户 | 时间
|
9d7e02
|
1 |
package com.iailab.module.data.channel.opcda.collector; |
潘 |
2 |
|
|
3 |
import com.iailab.module.data.channel.opcda.dto.WriteDTO; |
|
4 |
import com.iailab.module.data.channel.opcda.entity.ChannelOPCDADeviceEntity; |
|
5 |
import com.iailab.module.data.channel.opcda.entity.ChannelOPCDATagEntity; |
|
6 |
import lombok.extern.slf4j.Slf4j; |
|
7 |
import org.jinterop.dcom.common.JIException; |
|
8 |
import org.jinterop.dcom.core.JIVariant; |
|
9 |
import org.openscada.opc.lib.common.ConnectionInformation; |
|
10 |
import org.openscada.opc.lib.da.*; |
|
11 |
import org.springframework.stereotype.Component; |
|
12 |
|
|
13 |
import java.util.ArrayList; |
|
14 |
import java.util.List; |
|
15 |
import java.util.Map; |
|
16 |
import java.util.concurrent.Executors; |
|
17 |
import java.util.stream.Collectors; |
|
18 |
|
|
19 |
@Slf4j |
|
20 |
@Component |
|
21 |
public class OpcDAUtils { |
|
22 |
|
|
23 |
// 上次获取时间 |
|
24 |
private static long nextGetTime; |
|
25 |
// 获取间隔时间 |
|
26 |
private static long expGetTime = 1000 * 60; |
|
27 |
|
|
28 |
public synchronized Server createServer(ChannelOPCDADeviceEntity config) { |
|
29 |
if (System.currentTimeMillis() - nextGetTime < expGetTime) { |
|
30 |
throw new RuntimeException("获取OpcUaClient过快"); |
|
31 |
} |
|
32 |
nextGetTime = System.currentTimeMillis(); |
|
33 |
Server server; |
|
34 |
try { |
|
35 |
ConnectionInformation ci = new ConnectionInformation(); |
|
36 |
ci.setHost(config.getHost()); |
|
37 |
ci.setUser(config.getUser()); |
|
38 |
ci.setPassword(config.getPassword()); |
|
39 |
ci.setClsid(config.getClsId()); |
|
40 |
ci.setProgId(config.getProgId()); |
|
41 |
|
|
42 |
server = new Server(ci, Executors.newSingleThreadScheduledExecutor()); |
|
43 |
server.connect(); |
|
44 |
log.info("创建OpcUA客户端完成"); |
|
45 |
} catch (Exception e) { |
|
46 |
log.error("创建OpcUA客户端失败", e.getMessage()); |
|
47 |
throw new RuntimeException(e.getMessage()); |
|
48 |
} |
|
49 |
return server; |
|
50 |
} |
|
51 |
|
|
52 |
public static Map<Item, ItemState> readA(Group group, List<String[]> tags) throws AddFailedException, JIException { |
|
53 |
List<String> itemIds = tags.stream().map(t -> t[1]).collect(Collectors.toList()); |
|
54 |
Map<String, Item> stringItemMap = addItems(group, itemIds); |
|
55 |
List<Item> list = new ArrayList<>(stringItemMap.size()); |
|
56 |
for (Map.Entry<String, Item> entry : stringItemMap.entrySet()) { |
|
57 |
list.add(entry.getValue()); |
|
58 |
} |
|
59 |
Item[] items = list.toArray(new Item[stringItemMap.size()]); |
|
60 |
return group.read(true, items); |
|
61 |
} |
|
62 |
|
|
63 |
public static Map<Item, ItemState> read(Group group, List<ChannelOPCDATagEntity> tags) throws AddFailedException, JIException { |
|
64 |
List<String> itemIds = tags.stream().map(ChannelOPCDATagEntity::getItemId).collect(Collectors.toList()); |
|
65 |
Map<String, Item> stringItemMap = addItems(group, itemIds); |
|
66 |
List<Item> list = new ArrayList<>(stringItemMap.size()); |
|
67 |
for (Map.Entry<String, Item> entry : stringItemMap.entrySet()) { |
|
68 |
list.add(entry.getValue()); |
|
69 |
} |
|
70 |
Item[] items = list.toArray(new Item[stringItemMap.size()]); |
|
71 |
return group.read(true, items); |
|
72 |
} |
|
73 |
|
|
74 |
public static ItemState read(Item item) throws AddFailedException, JIException { |
|
75 |
return item.read(true); |
|
76 |
} |
|
77 |
|
|
78 |
public static Integer write(Item item,JIVariant value) throws AddFailedException, JIException { |
|
79 |
return item.write(value); |
|
80 |
} |
|
81 |
|
|
82 |
public static Map<Item, Integer> write(Group group, List<WriteDTO> writeDTOS) throws AddFailedException, JIException { |
|
83 |
WriteRequest[] writeRequests = new WriteRequest[writeDTOS.size()]; |
|
84 |
for (int i = 0; i < writeDTOS.size(); i++) { |
|
85 |
WriteDTO writeDTO = writeDTOS.get(i); |
|
86 |
WriteRequest writeRequest = new WriteRequest(writeDTO.getItem(),writeDTO.getValue()); |
|
87 |
writeRequests[i] = writeRequest; |
|
88 |
} |
|
89 |
return group.write(writeRequests); |
|
90 |
} |
|
91 |
|
|
92 |
public static Item addItem(Group group, String itemId) throws AddFailedException, JIException { |
|
93 |
List<String> itemIds = new ArrayList<>(1); |
|
94 |
itemIds.add(itemId); |
|
95 |
Map<String, Item> stringItemMap = addItems(group, itemIds); |
|
96 |
return stringItemMap.get(itemId); |
|
97 |
} |
|
98 |
|
|
99 |
public static Map<String, Item> addItems(Group group, List<String> itemIds) throws AddFailedException, JIException { |
|
100 |
String[] items = itemIds.toArray(new String[itemIds.size()]); |
|
101 |
return group.addItems(items); |
|
102 |
} |
|
103 |
|
|
104 |
public static Object getObjectValue(ItemState itemState) throws JIException { |
|
105 |
JIVariant value = itemState.getValue(); |
|
106 |
if (value.getType() == JIVariant.VT_UI2 || value.getType() == JIVariant.VT_UI4){ |
|
107 |
return value.getObjectAsUnsigned().getValue(); |
|
108 |
}else if (value.getType() == JIVariant.VT_I2){ |
|
109 |
return value.getObjectAsShort(); |
|
110 |
}else { |
|
111 |
return value.getObject(); |
|
112 |
} |
|
113 |
} |
|
114 |
|
|
115 |
} |