更新
This commit is contained in:
@@ -15,194 +15,228 @@ import java.util.*;
|
||||
|
||||
@Slf4j
|
||||
public class DeviceOpcProtocolRunable implements Runnable {
|
||||
List<OpcItemDto> protocols;
|
||||
OpcServerManageDto OpcServer;
|
||||
int error_num;
|
||||
String message;
|
||||
List<OpcItemDto> protocols;
|
||||
OpcServerManageDto OpcServer;
|
||||
int error_num;
|
||||
String message;
|
||||
private Server server;
|
||||
|
||||
public DeviceOpcProtocolRunable() {
|
||||
this.error_num = 0;
|
||||
this.message = null;
|
||||
}
|
||||
public DeviceOpcProtocolRunable() {
|
||||
this.error_num = 0;
|
||||
this.message = null;
|
||||
this.server = null;
|
||||
}
|
||||
|
||||
public List<OpcItemDto> getProtocols() {
|
||||
return this.protocols;
|
||||
}
|
||||
public List<OpcItemDto> getProtocols() {
|
||||
return this.protocols;
|
||||
}
|
||||
|
||||
public void setProtocols(List<OpcItemDto> protocols) {
|
||||
this.protocols = protocols;
|
||||
}
|
||||
public void setProtocols(List<OpcItemDto> protocols) {
|
||||
this.protocols = protocols;
|
||||
}
|
||||
|
||||
public OpcServerManageDto getOpcServer() {
|
||||
return this.OpcServer;
|
||||
}
|
||||
public OpcServerManageDto getOpcServer() {
|
||||
return this.OpcServer;
|
||||
}
|
||||
|
||||
public void setOpcServer(OpcServerManageDto opcServer) {
|
||||
this.OpcServer = opcServer;
|
||||
}
|
||||
public void setOpcServer(OpcServerManageDto opcServer) {
|
||||
this.OpcServer = opcServer;
|
||||
}
|
||||
|
||||
OpcItemDto getItem(String item) {
|
||||
Iterator var2 = this.protocols.iterator();
|
||||
OpcItemDto getItem(String item) {
|
||||
Iterator var2 = this.protocols.iterator();
|
||||
|
||||
OpcItemDto dto;
|
||||
do {
|
||||
if (!var2.hasNext()) {
|
||||
return null;
|
||||
}
|
||||
OpcItemDto dto;
|
||||
do {
|
||||
if (!var2.hasNext()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
dto = (OpcItemDto) var2.next();
|
||||
} while (!StrUtil.equals(item, dto.getItem_code()));
|
||||
dto = (OpcItemDto) var2.next();
|
||||
} while (!StrUtil.equals(item, dto.getItem_code()));
|
||||
|
||||
return dto;
|
||||
}
|
||||
return dto;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (is_error) {
|
||||
log.info("设备OPC数据同步配置异常");
|
||||
}
|
||||
|
||||
if (!OpcStartTag.is_run) {
|
||||
OpcStartTag.is_run = true;
|
||||
}
|
||||
|
||||
//线程名
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@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 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 ";
|
||||
while (it.hasNext()) {
|
||||
OpcItemDto protocol = (OpcItemDto) it.next();
|
||||
String item = protocol.getItem_code();
|
||||
itemsString.add(item);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (is_error) {
|
||||
log.info("设备OPC数据同步配置异常");
|
||||
}
|
||||
|
||||
if (!OpcStartTag.is_run) {
|
||||
OpcStartTag.is_run = true;
|
||||
}
|
||||
|
||||
//线程名
|
||||
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 = 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);
|
||||
}
|
||||
|
||||
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});
|
||||
|
||||
}
|
||||
// accessor_value.setValueWithPersistence(item.getId(),accessor_value.getValue(item.getId()));
|
||||
// accessor_value.getHistoryUnifiedData(item.getId());
|
||||
//设置值
|
||||
accessor_value.setValue(item.getId(), value);
|
||||
|
||||
}
|
||||
}
|
||||
} catch (Exception var30) {
|
||||
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)) {
|
||||
log.warn("", var30);
|
||||
}
|
||||
|
||||
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 ";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package org.nl.acs.opc;
|
||||
|
||||
|
||||
import org.nl.modules.wql.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import org.nl.modules.quartz.domain.QuartzLog;
|
||||
import org.nl.modules.quartz.repository.QuartzLogRepository;
|
||||
import org.nl.modules.quartz.service.QuartzJobService;
|
||||
import org.nl.modules.wql.util.SpringContextHolder;
|
||||
import org.quartz.DisallowConcurrentExecution;
|
||||
import org.quartz.JobExecutionContext;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.scheduling.quartz.QuartzJobBean;
|
||||
@@ -41,6 +42,7 @@ import java.util.concurrent.ThreadPoolExecutor;
|
||||
@Async
|
||||
@SuppressWarnings({"unchecked", "all"})
|
||||
@Slf4j
|
||||
@DisallowConcurrentExecution
|
||||
public class ExecutionJob extends QuartzJobBean {
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user