Browse Source

更新

master
gengby 2 years ago
parent
commit
1e961e0e3d
  1. 380
      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

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

@ -15,194 +15,228 @@ import java.util.*;
@Slf4j
public class DeviceOpcProtocolRunable implements Runnable {
List<OpcItemDto> protocols;
OpcServerManageDto OpcServer;
int error_num;
String message;
public DeviceOpcProtocolRunable() {
this.error_num = 0;
this.message = null;
}
public List<OpcItemDto> getProtocols() {
return this.protocols;
}
public void setProtocols(List<OpcItemDto> protocols) {
this.protocols = protocols;
}
List<OpcItemDto> protocols;
OpcServerManageDto OpcServer;
int error_num;
String message;
private Server server;
public DeviceOpcProtocolRunable() {
this.error_num = 0;
this.message = null;
this.server = null;
}
public List<OpcItemDto> getProtocols() {
return this.protocols;
}
public void setProtocols(List<OpcItemDto> protocols) {
this.protocols = protocols;
}
public OpcServerManageDto getOpcServer() {
return this.OpcServer;
}
public void setOpcServer(OpcServerManageDto opcServer) {
this.OpcServer = opcServer;
}
OpcItemDto getItem(String item) {
Iterator var2 = this.protocols.iterator();
OpcItemDto dto;
do {
if (!var2.hasNext()) {
return null;
}
dto = (OpcItemDto) var2.next();
} while (!StrUtil.equals(item, dto.getItem_code()));
return dto;
}
@Override
public void run() {
while (true) {
try {
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(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();
Iterator it = this.protocols.iterator();
public OpcServerManageDto getOpcServer() {
return this.OpcServer;
}
while (it.hasNext()) {
OpcItemDto protocol = (OpcItemDto) it.next();
String item = protocol.getItem_code();
itemsString.add(item);
}
public void setOpcServer(OpcServerManageDto opcServer) {
this.OpcServer = opcServer;
}
Map<String, Item> itemsMap = new LinkedHashMap();
boolean is_error = false;
StringBuilder err_message = new StringBuilder();
Iterator var7 = itemsString.iterator();
while (var7.hasNext()) {
String string = (String) var7.next();
try {
itemsMap.put(string, group.addItem(string));
log.trace("添加成功 {}", string);
} catch (Exception var29) {
err_message.append(string + ":" + var29.getMessage());
if (!is_error) {
is_error = true;
}
}
}
OpcItemDto getItem(String item) {
Iterator var2 = this.protocols.iterator();
if (is_error) {
log.info("设备OPC数据同步配置异常");
}
OpcItemDto dto;
do {
if (!var2.hasNext()) {
return null;
}
if (!OpcStartTag.is_run) {
OpcStartTag.is_run = true;
}
dto = (OpcItemDto) var2.next();
} while (!StrUtil.equals(item, dto.getItem_code()));
//线程名
String tag = Thread.currentThread().getName();
if (this.OpcServer != null) {
tag = tag + this.OpcServer.getOpc_code();
}
UnifiedDataAccessor accessor_value = UnifiedDataAccessorFactory.getAccessor(OpcConfig.udw_opc_value_key);
return dto;
}
boolean time_out = false;
@Override
public void run() {
label97:
while (true) {
try {
Server server = OpcServerUtl.getServerWithOutException(this.OpcServer.getOpc_host(), this.OpcServer.getCls_id(), this.OpcServer.getUser(), this.OpcServer.getPassword(), this.OpcServer.getDomain());
Group group = server.addGroup();
List<String> itemsString = new ArrayList();
Iterator it = this.protocols.iterator();
while (it.hasNext()) {
OpcItemDto protocol = (OpcItemDto) it.next();
String item = protocol.getItem_code();
itemsString.add(item);
}
long begin = System.currentTimeMillis();
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();
log.trace("{} 开始记时{}", tag, DateUtil.now());
long duration = end - begin;
log.trace("{} 读取耗时:{}", tag, duration);
System.out.println("线程:"+tag + " 读取耗时:"+ duration);
if (duration > 1000L) {
if (!time_out) {
log.warn(" {} 读取超时 : {}", tag, duration);
}
Map<String, Item> itemsMap = new LinkedHashMap();
boolean is_error = false;
StringBuilder err_message = new StringBuilder();
Iterator var7 = itemsString.iterator();
while (var7.hasNext()) {
String string = (String) var7.next();
try {
itemsMap.put(string, group.addItem(string));
log.trace("添加成功 {}", string);
} catch (Exception var29) {
err_message.append(string + ":" + var29.getMessage());
if (!is_error) {
is_error = true;
}
}
time_out = true;
} else {
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();
Iterator var18 = items.iterator();
while (true) {
Item item;
//当前值
Object value;
//旧的值
Object his;
do {
if (!var18.hasNext()) {
end = System.currentTimeMillis();
log.trace("{}", itemsString);
log.trace("{} 计算完成耗时{}", tag, end - begin);
Thread.sleep((long) OpcConfig.synchronized_millisecond);
if (this.error_num != 0) {
this.error_num = 0;
this.message = null;
}
continue label97;
}
item = (Item) var18.next();
ItemState itemState = (ItemState) itemStatus.get(item);
value = OpcUtl.getValue(item, itemState);
his = accessor_value.getValue(item.getId());
if (!ObjectUtl.isEquals(itemState.getQuality(), QualityTypeValue.OPC_QUALITY_GOOD) && his != null) {
log.warn("opc 值不健康 item: {}, 状态: {},当前读取值:{}, 系统内存值{} ", item.getId(), itemState.getQuality(), value, his);
}
} while (ObjectUtil.equal(value, his));//如果两次的值相等,不走下面的代码
OpcItemDto itemDto = this.getItem(item.getId());
//默认记录日志
if (true) {
StringBuilder sb = new StringBuilder();
//设备的ITEM项
List<String> relate_items = itemDto.getRelate_items();
Iterator var26 = relate_items.iterator();
while (var26.hasNext()) {
String relate = (String) var26.next();
Object obj = accessor_value.getValue(relate);
sb.append("key:" + relate + "value:" + obj + ";");
}
log.warn("信号{}变更从{}->{};信号快照:{}", new Object[]{item.getId(), his, value, sb});
if (is_error) {
log.info("设备OPC数据同步配置异常");
}
}
// accessor_value.setValueWithPersistence(item.getId(),accessor_value.getValue(item.getId()));
// accessor_value.getHistoryUnifiedData(item.getId());
//设置值
accessor_value.setValue(item.getId(), value);
if (!OpcStartTag.is_run) {
OpcStartTag.is_run = true;
}
}
}
} catch (Exception var30) {
if (this.server != null) {
try {
this.server.disconnect();
} catch (Exception var25) {
log.warn("{} : server disconnect", var25);
}
}
//线程名
String tag = Thread.currentThread().getName();
if (this.OpcServer != null) {
tag = tag + this.OpcServer.getOpc_code();
}
UnifiedDataAccessor accessor_value = UnifiedDataAccessorFactory.getAccessor(OpcConfig.udw_opc_value_key);
boolean time_out = false;
label97:
while (true) {
long begin = System.currentTimeMillis();
Map<Item, ItemState> itemStatus = group.read(true, (Item[]) itemsMap.values().toArray(new Item[0]));
long end = System.currentTimeMillis();
log.trace("{} 开始记时{}", tag, DateUtil.now());
long duration = end - begin;
log.trace("{} 读取耗时:{}", tag, duration);
if (duration > 1000L) {
if (!time_out) {
log.warn("{} 读取超时 : {}", tag, duration);
}
time_out = true;
} else {
time_out = false;
}
Set<Item> items = itemStatus.keySet();
Iterator var18 = items.iterator();
while (true) {
Item item;
//当前值
Object value;
//旧的值
Object his;
do {
if (!var18.hasNext()) {
end = System.currentTimeMillis();
log.trace("{}", itemsString);
log.trace("{} 计算完成耗时{}", tag, end - begin);
Thread.sleep((long) OpcConfig.synchronized_millisecond);
if (this.error_num != 0) {
this.error_num = 0;
this.message = null;
}
continue label97;
}
item = (Item) var18.next();
ItemState itemState = (ItemState) itemStatus.get(item);
value = OpcUtl.getValue(item, itemState);
his = accessor_value.getValue(item.getId());
if (!ObjectUtil.equal(itemState.getQuality(), QualityTypeValue.OPC_QUALITY_GOOD) && his != null) {
log.warn("opc 值不健康 item: {}, 状态: {}", item.getId(), itemState.getQuality());
}
} while (ObjectUtil.equal(value, his));//如果两次的值相等,不走下面的代码
OpcItemDto itemDto = this.getItem(item.getId());
if (itemDto.getNeed_log() != null && itemDto.getNeed_log()) {
StringBuilder sb = new StringBuilder();
//设备的ITEM项
List<String> relate_items = itemDto.getRelate_items();
Iterator var26 = relate_items.iterator();
while (var26.hasNext()) {
String relate = (String) var26.next();
Object obj = accessor_value.getValue(relate);
sb.append("key:" + relate + "value:" + obj + ";");
}
log.info("信号{}变更从{}->{};信号快照:{}", new Object[]{item.getId(), his, value, sb});
}
//设置值
accessor_value.setValue(item.getId(), value);
}
}
} catch (Exception var30) {
String error_message = "设备信息同步异常";
if (!StrUtil.equals(this.message, error_message)) {
log.warn("", var30);
}
this.server = null;
try {
Thread.sleep((long) (OpcConfig.synchronized_exception_wait_second * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
++this.error_num;
if (this.error_num > 3 && !StrUtil.equals(this.message, error_message)) {
log.info("设备同步通信异常");
this.message = error_message;
}
}
String error_message = "设备信息同步异常,"+var30;
if (!StrUtil.equals(this.message, error_message)) {
log.warn("", var30);
}
}
public static String formatDuring(long mss) {
long days = mss / 86400000L;
long hours = mss % 86400000L / 3600000L;
long minutes = mss % 3600000L / 60000L;
long seconds = mss % 60000L / 1000L;
return days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds ";
try {
Thread.sleep((long) (OpcConfig.synchronized_exception_wait_second * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
log.warn("OPC 读取线程读取异常{} :", e);
// Thread.currentThread().interrupt();不会真正停止线程
}
++this.error_num;
if (this.error_num > 3 && !StrUtil.equals(this.message, error_message)) {
log.info("设备同步通信异常");
this.message = error_message;
}
}
}
}
public static String formatDuring(long mss) {
long days = mss / 86400000L;
long hours = mss % 86400000L / 3600000L;
long minutes = mss % 3600000L / 60000L;
long seconds = mss % 60000L / 1000L;
return days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds ";
}
}

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