|
@ -4,19 +4,27 @@ import cn.hutool.core.util.NumberUtil; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.jinterop.dcom.common.JIException; |
|
|
import org.jinterop.dcom.common.JIException; |
|
|
import org.jinterop.dcom.core.*; |
|
|
import org.jinterop.dcom.core.*; |
|
|
import org.nl.exception.BadRequestException; |
|
|
import org.nl.acs.device_driver.driver.ItemValue; |
|
|
import org.nl.exception.WDKException; |
|
|
import org.nl.exception.WDKException; |
|
|
import org.openscada.opc.lib.common.ConnectionInformation; |
|
|
import org.openscada.opc.lib.common.ConnectionInformation; |
|
|
import org.openscada.opc.lib.da.*; |
|
|
import org.openscada.opc.lib.da.*; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
import java.util.Iterator; |
|
|
import java.util.Iterator; |
|
|
|
|
|
import java.util.List; |
|
|
import java.util.Map; |
|
|
import java.util.Map; |
|
|
import java.util.concurrent.Executors; |
|
|
import java.util.concurrent.Executors; |
|
|
|
|
|
|
|
|
@Slf4j |
|
|
@Slf4j |
|
|
public class OpcUtl { |
|
|
public class OpcUtl { |
|
|
private static int timeout = 180000; |
|
|
private static int timeout = 1*60*1000; |
|
|
private static String key = "rpc.socketTimeout"; |
|
|
private static String key = "rpc.socketTimeout"; |
|
|
|
|
|
public static int successNum=0; |
|
|
|
|
|
public static int errNum=0; |
|
|
|
|
|
|
|
|
|
|
|
static { |
|
|
|
|
|
checkTimeout(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public static void checkTimeout() { |
|
|
public static void checkTimeout() { |
|
|
if (Integer.getInteger(key, 0).intValue() != timeout) { |
|
|
if (Integer.getInteger(key, 0).intValue() != timeout) { |
|
@ -28,7 +36,18 @@ public class OpcUtl { |
|
|
|
|
|
|
|
|
public static void writeValue(Group group, WriteRequest... requests) { |
|
|
public static void writeValue(Group group, WriteRequest... requests) { |
|
|
try { |
|
|
try { |
|
|
Map e = group.write(requests); |
|
|
Map<Item, Integer> e=null; |
|
|
|
|
|
try{ |
|
|
|
|
|
e=group.write(requests); |
|
|
|
|
|
group.write(requests); |
|
|
|
|
|
}catch (Exception e1){ |
|
|
|
|
|
try{ |
|
|
|
|
|
e= group.write(requests); |
|
|
|
|
|
}catch (Exception e2){ |
|
|
|
|
|
e= group.write(requests); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
boolean is_success = true; |
|
|
boolean is_success = true; |
|
|
StringBuilder message = new StringBuilder(); |
|
|
StringBuilder message = new StringBuilder(); |
|
|
Iterator arg4 = e.keySet().iterator(); |
|
|
Iterator arg4 = e.keySet().iterator(); |
|
@ -50,16 +69,68 @@ public class OpcUtl { |
|
|
|
|
|
|
|
|
if (!is_success) { |
|
|
if (!is_success) { |
|
|
// throw new BusinessException(message.toString());
|
|
|
// throw new BusinessException(message.toString());
|
|
|
|
|
|
System.out.println("下发信号失败:"+message.toString()); |
|
|
|
|
|
System.out.println("下发信号失败原因:"+message.toString()); |
|
|
|
|
|
log.info("下发信号失败:"+message.toString()); |
|
|
|
|
|
throw new RuntimeException(message.toString()); |
|
|
} |
|
|
} |
|
|
} catch (JIException arg7) { |
|
|
} catch (JIException arg7) { |
|
|
// throw new BusinessException(arg7);
|
|
|
log.info("下发信号失败:"+arg7.getMessage()); |
|
|
|
|
|
System.out.println("下发信号失败原因:"+arg7.getMessage()); |
|
|
|
|
|
throw new RuntimeException(arg7); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void writeValue(Group group, ItemValue... values) throws WDKException { |
|
|
|
|
|
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 WDKException(var8); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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 BadRequestException { |
|
|
public static Object getValue(Item item, ItemState itemState) { |
|
|
if (NumberUtil.compare(itemState.getQuality(), Short.valueOf(QualityTypeValue.OPC_QUALITY_GOOD)) != 0) { |
|
|
if (NumberUtil.compare(itemState.getQuality(), Short.valueOf(QualityTypeValue.OPC_QUALITY_GOOD)) != 0) { |
|
|
if (item != null) { |
|
|
if (item != null) { |
|
|
log.debug("value is not good {} : {}", item.getId(), itemState.getQuality()); |
|
|
log.debug("value is not good {} : {}", item.getId(), itemState.getQuality()); |
|
|
|
|
|
// throw new WDKException("值不健康进行重连!");
|
|
|
} else { |
|
|
} else { |
|
|
log.debug("value is not good {}", itemState.getQuality()); |
|
|
log.debug("value is not good {}", itemState.getQuality()); |
|
|
} |
|
|
} |
|
@ -71,7 +142,7 @@ public class OpcUtl { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public static Object getValue(JIVariant jiVariant, String id) throws BadRequestException { |
|
|
public static Object getValue(JIVariant jiVariant, String id) { |
|
|
try { |
|
|
try { |
|
|
Object e = jiVariant.getObject(); |
|
|
Object e = jiVariant.getObject(); |
|
|
if (e instanceof IJIUnsigned) { |
|
|
if (e instanceof IJIUnsigned) { |
|
@ -136,7 +207,7 @@ public class OpcUtl { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} catch (JIException e) { |
|
|
} catch (JIException e) { |
|
|
throw new BadRequestException(e.getMessage()); |
|
|
throw new RuntimeException(e.getMessage()); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -150,12 +221,28 @@ public class OpcUtl { |
|
|
server = new Server(getConnection(host, clsid, user, password, domain), |
|
|
server = new Server(getConnection(host, clsid, user, password, domain), |
|
|
Executors.newSingleThreadScheduledExecutor()); |
|
|
Executors.newSingleThreadScheduledExecutor()); |
|
|
server.connect(); |
|
|
server.connect(); |
|
|
|
|
|
successNum++; |
|
|
return server; |
|
|
return server; |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
throw new WDKException(e); |
|
|
errNum++; |
|
|
|
|
|
// e.printStackTrace();
|
|
|
|
|
|
System.out.println("server error:"+e.getMessage()); |
|
|
|
|
|
throw new WDKException(e.getMessage()); |
|
|
|
|
|
}finally{ |
|
|
|
|
|
System.out.println("successNum:"+successNum); |
|
|
|
|
|
System.out.println("errNum:"+errNum); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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 ConnectionInformation getConnection(String host, String clsid, String user, String password, |
|
|
public static ConnectionInformation getConnection(String host, String clsid, String user, String password, |
|
|
String domain) { |
|
|
String domain) { |
|
|
ConnectionInformation connection = new ConnectionInformation(); |
|
|
ConnectionInformation connection = new ConnectionInformation(); |
|
@ -167,31 +254,44 @@ public class OpcUtl { |
|
|
return connection; |
|
|
return connection; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public static String read(String item) throws Exception { |
|
|
public static JIVariant getByteArrayVariantxx(int[] bytes) { |
|
|
System.out.println(item); |
|
|
Integer[] byte_Data = new Integer[bytes.length]; |
|
|
Server server = getServer("192.168.81.251", "7bc0cc8e-482c-47ca-abdc-0fe7f9c6e729", "administrator", "Huawei@123", ""); |
|
|
|
|
|
// String byteItemString = "RD1.RD1.1001.mode";
|
|
|
for(int i = 0; i < bytes.length; ++i) { |
|
|
Group group = server.addGroup(); |
|
|
byte_Data[i] = bytes[i]; |
|
|
Item byteItem = group.addItem(item); |
|
|
} |
|
|
ItemState itemState = null; |
|
|
|
|
|
JIVariant value = null; |
|
|
JIArray array = new JIArray(byte_Data, false); |
|
|
itemState = byteItem.read(true); |
|
|
JIVariant value = new JIVariant(array); |
|
|
value = itemState.getValue(); |
|
|
return value; |
|
|
String data = OpcUtl.getValue(byteItem, itemState) + ""; |
|
|
} |
|
|
return data; |
|
|
|
|
|
|
|
|
public static JIVariant getIntegerVariant(Integer integer) { |
|
|
|
|
|
return new JIVariant(integer); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception { |
|
|
public static JIVariant getStringVariant(String string) { |
|
|
Server server = getServer("192.168.81.251", "7bc0cc8e-482c-47ca-abdc-0fe7f9c6e729", "administrator", "Huawei@123", ""); |
|
|
return new JIVariant(string); |
|
|
String byteItemString = "RD1.RD1.1001.mode"; |
|
|
|
|
|
Group group = server.addGroup(); |
|
|
|
|
|
Item byteItem = group.addItem(byteItemString); |
|
|
|
|
|
ItemState itemState = null; |
|
|
|
|
|
JIVariant value = null; |
|
|
|
|
|
System.out.println(byteItem.getId()); |
|
|
|
|
|
itemState = byteItem.read(true); |
|
|
|
|
|
System.out.println(itemState.getQuality()); |
|
|
|
|
|
System.out.println(getValue(byteItem, itemState)); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static JIVariant getByteArrayVariant(byte[] bytes) { |
|
|
|
|
|
Byte[] byte_Data = new Byte[bytes.length]; |
|
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < bytes.length; ++i) { |
|
|
|
|
|
byte_Data[i] = bytes[i]; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
JIArray array = new JIArray(byte_Data, false); |
|
|
|
|
|
JIVariant value = new JIVariant(array); |
|
|
|
|
|
return value; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static JIVariant getByteArrayVariant(Byte[] bytes) { |
|
|
|
|
|
JIArray array = new JIArray(bytes, false); |
|
|
|
|
|
JIVariant value = new JIVariant(array); |
|
|
|
|
|
return value; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|