Skip to content

Commit 083ea6a

Browse files
authored
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. (#81)
1 parent bf4c157 commit 083ea6a

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.bigboxer23</groupId>
66
<artifactId>switchbotapi-java</artifactId>
7-
<version>1.1.6</version>
7+
<version>1.1.7</version>
88

99
<name>switchbotapi-java</name>
1010
<url>https://github.com/bigboxer23/switchbotapi-java</url>

src/main/java/com/bigboxer23/switch_bot/SwitchBotDeviceApi.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33
import com.bigboxer23.switch_bot.data.*;
44
import com.bigboxer23.utils.http.OkHttpUtil;
5+
import com.bigboxer23.utils.time.ITimeConstants;
56
import java.io.IOException;
67
import java.net.URLDecoder;
78
import java.nio.charset.StandardCharsets;
9+
import java.util.Collections;
810
import java.util.List;
11+
import java.util.Map;
912
import java.util.Optional;
13+
import java.util.stream.Collectors;
1014
import okhttp3.RequestBody;
1115
import okhttp3.Response;
1216
import org.slf4j.Logger;
@@ -17,8 +21,36 @@ public class SwitchBotDeviceApi {
1721
private static final Logger logger = LoggerFactory.getLogger(SwitchBotDeviceApi.class);
1822
private final SwitchBotApi provider;
1923

24+
private Map<String, String> deviceIdToNames;
25+
26+
private long deviceIdToNamesCacheTime = -1;
27+
2028
protected SwitchBotDeviceApi(SwitchBotApi provider) {
2129
this.provider = provider;
30+
refreshDeviceNameMap();
31+
}
32+
33+
public String getDeviceNameFromId(String deviceId) {
34+
refreshDeviceNameMap();
35+
return Optional.ofNullable(deviceIdToNames)
36+
.map(m -> m.getOrDefault(deviceId, deviceId))
37+
.orElse(deviceId);
38+
}
39+
40+
private synchronized void refreshDeviceNameMap() {
41+
if (deviceIdToNames != null && (System.currentTimeMillis() - ITimeConstants.HOUR) < deviceIdToNamesCacheTime) {
42+
return;
43+
}
44+
try {
45+
logger.info("Refreshing device id/name map...");
46+
deviceIdToNames = Collections.unmodifiableMap(
47+
getDevices().stream().collect(Collectors.toMap(Device::getDeviceId, Device::getDeviceName)));
48+
deviceIdToNamesCacheTime = System.currentTimeMillis();
49+
} catch (IOException e) {
50+
logger.error("Failed to refresh device names.", e);
51+
deviceIdToNames = null;
52+
deviceIdToNamesCacheTime = -1;
53+
}
2254
}
2355

2456
/**

src/test/java/com/bigboxer23/switch_bot/SwitchBotApiTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ public void testGetDevices() throws IOException {
2424
assertNotNull(devices.get(0).getDeviceId());
2525
}
2626

27+
@Test
28+
public void getDeviceNameFromId() throws IOException {
29+
Device device = instance.getDeviceApi().getDevices().get(0);
30+
String deviceName = instance.getDeviceApi().getDeviceNameFromId(device.getDeviceId());
31+
assertEquals(device.getDeviceName(), deviceName);
32+
assertEquals("test", instance.getDeviceApi().getDeviceNameFromId("test"));
33+
assertNull(instance.getDeviceApi().getDeviceNameFromId(null));
34+
}
35+
2736
@Test
2837
public void testDeviceStatus() throws IOException {
2938
try {

0 commit comments

Comments
 (0)