fix OPC读取
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package org.nl.acs.opc;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.nl.acs.udw.UnifiedDataAccessor;
|
||||
@@ -185,6 +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;
|
||||
}
|
||||
|
||||
if (!UnifiedDataAppService.isEquals(value, his)) {
|
||||
@@ -192,8 +194,9 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC
|
||||
if (true) {
|
||||
this.logItemChanged(itemId, accessor_value, value, itemDto);
|
||||
}
|
||||
|
||||
accessor_value.setValue(itemId, value);
|
||||
if(!ObjectUtil.isEmpty(value)){
|
||||
accessor_value.setValue(itemId, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -214,17 +217,17 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC
|
||||
random *= 1000;
|
||||
if (this.all_null < 3) {
|
||||
if (log.isWarnEnabled()) {
|
||||
log.warn("{} 所有内容都为空, all_null:{} ,暂定{}s", tag, all_null, 5000 + random);
|
||||
log.warn("{} 所有内容都为空, all_null:{} ,暂定{}s", tag, all_null,5000 + random);
|
||||
}
|
||||
|
||||
ThreadUtl.sleep((long) (5000 + random));
|
||||
} else if (this.all_null < 6) {
|
||||
if (log.isWarnEnabled()) {
|
||||
log.warn(tag + "重新创建server");
|
||||
log.warn("{} 所有内容都为空, all_null:{} ,暂定{}s", tag, all_null, 30000 + random);
|
||||
log.warn("{} 所有内容都为空, all_null:{} ,暂定{}s", tag, all_null,30000 + random);
|
||||
}
|
||||
// ThreadUtl.sleep((long) (30000 + random));
|
||||
ThreadUtl.sleep((long) ((new Random()).nextInt(3) + 1) * 1000);
|
||||
ThreadUtl.sleep((long) ((new Random()).nextInt(3) +1) * 1000);
|
||||
break start;
|
||||
} else if (this.all_null < 12) {
|
||||
if (log.isWarnEnabled()) {
|
||||
@@ -278,9 +281,6 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC
|
||||
}
|
||||
}
|
||||
|
||||
private void logItemChanged(String itemId, UnifiedDataAccessor accessor_value, Object value, OpcItemDto itemDto) {
|
||||
}
|
||||
|
||||
|
||||
private void runNew() {
|
||||
Async20Access accessor = null;
|
||||
@@ -369,64 +369,54 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC
|
||||
return days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds ";
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
public void changed(Item item, ItemState itemState) {
|
||||
String itemId = item.getId();
|
||||
|
||||
try {
|
||||
Object value = OpcUtl.getValue(item, itemState);
|
||||
UnifiedDataAccessor accessor_value = UnifiedDataAccessorFactory.getAccessor(OpcConfig.udw_opc_value_key);
|
||||
accessor_value.setValue(itemId, value);
|
||||
|
||||
// if (value != null) {
|
||||
// if (log.isTraceEnabled()) {
|
||||
// log.trace("Item {} new value: {}, Timestamp: {}", new Object[]{itemId, itemState.getValue(), itemState.getTimestamp().getTime()});
|
||||
// }
|
||||
// } else if (log.isInfoEnabled()) {
|
||||
// log.info("Item {} new value: {}, Timestamp: {}, Quality: {}", new Object[]{itemId, itemState.getValue(), itemState.getTimestamp().getTime(), itemState.getQuality()});
|
||||
// }
|
||||
log.trace("Item {} new value: {}, Timestamp: {}", new Object[]{itemId, itemState.getValue(), itemState.getTimestamp().getTime()});
|
||||
|
||||
OpcItemDto itemDto = this.getItem(itemId);
|
||||
// if (Boolean.TRUE.equals(itemDto.getNeed_log())) {
|
||||
// this.logItemChanged(itemId, accessor_value, value, itemDto);
|
||||
// }
|
||||
this.logItemChanged(itemId, accessor_value, value, itemDto);
|
||||
|
||||
} catch (Exception var7) {
|
||||
log.error(itemId, var7);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void logItemChanged(String itemId, UnifiedDataAccessor accessor_value, Object value, OpcItemDto itemDto) {
|
||||
Object his = accessor_value.getValue(itemId);
|
||||
List<String> relate_items = itemDto.getRelate_items();
|
||||
if (relate_items != null && !relate_items.isEmpty()) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Iterator var8 = relate_items.iterator();
|
||||
|
||||
// public void changed(Item item, ItemState itemState) {
|
||||
// String itemId = item.getId();
|
||||
//
|
||||
// try {
|
||||
// Object value = OpcUtl.getValue(item, itemState);
|
||||
// UnifiedDataAccessor accessor_value = UnifiedDataAccessorFactory.getAccessor(OpcConfig.udw_opc_value_key);
|
||||
// accessor_value.setValue(itemId, value);
|
||||
//
|
||||
//// if (value != null) {
|
||||
//// if (log.isTraceEnabled()) {
|
||||
//// log.trace("Item {} new value: {}, Timestamp: {}", new Object[]{itemId, itemState.getValue(), itemState.getTimestamp().getTime()});
|
||||
//// }
|
||||
//// } else if (log.isInfoEnabled()) {
|
||||
//// log.info("Item {} new value: {}, Timestamp: {}, Quality: {}", new Object[]{itemId, itemState.getValue(), itemState.getTimestamp().getTime(), itemState.getQuality()});
|
||||
//// }
|
||||
// log.trace("Item {} new value: {}, Timestamp: {}", new Object[]{itemId, itemState.getValue(), itemState.getTimestamp().getTime()});
|
||||
//
|
||||
// OpcItemDto itemDto = this.getItem(itemId);
|
||||
//// if (Boolean.TRUE.equals(itemDto.getNeed_log())) {
|
||||
//// this.logItemChanged(itemId, accessor_value, value, itemDto);
|
||||
//// }
|
||||
// this.logItemChanged(itemId, accessor_value, value, itemDto);
|
||||
//
|
||||
// } catch (Exception var7) {
|
||||
// log.error(itemId, var7);
|
||||
// }
|
||||
//
|
||||
// }
|
||||
|
||||
// private void logItemChanged(String itemId, UnifiedDataAccessor accessor_value, Object value, OpcItemDto itemDto) {
|
||||
// LuceneExecuteLogService lucene = SpringContextHolder.getBean(LuceneExecuteLogService.class);
|
||||
// Object his = accessor_value.getValue(itemId);
|
||||
// List<String> relate_items = itemDto.getRelate_items();
|
||||
// if (relate_items != null && !relate_items.isEmpty()) {
|
||||
// StringBuilder sb = new StringBuilder();
|
||||
// Iterator var8 = relate_items.iterator();
|
||||
//
|
||||
// while (var8.hasNext()) {
|
||||
// String relate = (String) var8.next();
|
||||
// Object obj = accessor_value.getValue(relate);
|
||||
// sb.append("key:" + relate + "value:" + obj + ";");
|
||||
// }
|
||||
// if (!itemDto.getItem_code().endsWith("heartbeat")) {
|
||||
// lucene.deviceExecuteLog(new LuceneLogDto(itemDto.getOpc_server_code(), itemDto.getOpc_plc_code(), itemDto.getDevice_code(), itemDto.getItem_code().substring(itemDto.getItem_code().lastIndexOf(".")+1), String.valueOf(his), String.valueOf(value)));
|
||||
// }
|
||||
//
|
||||
// } else {
|
||||
// if (!itemDto.getItem_code().endsWith("heartbeat")) {
|
||||
// lucene.deviceExecuteLog(new LuceneLogDto(itemDto.getOpc_server_code(), itemDto.getOpc_plc_code(), itemDto.getDevice_code(), itemDto.getItem_code().substring(itemDto.getItem_code().lastIndexOf(".")+1), String.valueOf(his), String.valueOf(value)));
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// }
|
||||
while (var8.hasNext()) {
|
||||
String relate = (String) var8.next();
|
||||
Object obj = accessor_value.getValue(relate);
|
||||
sb.append("key:" + relate + "value:" + obj + ";");
|
||||
}
|
||||
log.warn("设备:{}信号{}变更从{}->{};信号快照:{}", new Object[]{itemDto.getDevice_code(), itemId, his, value, sb});
|
||||
// this.businessLogger.setResource(itemDto.getDevice_code(), itemDto.getDevice_name()).log("信号{}变更从{}->{};信号快照:{}", new Object[]{itemId, his, value, sb});
|
||||
} else {
|
||||
log.warn("设备:{}信号{}变更从{}->{};信号快照:{}", new Object[]{itemDto.getDevice_code(), itemId, his, value});
|
||||
// this.businessLogger.setResource(itemDto.getDevice_code(), itemDto.getDevice_name()).log("信号{}变更从{}->{}", new Object[]{itemId, his, value});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -42,10 +42,10 @@ public class DeviceOpcSynchronizeAutoRun extends AbstractAutoRunnable {
|
||||
|
||||
Map<String, OpcServerManageDto> servers = this.opcServerManageService.queryAllServerMap();
|
||||
Map<String, List<List<OpcItemDto>>> pros;
|
||||
do{
|
||||
Thread.sleep(1000L);
|
||||
pros = this.deviceAppService.findAllFormatProtocolFromDriver();
|
||||
}while (ObjectUtil.isEmpty(pros));
|
||||
do{
|
||||
Thread.sleep(1000L);
|
||||
pros = this.deviceAppService.findAllFormatProtocolFromDriver();
|
||||
}while (ObjectUtil.isEmpty(pros));
|
||||
Set<String> keys = pros.keySet();
|
||||
Iterator var4 = keys.iterator();
|
||||
//代码执行一次
|
||||
|
||||
@@ -20,8 +20,6 @@ import java.util.concurrent.Executors;
|
||||
public class OpcUtl {
|
||||
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();
|
||||
@@ -37,18 +35,7 @@ public class OpcUtl {
|
||||
|
||||
public static void writeValue(Group group, WriteRequest... requests) throws WDKException {
|
||||
try {
|
||||
Map<Item, Integer> 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);
|
||||
}
|
||||
}
|
||||
|
||||
Map<Item, Integer> e=group.write(requests);
|
||||
boolean is_success = true;
|
||||
StringBuilder message = new StringBuilder();
|
||||
Iterator arg4 = e.keySet().iterator();
|
||||
@@ -69,16 +56,12 @@ 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 WDKException(message.toString());
|
||||
}
|
||||
} catch (JIException arg7) {
|
||||
log.info("下发信号失败:"+arg7.getMessage());
|
||||
System.out.println("下发信号失败原因:"+arg7.getMessage());
|
||||
throw new WDKException(arg7);
|
||||
log.info("下发信号失败Exception:"+arg7.getMessage());
|
||||
throw new WDKException("下发信号失败Exception:"+arg7);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,7 +77,6 @@ public class OpcUtl {
|
||||
Item item = group.addItem(value.getItem_code());
|
||||
ws.add(new WriteRequest(item, getVariant(value.getItem_value())));
|
||||
}
|
||||
|
||||
writeValue(group, (WriteRequest[])ws.toArray(new WriteRequest[0]));
|
||||
}
|
||||
|
||||
@@ -222,16 +204,10 @@ public class OpcUtl {
|
||||
server = new Server(getConnection(host, clsid, user, password, domain),
|
||||
Executors.newSingleThreadScheduledExecutor());
|
||||
server.connect();
|
||||
successNum++;
|
||||
return server;
|
||||
} catch (Exception 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user