|
|
@ -4,27 +4,19 @@ 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.exception.BadRequestException; |
|
|
|
import org.nl.exception.WDKException; |
|
|
|
import org.openscada.opc.lib.common.ConnectionInformation; |
|
|
|
import org.openscada.opc.lib.da.*; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Iterator; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.concurrent.Executors; |
|
|
|
|
|
|
|
@Slf4j |
|
|
|
public class OpcUtl { |
|
|
|
private static int timeout = 1*60*1000; |
|
|
|
private static int timeout = 180000; |
|
|
|
private static String key = "rpc.socketTimeout"; |
|
|
|
public static int successNum=0; |
|
|
|
public static int errNum=0; |
|
|
|
|
|
|
|
static { |
|
|
|
checkTimeout(); |
|
|
|
} |
|
|
|
|
|
|
|
public static void checkTimeout() { |
|
|
|
if (Integer.getInteger(key, 0).intValue() != timeout) { |
|
|
@ -36,18 +28,7 @@ public class OpcUtl { |
|
|
|
|
|
|
|
public static void writeValue(Group group, WriteRequest... requests) { |
|
|
|
try { |
|
|
|
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); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Map e = group.write(requests); |
|
|
|
boolean is_success = true; |
|
|
|
StringBuilder message = new StringBuilder(); |
|
|
|
Iterator arg4 = e.keySet().iterator(); |
|
|
@ -69,68 +50,16 @@ public class OpcUtl { |
|
|
|
|
|
|
|
if (!is_success) { |
|
|
|
// 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) { |
|
|
|
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[]"); |
|
|
|
// throw new BusinessException(arg7);
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static Object getValue(Item item, ItemState itemState) { |
|
|
|
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()); |
|
|
|
} |
|
|
@ -142,7 +71,7 @@ public class OpcUtl { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static Object getValue(JIVariant jiVariant, String id) { |
|
|
|
public static Object getValue(JIVariant jiVariant, String id) throws BadRequestException { |
|
|
|
try { |
|
|
|
Object e = jiVariant.getObject(); |
|
|
|
if (e instanceof IJIUnsigned) { |
|
|
@ -207,7 +136,7 @@ public class OpcUtl { |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (JIException e) { |
|
|
|
throw new RuntimeException(e.getMessage()); |
|
|
|
throw new BadRequestException(e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -221,28 +150,12 @@ public class OpcUtl { |
|
|
|
server = new Server(getConnection(host, clsid, user, password, domain), |
|
|
|
Executors.newSingleThreadScheduledExecutor()); |
|
|
|
server.connect(); |
|
|
|
successNum++; |
|
|
|
return server; |
|
|
|
} catch (Exception 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); |
|
|
|
throw new WDKException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
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, |
|
|
|
String domain) { |
|
|
|
ConnectionInformation connection = new ConnectionInformation(); |
|
|
@ -254,44 +167,31 @@ public class OpcUtl { |
|
|
|
return connection; |
|
|
|
} |
|
|
|
|
|
|
|
public static JIVariant getByteArrayVariantxx(int[] bytes) { |
|
|
|
Integer[] byte_Data = new Integer[bytes.length]; |
|
|
|
|
|
|
|
for(int i = 0; i < bytes.length; ++i) { |
|
|
|
byte_Data[i] = bytes[i]; |
|
|
|
public static String read(String item) throws Exception { |
|
|
|
System.out.println(item); |
|
|
|
Server server = getServer("192.168.81.251", "7bc0cc8e-482c-47ca-abdc-0fe7f9c6e729", "administrator", "Huawei@123", ""); |
|
|
|
// String byteItemString = "RD1.RD1.1001.mode";
|
|
|
|
Group group = server.addGroup(); |
|
|
|
Item byteItem = group.addItem(item); |
|
|
|
ItemState itemState = null; |
|
|
|
JIVariant value = null; |
|
|
|
itemState = byteItem.read(true); |
|
|
|
value = itemState.getValue(); |
|
|
|
String data = OpcUtl.getValue(byteItem, itemState) + ""; |
|
|
|
return data; |
|
|
|
} |
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception { |
|
|
|
Server server = getServer("192.168.81.251", "7bc0cc8e-482c-47ca-abdc-0fe7f9c6e729", "administrator", "Huawei@123", ""); |
|
|
|
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)); |
|
|
|
} |
|
|
|
|
|
|
|
JIArray array = new JIArray(byte_Data, false); |
|
|
|
JIVariant value = new JIVariant(array); |
|
|
|
return value; |
|
|
|
} |
|
|
|
|
|
|
|
public static JIVariant getIntegerVariant(Integer integer) { |
|
|
|
return new JIVariant(integer); |
|
|
|
} |
|
|
|
|
|
|
|
public static JIVariant getStringVariant(String string) { |
|
|
|
return new JIVariant(string); |
|
|
|
} |
|
|
|
|
|
|
|
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; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|