This repository was archived by the owner on Mar 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathGateway.java
More file actions
239 lines (212 loc) · 9.06 KB
/
Gateway.java
File metadata and controls
239 lines (212 loc) · 9.06 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
Copyright 2020 Stijn Groenen
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package nl.stijngroenen.tradfri.device;
import nl.stijngroenen.tradfri.device.event.EventHandler;
import nl.stijngroenen.tradfri.payload.AuthenticateRequest;
import nl.stijngroenen.tradfri.payload.AuthenticateResponse;
import nl.stijngroenen.tradfri.payload.DeviceResponse;
import nl.stijngroenen.tradfri.util.ApiEndpoint;
import nl.stijngroenen.tradfri.util.CoapClient;
import nl.stijngroenen.tradfri.util.Credentials;
import org.apache.commons.lang3.RandomStringUtils;
import java.util.ArrayList;
import java.util.List;
/**
* The class that is used to communicate with the IKEA TRÅDFRI gateway
* @author Stijn Groenen
* @version 1.2.0
*/
public class Gateway {
/**
* A CoAP client that can be used to communicate with the IKEA TRÅDFRI gateway
*/
private CoapClient coapClient;
/**
* The observer that observes the IKEA TRÅDFRI gateway to automagically detect changes
*/
private GatewayObserver observer;
/**
* The event handlers registered for the device
*/
private List<EventHandler> eventHandlers;
/**
* Construct the Gateway class
* @param ip The IP-address of the IKEA TRÅDFRI gateway
* @since 1.0.0
*/
public Gateway(String ip) {
ApiEndpoint.setGatewayIp(ip);
coapClient = new CoapClient();
eventHandlers = new ArrayList<>();
}
/**
* Connect and authenticate to the IKEA TRÅDFRI gateway using a security code
* @param securityCode The security code of the IKEA TRÅDFRI gateway
* @return Credentials that can be used to authenticate to the IKEA TRÅDFRI gateway
* @since 1.0.0
*/
public Credentials connect(String securityCode) {
String identity = RandomStringUtils.randomAlphanumeric(16);
AuthenticateRequest request = new AuthenticateRequest();
request.setIdentity(identity);
setCredentials("Client_identity", securityCode);
AuthenticateResponse response = coapClient.post(ApiEndpoint.getUri(ApiEndpoint.AUTHENTICATE), request, AuthenticateResponse.class);
if(response == null) return null;
Credentials credentials = new Credentials(identity, response.getPresharedKey());
setCredentials(credentials);
return credentials;
}
/**
* Connect and authenticate to the IKEA TRÅDFRI gateway using credentials
* @param credentials The credentials that can be used to authenticate to the IKEA TRÅDFRI gateway
* @return Credentials that can be used to authenticate to the IKEA TRÅDFRI gateway
* @since 1.0.0
*/
public Credentials connect(Credentials credentials){
setCredentials(credentials);
return credentials;
}
/**
* Change the credentials used to communicate with the IKEA TRÅDFRI gateway
* @param credentials The new credentials that can be used to authenticate to the IKEA TRÅDFRI gateway
* @since 1.0.0
*/
public void setCredentials(Credentials credentials){
coapClient.setCredentials(credentials);
}
/**
* Change the credentials used to communicate with the IKEA TRÅDFRI gateway
* @param identity The new identity that can be used to authenticate to the IKEA TRÅDFRI gateway
* @param key The new key that can be used to authenticate to the IKEA TRÅDFRI gateway
* @since 1.0.0
*/
public void setCredentials(String identity, String key){
Credentials credentials = new Credentials(identity, key);
setCredentials(credentials);
}
/**
* Get the credentials used to communicate with the IKEA TRÅDFRI gateway
* @return The credentials that can be used to authenticate to the IKEA TRÅDFRI gateway
* @since 1.0.0
*/
public Credentials getCredentials(){
return coapClient.getCredentials();
}
/**
* Get timeout for connections to the IKEA TRÅDFRI gateway (in milliseconds)
* @return The timeout for connections to the IKEA TRÅDFRI gateway (in milliseconds)
* @since 1.2.0
*/
public long getTimeout() {
return coapClient.getTimeout();
}
/**
* Change the timeout for connections to the IKEA TRÅDFRI gateway (in milliseconds)
* @param timeout The new timeout for connections to the IKEA TRÅDFRI gateway (in milliseconds)
* @since 1.2.0
*/
public void setTimeout(long timeout){
coapClient.setTimeout(timeout);
}
/**
* Get the ids of the devices registered to the IKEA TRÅDFRI gateway
* @return An array of the ids of the devices registered to the IKEA TRÅDFRI gateway
* @since 1.0.0
*/
public int[] getDeviceIds(){
return coapClient.get(ApiEndpoint.getUri(ApiEndpoint.DEVICES), int[].class);
}
/**
* Get the a device registered to the IKEA TRÅDFRI gateway
* @param id The id of a device registered to the IKEA TRÅDFRI gateway
* @return The device with the provided id
* @since 1.0.0
*/
public Device getDevice(int id){
DeviceResponse response = coapClient.get(ApiEndpoint.getUri(ApiEndpoint.DEVICES, String.valueOf(id)), DeviceResponse.class);
if(response == null){
return null;
}else if(response.getLightProperties() != null && response.getLightProperties().length > 0){
return new Light(response.getName(), response.getCreationDate(), response.getInstanceId(), response.getDeviceInfo(), response.getLightProperties()[0], coapClient);
}else if(response.getPlugProperties() != null && response.getPlugProperties().length > 0){
return new Plug(response.getName(), response.getCreationDate(), response.getInstanceId(), response.getDeviceInfo(), response.getPlugProperties()[0], coapClient);
}else if(response.getDeviceInfo().getModelName().equals("TRADFRI remote control")){
return new Remote(response.getName(), response.getCreationDate(), response.getInstanceId(), response.getDeviceInfo(), coapClient);
}else if(response.getDeviceInfo().getModelName().equals("TRADFRI motion sensor")){
return new MotionSensor(response.getName(), response.getCreationDate(), response.getInstanceId(), response.getDeviceInfo(), coapClient);
}else if(response.getDeviceInfo().getModelName().contains("blind")) {
return new Blind(response.getName(), response.getCreationDate(), response.getInstanceId(), response.getDeviceInfo(), coapClient);
} else {
return new Device(response.getName(), response.getCreationDate(), response.getInstanceId(), response.getDeviceInfo(), coapClient);
}
}
/**
* Get the devices registered to the IKEA TRÅDFRI gateway
* @return An array of the devices registered to the IKEA TRÅDFRI gateway
* @since 1.0.0
*/
public Device[] getDevices(){
ArrayList<Device> deviceList = new ArrayList<>();
int[] deviceIds = getDeviceIds();
if(deviceIds == null) return null;
for(int deviceId: deviceIds){
Device device = getDevice(deviceId);
deviceList.add(device);
}
Device[] devices = new Device[deviceList.size()];
deviceList.toArray(devices);
return devices;
}
/**
* Enable observe to automagically detect changes to the device
* @return True if successfully enabled observe, false if not
* @since 1.0.0
*/
public boolean enableObserve() {
if(observer == null) observer = new GatewayObserver(this, this.coapClient);
return observer.start();
}
/**
* Disable observe
* @return True if successfully disabled observe, false if not
* @since 1.0.0
*/
public boolean disableObserve() {
if(observer == null) return false;
return observer.stop();
}
/**
* Get a list of event handlers for the IKEA TRÅDFRI gateway
* @return A list of event handlers for the IKEA TRÅDFRI gateway
* @since 1.0.0
*/
public List<EventHandler> getEventHandlers(){
return eventHandlers;
}
/**
* Add an event handler to the IKEA TRÅDFRI gateway
* @param eventHandler The event handler to add to the IKEA TRÅDFRI gateway
* @since 1.0.0
*/
public void addEventHandler(EventHandler eventHandler){
this.eventHandlers.add(eventHandler);
}
/**
* Remove an event handler from the IKEA TRÅDFRI gateway
* @param eventHandler The event handler to remove from the IKEA TRÅDFRI gateway
* @since 1.0.0
*/
public void removeEventHandler(EventHandler eventHandler){
this.eventHandlers.remove(eventHandler);
}
}