From f2cb26dd8b02da2c6cebbdfaf3b98f2749faad9d Mon Sep 17 00:00:00 2001 From: Matt Jones Date: Wed, 23 Oct 2024 14:16:58 -0500 Subject: [PATCH] feat: add a map to translate id to name, make sure to use caching so we don't have to go to the switchbot API constantly. --- pom.xml | 2 +- .../switch_bot/SwitchBotDeviceApi.java | 32 +++++++++++++++++++ .../switch_bot/SwitchBotApiTest.java | 9 ++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 3eb4bf7..62c3fe9 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.bigboxer23 switchbotapi-java - 1.1.6 + 1.1.7 switchbotapi-java https://github.com/bigboxer23/switchbotapi-java diff --git a/src/main/java/com/bigboxer23/switch_bot/SwitchBotDeviceApi.java b/src/main/java/com/bigboxer23/switch_bot/SwitchBotDeviceApi.java index 790840b..3290f42 100644 --- a/src/main/java/com/bigboxer23/switch_bot/SwitchBotDeviceApi.java +++ b/src/main/java/com/bigboxer23/switch_bot/SwitchBotDeviceApi.java @@ -2,11 +2,15 @@ import com.bigboxer23.switch_bot.data.*; import com.bigboxer23.utils.http.OkHttpUtil; +import com.bigboxer23.utils.time.ITimeConstants; import java.io.IOException; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; +import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.Optional; +import java.util.stream.Collectors; import okhttp3.RequestBody; import okhttp3.Response; import org.slf4j.Logger; @@ -17,8 +21,36 @@ public class SwitchBotDeviceApi { private static final Logger logger = LoggerFactory.getLogger(SwitchBotDeviceApi.class); private final SwitchBotApi provider; + private Map deviceIdToNames; + + private long deviceIdToNamesCacheTime = -1; + protected SwitchBotDeviceApi(SwitchBotApi provider) { this.provider = provider; + refreshDeviceNameMap(); + } + + public String getDeviceNameFromId(String deviceId) { + refreshDeviceNameMap(); + return Optional.ofNullable(deviceIdToNames) + .map(m -> m.getOrDefault(deviceId, deviceId)) + .orElse(deviceId); + } + + private synchronized void refreshDeviceNameMap() { + if (deviceIdToNames != null && (System.currentTimeMillis() - ITimeConstants.HOUR) < deviceIdToNamesCacheTime) { + return; + } + try { + logger.info("Refreshing device id/name map..."); + deviceIdToNames = Collections.unmodifiableMap( + getDevices().stream().collect(Collectors.toMap(Device::getDeviceId, Device::getDeviceName))); + deviceIdToNamesCacheTime = System.currentTimeMillis(); + } catch (IOException e) { + logger.error("Failed to refresh device names.", e); + deviceIdToNames = null; + deviceIdToNamesCacheTime = -1; + } } /** diff --git a/src/test/java/com/bigboxer23/switch_bot/SwitchBotApiTest.java b/src/test/java/com/bigboxer23/switch_bot/SwitchBotApiTest.java index 1eb3ea4..2b3443f 100644 --- a/src/test/java/com/bigboxer23/switch_bot/SwitchBotApiTest.java +++ b/src/test/java/com/bigboxer23/switch_bot/SwitchBotApiTest.java @@ -24,6 +24,15 @@ public void testGetDevices() throws IOException { assertNotNull(devices.get(0).getDeviceId()); } + @Test + public void getDeviceNameFromId() throws IOException { + Device device = instance.getDeviceApi().getDevices().get(0); + String deviceName = instance.getDeviceApi().getDeviceNameFromId(device.getDeviceId()); + assertEquals(device.getDeviceName(), deviceName); + assertEquals("test", instance.getDeviceApi().getDeviceNameFromId("test")); + assertNull(instance.getDeviceApi().getDeviceNameFromId(null)); + } + @Test public void testDeviceStatus() throws IOException { try {