fix OPC读取
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package org.nl.acs.opc;
|
package org.nl.acs.opc;
|
||||||
|
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.nl.acs.udw.UnifiedDataAccessor;
|
import org.nl.acs.udw.UnifiedDataAccessor;
|
||||||
@@ -185,6 +186,7 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC
|
|||||||
Object his = accessor_value.getValue(itemId);
|
Object his = accessor_value.getValue(itemId);
|
||||||
if (!ObjectUtl.isEquals(itemState.getQuality(), QualityTypeValue.OPC_QUALITY_GOOD) && his != null) {
|
if (!ObjectUtl.isEquals(itemState.getQuality(), QualityTypeValue.OPC_QUALITY_GOOD) && his != null) {
|
||||||
log.warn("opc 值不健康 item: {}, 状态: {}", itemId, itemState.getQuality());
|
log.warn("opc 值不健康 item: {}, 状态: {}", itemId, itemState.getQuality());
|
||||||
|
valueAllNotNull = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!UnifiedDataAppService.isEquals(value, his)) {
|
if (!UnifiedDataAppService.isEquals(value, his)) {
|
||||||
@@ -192,10 +194,11 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC
|
|||||||
if (true) {
|
if (true) {
|
||||||
this.logItemChanged(itemId, accessor_value, value, itemDto);
|
this.logItemChanged(itemId, accessor_value, value, itemDto);
|
||||||
}
|
}
|
||||||
|
if(!ObjectUtil.isEmpty(value)){
|
||||||
accessor_value.setValue(itemId, value);
|
accessor_value.setValue(itemId, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
end = System.currentTimeMillis();
|
end = System.currentTimeMillis();
|
||||||
if (log.isTraceEnabled()) {
|
if (log.isTraceEnabled()) {
|
||||||
@@ -214,17 +217,17 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC
|
|||||||
random *= 1000;
|
random *= 1000;
|
||||||
if (this.all_null < 3) {
|
if (this.all_null < 3) {
|
||||||
if (log.isWarnEnabled()) {
|
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));
|
ThreadUtl.sleep((long) (5000 + random));
|
||||||
} else if (this.all_null < 6) {
|
} else if (this.all_null < 6) {
|
||||||
if (log.isWarnEnabled()) {
|
if (log.isWarnEnabled()) {
|
||||||
log.warn(tag + "重新创建server");
|
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) (30000 + random));
|
||||||
ThreadUtl.sleep((long) ((new Random()).nextInt(3) + 1) * 1000);
|
ThreadUtl.sleep((long) ((new Random()).nextInt(3) +1) * 1000);
|
||||||
break start;
|
break start;
|
||||||
} else if (this.all_null < 12) {
|
} else if (this.all_null < 12) {
|
||||||
if (log.isWarnEnabled()) {
|
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() {
|
private void runNew() {
|
||||||
Async20Access accessor = null;
|
Async20Access accessor = null;
|
||||||
@@ -369,64 +369,54 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC
|
|||||||
return days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds ";
|
return days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds ";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void changed(Item item, ItemState itemState) {
|
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) {
|
while (var8.hasNext()) {
|
||||||
// String itemId = item.getId();
|
String relate = (String) var8.next();
|
||||||
//
|
Object obj = accessor_value.getValue(relate);
|
||||||
// try {
|
sb.append("key:" + relate + "value:" + obj + ";");
|
||||||
// Object value = OpcUtl.getValue(item, itemState);
|
}
|
||||||
// UnifiedDataAccessor accessor_value = UnifiedDataAccessorFactory.getAccessor(OpcConfig.udw_opc_value_key);
|
log.warn("设备:{}信号{}变更从{}->{};信号快照:{}", new Object[]{itemDto.getDevice_code(), itemId, his, value, sb});
|
||||||
// accessor_value.setValue(itemId, value);
|
// this.businessLogger.setResource(itemDto.getDevice_code(), itemDto.getDevice_name()).log("信号{}变更从{}->{};信号快照:{}", new Object[]{itemId, his, value, sb});
|
||||||
//
|
} else {
|
||||||
//// if (value != null) {
|
log.warn("设备:{}信号{}变更从{}->{};信号快照:{}", new Object[]{itemDto.getDevice_code(), itemId, his, value});
|
||||||
//// if (log.isTraceEnabled()) {
|
// this.businessLogger.setResource(itemDto.getDevice_code(), itemDto.getDevice_name()).log("信号{}变更从{}->{}", new Object[]{itemId, his, value});
|
||||||
//// 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)));
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,8 +20,6 @@ import java.util.concurrent.Executors;
|
|||||||
public class OpcUtl {
|
public class OpcUtl {
|
||||||
private static int timeout = 1*60*1000;
|
private static int timeout = 1*60*1000;
|
||||||
private static String key = "rpc.socketTimeout";
|
private static String key = "rpc.socketTimeout";
|
||||||
public static int successNum=0;
|
|
||||||
public static int errNum=0;
|
|
||||||
|
|
||||||
static {
|
static {
|
||||||
checkTimeout();
|
checkTimeout();
|
||||||
@@ -37,18 +35,7 @@ public class OpcUtl {
|
|||||||
|
|
||||||
public static void writeValue(Group group, WriteRequest... requests) throws WDKException {
|
public static void writeValue(Group group, WriteRequest... requests) throws WDKException {
|
||||||
try {
|
try {
|
||||||
Map<Item, Integer> e=null;
|
Map<Item, Integer> e=group.write(requests);
|
||||||
try{
|
|
||||||
e=group.write(requests);
|
|
||||||
//group.write(requests);
|
|
||||||
}catch (Exception e1){
|
|
||||||
try{
|
|
||||||
e= group.write(requests);
|
|
||||||
}catch (Exception e2){
|
|
||||||
e= group.write(requests);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean is_success = true;
|
boolean is_success = true;
|
||||||
StringBuilder message = new StringBuilder();
|
StringBuilder message = new StringBuilder();
|
||||||
Iterator arg4 = e.keySet().iterator();
|
Iterator arg4 = e.keySet().iterator();
|
||||||
@@ -69,16 +56,12 @@ public class OpcUtl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!is_success) {
|
if (!is_success) {
|
||||||
// throw new BusinessException(message.toString());
|
|
||||||
System.out.println("下发信号失败:"+message.toString());
|
|
||||||
System.out.println("下发信号失败原因:"+message.toString());
|
|
||||||
log.info("下发信号失败:"+message.toString());
|
log.info("下发信号失败:"+message.toString());
|
||||||
throw new WDKException(message.toString());
|
throw new WDKException(message.toString());
|
||||||
}
|
}
|
||||||
} catch (JIException arg7) {
|
} catch (JIException arg7) {
|
||||||
log.info("下发信号失败:"+arg7.getMessage());
|
log.info("下发信号失败Exception:"+arg7.getMessage());
|
||||||
System.out.println("下发信号失败原因:"+arg7.getMessage());
|
throw new WDKException("下发信号失败Exception:"+arg7);
|
||||||
throw new WDKException(arg7);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,7 +77,6 @@ public class OpcUtl {
|
|||||||
Item item = group.addItem(value.getItem_code());
|
Item item = group.addItem(value.getItem_code());
|
||||||
ws.add(new WriteRequest(item, getVariant(value.getItem_value())));
|
ws.add(new WriteRequest(item, getVariant(value.getItem_value())));
|
||||||
}
|
}
|
||||||
|
|
||||||
writeValue(group, (WriteRequest[])ws.toArray(new WriteRequest[0]));
|
writeValue(group, (WriteRequest[])ws.toArray(new WriteRequest[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -222,16 +204,10 @@ public class OpcUtl {
|
|||||||
server = new Server(getConnection(host, clsid, user, password, domain),
|
server = new Server(getConnection(host, clsid, user, password, domain),
|
||||||
Executors.newSingleThreadScheduledExecutor());
|
Executors.newSingleThreadScheduledExecutor());
|
||||||
server.connect();
|
server.connect();
|
||||||
successNum++;
|
|
||||||
return server;
|
return server;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
errNum++;
|
|
||||||
// e.printStackTrace();
|
|
||||||
System.out.println("server error:"+e.getMessage());
|
System.out.println("server error:"+e.getMessage());
|
||||||
throw new WDKException(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