Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.bigboxer23</groupId>
<artifactId>switchbotapi-java</artifactId>
<version>1.1.6</version>
<version>1.1.7</version>

<name>switchbotapi-java</name>
<url>https://github.com/bigboxer23/switchbotapi-java</url>
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/com/bigboxer23/switch_bot/SwitchBotDeviceApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,8 +21,36 @@ public class SwitchBotDeviceApi {
private static final Logger logger = LoggerFactory.getLogger(SwitchBotDeviceApi.class);
private final SwitchBotApi provider;

private Map<String, String> 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;
}
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/com/bigboxer23/switch_bot/SwitchBotApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down