|
|
@ -1,118 +1,119 @@ |
|
|
|
package org.nl.acs.opc; |
|
|
|
|
|
|
|
import java.net.UnknownHostException; |
|
|
|
import cn.hutool.core.util.NumberUtil; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.jinterop.dcom.common.JIException; |
|
|
|
import org.jinterop.dcom.core.*; |
|
|
|
import org.nl.acs.device_driver.driver.ItemValue; |
|
|
|
import org.nl.modules.common.exception.BadRequestException; |
|
|
|
import org.nl.modules.wql.exception.WDKException; |
|
|
|
import org.openscada.opc.lib.common.ConnectionInformation; |
|
|
|
import org.openscada.opc.lib.da.*; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Collection; |
|
|
|
import java.util.Iterator; |
|
|
|
import java.util.LinkedHashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.concurrent.Executors; |
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.jinterop.dcom.common.JIException; |
|
|
|
import org.jinterop.dcom.core.IJIUnsigned; |
|
|
|
import org.jinterop.dcom.core.JIArray; |
|
|
|
import org.jinterop.dcom.core.JIString; |
|
|
|
import org.jinterop.dcom.core.JIUnsignedByte; |
|
|
|
import org.jinterop.dcom.core.JIUnsignedShort; |
|
|
|
import org.jinterop.dcom.core.JIVariant; |
|
|
|
import org.nl.modules.wql.exception.WDKException; |
|
|
|
import org.openscada.opc.dcom.list.ClassDetails; |
|
|
|
import org.openscada.opc.lib.common.AlreadyConnectedException; |
|
|
|
import org.openscada.opc.lib.common.ConnectionInformation; |
|
|
|
import org.openscada.opc.lib.da.AddFailedException; |
|
|
|
import org.openscada.opc.lib.da.AutoReconnectController; |
|
|
|
import org.openscada.opc.lib.da.Group; |
|
|
|
import org.openscada.opc.lib.da.Item; |
|
|
|
import org.openscada.opc.lib.da.ItemState; |
|
|
|
import org.openscada.opc.lib.da.Server; |
|
|
|
import org.openscada.opc.lib.da.WriteRequest; |
|
|
|
import org.openscada.opc.lib.list.Categories; |
|
|
|
import org.openscada.opc.lib.list.Category; |
|
|
|
import org.openscada.opc.lib.list.ServerList; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
@Slf4j |
|
|
|
public class OpcUtl { |
|
|
|
private static int timeout = 180000; |
|
|
|
private static int timeout = 1*60*1000; |
|
|
|
private static String key = "rpc.socketTimeout"; |
|
|
|
|
|
|
|
static { |
|
|
|
checkTimeout(); |
|
|
|
} |
|
|
|
|
|
|
|
public static void checkTimeout() { |
|
|
|
if (Integer.getInteger(key, 0) != timeout) { |
|
|
|
if (Integer.getInteger(key, 0).intValue() != timeout) { |
|
|
|
System.setProperty(key, String.valueOf(timeout)); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
private OpcUtl() { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static List<Map<String, String>> showAllOpcServer(String host, String user, String password, String domain) throws Exception { |
|
|
|
public static void writeValue(Group group, WriteRequest... requests) throws WDKException { |
|
|
|
try { |
|
|
|
List<Map<String, String>> listResult = new ArrayList(); |
|
|
|
ServerList serverList = new ServerList(host, user, password, domain); |
|
|
|
Collection<ClassDetails> classDetails = serverList.listServersWithDetails(new Category[]{Categories.OPCDAServer10, Categories.OPCDAServer20, Categories.OPCDAServer30}, new Category[0]); |
|
|
|
Iterator var7 = classDetails.iterator(); |
|
|
|
|
|
|
|
while(var7.hasNext()) { |
|
|
|
ClassDetails details = (ClassDetails)var7.next(); |
|
|
|
Map<String, String> result = new LinkedHashMap(); |
|
|
|
result.put("classId", details.getClsId()); |
|
|
|
result.put("progId", details.getProgId()); |
|
|
|
result.put("description", details.getDescription()); |
|
|
|
listResult.add(result); |
|
|
|
Map<Item, Integer> e=group.write(requests); |
|
|
|
boolean is_success = true; |
|
|
|
StringBuilder message = new StringBuilder(); |
|
|
|
Iterator arg4 = e.keySet().iterator(); |
|
|
|
|
|
|
|
while (arg4.hasNext()) { |
|
|
|
Item item = (Item) arg4.next(); |
|
|
|
Integer integer = (Integer) e.get(item); |
|
|
|
if (integer.intValue() != 0) { |
|
|
|
if (is_success) { |
|
|
|
is_success = false; |
|
|
|
} |
|
|
|
|
|
|
|
message.append(item.getId()); |
|
|
|
message.append(":error code:"); |
|
|
|
message.append(integer); |
|
|
|
message.append("; "); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return listResult; |
|
|
|
} catch (UnknownHostException | JIException | IllegalArgumentException var10) { |
|
|
|
throw new Exception(var10); |
|
|
|
if (!is_success) { |
|
|
|
log.info("下发信号失败:"+message.toString()); |
|
|
|
throw new WDKException(message.toString()); |
|
|
|
} |
|
|
|
} catch (JIException arg7) { |
|
|
|
log.info("下发信号失败Exception:"+arg7.getMessage()); |
|
|
|
throw new WDKException("下发信号失败Exception:"+arg7); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static Server getServer(String host, String clsid, String user, String password, String domain) |
|
|
|
throws WDKException { |
|
|
|
checkTimeout(); |
|
|
|
Server server = null; |
|
|
|
|
|
|
|
public static void writeValue(Group group, ItemValue... values) throws WDKException { |
|
|
|
try { |
|
|
|
server = new Server(getConnection(host, clsid, user, password, domain), |
|
|
|
Executors.newSingleThreadScheduledExecutor()); |
|
|
|
server.connect(); |
|
|
|
return server; |
|
|
|
} catch (Exception e) { |
|
|
|
System.out.println("server error:"+e.getMessage()); |
|
|
|
throw new WDKException(e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
if (values != null && values.length > 0) { |
|
|
|
List<WriteRequest> ws = new ArrayList(); |
|
|
|
ItemValue[] var3 = values; |
|
|
|
int var4 = values.length; |
|
|
|
|
|
|
|
for(int var5 = 0; var5 < var4; ++var5) { |
|
|
|
ItemValue value = var3[var5]; |
|
|
|
Item item = group.addItem(value.getItem_code()); |
|
|
|
ws.add(new WriteRequest(item, getVariant(value.getItem_value()))); |
|
|
|
} |
|
|
|
writeValue(group, (WriteRequest[])ws.toArray(new WriteRequest[0])); |
|
|
|
} |
|
|
|
|
|
|
|
public static Server getAutoServer(String host, String clsid, String user, String password, String domain) throws Exception { |
|
|
|
checkTimeout(); |
|
|
|
Server server = null; |
|
|
|
server = new Server(getConnection(host, clsid, user, password, domain), Executors.newSingleThreadScheduledExecutor()); |
|
|
|
AutoReconnectController autoReconnectController = new AutoReconnectController(server); |
|
|
|
autoReconnectController.connect(); |
|
|
|
return server; |
|
|
|
} catch (AddFailedException | JIException var8) { |
|
|
|
throw new WDKException(var8); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static ConnectionInformation getConnection(String host, String clsid, String user, String password, String domain) { |
|
|
|
ConnectionInformation connection = new ConnectionInformation(); |
|
|
|
connection.setHost(host); |
|
|
|
connection.setClsid(clsid); |
|
|
|
connection.setUser(user); |
|
|
|
connection.setPassword(password); |
|
|
|
connection.setDomain(domain); |
|
|
|
return connection; |
|
|
|
public static JIVariant getVariant(Object object) { |
|
|
|
if (object instanceof Integer) { |
|
|
|
return getIntegerVariant((Integer)object); |
|
|
|
} else if (object instanceof String) { |
|
|
|
return getStringVariant((String)object); |
|
|
|
} else if (object instanceof byte[]) { |
|
|
|
return getByteArrayVariant((byte[])((byte[])object)); |
|
|
|
} else if (object instanceof Byte[]) { |
|
|
|
return getByteArrayVariant((Byte[])((Byte[])object)); |
|
|
|
} else if (object instanceof Boolean) { |
|
|
|
return new JIVariant((Boolean)object); |
|
|
|
} else if (object instanceof int[]) { |
|
|
|
return getByteArrayVariantxx((int[])((int[])object)); |
|
|
|
} else if (object instanceof Integer[]) { |
|
|
|
JIArray array = new JIArray((Integer)object, false); |
|
|
|
JIVariant value = new JIVariant(array); |
|
|
|
return value; |
|
|
|
} else if (object instanceof JIVariant) { |
|
|
|
return (JIVariant)object; |
|
|
|
} else { |
|
|
|
throw new WDKException("未实现目前支持是int,string,byte[]"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static Object getValue(Item item, ItemState itemState) throws Exception { |
|
|
|
if (!ObjectUtl.isEquals(itemState.getQuality(), QualityTypeValue.OPC_QUALITY_GOOD)) { |
|
|
|
public static Object getValue(Item item, ItemState itemState) throws BadRequestException { |
|
|
|
if (NumberUtil.compare(itemState.getQuality(), Short.valueOf(QualityTypeValue.OPC_QUALITY_GOOD)) != 0) { |
|
|
|
if (item != null) { |
|
|
|
log.debug("value is not good {} : {}", item.getId(), itemState.getQuality()); |
|
|
|
// throw new WDKException("值不健康进行重连!");
|
|
|
|
} else { |
|
|
|
log.debug("value is not good {}", itemState.getQuality()); |
|
|
|
} |
|
|
@ -124,36 +125,24 @@ public class OpcUtl { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static int getIntegerValue(Item item, ItemState itemState) throws Exception { |
|
|
|
return (Integer)getValue(item, itemState); |
|
|
|
} |
|
|
|
|
|
|
|
public static String getStringValue(Item item, ItemState itemState) throws Exception { |
|
|
|
return (String)getValue(item, itemState); |
|
|
|
} |
|
|
|
|
|
|
|
public static byte[] getByteArrayValue(Item item, ItemState itemState) throws Exception { |
|
|
|
return (byte[])((byte[])getValue(item, itemState)); |
|
|
|
} |
|
|
|
|
|
|
|
public static Object getValue(JIVariant jiVariant, String id) { |
|
|
|
public static Object getValue(JIVariant jiVariant, String id) throws BadRequestException { |
|
|
|
try { |
|
|
|
Object object = jiVariant.getObject(); |
|
|
|
if (object instanceof IJIUnsigned) { |
|
|
|
return ((IJIUnsigned)object).getValue().intValue(); |
|
|
|
} else if (object instanceof Boolean) { |
|
|
|
return jiVariant.getObjectAsBoolean() ? 1 : 0; |
|
|
|
} else if (object instanceof JIString) { |
|
|
|
return ((JIString)object).getString(); |
|
|
|
} else if (!(object instanceof JIArray)) { |
|
|
|
if (object instanceof Integer) { |
|
|
|
Object e = jiVariant.getObject(); |
|
|
|
if (e instanceof IJIUnsigned) { |
|
|
|
return Integer.valueOf(((IJIUnsigned) e).getValue().intValue()); |
|
|
|
} else if (e instanceof Boolean) { |
|
|
|
return jiVariant.getObjectAsBoolean() ? Integer.valueOf(1) : Integer.valueOf(0); |
|
|
|
} else if (e instanceof JIString) { |
|
|
|
return ((JIString) e).getString(); |
|
|
|
} else if (!(e instanceof JIArray)) { |
|
|
|
if (e instanceof Integer) { |
|
|
|
return jiVariant.getObject(); |
|
|
|
} else if (object instanceof Short) { |
|
|
|
} else if (e instanceof Short) { |
|
|
|
return jiVariant.getObject(); |
|
|
|
} else if (object instanceof Float) { |
|
|
|
} else if (e instanceof Float) { |
|
|
|
return jiVariant.getObject(); |
|
|
|
} else { |
|
|
|
System.err.println(id + "不明类型" + object.getClass()); |
|
|
|
System.err.println(id + "不明类型" + e.getClass()); |
|
|
|
if (jiVariant.getType() == 0) { |
|
|
|
System.err.println("因类型为emtpy 返回 null"); |
|
|
|
return null; |
|
|
@ -165,15 +154,15 @@ public class OpcUtl { |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
Class<?> clazz = ((JIArray)object).getArrayClass(); |
|
|
|
Class clazz = ((JIArray) e).getArrayClass(); |
|
|
|
int[] r; |
|
|
|
int i; |
|
|
|
if (JIUnsignedByte.class.isAssignableFrom(clazz)) { |
|
|
|
JIUnsignedByte[] array = (JIUnsignedByte[])((JIUnsignedByte[])((JIArray)object).getArrayInstance()); |
|
|
|
r = new int[array.length]; |
|
|
|
JIUnsignedByte[] arg7 = (JIUnsignedByte[]) ((JIUnsignedByte[]) ((JIArray) e).getArrayInstance()); |
|
|
|
r = new int[arg7.length]; |
|
|
|
|
|
|
|
for(i = 0; i < array.length; ++i) { |
|
|
|
r[i] = array[i].getValue().byteValue(); |
|
|
|
for (i = 0; i < arg7.length; ++i) { |
|
|
|
r[i] = arg7[i].getValue().byteValue(); |
|
|
|
} |
|
|
|
|
|
|
|
return r; |
|
|
@ -186,99 +175,60 @@ public class OpcUtl { |
|
|
|
System.err.println("因类型为null 返回 null"); |
|
|
|
return null; |
|
|
|
} else { |
|
|
|
return ((JIArray)object).getArrayInstance(); |
|
|
|
return ((JIArray) e).getArrayInstance(); |
|
|
|
} |
|
|
|
} else { |
|
|
|
JIUnsignedShort[] array = (JIUnsignedShort[])((JIUnsignedShort[])((JIArray)object).getArrayInstance()); |
|
|
|
JIUnsignedShort[] array = (JIUnsignedShort[]) ((JIUnsignedShort[]) ((JIArray) e) |
|
|
|
.getArrayInstance()); |
|
|
|
r = new int[array.length]; |
|
|
|
|
|
|
|
for(i = 0; i < array.length; ++i) { |
|
|
|
for (i = 0; i < array.length; ++i) { |
|
|
|
r[i] = array[i].getValue().intValue(); |
|
|
|
} |
|
|
|
|
|
|
|
return r; |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (JIException var7) { |
|
|
|
// throw new Exception(var7);
|
|
|
|
var7.printStackTrace(); |
|
|
|
} catch (JIException e) { |
|
|
|
throw new BadRequestException(e.getMessage()); |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
public static void writeValue(Group group, WriteRequest... requests) throws Exception { |
|
|
|
try { |
|
|
|
Map<Item, Integer> write = group.write(requests); |
|
|
|
boolean is_success = true; |
|
|
|
StringBuilder message = new StringBuilder(); |
|
|
|
Iterator var5 = write.keySet().iterator(); |
|
|
|
|
|
|
|
while(var5.hasNext()) { |
|
|
|
Item item = (Item)var5.next(); |
|
|
|
Integer integer = (Integer)write.get(item); |
|
|
|
if (integer != 0) { |
|
|
|
if (is_success) { |
|
|
|
is_success = false; |
|
|
|
} |
|
|
|
|
|
|
|
message.append(item.getId()); |
|
|
|
message.append(":error code:"); |
|
|
|
message.append(integer); |
|
|
|
message.append("; "); |
|
|
|
} |
|
|
|
} |
|
|
|
public static Server getServer(String host, String clsid, String user, String password, String domain) |
|
|
|
throws WDKException { |
|
|
|
checkTimeout(); |
|
|
|
Server server = null; |
|
|
|
|
|
|
|
if (!is_success) { |
|
|
|
throw new Exception(message.toString()); |
|
|
|
} |
|
|
|
} catch (JIException var8) { |
|
|
|
throw new Exception(var8); |
|
|
|
try { |
|
|
|
server = new Server(getConnection(host, clsid, user, password, domain), |
|
|
|
Executors.newSingleThreadScheduledExecutor()); |
|
|
|
server.connect(); |
|
|
|
return server; |
|
|
|
} catch (Exception e) { |
|
|
|
System.out.println("server error:"+e.getMessage()); |
|
|
|
throw new WDKException(e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static void writeValue(Group group, ItemValue... values) throws Exception { |
|
|
|
try { |
|
|
|
if (values != null && values.length > 0) { |
|
|
|
List<WriteRequest> ws = new ArrayList(); |
|
|
|
ItemValue[] var3 = values; |
|
|
|
int var4 = values.length; |
|
|
|
|
|
|
|
for(int var5 = 0; var5 < var4; ++var5) { |
|
|
|
ItemValue value = var3[var5]; |
|
|
|
Item item = group.addItem(value.getItem_code()); |
|
|
|
ws.add(new WriteRequest(item, getVariant(value.getItem_value()))); |
|
|
|
} |
|
|
|
|
|
|
|
writeValue(group, (WriteRequest[])ws.toArray(new WriteRequest[0])); |
|
|
|
} |
|
|
|
|
|
|
|
} catch (AddFailedException | JIException var8) { |
|
|
|
throw new Exception(var8); |
|
|
|
} |
|
|
|
public static Server getAutoServer(String host, String clsid, String user, String password, String domain) throws WDKException { |
|
|
|
checkTimeout(); |
|
|
|
Server server = null; |
|
|
|
server = new Server(getConnection(host, clsid, user, password, domain), Executors.newSingleThreadScheduledExecutor()); |
|
|
|
AutoReconnectController autoReconnectController = new AutoReconnectController(server); |
|
|
|
autoReconnectController.connect(); |
|
|
|
return server; |
|
|
|
} |
|
|
|
|
|
|
|
public static JIVariant getVariant(Object object) throws Exception { |
|
|
|
if (object instanceof Integer) { |
|
|
|
return getIntegerVariant((Integer)object); |
|
|
|
} else if (object instanceof String) { |
|
|
|
return getStringVariant((String)object); |
|
|
|
} else if (object instanceof byte[]) { |
|
|
|
return getByteArrayVariant((byte[])((byte[])object)); |
|
|
|
} else if (object instanceof Byte[]) { |
|
|
|
return getByteArrayVariant((Byte[])((Byte[])object)); |
|
|
|
} else if (object instanceof Boolean) { |
|
|
|
return new JIVariant((Boolean)object); |
|
|
|
} else if (object instanceof int[]) { |
|
|
|
return getByteArrayVariantxx((int[])((int[])object)); |
|
|
|
} else if (object instanceof Integer[]) { |
|
|
|
JIArray array = new JIArray((Integer)object, false); |
|
|
|
JIVariant value = new JIVariant(array); |
|
|
|
return value; |
|
|
|
} else if (object instanceof JIVariant) { |
|
|
|
return (JIVariant)object; |
|
|
|
} else { |
|
|
|
throw new Exception("未实现目前支持是int,string,byte[]"); |
|
|
|
} |
|
|
|
public static ConnectionInformation getConnection(String host, String clsid, String user, String password, |
|
|
|
String domain) { |
|
|
|
ConnectionInformation connection = new ConnectionInformation(); |
|
|
|
connection.setHost(host); |
|
|
|
connection.setClsid(clsid); |
|
|
|
connection.setUser(user); |
|
|
|
connection.setPassword(password); |
|
|
|
connection.setDomain(domain); |
|
|
|
return connection; |
|
|
|
} |
|
|
|
|
|
|
|
public static JIVariant getByteArrayVariantxx(int[] bytes) { |
|
|
@ -319,8 +269,6 @@ public class OpcUtl { |
|
|
|
return value; |
|
|
|
} |
|
|
|
|
|
|
|
static { |
|
|
|
checkTimeout(); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|