This commit is contained in:
USER-20220102CG\noblelift
2023-02-01 22:08:25 +08:00

View File

@@ -15,204 +15,205 @@ 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;
this.server = 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()));
return dto;
}
@Override
public void run() {
while (true) {
try {
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数据同步配置异常," + err_message);
}
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) {
System.out.println("label97");
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);
}
dto = (OpcItemDto) var2.next();
} while (!StrUtil.equals(item, dto.getItem_code()));
time_out = true;
} else {
time_out = false;
}
return dto;
}
Set<Item> items = itemStatus.keySet();
Iterator var18 = items.iterator();
@Override
public void run() {
while (true) {
try {
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);
while (true) {
System.out.println("label98");
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;
}
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数据同步配置异常," + err_message);
}
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) {
if (this.server != null) {
try {
this.server.disconnect();
} catch (Exception var6) {
}
this.server = null;
continue label97;
}
String error_message = "设备信息同步异常";
if (!StrUtil.equals(this.message, error_message)) {
log.warn("", var30);
}
item = (Item) var18.next();
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)); // 如果两次的值相等,不走下面的代码
try {
Thread.sleep((long) (OpcConfig.synchronized_exception_wait_second * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
++this.error_num;
if (this.error_num > 3 && !StrUtil.equals(this.message, error_message)) {
log.info("设备同步通信异常");
this.message = error_message;
}
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});
}
System.out.println("设置值:【"+item.getId()+"】-->"+value);
// 设置值
accessor_value.setValue(item.getId(), value);
}
}
} catch (Exception var30) {
if (this.server != null) {
try {
this.server.disconnect();
} catch (Exception var6) {
}
this.server = null;
}
}
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 ";
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();
}
++this.error_num;
if (this.error_num > 3 && !StrUtil.equals(this.message, error_message)) {
log.info("设备同步通信异常");
this.message = error_message;
}
}
}
}
}