diff --git a/src/main/java/com/dispose/service/DisposeAbilityRouterService.java b/src/main/java/com/dispose/service/DisposeAbilityRouterService.java index 71464590..a0bd0ed7 100644 --- a/src/main/java/com/dispose/service/DisposeAbilityRouterService.java +++ b/src/main/java/com/dispose/service/DisposeAbilityRouterService.java @@ -18,9 +18,10 @@ public interface DisposeAbilityRouterService { * * @param ipAddr the ip addr * @param ipPort the ip port + * @param deviceType the device type * @return the ability device */ - AbilityInfo getAbilityDevice(String ipAddr, String ipPort); + AbilityInfo getAbilityDevice(String ipAddr, String ipPort, String deviceType); /** * Gets ability device. diff --git a/src/main/java/com/dispose/service/impl/DisposeAbilityRouterServiceImpl.java b/src/main/java/com/dispose/service/impl/DisposeAbilityRouterServiceImpl.java index 103c4423..e41b000a 100644 --- a/src/main/java/com/dispose/service/impl/DisposeAbilityRouterServiceImpl.java +++ b/src/main/java/com/dispose/service/impl/DisposeAbilityRouterServiceImpl.java @@ -63,13 +63,14 @@ public class DisposeAbilityRouterServiceImpl implements DisposeAbilityRouterServ /** * Gets ability device. * - * @param ipAddr the ip addr - * @param ipPort the ip port + * @param ipAddr the ip addr + * @param ipPort the ip port + * @param deviceType the device type * @return the ability device */ @Override - public AbilityInfo getAbilityDevice(String ipAddr, String ipPort) { - return disposeAbilityMap.get(getAbilityDeviceHashKey(ipAddr, ipPort)); + public AbilityInfo getAbilityDevice(String ipAddr, String ipPort, String deviceType) { + return disposeAbilityMap.get(getAbilityDeviceHashKey(ipAddr, ipPort, deviceType)); } /** @@ -86,7 +87,8 @@ public class DisposeAbilityRouterServiceImpl implements DisposeAbilityRouterServ .orElse(null); if (dev != null) { - return disposeAbilityMap.get(getAbilityDeviceHashKey(dev.getIpAddr(), dev.getIpPort())); + return disposeAbilityMap.get(getAbilityDeviceHashKey(dev.getIpAddr(), dev.getIpPort(), + String.valueOf(dev.getDeviceType()))); } return null; @@ -113,7 +115,7 @@ public class DisposeAbilityRouterServiceImpl implements DisposeAbilityRouterServ DisposeAbility db; String httpType = dev.getUrlType() == HttpType.HTTP ? "http://" : "https://"; - String addr = getAbilityDeviceHashKey(dev.getIpAddr(), dev.getIpPort()); + String addr = (dev.getIpPort() == null || dev.getIpPort().length() == 0) ? dev.getIpAddr() : (dev.getIpAddr() + ":" + dev.getIpPort()); String url = httpType + addr + "/" + dev.getUrlPath(); switch (dev.getDeviceType()) { @@ -145,8 +147,11 @@ public class DisposeAbilityRouterServiceImpl implements DisposeAbilityRouterServ db.initDeviceEnv(url, dev.getUserName(), dev.getPassword()); db.getDisposeDeviceProtectObject(); + String hashKey = getAbilityDeviceHashKey(dev.getIpAddr(), dev.getIpPort(), + String.valueOf(dev.getDeviceType())); + // 缓存处置设备到Hash表中 - disposeAbilityMap.put(addr, AbilityInfo.builder() + disposeAbilityMap.put(hashKey, AbilityInfo.builder() .db(db) .dev(dev) .linkStatus(false) @@ -206,7 +211,8 @@ public class DisposeAbilityRouterServiceImpl implements DisposeAbilityRouterServ * @param ipPort the ip port * @return the ability device hash key */ - private String getAbilityDeviceHashKey(String ipAddr, String ipPort) { - return (ipPort == null || ipPort.length() == 0) ? ipAddr : (ipAddr + ":" + ipPort); + private String getAbilityDeviceHashKey(String ipAddr, String ipPort, String deviceType) { + String addrPort = ipPort == null || ipPort.length() == 0 ? ipAddr : (ipAddr + ":" + ipPort); + return addrPort + "-" + deviceType; } } diff --git a/src/test/java/com/dispose/test/dev/service/DisposeAbilityRouterServiceTest.java b/src/test/java/com/dispose/test/dev/service/DisposeAbilityRouterServiceTest.java index 21193671..53c9eba2 100644 --- a/src/test/java/com/dispose/test/dev/service/DisposeAbilityRouterServiceTest.java +++ b/src/test/java/com/dispose/test/dev/service/DisposeAbilityRouterServiceTest.java @@ -149,7 +149,8 @@ public class DisposeAbilityRouterServiceTest extends InitTestEnvironment { e.printStackTrace(); } - AbilityInfo abilityInfo = disposeAbilityRouterService.getAbilityDevice(v.getIpAddr(), v.getIpPort()); + AbilityInfo abilityInfo = disposeAbilityRouterService.getAbilityDevice(v.getIpAddr(), v.getIpPort(), + String.valueOf(v.getDeviceType())); Assert.assertNotNull(abilityInfo); @@ -251,7 +252,8 @@ public class DisposeAbilityRouterServiceTest extends InitTestEnvironment { log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(deviceList)); deviceList.forEach(v -> { - AbilityInfo abilityInfo = disposeAbilityRouterService.getAbilityDevice(v.getIpAddr(), v.getIpPort()); + AbilityInfo abilityInfo = disposeAbilityRouterService.getAbilityDevice(v.getIpAddr(), v.getIpPort(), + String.valueOf(v.getDeviceType())); Assert.assertNotNull(abilityInfo); Assert.assertNotNull(abilityInfo.getDev());