-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwitchBotDeviceApi.java
More file actions
115 lines (104 loc) · 3.79 KB
/
SwitchBotDeviceApi.java
File metadata and controls
115 lines (104 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.bigboxer23.switch_bot;
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 lombok.extern.slf4j.Slf4j;
import okhttp3.RequestBody;
import okhttp3.Response;
/** */
@Slf4j
public class SwitchBotDeviceApi {
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 {
log.info("Refreshing device id/name map...");
deviceIdToNames = Collections.unmodifiableMap(
getDevices().stream().collect(Collectors.toMap(Device::getDeviceId, Device::getDeviceName)));
deviceIdToNamesCacheTime = System.currentTimeMillis();
} catch (IOException e) {
log.error("Failed to refresh device names.", e);
deviceIdToNames = null;
deviceIdToNamesCacheTime = -1;
}
}
/**
* @see <a href="https://github.com/OpenWonderLabs/SwitchBotAPI#get-device-list">Get device
* list</a>
* @return list of devices associated with the switchBot acct
* @throws IOException error class thrown for various reasons
*/
public List<Device> getDevices() throws IOException {
try (Response response = OkHttpUtil.getSynchronous(SwitchBotApi.baseUrl + "v1.1/devices", provider.addAuth())) {
return parseResponse(response, ApiResponse.class).getBody().getDeviceList();
}
}
/**
* @see <a href="https://github.com/OpenWonderLabs/SwitchBotAPI#get-device-status">Get device
* status</a>
* @param deviceId id of the device to query
*/
public Device getDeviceStatus(String deviceId) throws IOException {
if (deviceId == null) {
log.error("Need valid device id");
return null;
}
try (Response response = OkHttpUtil.getSynchronous(
SwitchBotApi.baseUrl + "v1.1/devices/" + deviceId + "/status", provider.addAuth())) {
return parseResponse(response, DeviceApiResponse.class).getBody();
}
}
/**
* @see <a
* href="https://github.com/OpenWonderLabs/SwitchBotAPI#send-device-control-commands">send
* device control commands</a>
*/
public IApiResponse sendDeviceControlCommands(String deviceId, DeviceCommand command) throws IOException {
String stringCommand = provider.getMoshi().adapter(DeviceCommand.class).toJson(command);
try (Response response = OkHttpUtil.postSynchronous(
SwitchBotApi.baseUrl + "v1.1/devices/" + deviceId + "/commands",
RequestBody.create(URLDecoder.decode(stringCommand, StandardCharsets.UTF_8.displayName())
.getBytes(StandardCharsets.UTF_8)),
provider.addAuth())) {
return parseResponse(response, DeviceApiResponse.class);
}
}
/**
* API is a bit weird, and need to check in returned body for status code as well
*
* @param response
* @param clazz
* @return
* @param <T>
* @throws IOException
*/
private <T extends IApiResponse> T parseResponse(Response response, Class<T> clazz) throws IOException {
Optional<T> apiResponse = OkHttpUtil.getBody(response, clazz);
if (!provider.checkForError(response, (Optional<IApiResponse>) apiResponse)) {
throw new IOException(response.code() + " " + response.message());
}
return apiResponse.get();
}
}