package com.iailab.module.data.channel.opcda.collector; import com.iailab.module.data.channel.opcda.dto.WriteDTO; import com.iailab.module.data.channel.opcda.entity.ChannelOPCDADeviceEntity; import com.iailab.module.data.channel.opcda.entity.ChannelOPCDATagEntity; import lombok.extern.slf4j.Slf4j; import org.jinterop.dcom.common.JIException; import org.jinterop.dcom.core.JIVariant; import org.openscada.opc.lib.common.ConnectionInformation; import org.openscada.opc.lib.da.*; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.Executors; import java.util.stream.Collectors; @Slf4j @Component public class OpcDAUtils { // 上次获取时间 private static long nextGetTime; // 获取间隔时间 private static long expGetTime = 1000 * 60; public synchronized Server createServer(ChannelOPCDADeviceEntity config) { if (System.currentTimeMillis() - nextGetTime < expGetTime) { throw new RuntimeException("获取OpcUaClient过快"); } nextGetTime = System.currentTimeMillis(); Server server; try { ConnectionInformation ci = new ConnectionInformation(); ci.setHost(config.getHost()); ci.setUser(config.getUser()); ci.setPassword(config.getPassword()); ci.setClsid(config.getClsId()); ci.setProgId(config.getProgId()); server = new Server(ci, Executors.newSingleThreadScheduledExecutor()); server.connect(); log.info("创建OpcUA客户端完成"); } catch (Exception e) { log.error("创建OpcUA客户端失败", e.getMessage()); throw new RuntimeException(e.getMessage()); } return server; } public static Map readA(Group group, List tags) throws AddFailedException, JIException { List itemIds = tags.stream().map(t -> t[1]).collect(Collectors.toList()); Map stringItemMap = addItems(group, itemIds); List list = new ArrayList<>(stringItemMap.size()); for (Map.Entry entry : stringItemMap.entrySet()) { list.add(entry.getValue()); } Item[] items = list.toArray(new Item[stringItemMap.size()]); return group.read(true, items); } public static Map read(Group group, List tags) throws AddFailedException, JIException { List itemIds = tags.stream().map(ChannelOPCDATagEntity::getItemId).collect(Collectors.toList()); Map stringItemMap = addItems(group, itemIds); List list = new ArrayList<>(stringItemMap.size()); for (Map.Entry entry : stringItemMap.entrySet()) { list.add(entry.getValue()); } Item[] items = list.toArray(new Item[stringItemMap.size()]); return group.read(true, items); } public static ItemState read(Item item) throws AddFailedException, JIException { return item.read(true); } public static Integer write(Item item,JIVariant value) throws AddFailedException, JIException { return item.write(value); } public static Map write(Group group, List writeDTOS) throws AddFailedException, JIException { WriteRequest[] writeRequests = new WriteRequest[writeDTOS.size()]; for (int i = 0; i < writeDTOS.size(); i++) { WriteDTO writeDTO = writeDTOS.get(i); WriteRequest writeRequest = new WriteRequest(writeDTO.getItem(),writeDTO.getValue()); writeRequests[i] = writeRequest; } return group.write(writeRequests); } public static Item addItem(Group group, String itemId) throws AddFailedException, JIException { List itemIds = new ArrayList<>(1); itemIds.add(itemId); Map stringItemMap = addItems(group, itemIds); return stringItemMap.get(itemId); } public static Map addItems(Group group, List itemIds) throws AddFailedException, JIException { String[] items = itemIds.toArray(new String[itemIds.size()]); return group.addItems(items); } public static Object getObjectValue(ItemState itemState) throws JIException { JIVariant value = itemState.getValue(); if (value.getType() == JIVariant.VT_UI2 || value.getType() == JIVariant.VT_UI4){ return value.getObjectAsUnsigned().getValue(); }else if (value.getType() == JIVariant.VT_I2){ return value.getObjectAsShort(); }else { return value.getObject(); } } }