|
|
|
@@ -27,12 +27,8 @@ import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import java.time.Duration;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
@@ -49,7 +45,9 @@ public class DeviceManager implements InitializingBean {
|
|
|
|
|
|
|
|
|
|
private static final Map<Long, AtomicInteger> ROUND_MAP = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
private Map<Long, List<DeviceContext>> deviceMap = new HashMap<>();
|
|
|
|
|
// private Map<Long, List<DeviceContext>> deviceMap = new HashMap<>();
|
|
|
|
|
private Map<Long, List<Long>> serviceDeviceMap;
|
|
|
|
|
private final Map<Long, DeviceContext> usedDeviceMap = new ConcurrentHashMap<>();
|
|
|
|
|
|
|
|
|
|
private boolean enableSoftDevice = false;
|
|
|
|
|
private TMKContext softContext;
|
|
|
|
@@ -79,14 +77,20 @@ public class DeviceManager implements InitializingBean {
|
|
|
|
|
if (atomicInteger.get() > Integer.MAX_VALUE - 10000) {
|
|
|
|
|
atomicInteger.set(1);
|
|
|
|
|
}
|
|
|
|
|
List<DeviceContext> contexts = new ArrayList<>();
|
|
|
|
|
for (Long serviceId : serviceIds) {
|
|
|
|
|
Optional.ofNullable(deviceMap.get(serviceId))
|
|
|
|
|
.ifPresent(contexts::addAll);
|
|
|
|
|
}
|
|
|
|
|
List<DeviceContext> contexts = serviceDeviceMap.entrySet().stream()
|
|
|
|
|
.filter(it -> serviceIds.contains(it.getKey()))
|
|
|
|
|
.map(Map.Entry::getValue)
|
|
|
|
|
.flatMap(Collection::stream)
|
|
|
|
|
.map(usedDeviceMap::get)
|
|
|
|
|
.filter(Objects::nonNull)
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
DeviceContext device = getNextDevice(contexts, atomicInteger.getAndIncrement());
|
|
|
|
|
TMKContext tmkContext;
|
|
|
|
|
if (device == null) {
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
|
|
|
|
|
log.debug("request: {}, use soft device 1", request.getRequestURI());
|
|
|
|
|
}
|
|
|
|
|
tmkContext = getSoftContext();
|
|
|
|
|
} else {
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
@@ -98,6 +102,11 @@ public class DeviceManager implements InitializingBean {
|
|
|
|
|
tmkContext = pool.borrowObject(2000);
|
|
|
|
|
tmkContext.setPool(pool);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
usedDeviceMap.remove(device.getId());
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
|
|
|
|
|
log.debug("request: {}, use soft device 2", request.getRequestURI());
|
|
|
|
|
}
|
|
|
|
|
tmkContext = getSoftContext();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@@ -146,7 +155,6 @@ public class DeviceManager implements InitializingBean {
|
|
|
|
|
return null; // 理论上不会到这里
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void syncDevice() {
|
|
|
|
|
log.debug(">>>>>>>>>>>>>>> start sync device <<<<<<<<<<<<<<<");
|
|
|
|
|
enableSoftDevice = tmkService.isEnableSoftDevice();
|
|
|
|
@@ -158,60 +166,52 @@ public class DeviceManager implements InitializingBean {
|
|
|
|
|
);
|
|
|
|
|
if (CollectionUtils.isEmpty(devices)) {
|
|
|
|
|
log.info("no device for sync ...");
|
|
|
|
|
deviceMap.clear();
|
|
|
|
|
usedDeviceMap.clear();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Map<Long, List<Device>> groupDeviceMap = devices.stream().collect(Collectors.groupingBy(Device::getGroupId));
|
|
|
|
|
Map<Long, List<Long>> groupDeviceMap = devices.stream()
|
|
|
|
|
.collect(Collectors.groupingBy(Device::getGroupId, Collectors.mapping(Device::getId, Collectors.toList())));
|
|
|
|
|
List<CryptoServiceDeviceGroup> serviceDeviceGroups = cryptoServiceDeviceGroupMapper
|
|
|
|
|
.selectList(new LambdaQueryWrapper<CryptoServiceDeviceGroup>()
|
|
|
|
|
.in(CryptoServiceDeviceGroup::getDeviceGroupId, groupDeviceMap.keySet()));
|
|
|
|
|
if (CollectionUtils.isEmpty(serviceDeviceGroups)) {
|
|
|
|
|
deviceMap.clear();
|
|
|
|
|
usedDeviceMap.clear();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Map<Long, List<Device>> waitSyncMap = serviceDeviceGroups.stream()
|
|
|
|
|
serviceDeviceMap = serviceDeviceGroups.stream()
|
|
|
|
|
.collect(Collectors.toMap(CryptoServiceDeviceGroup::getServiceId,
|
|
|
|
|
it -> groupDeviceMap.get(it.getDeviceGroupId())));
|
|
|
|
|
|
|
|
|
|
for (Map.Entry<Long, List<Device>> entry : waitSyncMap.entrySet()) {
|
|
|
|
|
|
|
|
|
|
deviceMap.compute(entry.getKey(), (k, old) -> {
|
|
|
|
|
if (CollectionUtils.isEmpty(old)) {
|
|
|
|
|
return entry.getValue().stream().map(this::mapToContext).filter(Objects::nonNull).collect(Collectors.toList());
|
|
|
|
|
for (Device device : devices) {
|
|
|
|
|
DeviceCheckRes checkRes = tmkService.checkDevice(device);
|
|
|
|
|
if (checkRes.isHasError()) {
|
|
|
|
|
log.warn("设备:{}, 检测异常:{}", device.getServiceIp(), checkRes.getMessage());
|
|
|
|
|
usedDeviceMap.remove(device.getId());
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
DeviceContext exist = usedDeviceMap.get(device.getId());
|
|
|
|
|
if (Objects.isNull(exist)) {
|
|
|
|
|
usedDeviceMap.remove(device.getId());
|
|
|
|
|
DeviceContext context = getDeviceContext(device, checkRes);
|
|
|
|
|
if (context != null) {
|
|
|
|
|
usedDeviceMap.put(device.getId(), context);
|
|
|
|
|
}
|
|
|
|
|
List<String> newSerials = entry.getValue().stream()
|
|
|
|
|
.map(Device::getDeviceSerial)
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
List<String> oldSerials = old.stream()
|
|
|
|
|
.map(DeviceContext::getDeviceSerial)
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
List<DeviceContext> nc = old.stream()
|
|
|
|
|
.filter(it -> newSerials.contains(it.getDeviceSerial()))
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
List<Device> waitSync = entry.getValue().stream()
|
|
|
|
|
.filter(it -> !oldSerials.contains(it.getDeviceSerial()))
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
nc.addAll(waitSync.stream().map(this::mapToContext).filter(Objects::nonNull).collect(Collectors.toList()));
|
|
|
|
|
return nc;
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
if (!Objects.equals(checkRes.getDeviceSerial(), exist.getDeviceSerial())
|
|
|
|
|
|| !Objects.equals(checkRes.getPubKey(), exist.getPubKey())) {
|
|
|
|
|
DeviceContext context = getDeviceContext(device, checkRes);
|
|
|
|
|
if (context != null) {
|
|
|
|
|
usedDeviceMap.put(device.getId(), context);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private DeviceContext mapToContext(Device device) {
|
|
|
|
|
private static DeviceContext getDeviceContext(Device device, DeviceCheckRes checkRes) {
|
|
|
|
|
try {
|
|
|
|
|
Assert.hasText(device.getEncTmk(), "TMK 状态异常");
|
|
|
|
|
DeviceCheckRes checkRes = tmkService.checkDevice(device);
|
|
|
|
|
if (!Objects.equals(checkRes.getDeviceSerial(), device.getDeviceSerial())
|
|
|
|
|
|| !Objects.equals(checkRes.getPubKey(), device.getPubKey())) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
DeviceContext context = new DeviceContext();
|
|
|
|
|
context.setId(device.getId());
|
|
|
|
|
context.setIp(device.getServiceIp());
|
|
|
|
|
context.setPort(device.getServicePort());
|
|
|
|
|
context.setModel(device.getManufacturerModel());
|
|
|
|
@@ -234,7 +234,6 @@ public class DeviceManager implements InitializingBean {
|
|
|
|
|
TMKContextFactory tenantTMKContextFactory = new TMKContextFactory(checkRes.getSdfApiAdapter(), context);
|
|
|
|
|
GenericObjectPool<TMKContext> pool = new GenericObjectPool<>(tenantTMKContextFactory, config);
|
|
|
|
|
context.setPool(pool);
|
|
|
|
|
|
|
|
|
|
return context;
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
log.warn("device conn error: {}", device, ex);
|
|
|
|
|