|
|
@ -13,163 +13,163 @@ import java.util.concurrent.ConcurrentHashMap; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
public class OpcEntity { |
|
|
|
|
|
|
|
OpcServerService opcServerService = SpringContextHolder.getBean(OpcServerService.class); |
|
|
|
private final UnifiedDataAccessor udw; |
|
|
|
private Map<String, List<Item>> items; |
|
|
|
private Map<String, List<String>> someFailDevices; |
|
|
|
private String opcCode; |
|
|
|
|
|
|
|
public OpcEntity(String opcCode) { |
|
|
|
this.udw = UnifiedDataAccessorFactory.getAccessor(OpcConfig.udw_opc_value_key); |
|
|
|
this.items = new ConcurrentHashMap(); |
|
|
|
this.someFailDevices = new ConcurrentHashMap(); |
|
|
|
this.opcCode = opcCode; |
|
|
|
} |
|
|
|
|
|
|
|
public void reload(List<List<OpcItemDto>> opcItemDtos) { |
|
|
|
Map<String, List<String>> itemCodes = new ConcurrentHashMap(); |
|
|
|
(opcItemDtos.stream().flatMap(Collection::stream).collect(Collectors.groupingBy(OpcItemDto::getDevice_code))).forEach((deviceCodes, opcItemDtoList) -> { |
|
|
|
itemCodes.put(deviceCodes, opcItemDtoList.stream().map(OpcItemDto::getItem_code).collect(Collectors.toList())); |
|
|
|
}); |
|
|
|
DeviceOpcSynchronizeAutoRun.itemCodeOpcItemDtoMapping.putAll((Map)opcItemDtos.stream().flatMap(Collection::stream).collect(Collectors.toMap(OpcItemDto::getItem_code, (obj) -> { |
|
|
|
return obj; |
|
|
|
},(k, v) -> k))); |
|
|
|
if (this.items.size() == 0) { |
|
|
|
itemCodes.values().stream().flatMap(Collection::stream).forEach((key) -> { |
|
|
|
this.udw.setValue(key, (Object)null); |
|
|
|
}); |
|
|
|
this.addItemsIntoGroup(itemCodes); |
|
|
|
} else { |
|
|
|
if (this.someFailDevices.size() > 0) { |
|
|
|
this.reAddDevices(); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void reAddDevices() { |
|
|
|
Map<String, List<Item>> addItems = new ConcurrentHashMap(); |
|
|
|
StringBuilder err_message = new StringBuilder(); |
|
|
|
this.someFailDevices.forEach((deviceCode, itemCodesList) -> { |
|
|
|
itemCodesList.forEach((itemCode) -> { |
|
|
|
try { |
|
|
|
Group group = DeviceOpcSynchronizeAutoRun.getGroup(this.opcCode); |
|
|
|
((List)addItems.computeIfAbsent(deviceCode, (key) -> { |
|
|
|
return new ArrayList(); |
|
|
|
})).add(group.addItem(itemCode)); |
|
|
|
} catch (Exception var6) { |
|
|
|
err_message.append(itemCode).append(" 添加失败; "); |
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
List<Item> deviceItems = (List)addItems.get(deviceCode); |
|
|
|
if (deviceItems != null && deviceItems.size() == itemCodesList.size()) { |
|
|
|
this.someFailDevices.remove(deviceCode); |
|
|
|
} else if (itemCodesList.size() == 0) { |
|
|
|
addItems.remove(deviceCode); |
|
|
|
} else { |
|
|
|
assert deviceItems != null; |
|
|
|
|
|
|
|
((List)this.someFailDevices.get(deviceCode)).removeAll(deviceItems); |
|
|
|
} |
|
|
|
|
|
|
|
synchronized(this.opcCode.intern()) { |
|
|
|
this.items.putAll(addItems); |
|
|
|
} |
|
|
|
|
|
|
|
if (err_message.length() > 0) { |
|
|
|
String errMsg = err_message.toString(); |
|
|
|
//this.log.warn("{}:{}", com.wxzd.wcs.opc.OpcConfig.resource_code, errMsg);
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
private void addItemsIntoGroup(Map<String, List<String>> itemCodes) { |
|
|
|
try { |
|
|
|
Group group = DeviceOpcSynchronizeAutoRun.getGroup(this.opcCode); |
|
|
|
StringBuilder err_message = new StringBuilder(); |
|
|
|
Map<String, List<Item>> items = new ConcurrentHashMap(); |
|
|
|
itemCodes.forEach((deviceCode, itemCodesList) -> { |
|
|
|
itemCodesList.forEach((itemCode) -> { |
|
|
|
try { |
|
|
|
((List)items.computeIfAbsent(deviceCode, (key) -> { |
|
|
|
return new ArrayList(); |
|
|
|
})).add(group.addItem(itemCode)); |
|
|
|
} catch (Exception var7) { |
|
|
|
((List)this.someFailDevices.computeIfAbsent(deviceCode, (key) -> { |
|
|
|
return new ArrayList(); |
|
|
|
})).add(itemCode); |
|
|
|
this.udw.setValue(itemCode, (Object)null); |
|
|
|
err_message.append(itemCode).append(" 添加失败; "); |
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
List<Item> deviceItems = (List)items.get(deviceCode); |
|
|
|
if (deviceItems != null && deviceItems.size() != itemCodesList.size()) { |
|
|
|
items.remove(deviceCode); |
|
|
|
this.someFailDevices.put(deviceCode, itemCodesList); |
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
synchronized(this.opcCode.intern()) { |
|
|
|
this.items = items; |
|
|
|
} |
|
|
|
|
|
|
|
if (err_message.length() > 0) { |
|
|
|
String errMsg = err_message.toString(); |
|
|
|
// this.log.warn("{}:{}", OpcConfig.resource_code, errMsg);
|
|
|
|
} |
|
|
|
} catch (Exception var8) { |
|
|
|
var8.printStackTrace(); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public void cleanUdwCache() { |
|
|
|
this.items.values().stream().flatMap(Collection::stream).map(Item::getId).forEach((key) -> { |
|
|
|
this.udw.setValue(key, (Object)null); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
public Map<Item, ItemState> readAll() throws Exception { |
|
|
|
return opcServerService.getServer(this.opcCode).read(true, (Item[])this.items.values().stream().flatMap(Collection::stream).toArray((x$0) -> { |
|
|
|
return new Item[x$0]; |
|
|
|
})); |
|
|
|
} |
|
|
|
|
|
|
|
public Map<Item, ItemState> readDividually() { |
|
|
|
Map<Item, ItemState> result = new HashMap(); |
|
|
|
CompletableFuture[] futures = (CompletableFuture[])this.items.entrySet().stream().map((entry) -> { |
|
|
|
return CompletableFuture.runAsync(() -> { |
|
|
|
try { |
|
|
|
Group group = DeviceOpcSynchronizeAutoRun.getGroup(this.opcCode); |
|
|
|
result.putAll(group.read(true, (Item[])((List)entry.getValue()).toArray(new Item[0]))); |
|
|
|
} catch (Exception var5) { |
|
|
|
String deviceCode = (String)entry.getKey(); |
|
|
|
// to do
|
|
|
|
// this.someFailDevices.put(deviceCode, ((List)entry.getValue()).stream().map(Item::getId).collect(Collectors.toList()));
|
|
|
|
this.items.remove(deviceCode); |
|
|
|
} |
|
|
|
|
|
|
|
}, DeviceOpcSynchronizeAutoRun.executorService); |
|
|
|
}).toArray((x$0) -> { |
|
|
|
return new CompletableFuture[x$0]; |
|
|
|
}); |
|
|
|
CompletableFuture.allOf(futures).join(); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public UnifiedDataAccessor getUdw() { |
|
|
|
return this.udw; |
|
|
|
} |
|
|
|
|
|
|
|
public Map<String, List<Item>> getItems() { |
|
|
|
return this.items; |
|
|
|
} |
|
|
|
|
|
|
|
public String getOpcCode() { |
|
|
|
return this.opcCode; |
|
|
|
} |
|
|
|
// OpcServerService opcServerService = SpringContextHolder.getBean(OpcServerService.class);
|
|
|
|
// private final UnifiedDataAccessor udw;
|
|
|
|
// private Map<String, List<Item>> items;
|
|
|
|
// private Map<String, List<String>> someFailDevices;
|
|
|
|
// private String opcCode;
|
|
|
|
//
|
|
|
|
// public OpcEntity(String opcCode) {
|
|
|
|
// this.udw = UnifiedDataAccessorFactory.getAccessor(OpcConfig.udw_opc_value_key);
|
|
|
|
// this.items = new ConcurrentHashMap();
|
|
|
|
// this.someFailDevices = new ConcurrentHashMap();
|
|
|
|
// this.opcCode = opcCode;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// public void reload(List<List<OpcItemDto>> opcItemDtos) {
|
|
|
|
// Map<String, List<String>> itemCodes = new ConcurrentHashMap();
|
|
|
|
// (opcItemDtos.stream().flatMap(Collection::stream).collect(Collectors.groupingBy(OpcItemDto::getDevice_code))).forEach((deviceCodes, opcItemDtoList) -> {
|
|
|
|
// itemCodes.put(deviceCodes, opcItemDtoList.stream().map(OpcItemDto::getItem_code).collect(Collectors.toList()));
|
|
|
|
// });
|
|
|
|
// DeviceOpcSynchronizeAutoRun.itemCodeOpcItemDtoMapping.putAll((Map)opcItemDtos.stream().flatMap(Collection::stream).collect(Collectors.toMap(OpcItemDto::getItem_code, (obj) -> {
|
|
|
|
// return obj;
|
|
|
|
// },(k, v) -> k)));
|
|
|
|
// if (this.items.size() == 0) {
|
|
|
|
// itemCodes.values().stream().flatMap(Collection::stream).forEach((key) -> {
|
|
|
|
// this.udw.setValue(key, (Object)null);
|
|
|
|
// });
|
|
|
|
// this.addItemsIntoGroup(itemCodes);
|
|
|
|
// } else {
|
|
|
|
// if (this.someFailDevices.size() > 0) {
|
|
|
|
// this.reAddDevices();
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// private void reAddDevices() {
|
|
|
|
// Map<String, List<Item>> addItems = new ConcurrentHashMap();
|
|
|
|
// StringBuilder err_message = new StringBuilder();
|
|
|
|
// this.someFailDevices.forEach((deviceCode, itemCodesList) -> {
|
|
|
|
// itemCodesList.forEach((itemCode) -> {
|
|
|
|
// try {
|
|
|
|
// Group group = DeviceOpcSynchronizeAutoRun.getGroup(this.opcCode);
|
|
|
|
// ((List)addItems.computeIfAbsent(deviceCode, (key) -> {
|
|
|
|
// return new ArrayList();
|
|
|
|
// })).add(group.addItem(itemCode));
|
|
|
|
// } catch (Exception var6) {
|
|
|
|
// err_message.append(itemCode).append(" 添加失败; ");
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// });
|
|
|
|
// List<Item> deviceItems = (List)addItems.get(deviceCode);
|
|
|
|
// if (deviceItems != null && deviceItems.size() == itemCodesList.size()) {
|
|
|
|
// this.someFailDevices.remove(deviceCode);
|
|
|
|
// } else if (itemCodesList.size() == 0) {
|
|
|
|
// addItems.remove(deviceCode);
|
|
|
|
// } else {
|
|
|
|
// assert deviceItems != null;
|
|
|
|
//
|
|
|
|
// ((List)this.someFailDevices.get(deviceCode)).removeAll(deviceItems);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// synchronized(this.opcCode.intern()) {
|
|
|
|
// this.items.putAll(addItems);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// if (err_message.length() > 0) {
|
|
|
|
// String errMsg = err_message.toString();
|
|
|
|
// //this.log.warn("{}:{}", com.wxzd.wcs.opc.OpcConfig.resource_code, errMsg);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// private void addItemsIntoGroup(Map<String, List<String>> itemCodes) {
|
|
|
|
// try {
|
|
|
|
// Group group = DeviceOpcSynchronizeAutoRun.getGroup(this.opcCode);
|
|
|
|
// StringBuilder err_message = new StringBuilder();
|
|
|
|
// Map<String, List<Item>> items = new ConcurrentHashMap();
|
|
|
|
// itemCodes.forEach((deviceCode, itemCodesList) -> {
|
|
|
|
// itemCodesList.forEach((itemCode) -> {
|
|
|
|
// try {
|
|
|
|
// ((List)items.computeIfAbsent(deviceCode, (key) -> {
|
|
|
|
// return new ArrayList();
|
|
|
|
// })).add(group.addItem(itemCode));
|
|
|
|
// } catch (Exception var7) {
|
|
|
|
// ((List)this.someFailDevices.computeIfAbsent(deviceCode, (key) -> {
|
|
|
|
// return new ArrayList();
|
|
|
|
// })).add(itemCode);
|
|
|
|
// this.udw.setValue(itemCode, (Object)null);
|
|
|
|
// err_message.append(itemCode).append(" 添加失败; ");
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// });
|
|
|
|
// List<Item> deviceItems = (List)items.get(deviceCode);
|
|
|
|
// if (deviceItems != null && deviceItems.size() != itemCodesList.size()) {
|
|
|
|
// items.remove(deviceCode);
|
|
|
|
// this.someFailDevices.put(deviceCode, itemCodesList);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// });
|
|
|
|
// synchronized(this.opcCode.intern()) {
|
|
|
|
// this.items = items;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// if (err_message.length() > 0) {
|
|
|
|
// String errMsg = err_message.toString();
|
|
|
|
//// this.log.warn("{}:{}", OpcConfig.resource_code, errMsg);
|
|
|
|
// }
|
|
|
|
// } catch (Exception var8) {
|
|
|
|
// var8.printStackTrace();
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// public void cleanUdwCache() {
|
|
|
|
// this.items.values().stream().flatMap(Collection::stream).map(Item::getId).forEach((key) -> {
|
|
|
|
// this.udw.setValue(key, (Object)null);
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// public Map<Item, ItemState> readAll() throws Exception {
|
|
|
|
// return opcServerService.getServer(this.opcCode).read(true, (Item[])this.items.values().stream().flatMap(Collection::stream).toArray((x$0) -> {
|
|
|
|
// return new Item[x$0];
|
|
|
|
// }));
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// public Map<Item, ItemState> readDividually() {
|
|
|
|
// Map<Item, ItemState> result = new HashMap();
|
|
|
|
// CompletableFuture[] futures = (CompletableFuture[])this.items.entrySet().stream().map((entry) -> {
|
|
|
|
// return CompletableFuture.runAsync(() -> {
|
|
|
|
// try {
|
|
|
|
// Group group = DeviceOpcSynchronizeAutoRun.getGroup(this.opcCode);
|
|
|
|
// result.putAll(group.read(true, (Item[])((List)entry.getValue()).toArray(new Item[0])));
|
|
|
|
// } catch (Exception var5) {
|
|
|
|
// String deviceCode = (String)entry.getKey();
|
|
|
|
// // to do
|
|
|
|
//// this.someFailDevices.put(deviceCode, ((List)entry.getValue()).stream().map(Item::getId).collect(Collectors.toList()));
|
|
|
|
// this.items.remove(deviceCode);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// }, DeviceOpcSynchronizeAutoRun.executorService);
|
|
|
|
// }).toArray((x$0) -> {
|
|
|
|
// return new CompletableFuture[x$0];
|
|
|
|
// });
|
|
|
|
// CompletableFuture.allOf(futures).join();
|
|
|
|
// return result;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// public UnifiedDataAccessor getUdw() {
|
|
|
|
// return this.udw;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// public Map<String, List<Item>> getItems() {
|
|
|
|
// return this.items;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// public String getOpcCode() {
|
|
|
|
// return this.opcCode;
|
|
|
|
// }
|
|
|
|
} |
|
|
|