diff --git a/hd/nladmin-system/src/main/java/org/nl/acs/device/service/impl/ConveyorDevice.java b/hd/nladmin-system/src/main/java/org/nl/acs/device/service/impl/ConveyorDevice.java index 9a685f3..9f12089 100644 --- a/hd/nladmin-system/src/main/java/org/nl/acs/device/service/impl/ConveyorDevice.java +++ b/hd/nladmin-system/src/main/java/org/nl/acs/device/service/impl/ConveyorDevice.java @@ -43,16 +43,16 @@ public class ConveyorDevice { @SneakyThrows @Override public void run() { - System.out.println("test"); - String MODE = "RD1.RD1." + id + ".mode"; - String code = OpcUtl.read(MODE); - if ("2".equals(code)) { - TaskService taskserver = new TaskServiceImpl(); - TaskDto dto = new TaskDto(); - dto.setStart_point_code(id); - dto.setNext_point_code("1002"); - taskserver.create(dto); - } +// System.out.println("test"); +// String MODE = "RD1.RD1." + id + ".mode"; +// String code = OpcUtl.read(MODE); +// if ("2".equals(code)) { +// TaskService taskserver = new TaskServiceImpl(); +// TaskDto dto = new TaskDto(); +// dto.setStart_point_code(id); +// dto.setNext_point_code("1002"); +// taskserver.create(dto); +// } } } } diff --git a/hd/nladmin-system/src/main/java/org/nl/acs/device/service/impl/Test.java b/hd/nladmin-system/src/main/java/org/nl/acs/device/service/impl/Test.java index 8e73a4e..13a956d 100644 --- a/hd/nladmin-system/src/main/java/org/nl/acs/device/service/impl/Test.java +++ b/hd/nladmin-system/src/main/java/org/nl/acs/device/service/impl/Test.java @@ -5,14 +5,14 @@ import java.util.List; public class Test { - public static void main(String[] args) { - String device = "1001,1002"; - List list = new ArrayList<>(); - list.add("1001"); - list.add("1002"); - for (int i = 0; i < list.size(); i++) { - ConveyorDevice condev = new ConveyorDevice(list.get(i).toString()); - } - - } +// public static void main(String[] args) { +// String device = "1001,1002"; +// List list = new ArrayList<>(); +// list.add("1001"); +// list.add("1002"); +// for (int i = 0; i < list.size(); i++) { +// ConveyorDevice condev = new ConveyorDevice(list.get(i).toString()); +// } +// +// } } diff --git a/hd/nladmin-system/src/main/java/org/nl/acs/device_driver/driver/ItemValue.java b/hd/nladmin-system/src/main/java/org/nl/acs/device_driver/driver/ItemValue.java new file mode 100644 index 0000000..824c769 --- /dev/null +++ b/hd/nladmin-system/src/main/java/org/nl/acs/device_driver/driver/ItemValue.java @@ -0,0 +1,36 @@ +package org.nl.acs.device_driver.driver; + +/** + * @author ldjun + * @version 1.0 + * @date 2023年02月01日 11:21 + * @desc desc + */ +public class ItemValue { + private String item_code; + private Object item_value; + + public ItemValue() { + } + + public ItemValue(String item_code, Object item_value) { + this.item_code = item_code; + this.item_value = item_value; + } + + public String getItem_code() { + return this.item_code; + } + + public void setItem_code(String item_code) { + this.item_code = item_code; + } + + public Object getItem_value() { + return this.item_value; + } + + public void setItem_value(Object item_value) { + this.item_value = item_value; + } +} diff --git a/hd/nladmin-system/src/main/java/org/nl/acs/device_driver/standard_inspect_site_smart200/StandardInspectSiteSmartDeviceDriver.java b/hd/nladmin-system/src/main/java/org/nl/acs/device_driver/standard_inspect_site_smart200/StandardInspectSiteSmartDeviceDriver.java index f237643..b17d5ed 100644 --- a/hd/nladmin-system/src/main/java/org/nl/acs/device_driver/standard_inspect_site_smart200/StandardInspectSiteSmartDeviceDriver.java +++ b/hd/nladmin-system/src/main/java/org/nl/acs/device_driver/standard_inspect_site_smart200/StandardInspectSiteSmartDeviceDriver.java @@ -380,6 +380,10 @@ public class StandardInspectSiteSmartDeviceDriver extends AbstractOpcDeviceDrive } + if (action == 0 && ("1005".equals(devicecode) || "1006".equals(devicecode))) { + writing(0); + } + } last_mode = mode; last_error = error; diff --git a/hd/nladmin-system/src/main/java/org/nl/acs/opc/OpcUtl.java b/hd/nladmin-system/src/main/java/org/nl/acs/opc/OpcUtl.java index c386e1c..18f22d2 100644 --- a/hd/nladmin-system/src/main/java/org/nl/acs/opc/OpcUtl.java +++ b/hd/nladmin-system/src/main/java/org/nl/acs/opc/OpcUtl.java @@ -4,19 +4,27 @@ 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.exception.BadRequestException; +import org.nl.acs.device_driver.driver.ItemValue; 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 = 180000; + private static int timeout = 1*60*1000; 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) { @@ -28,7 +36,18 @@ public class OpcUtl { public static void writeValue(Group group, WriteRequest... requests) { try { - Map e = group.write(requests); + Map 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; StringBuilder message = new StringBuilder(); Iterator arg4 = e.keySet().iterator(); @@ -50,16 +69,68 @@ 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) { - // 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 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 (item != null) { log.debug("value is not good {} : {}", item.getId(), itemState.getQuality()); +// throw new WDKException("值不健康进行重连!"); } else { 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 { Object e = jiVariant.getObject(); if (e instanceof IJIUnsigned) { @@ -136,7 +207,7 @@ public class OpcUtl { } } } 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), Executors.newSingleThreadScheduledExecutor()); server.connect(); + successNum++; return server; } 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, String domain) { ConnectionInformation connection = new ConnectionInformation(); @@ -167,31 +254,44 @@ public class OpcUtl { return connection; } - 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 JIVariant getByteArrayVariantxx(int[] bytes) { + Integer[] byte_Data = new Integer[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 getIntegerVariant(Integer integer) { + return new JIVariant(integer); } - 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)); + 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; + } + + }