rev:提交aps相关代码
This commit is contained in:
@@ -14,394 +14,352 @@ import org.openscada.opc.lib.da.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
||||
@Slf4j
|
||||
public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerConnectionStateListener {
|
||||
List<OpcItemDto> protocols;
|
||||
OpcServerManageDto OpcServer;
|
||||
int error_num;
|
||||
String message;
|
||||
int maxResartNum;
|
||||
private Server server;
|
||||
private Group group;
|
||||
boolean flag = false;
|
||||
private int all_null;
|
||||
private Map<String, OpcItemDto> itemSearchCache;
|
||||
|
||||
public DeviceOpcProtocolRunable() {
|
||||
this.error_num = 0;
|
||||
this.all_null = 0;
|
||||
this.message = null;
|
||||
this.itemSearchCache = new HashMap();
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
private OpcItemDto getItem(String item) {
|
||||
OpcItemDto x = (OpcItemDto) this.itemSearchCache.get(item);
|
||||
if (x == null) {
|
||||
Iterator var3 = this.protocols.iterator();
|
||||
|
||||
while (var3.hasNext()) {
|
||||
OpcItemDto dto = (OpcItemDto) var3.next();
|
||||
if (StrUtil.equals(item, dto.getItem_code())) {
|
||||
x = dto;
|
||||
this.itemSearchCache.put(item, dto);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (OpcConfig.opc_item_read_using_callback) {
|
||||
this.runNew();
|
||||
} else {
|
||||
this.runOld();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void runOld() {
|
||||
OpcServerService opcServerService = SpringContextHolder.getBean(OpcServerService .class);
|
||||
while (true) {
|
||||
start:
|
||||
try {
|
||||
if (this.group != null) {
|
||||
group.clear();
|
||||
group.remove();
|
||||
log.trace("清理group...");
|
||||
}
|
||||
if (this.server != null) {
|
||||
server.disconnect();
|
||||
log.trace("清理server...");
|
||||
}
|
||||
// this.server = OpcServerUtl.getServerWithOutException(this.OpcServer.getOpc_host(), this.OpcServer.getCls_id(), this.OpcServer.getUser(), this.OpcServer.getPassword(), this.OpcServer.getDomain());
|
||||
// this.server.addStateListener(this);
|
||||
// group = this.server.addGroup();
|
||||
group =opcServerService.getServer(this.getOpcServer().getOpc_code());
|
||||
|
||||
List<String> itemsString = new ArrayList();
|
||||
Iterator var3 = this.protocols.iterator();
|
||||
|
||||
while (var3.hasNext()) {
|
||||
OpcItemDto protocol = (OpcItemDto) var3.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 var6 = itemsString.iterator();
|
||||
|
||||
while (var6.hasNext()) {
|
||||
String string = (String) var6.next();
|
||||
|
||||
try {
|
||||
Item item = group.addItem(string);
|
||||
itemsMap.put(string, item);
|
||||
log.trace("添加成功 {}", string);
|
||||
} catch (Exception var26) {
|
||||
err_message.append(string + ":" + var26.getMessage());
|
||||
if (!is_error) {
|
||||
is_error = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String tag;
|
||||
if (is_error) {
|
||||
tag = err_message.toString();
|
||||
log.warn("{}:{}", OpcConfig.resource_code, tag);
|
||||
}
|
||||
|
||||
if (!OpcStartTag.is_run) {
|
||||
OpcStartTag.is_run = true;
|
||||
}
|
||||
|
||||
tag = "";
|
||||
if (log.isWarnEnabled()) {
|
||||
tag = Thread.currentThread().getName();
|
||||
if (this.OpcServer != null) {
|
||||
tag = tag + this.getOpcGroupID();
|
||||
}
|
||||
}
|
||||
|
||||
UnifiedDataAccessor accessor_value = UnifiedDataAccessorFactory.getAccessor(OpcConfig.udw_opc_value_key);
|
||||
boolean time_out = false;
|
||||
|
||||
while (DeviceOpcSynchronizeAutoRun.isRun) {
|
||||
long begin = System.currentTimeMillis();
|
||||
if (log.isTraceEnabled()) {
|
||||
log.trace("{} 开始记时{}", tag, DateUtil.now());
|
||||
}
|
||||
|
||||
Map<Item, ItemState> itemStatus = group.read(true, (Item[]) itemsMap.values().toArray(new Item[0]));
|
||||
long end = System.currentTimeMillis();
|
||||
long duration = end - begin;
|
||||
if (log.isTraceEnabled()) {
|
||||
log.trace("{} 读取耗时:{}", tag, duration);
|
||||
}
|
||||
|
||||
if (duration > 1000L) {
|
||||
if (!time_out) {
|
||||
log.warn("{} 读取超时 : {}", tag, duration);
|
||||
}
|
||||
|
||||
time_out = true;
|
||||
} else {
|
||||
time_out = false;
|
||||
}
|
||||
|
||||
boolean valueAllNotNull = false;
|
||||
Set<Item> items = itemStatus.keySet();
|
||||
Iterator var18 = items.iterator();
|
||||
|
||||
while (var18.hasNext()) {
|
||||
Item item = (Item) var18.next();
|
||||
ItemState itemState = (ItemState) itemStatus.get(item);
|
||||
Object value = OpcUtl.getValue(item, itemState);
|
||||
if (value != null) {
|
||||
valueAllNotNull = true;
|
||||
}
|
||||
|
||||
String itemId = item.getId();
|
||||
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 = false;
|
||||
}
|
||||
|
||||
if (!UnifiedDataAppService.isEquals(value, his)) {
|
||||
OpcItemDto itemDto = this.getItem(itemId);
|
||||
if (true) {
|
||||
this.logItemChanged(itemId, accessor_value, value, itemDto);
|
||||
}
|
||||
if(!ObjectUtil.isEmpty(value)){
|
||||
accessor_value.setValue(itemId, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
end = System.currentTimeMillis();
|
||||
if (log.isTraceEnabled()) {
|
||||
log.trace("{}", itemsString);
|
||||
log.trace("{} 计算完成耗时{}", tag, end - begin);
|
||||
}
|
||||
|
||||
ThreadUtl.sleep((long) OpcConfig.synchronized_millisecond);
|
||||
if (this.error_num != 0) {
|
||||
this.error_num = 0;
|
||||
this.message = null;
|
||||
}
|
||||
|
||||
if (!valueAllNotNull) {
|
||||
int random = (new Random()).nextInt(10) + 1;
|
||||
random *= 1000;
|
||||
if (this.all_null < 3) {
|
||||
if (log.isWarnEnabled()) {
|
||||
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);
|
||||
}
|
||||
// ThreadUtl.sleep((long) (30000 + random));
|
||||
ThreadUtl.sleep((long) ((new Random()).nextInt(3) +1) * 10000);
|
||||
break start;
|
||||
} else if (this.all_null < 12) {
|
||||
if (log.isWarnEnabled()) {
|
||||
log.warn("{} 所有内容都为空, all_null:{} ,暂定{}ms", tag, all_null, '\uea60' + random);
|
||||
}
|
||||
|
||||
ThreadUtl.sleep((long) ('\uea60' + random));
|
||||
} else {
|
||||
if (log.isWarnEnabled()) {
|
||||
log.warn("{} 所有内容都为空, all_null:{} ,暂定{}ms", tag, all_null, 120000 + random);
|
||||
}
|
||||
|
||||
ThreadUtl.sleep((long) (120000 + random));
|
||||
}
|
||||
|
||||
++this.all_null;
|
||||
} else {
|
||||
this.all_null = 0;
|
||||
}
|
||||
// break start;
|
||||
|
||||
}
|
||||
|
||||
log.warn("opc线程停止。。。");
|
||||
return;
|
||||
} catch (Exception var27) {
|
||||
if (this.server != null) {
|
||||
try {
|
||||
this.server.disconnect();
|
||||
} catch (Exception var25) {
|
||||
}
|
||||
}
|
||||
|
||||
this.server = null;
|
||||
if (!DeviceOpcSynchronizeAutoRun.isRun) {
|
||||
log.warn("opc线程停止2。。。");
|
||||
return;
|
||||
}
|
||||
|
||||
String error_message = "设备信息同步异常";
|
||||
if (!StrUtil.equals(this.message, error_message)) {
|
||||
log.warn(error_message, var27);
|
||||
}
|
||||
|
||||
ThreadUtl.sleep((long) (OpcConfig.synchronized_exception_wait_second * 1000));
|
||||
++this.error_num;
|
||||
if (this.error_num > 3 && !StrUtil.equals(this.message, error_message)) {
|
||||
this.message = error_message;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void runNew() {
|
||||
Async20Access accessor = null;
|
||||
|
||||
while (true) {
|
||||
String opcGroupId = this.getOpcGroupID();
|
||||
|
||||
try {
|
||||
if (this.server == null) {
|
||||
this.server = OpcServerUtl.getServerWithOutException(this.OpcServer.getOpc_host(), this.OpcServer.getCls_id(), this.OpcServer.getUser(), this.OpcServer.getPassword(), this.OpcServer.getDomain());
|
||||
this.server.addStateListener(this);
|
||||
accessor = new Async20Access(this.server, OpcConfig.synchronized_millisecond, true);
|
||||
Iterator var9 = this.protocols.iterator();
|
||||
|
||||
while (var9.hasNext()) {
|
||||
OpcItemDto protocol = (OpcItemDto) var9.next();
|
||||
String itemId = protocol.getItem_code();
|
||||
accessor.addItem(itemId, this);
|
||||
}
|
||||
|
||||
accessor.bind();
|
||||
log.info("Async20Access bind {}", opcGroupId);
|
||||
}
|
||||
|
||||
Thread.sleep((long) (OpcConfig.synchronized_exception_wait_second * 1000));
|
||||
} catch (Exception var8) {
|
||||
if (accessor != null) {
|
||||
try {
|
||||
log.warn("Async20Access unbind {}", opcGroupId);
|
||||
accessor.unbind();
|
||||
} catch (Exception var7) {
|
||||
var7.printStackTrace();
|
||||
}
|
||||
|
||||
accessor = null;
|
||||
}
|
||||
|
||||
if (this.server != null) {
|
||||
try {
|
||||
this.server.disconnect();
|
||||
} catch (Exception var6) {
|
||||
}
|
||||
public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerConnectionStateListener{
|
||||
List<OpcItemDto> protocols;
|
||||
OpcServerManageDto OpcServer;
|
||||
int error_num;
|
||||
String message;
|
||||
int maxResartNum;
|
||||
private Server server;
|
||||
private Group group;
|
||||
boolean flag = false;
|
||||
private int all_null;
|
||||
private Map<String,OpcItemDto> itemSearchCache;
|
||||
|
||||
public DeviceOpcProtocolRunable() {
|
||||
this.error_num = 0;
|
||||
this.all_null = 0;
|
||||
this.message = null;
|
||||
this.itemSearchCache = new HashMap();
|
||||
this.server = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (var8 instanceof InterruptedException) {
|
||||
log.warn("OPC 同步线程(%s)被中断", opcGroupId);
|
||||
return;
|
||||
}
|
||||
public List<OpcItemDto> getProtocols() {
|
||||
return this.protocols;
|
||||
}
|
||||
|
||||
log.warn("设备信息同步异常", var8);
|
||||
ThreadUtl.sleep((long) (OpcConfig.synchronized_exception_wait_second * 1000));
|
||||
String error_message = var8.getMessage();
|
||||
if (error_message == null) {
|
||||
error_message = var8.toString();
|
||||
}
|
||||
public void setProtocols(List<OpcItemDto> protocols) {
|
||||
this.protocols = protocols;
|
||||
}
|
||||
|
||||
++this.error_num;
|
||||
if (this.error_num > 3 && !StrUtil.equals(this.message, error_message)) {
|
||||
this.message = error_message;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public OpcServerManageDto getOpcServer() {
|
||||
return this.OpcServer;
|
||||
}
|
||||
|
||||
public void setOpcServer(OpcServerManageDto opcServer) {
|
||||
this.OpcServer = opcServer;
|
||||
}
|
||||
|
||||
public void connectionStateChanged(boolean connected) {
|
||||
if (!connected) {
|
||||
this.server = null;
|
||||
}
|
||||
private OpcItemDto getItem(String item) {
|
||||
OpcItemDto x = (OpcItemDto) this.itemSearchCache.get(item);
|
||||
if(x == null) {
|
||||
Iterator var3 = this.protocols.iterator();
|
||||
while(var3.hasNext()) {
|
||||
OpcItemDto dto = (OpcItemDto) var3.next();
|
||||
if(StrUtil.equals(item, dto.getItem_code())) {
|
||||
x = dto;
|
||||
this.itemSearchCache.put(item, dto);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
log.warn("opc server {} {}", this.getOpcGroupID(), connected ? "connected" : "disconnected");
|
||||
}
|
||||
@Override
|
||||
public void run() {
|
||||
if(OpcConfig.opc_item_read_using_callback) {
|
||||
this.runNew();
|
||||
}
|
||||
else{
|
||||
this.runOld();
|
||||
}
|
||||
}
|
||||
|
||||
private String getOpcGroupID() {
|
||||
String var10000 = this.OpcServer.getOpc_code();
|
||||
return var10000 + "(" + this.protocols.size() + " items)";
|
||||
}
|
||||
private void runOld() {
|
||||
OpcServerService opcServerService = SpringContextHolder.getBean(OpcServerService.class);
|
||||
while(true) {
|
||||
start:
|
||||
try {
|
||||
if(this.group != null) {
|
||||
group.clear();
|
||||
group.remove();
|
||||
log.trace("清理group...");
|
||||
}
|
||||
if(this.server != null) {
|
||||
server.disconnect();
|
||||
log.trace("清理server...");
|
||||
}
|
||||
// this.server = OpcServerUtl.getServerWithOutException(this.OpcServer.getOpc_host(), this.OpcServer.getCls_id(), this.OpcServer.getUser(), this.OpcServer.getPassword(), this.OpcServer.getDomain());
|
||||
// this.server.addStateListener(this);
|
||||
// group = this.server.addGroup();
|
||||
group = opcServerService.getServer(this.getOpcServer().getOpc_code());
|
||||
List<String> itemsString = new ArrayList();
|
||||
Iterator var3 = this.protocols.iterator();
|
||||
while(var3.hasNext()) {
|
||||
OpcItemDto protocol = (OpcItemDto) var3.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 var6 = itemsString.iterator();
|
||||
while(var6.hasNext()) {
|
||||
String string = (String) var6.next();
|
||||
try {
|
||||
Item item = group.addItem(string);
|
||||
itemsMap.put(string, item);
|
||||
log.trace("添加成功 {}", string);
|
||||
}
|
||||
catch(Exception var26) {
|
||||
err_message.append(string + ":" + var26.getMessage());
|
||||
if(!is_error) {
|
||||
is_error = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
String tag;
|
||||
if(is_error) {
|
||||
tag = err_message.toString();
|
||||
log.warn("{}:{}", OpcConfig.resource_code, tag);
|
||||
}
|
||||
if(!OpcStartTag.is_run) {
|
||||
OpcStartTag.is_run = true;
|
||||
}
|
||||
tag = "";
|
||||
if(log.isWarnEnabled()) {
|
||||
tag = Thread.currentThread().getName();
|
||||
if(this.OpcServer != null) {
|
||||
tag = tag + this.getOpcGroupID();
|
||||
}
|
||||
}
|
||||
UnifiedDataAccessor accessor_value = UnifiedDataAccessorFactory.getAccessor(OpcConfig.udw_opc_value_key);
|
||||
boolean time_out = false;
|
||||
while(DeviceOpcSynchronizeAutoRun.isRun) {
|
||||
long begin = System.currentTimeMillis();
|
||||
if(log.isTraceEnabled()) {
|
||||
log.trace("{} 开始记时{}", tag, DateUtil.now());
|
||||
}
|
||||
Map<Item,ItemState> itemStatus = group.read(true, (Item[]) itemsMap.values().toArray(new Item[0]));
|
||||
long end = System.currentTimeMillis();
|
||||
long duration = end - begin;
|
||||
if(log.isTraceEnabled()) {
|
||||
log.trace("{} 读取耗时:{}", tag, duration);
|
||||
}
|
||||
if(duration > 1000L) {
|
||||
if(!time_out) {
|
||||
log.warn("{} 读取超时 : {}", tag, duration);
|
||||
}
|
||||
time_out = true;
|
||||
}
|
||||
else{
|
||||
time_out = false;
|
||||
}
|
||||
boolean valueAllNotNull = false;
|
||||
Set<Item> items = itemStatus.keySet();
|
||||
Iterator var18 = items.iterator();
|
||||
while(var18.hasNext()) {
|
||||
Item item = (Item) var18.next();
|
||||
ItemState itemState = (ItemState) itemStatus.get(item);
|
||||
Object value = OpcUtl.getValue(item, itemState);
|
||||
if(value != null) {
|
||||
valueAllNotNull = true;
|
||||
}
|
||||
String itemId = item.getId();
|
||||
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 = false;
|
||||
}
|
||||
if(!UnifiedDataAppService.isEquals(value, his)) {
|
||||
OpcItemDto itemDto = this.getItem(itemId);
|
||||
if(true) {
|
||||
this.logItemChanged(itemId, accessor_value, value, itemDto);
|
||||
}
|
||||
if(!ObjectUtil.isEmpty(value)) {
|
||||
accessor_value.setValue(itemId, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
end = System.currentTimeMillis();
|
||||
if(log.isTraceEnabled()) {
|
||||
log.trace("{}", itemsString);
|
||||
log.trace("{} 计算完成耗时{}", tag, end - begin);
|
||||
}
|
||||
ThreadUtl.sleep((long) OpcConfig.synchronized_millisecond);
|
||||
if(this.error_num != 0) {
|
||||
this.error_num = 0;
|
||||
this.message = null;
|
||||
}
|
||||
if(!valueAllNotNull) {
|
||||
int random = (new Random()).nextInt(10) + 1;
|
||||
random *= 1000;
|
||||
if(this.all_null < 3) {
|
||||
if(log.isWarnEnabled()) {
|
||||
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);
|
||||
}
|
||||
// ThreadUtl.sleep((long) (30000 + random));
|
||||
ThreadUtl.sleep((long) ((new Random()).nextInt(3) + 1) * 10000);
|
||||
break start;
|
||||
}
|
||||
else if(this.all_null < 12) {
|
||||
if(log.isWarnEnabled()) {
|
||||
log.warn("{} 所有内容都为空, all_null:{} ,暂定{}ms", tag, all_null, '\uea60' + random);
|
||||
}
|
||||
ThreadUtl.sleep((long) ('\uea60' + random));
|
||||
}
|
||||
else{
|
||||
if(log.isWarnEnabled()) {
|
||||
log.warn("{} 所有内容都为空, all_null:{} ,暂定{}ms", tag, all_null, 120000 + random);
|
||||
}
|
||||
ThreadUtl.sleep((long) (120000 + random));
|
||||
}
|
||||
++this.all_null;
|
||||
}
|
||||
else{
|
||||
this.all_null = 0;
|
||||
}
|
||||
// break start;
|
||||
}
|
||||
log.warn("opc线程停止。。。");
|
||||
return;
|
||||
}
|
||||
catch(Exception var27) {
|
||||
if(this.server != null) {
|
||||
try {
|
||||
this.server.disconnect();
|
||||
}
|
||||
catch(Exception var25) {
|
||||
}
|
||||
}
|
||||
this.server = null;
|
||||
if(!DeviceOpcSynchronizeAutoRun.isRun) {
|
||||
log.warn("opc线程停止2。。。");
|
||||
return;
|
||||
}
|
||||
String error_message = "设备信息同步异常";
|
||||
if(!StrUtil.equals(this.message, error_message)) {
|
||||
log.warn(error_message, var27);
|
||||
}
|
||||
ThreadUtl.sleep((long) (OpcConfig.synchronized_exception_wait_second * 1000));
|
||||
++this.error_num;
|
||||
if(this.error_num > 3 && !StrUtil.equals(this.message, error_message)) {
|
||||
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 ";
|
||||
}
|
||||
private void runNew() {
|
||||
Async20Access accessor = null;
|
||||
while(true) {
|
||||
String opcGroupId = this.getOpcGroupID();
|
||||
try {
|
||||
if(this.server == null) {
|
||||
this.server = OpcServerUtl.getServerWithOutException(this.OpcServer.getOpc_host(), this.OpcServer.getCls_id(), this.OpcServer.getUser(), this.OpcServer.getPassword(), this.OpcServer.getDomain());
|
||||
this.server.addStateListener(this);
|
||||
accessor = new Async20Access(this.server, OpcConfig.synchronized_millisecond, true);
|
||||
Iterator var9 = this.protocols.iterator();
|
||||
while(var9.hasNext()) {
|
||||
OpcItemDto protocol = (OpcItemDto) var9.next();
|
||||
String itemId = protocol.getItem_code();
|
||||
accessor.addItem(itemId, this);
|
||||
}
|
||||
accessor.bind();
|
||||
log.info("Async20Access bind {}", opcGroupId);
|
||||
}
|
||||
Thread.sleep((long) (OpcConfig.synchronized_exception_wait_second * 1000));
|
||||
}
|
||||
catch(Exception var8) {
|
||||
if(accessor != null) {
|
||||
try {
|
||||
log.warn("Async20Access unbind {}", opcGroupId);
|
||||
accessor.unbind();
|
||||
}
|
||||
catch(Exception var7) {
|
||||
var7.printStackTrace();
|
||||
}
|
||||
accessor = null;
|
||||
}
|
||||
if(this.server != null) {
|
||||
try {
|
||||
this.server.disconnect();
|
||||
}
|
||||
catch(Exception var6) {
|
||||
}
|
||||
this.server = null;
|
||||
}
|
||||
if(var8 instanceof InterruptedException) {
|
||||
log.warn("OPC 同步线程(%s)被中断", opcGroupId);
|
||||
return;
|
||||
}
|
||||
log.warn("设备信息同步异常", var8);
|
||||
ThreadUtl.sleep((long) (OpcConfig.synchronized_exception_wait_second * 1000));
|
||||
String error_message = var8.getMessage();
|
||||
if(error_message == null) {
|
||||
error_message = var8.toString();
|
||||
}
|
||||
++this.error_num;
|
||||
if(this.error_num > 3 && !StrUtil.equals(this.message, error_message)) {
|
||||
this.message = error_message;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void connectionStateChanged(boolean connected) {
|
||||
if(!connected) {
|
||||
this.server = null;
|
||||
}
|
||||
log.warn("opc server {} {}", this.getOpcGroupID(), connected ? "connected" : "disconnected");
|
||||
}
|
||||
|
||||
public void changed(Item item, ItemState itemState) {
|
||||
String itemId = item.getId();
|
||||
private String getOpcGroupID() {
|
||||
String var10000 = this.OpcServer.getOpc_code();
|
||||
return var10000 + "(" + this.protocols.size() + " items)";
|
||||
}
|
||||
|
||||
try {
|
||||
Object value = OpcUtl.getValue(item, itemState);
|
||||
UnifiedDataAccessor accessor_value = UnifiedDataAccessorFactory.getAccessor(OpcConfig.udw_opc_value_key);
|
||||
accessor_value.setValue(itemId, value);
|
||||
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 ";
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
}
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user