Browse Source

更新

master
gengby 2 years ago
parent
commit
1e961e0e3d
  1. 52
      hd/nladmin-system/src/main/java/org/nl/acs/opc/DeviceOpcProtocolRunable.java
  2. 73
      hd/nladmin-system/src/main/java/org/nl/acs/opc/ObjectUtl.java

52
hd/nladmin-system/src/main/java/org/nl/acs/opc/DeviceOpcProtocolRunable.java

@ -19,10 +19,12 @@ public class DeviceOpcProtocolRunable implements Runnable {
OpcServerManageDto OpcServer; OpcServerManageDto OpcServer;
int error_num; int error_num;
String message; String message;
private Server server;
public DeviceOpcProtocolRunable() { public DeviceOpcProtocolRunable() {
this.error_num = 0; this.error_num = 0;
this.message = null; this.message = null;
this.server = null;
} }
public List<OpcItemDto> getProtocols() { public List<OpcItemDto> getProtocols() {
@ -60,8 +62,12 @@ public class DeviceOpcProtocolRunable implements Runnable {
public void run() { public void run() {
while (true) { while (true) {
try { try {
Server server = OpcServerUtl.getServerWithOutException(this.OpcServer.getOpc_host(), this.OpcServer.getCls_id(), this.OpcServer.getUser(), this.OpcServer.getPassword(), this.OpcServer.getDomain()); this.server = OpcServerUtl.getServerWithOutException(this.OpcServer.getOpc_host(), this.OpcServer.getCls_id(), this.OpcServer.getUser(), this.OpcServer.getPassword(), this.OpcServer.getDomain());
Group group = server.addGroup(); // Group group = server.addGroup(this.OpcServer.getOpc_host());
Group group = server.addGroup(this.OpcServer.getOpc_code());
if(ObjectUtil.isEmpty(group)){
log.info("group is null ");
}
List<String> itemsString = new ArrayList(); List<String> itemsString = new ArrayList();
Iterator it = this.protocols.iterator(); Iterator it = this.protocols.iterator();
@ -110,11 +116,19 @@ public class DeviceOpcProtocolRunable implements Runnable {
label97: label97:
while (true) { while (true) {
long begin = System.currentTimeMillis(); long begin = System.currentTimeMillis();
Map<Item, ItemState> itemStatus = group.read(true, (Item[]) itemsMap.values().toArray(new Item[0])); Map<Item, ItemState> itemStatus = null;
try {
itemStatus = group.read(true, (Item[]) itemsMap.values().toArray(new Item[0]));
} catch (Exception e){
System.out.println("数据同步异常:"+ this.getOpcServer().getOpc_code());
log.trace("数据同步异常:{}", this.getOpcServer().getOpc_code());
//e.printStackTrace();
}
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
log.trace("{} 开始记时{}", tag, DateUtil.now()); log.trace("{} 开始记时{}", tag, DateUtil.now());
long duration = end - begin; long duration = end - begin;
log.trace("{} 读取耗时:{}", tag, duration); log.trace("{} 读取耗时:{}", tag, duration);
System.out.println("线程:"+tag + " 读取耗时:"+ duration);
if (duration > 1000L) { if (duration > 1000L) {
if (!time_out) { if (!time_out) {
log.warn(" {} 读取超时 : {}", tag, duration); log.warn(" {} 读取超时 : {}", tag, duration);
@ -125,6 +139,12 @@ public class DeviceOpcProtocolRunable implements Runnable {
time_out = false; time_out = false;
} }
if(ObjectUtil.isEmpty(itemStatus)) {
System.out.println( tag + " :itemStatus is null");
log.warn(" {} 读取异常 : {} itemStatus is null", tag);
}
Set<Item> items = itemStatus.keySet(); Set<Item> items = itemStatus.keySet();
Iterator var18 = items.iterator(); Iterator var18 = items.iterator();
@ -151,13 +171,14 @@ public class DeviceOpcProtocolRunable implements Runnable {
ItemState itemState = (ItemState) itemStatus.get(item); ItemState itemState = (ItemState) itemStatus.get(item);
value = OpcUtl.getValue(item, itemState); value = OpcUtl.getValue(item, itemState);
his = accessor_value.getValue(item.getId()); his = accessor_value.getValue(item.getId());
if (!ObjectUtil.equal(itemState.getQuality(), QualityTypeValue.OPC_QUALITY_GOOD) && his != null) { if (!ObjectUtl.isEquals(itemState.getQuality(), QualityTypeValue.OPC_QUALITY_GOOD) && his != null) {
log.warn("opc 值不健康 item: {}, 状态: {}", item.getId(), itemState.getQuality()); log.warn("opc 值不健康 item: {}, 状态: {},当前读取值:{}, 系统内存值{} ", item.getId(), itemState.getQuality(), value, his);
} }
} while (ObjectUtil.equal(value, his));//如果两次的值相等,不走下面的代码 } while (ObjectUtil.equal(value, his));//如果两次的值相等,不走下面的代码
OpcItemDto itemDto = this.getItem(item.getId()); OpcItemDto itemDto = this.getItem(item.getId());
if (itemDto.getNeed_log() != null && itemDto.getNeed_log()) { //默认记录日志
if (true) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
//设备的ITEM项 //设备的ITEM项
List<String> relate_items = itemDto.getRelate_items(); List<String> relate_items = itemDto.getRelate_items();
@ -169,16 +190,28 @@ public class DeviceOpcProtocolRunable implements Runnable {
sb.append("key:" + relate + "value:" + obj + ";"); sb.append("key:" + relate + "value:" + obj + ";");
} }
log.info("信号{}变更从{}->{};信号快照:{}", new Object[]{item.getId(), his, value, sb}); log.warn("信号{}变更从{}->{};信号快照:{}", new Object[]{item.getId(), his, value, sb});
} }
// accessor_value.setValueWithPersistence(item.getId(),accessor_value.getValue(item.getId()));
// accessor_value.getHistoryUnifiedData(item.getId());
//设置值 //设置值
accessor_value.setValue(item.getId(), value); accessor_value.setValue(item.getId(), value);
} }
} }
} catch (Exception var30) { } catch (Exception var30) {
String error_message = "设备信息同步异常"; if (this.server != null) {
try {
this.server.disconnect();
} catch (Exception var25) {
log.warn("{} : server disconnect", var25);
}
}
this.server = null;
String error_message = "设备信息同步异常,"+var30;
if (!StrUtil.equals(this.message, error_message)) { if (!StrUtil.equals(this.message, error_message)) {
log.warn("", var30); log.warn("", var30);
} }
@ -187,7 +220,8 @@ public class DeviceOpcProtocolRunable implements Runnable {
Thread.sleep((long) (OpcConfig.synchronized_exception_wait_second * 1000)); Thread.sleep((long) (OpcConfig.synchronized_exception_wait_second * 1000));
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
Thread.currentThread().interrupt(); log.warn("OPC 读取线程读取异常{} :", e);
// Thread.currentThread().interrupt();不会真正停止线程
} }
++this.error_num; ++this.error_num;
if (this.error_num > 3 && !StrUtil.equals(this.message, error_message)) { if (this.error_num > 3 && !StrUtil.equals(this.message, error_message)) {

73
hd/nladmin-system/src/main/java/org/nl/acs/opc/ObjectUtl.java

@ -0,0 +1,73 @@
package org.nl.acs.opc;
import org.nl.exception.WDKException;
import java.util.Arrays;
import java.util.Objects;
public class ObjectUtl {
private ObjectUtl() {
}
public static boolean isEquals(Object a, Object b) {
if (a == null && b == null) {
return true;
} else if (a != null && b != null) {
if (a.getClass().isArray()) {
if (a instanceof boolean[]) {
return Arrays.equals((boolean[]) ((boolean[]) a), (boolean[]) ((boolean[]) b));
} else if (a instanceof byte[]) {
return Arrays.equals((byte[]) ((byte[]) a), (byte[]) ((byte[]) b));
} else if (a instanceof int[]) {
return Arrays.equals((int[]) ((int[]) a), (int[]) ((int[]) b));
} else if (a instanceof long[]) {
return Arrays.equals((long[]) ((long[]) a), (long[]) ((long[]) b));
} else if (a instanceof double[]) {
return Arrays.equals((double[]) ((double[]) a), (double[]) ((double[]) b));
} else if (a instanceof short[]) {
return Arrays.equals((short[]) ((short[]) a), (short[]) ((short[]) b));
} else if (a instanceof char[]) {
return Arrays.equals((char[]) ((char[]) a), (char[]) ((char[]) b));
} else if (a instanceof float[]) {
return Arrays.equals((float[]) ((float[]) a), (float[]) ((float[]) b));
} else if (a instanceof Object[]) {
return Arrays.equals((Object[]) ((Object[]) a), (Object[]) ((Object[]) b));
} else {
throw new WDKException("未实现");
}
} else {
return Objects.equals(a, b);
}
} else {
return false;
}
}
public static boolean isTrue(Boolean boolean_) {
return boolean_ != null && isEquals(boolean_, true);
}
public static boolean isTrue(Boolean targetBoolean, boolean defaultBoolean) {
return targetBoolean == null ? defaultBoolean : targetBoolean;
}
public static boolean isFalse(Boolean boolean_) {
return boolean_ != null && isEquals(boolean_, false);
}
public static boolean isObject(Class<?> clazz) {
if (clazz == null) {
return false;
} else if (clazz.getClass().isArray()) {
return false;
} else {
return Object.class.isAssignableFrom(clazz);
}
}
}
Loading…
Cancel
Save