|
@ -15,194 +15,228 @@ import java.util.*; |
|
|
|
|
|
|
|
|
@Slf4j |
|
|
@Slf4j |
|
|
public class DeviceOpcProtocolRunable implements Runnable { |
|
|
public class DeviceOpcProtocolRunable implements Runnable { |
|
|
List<OpcItemDto> protocols; |
|
|
List<OpcItemDto> protocols; |
|
|
OpcServerManageDto OpcServer; |
|
|
OpcServerManageDto OpcServer; |
|
|
int error_num; |
|
|
int error_num; |
|
|
String message; |
|
|
String message; |
|
|
|
|
|
private Server server; |
|
|
public DeviceOpcProtocolRunable() { |
|
|
|
|
|
this.error_num = 0; |
|
|
public DeviceOpcProtocolRunable() { |
|
|
this.message = null; |
|
|
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 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() { |
|
|
while (it.hasNext()) { |
|
|
return this.OpcServer; |
|
|
OpcItemDto protocol = (OpcItemDto) it.next(); |
|
|
} |
|
|
String item = protocol.getItem_code(); |
|
|
|
|
|
itemsString.add(item); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public void setOpcServer(OpcServerManageDto opcServer) { |
|
|
Map<String, Item> itemsMap = new LinkedHashMap(); |
|
|
this.OpcServer = opcServer; |
|
|
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) { |
|
|
if (is_error) { |
|
|
Iterator var2 = this.protocols.iterator(); |
|
|
log.info("设备OPC数据同步配置异常"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
OpcItemDto dto; |
|
|
if (!OpcStartTag.is_run) { |
|
|
do { |
|
|
OpcStartTag.is_run = true; |
|
|
if (!var2.hasNext()) { |
|
|
} |
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
label97: |
|
|
public void run() { |
|
|
|
|
|
while (true) { |
|
|
while (true) { |
|
|
try { |
|
|
long begin = System.currentTimeMillis(); |
|
|
Server server = OpcServerUtl.getServerWithOutException(this.OpcServer.getOpc_host(), this.OpcServer.getCls_id(), this.OpcServer.getUser(), this.OpcServer.getPassword(), this.OpcServer.getDomain()); |
|
|
Map<Item, ItemState> itemStatus = null; |
|
|
Group group = server.addGroup(); |
|
|
try { |
|
|
List<String> itemsString = new ArrayList(); |
|
|
itemStatus = group.read(true, (Item[]) itemsMap.values().toArray(new Item[0])); |
|
|
Iterator it = this.protocols.iterator(); |
|
|
} catch (Exception e){ |
|
|
|
|
|
System.out.println("数据同步异常:"+ this.getOpcServer().getOpc_code()); |
|
|
while (it.hasNext()) { |
|
|
log.trace("数据同步异常:{}", this.getOpcServer().getOpc_code()); |
|
|
OpcItemDto protocol = (OpcItemDto) it.next(); |
|
|
//e.printStackTrace();
|
|
|
String item = protocol.getItem_code(); |
|
|
} |
|
|
itemsString.add(item); |
|
|
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(); |
|
|
time_out = true; |
|
|
boolean is_error = false; |
|
|
} else { |
|
|
StringBuilder err_message = new StringBuilder(); |
|
|
time_out = false; |
|
|
Iterator var7 = itemsString.iterator(); |
|
|
} |
|
|
|
|
|
|
|
|
while (var7.hasNext()) { |
|
|
if(ObjectUtil.isEmpty(itemStatus)) { |
|
|
String string = (String) var7.next(); |
|
|
System.out.println( tag + " :itemStatus is null"); |
|
|
|
|
|
log.warn(" {} 读取异常 : {} itemStatus is null", tag); |
|
|
try { |
|
|
} |
|
|
itemsMap.put(string, group.addItem(string)); |
|
|
|
|
|
log.trace("添加成功 {}", string); |
|
|
|
|
|
} catch (Exception var29) { |
|
|
Set<Item> items = itemStatus.keySet(); |
|
|
err_message.append(string + ":" + var29.getMessage()); |
|
|
Iterator var18 = items.iterator(); |
|
|
if (!is_error) { |
|
|
|
|
|
is_error = true; |
|
|
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); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
//线程名
|
|
|
this.server = null; |
|
|
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 { |
|
|
String error_message = "设备信息同步异常,"+var30; |
|
|
Thread.sleep((long) (OpcConfig.synchronized_exception_wait_second * 1000)); |
|
|
if (!StrUtil.equals(this.message, error_message)) { |
|
|
} catch (InterruptedException e) { |
|
|
log.warn("", var30); |
|
|
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; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static String formatDuring(long mss) { |
|
|
try { |
|
|
long days = mss / 86400000L; |
|
|
Thread.sleep((long) (OpcConfig.synchronized_exception_wait_second * 1000)); |
|
|
long hours = mss % 86400000L / 3600000L; |
|
|
} catch (InterruptedException e) { |
|
|
long minutes = mss % 3600000L / 60000L; |
|
|
e.printStackTrace(); |
|
|
long seconds = mss % 60000L / 1000L; |
|
|
log.warn("OPC 读取线程读取异常{} :", e); |
|
|
return days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds "; |
|
|
// 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 "; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|