From ae5457ada5bfbe8860448a21d0446ee34cb62b14 Mon Sep 17 00:00:00 2001 From: chenlinghy Date: Fri, 22 Jan 2021 14:29:55 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=E4=BF=AE=E6=94=B9=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E8=AE=BE=E5=A4=87=E4=BF=A1=E6=81=AF=E7=9A=84=E5=93=88?= =?UTF-8?q?=E5=B8=8C=E8=A1=A8key=E5=80=BC=202.=E4=BF=AE=E6=94=B9=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E8=AE=BE=E5=A4=87=E4=BF=A1=E6=81=AF=E7=9A=84=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/DisposeAbilityRouterService.java | 3 ++- .../impl/DisposeAbilityRouterServiceImpl.java | 24 ++++++++++++------- .../DisposeAbilityRouterServiceTest.java | 6 +++-- 3 files changed, 21 insertions(+), 12 deletions(-) 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());