fix 读信号 写信号

This commit is contained in:
张江玮
2023-10-09 15:44:29 +08:00
parent e56ff4b0fb
commit 33b6f68778
2 changed files with 23 additions and 12 deletions

View File

@@ -157,25 +157,35 @@ public class ReadUtil {
}
public static void write(Map<String, Object> strings, Server server) {
write(strings, server, 0);
}
public static void write(Map<String, Object> strings, Server server, int writeCount) {
try {
//Group group = this.opcServerService.getServer(opcServiceCode);
if (writeCount == 3) {
return;
}
try {
server.connect();
} catch (AlreadyConnectedException ignored) {
}
Group group = server.addGroup();
Iterator it = strings.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
for (String key : strings.keySet()) {
Item byteItem = group.addItem(key);
Object o = strings.get(key);
if (o == null || o.equals("")) {
break;
if (o == null) {
continue;
}
WriteRequest write1 = new WriteRequest(byteItem, new JIVariant(o.toString()));
List<WriteRequest> list = new ArrayList();
List<WriteRequest> list = new ArrayList<>();
list.add(write1);
OpcUtl.writeValue(group, (WriteRequest[]) list.toArray(new WriteRequest[0]));
OpcUtl.writeValue(group, list.toArray(new WriteRequest[0]));
}
server.disconnect();
} catch (Exception e) {
e.printStackTrace();
write(strings, server, ++writeCount);
} finally {
server.disconnect();
}
}

View File

@@ -26,6 +26,7 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC
private int all_null;
private Map<String, OpcItemDto> itemSearchCache;
public DeviceOpcProtocolRunable() {
this.error_num = 0;
this.all_null = 0;
@@ -93,7 +94,6 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC
server.disconnect();
log.trace("清理server...");
}
this.server = OpcServerUtl.getServerWithOutException(this.OpcServer.getOpc_host(), this.OpcServer.getCls_id(), this.OpcServer.getUser(), this.OpcServer.getPassword(), this.OpcServer.getDomain());
this.server.addStateListener(this);
group = this.server.addGroup();
@@ -186,7 +186,7 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC
Object his = accessor_value.getValue(itemId);
if (!ObjectUtl.isEquals(itemState.getQuality(), QualityTypeValue.OPC_QUALITY_GOOD) && his != null) {
log.warn("opc 值不健康 item: {}, 状态: {}", itemId, itemState.getQuality());
valueAllNotNull = true;
valueAllNotNull = false;
}
if (!UnifiedDataAppService.isEquals(value, his)) {
@@ -220,7 +220,8 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC
log.warn("{} 所有内容都为空, all_null:{} ,暂定{}s", tag, all_null,5000 + random);
}
ThreadUtl.sleep((long) (5000 + random));
ThreadUtl.sleep((long) ((new Random()).nextInt(3) +1) * 1000);
break start;
} else if (this.all_null < 6) {
if (log.isWarnEnabled()) {
log.warn(tag + "重新创建server");