forked from IOT-DSA/dslink-java-bitpool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitpoolConn.java
More file actions
393 lines (332 loc) · 13.5 KB
/
BitpoolConn.java
File metadata and controls
393 lines (332 loc) · 13.5 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package bitpool;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.swagger.client.ApiClient;
import io.swagger.client.ApiException;
import io.swagger.client.api.AuthenticationApi;
import io.swagger.client.api.PoolsApi;
import io.swagger.client.api.TimezonesApi;
import io.swagger.client.model.BitPoolInfrastructureDTOAUTHLogin2;
import io.swagger.client.model.BitPoolInfrastructureDTOBPRegisterPool;
import io.swagger.client.model.BitPoolInfrastructureDTOCreateAuthKeyDto;
import io.swagger.client.model.BitPoolInfrastructureDTOCreateAuthKeyDto.KeyTypeEnum;
import io.swagger.client.model.BitPoolInfrastructureDTOTimeZoneDto;
import io.swagger.client.model.BitPoolServerModelBitPoolEntity;
import org.dsa.iot.dslink.node.Node;
import org.dsa.iot.dslink.node.Permission;
import org.dsa.iot.dslink.node.actions.Action;
import org.dsa.iot.dslink.node.actions.ActionResult;
import org.dsa.iot.dslink.node.actions.EditorType;
import org.dsa.iot.dslink.node.actions.Parameter;
import org.dsa.iot.dslink.node.value.Value;
import org.dsa.iot.dslink.node.value.ValueType;
import org.dsa.iot.dslink.util.StringUtils;
import org.dsa.iot.dslink.util.handler.Handler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class BitpoolConn {
private final static Logger LOGGER;
static {
LOGGER = LoggerFactory.getLogger(BitpoolConn.class);
}
Node node;
BitpoolLink link;
final private Node statNode;
final ApiClient apiClient = new ApiClient();
final AuthenticationApi authApi = new AuthenticationApi(apiClient);
final PoolsApi poolsApi = new PoolsApi(apiClient);
Map<String, String> timezones = null;
public BitpoolConn(BitpoolLink link, Node node) {
this.link = link;
this.node = node;
this.statNode = node.createChild("STATUS").setValueType(ValueType.STRING).setValue(new Value("Not logged in")).build();
this.statNode.setSerializable(false);
}
void init() {
Action act = new Action(Permission.READ, new RemoveHandler());
Node anode = node.getChild("remove");
if (anode == null) node.createChild("remove").setAction(act).build().setSerializable(false);
else anode.setAction(act);
act = new Action(Permission.READ, new LoginHandler());
act.addParameter(new Parameter("Email", ValueType.STRING, node.getAttribute("Email")));
Parameter param = new Parameter("Password", ValueType.STRING, node.getAttribute("Password"));
param.setEditorType(EditorType.PASSWORD);
act.addParameter(param);
act.addParameter(new Parameter("Organisation", ValueType.STRING, node.getAttribute("Organisation")));
act.addParameter(new Parameter("Place", ValueType.STRING, node.getAttribute("Place")));
act.addParameter(new Parameter("Polling interval", ValueType.NUMBER, node.getAttribute("Polling interval")));
act.addParameter(new Parameter("Load data manually", ValueType.BOOL, node.getAttribute("Load data manually")));
anode = node.getChild("edit");
if (anode == null) node.createChild("edit").setAction(act).build().setSerializable(false);
else anode.setAction(act);
// act = new Action(Permission.READ, new UpdateIntervalHandler());
// act.addParameter(new Parameter("Polling interval", ValueType.NUMBER, node.getAttribute("Polling interval")));
// anode = node.getChild("edit polling interval");
// if (anode == null) node.createChild("edit polling interval").setAction(act).build().setSerializable(false);
// else anode.setAction(act);
if (node.getAttribute("API key") != null) {
String apiKey = node.getAttribute("API key").getString();
//apiClient.setApiKeyPrefix(apiKey.split(" ")[0]);
apiClient.setApiKey(apiKey);
if (timezones == null) {
List<BitPoolInfrastructureDTOTimeZoneDto> tzList = null;
try {
tzList = new TimezonesApi(apiClient).timezonesTimeZones();
} catch (ApiException e) {
LOGGER.debug("", e);
// TODO Auto-generated catch block
} catch (Exception e) {
LOGGER.debug("", e);
}
if (tzList != null && tzList.size()>0) {
timezones = new HashMap<String, String>();
for (BitPoolInfrastructureDTOTimeZoneDto tz: tzList) {
timezones.put(tz.getDisplayName(), tz.getId());
}
}
}
//node.removeChild("login");
statNode.setValue(new Value("Logged In"));
if (node.getAttribute("Load data manually").getBool()) {
act = new Action(Permission.READ, new Handler<ActionResult>() {
public void handle(ActionResult event) {
loadPools();
}
});
anode = node.getChild("load all pools");
if (anode == null) node.createChild("load all pools").setAction(act).build().setSerializable(false);
else anode.setAction(act);
act = new Action(Permission.READ, new LoadPoolHandler());
act.addParameter(new Parameter("Pool key", ValueType.STRING));
anode = node.getChild("load pool");
if (anode == null) node.createChild("load pool").setAction(act).build().setSerializable(false);
else anode.setAction(act);
} else {
loadPools();
}
act = new Action(Permission.READ, new LogoutHandler());
anode = node.getChild("logout");
if (anode == null) node.createChild("logout").setAction(act).build().setSerializable(false);
else anode.setAction(act);
act = new Action(Permission.READ, new CreatePoolHandler());
act.addParameter(new Parameter("Poolname", ValueType.STRING));
act.addParameter(new Parameter("Public", ValueType.BOOL));
act.addParameter(new Parameter("Virtual", ValueType.BOOL));
anode = node.getChild("create pool");
if (anode == null) node.createChild("create pool").setAction(act).build().setSerializable(false);
else anode.setAction(act);
} else {
node.removeChild("logout");
node.removeChild("create pool");
}
}
void login(String email, String pass, String org, String place) {
BitPoolInfrastructureDTOAUTHLogin2 logindata = new BitPoolInfrastructureDTOAUTHLogin2();
logindata.setUsername(email);
logindata.setPassword(pass);
logindata.setOrganisation(org);
logindata.setPlace(place);
try {
String authkey = new AuthenticationApi().authenticationLogin(logindata);
//?
//apiClient.setApiKeyPrefix(authkey.split(" ")[0]);
apiClient.setApiKey(authkey);
BitPoolInfrastructureDTOCreateAuthKeyDto data = new BitPoolInfrastructureDTOCreateAuthKeyDto();
data.setKeyType(KeyTypeEnum.STANDARD);
data.setPlace(place);
String permKey = authApi.authenticationCreateAuthKey(data);
// apiClient.setApiKey(permKey);
node.setAttribute("API key", new Value(permKey));
} catch (ApiException e) {
node.removeAttribute("API key");
statNode.setValue(new Value("Login failed"));
return;
}
}
private class LoginHandler implements Handler<ActionResult> {
public void handle(ActionResult event) {
String email = event.getParameter("Email", ValueType.STRING).getString();
String pass = event.getParameter("Password", ValueType.STRING).getString();
String org = event.getParameter("Organisation", ValueType.STRING).getString();
String place = event.getParameter("Place", ValueType.STRING).getString();
double interval = event.getParameter("Polling interval", ValueType.NUMBER).getNumber().doubleValue();
boolean manLoad = event.getParameter("Load data manually", ValueType.BOOL).getBool();
String name = StringUtils.encodeName(email);
if (!name.equals(node.getName())) {
Node child = link.node.createChild(name).build();
child.setAttribute("Email", new Value(email));
child.setAttribute("Password", new Value(pass));
child.setAttribute("Organisation", new Value(org));
child.setAttribute("Place", new Value(place));
child.setAttribute("Polling interval", new Value(interval));
child.setAttribute("Load data manually", new Value(manLoad));
BitpoolConn conn = new BitpoolConn(link, child);
conn.login(email, pass, org, place);
conn.init();
remove();
return;
}
if (new Value(email).equals(node.getAttribute("Email")) &&
new Value(pass).equals(node.getAttribute("Password")) &&
new Value(org).equals(node.getAttribute("Organisation"))) {
if (!new Value(manLoad).equals(node.getAttribute("Load data manually"))) {
node.setAttribute("Load data manually", new Value(manLoad));
clear();
}
if (!new Value(place).equals(node.getAttribute("Place"))) {
node.setAttribute("Place", new Value(place));
login(email, pass, org, place);
}
node.setAttribute("Polling interval", new Value(interval));
init();
} else {
logout();
node.setAttribute("Email", new Value(email));
node.setAttribute("Password", new Value(pass));
node.setAttribute("Organisation", new Value(org));
node.setAttribute("Place", new Value(place));
node.setAttribute("Polling interval", new Value(interval));
node.setAttribute("Load data manually", new Value(manLoad));
login(email, pass, org, place);
init();
}
}
}
// private class UpdateIntervalHandler implements Handler<ActionResult> {
// public void handle(ActionResult event) {
// double interval = event.getParameter("Polling interval", ValueType.NUMBER).getNumber().doubleValue();
// node.setAttribute("Polling interval", new Value(interval));
// init();
// }
// }
private class LogoutHandler implements Handler<ActionResult> {
public void handle(ActionResult event) {
logout();
init();
}
}
private class RemoveHandler implements Handler<ActionResult> {
public void handle(ActionResult event) {
remove();
}
}
private void logout() {
try {
authApi.authenticationCancelAuthKey(node.getAttribute("API key").getString());
} catch (ApiException e) {
LOGGER.debug("", e);
// TODO Auto-generated catch block
}
node.removeAttribute("API key");
statNode.setValue(new Value("Logged out"));
//apiClient.setApiKeyPrefix(null);
apiClient.setApiKey(null);
clear();
}
private void clear() {
if (node.getChildren() == null) return;
for (Node child: node.getChildren().values()) {
if (child != statNode && child.getAction() == null) node.removeChild(child);
}
}
private void remove() {
if (node.getAttribute("API key") != null) logout();
node.clearChildren();
node.getParent().removeChild(node);
}
private void loadPools() {
try {
List<BitPoolServerModelBitPoolEntity> pools = poolsApi.poolsGetPools();
for (BitPoolServerModelBitPoolEntity pool: pools) {
if (node.getChild(pool.getName()) == null) {
Node child = node.createChild(pool.getName()).build();
final BitpoolPool bp = makePoolObj(pool, child);
child.getListener().setOnListHandler(new Handler<Node>() {
private boolean loaded = false;
public void handle(Node event) {
if (!loaded) bp.init();
loaded = true;
}
});
}
}
} catch (ApiException e) {
LOGGER.debug("", e);
// TODO Auto-generated catch block
}
}
private class LoadPoolHandler implements Handler<ActionResult> {
public void handle(ActionResult event) {
String key = event.getParameter("Pool key", ValueType.STRING).getString();
try {
BitPoolServerModelBitPoolEntity pool = poolsApi.poolsGetPool(key);
if (node.getChild(pool.getName()) == null) {
Node child = node.createChild(pool.getName()).build();
BitpoolPool bp = makePoolObj(pool, child);
bp.init();
}
} catch (ApiException e) {
// TODO Auto-generated catch block
LOGGER.debug("", e);
}
}
}
private class CreatePoolHandler implements Handler<ActionResult> {
public void handle(ActionResult event) {
String poolName = event.getParameter("Poolname", ValueType.STRING).getString();
boolean pub = event.getParameter("Public", ValueType.BOOL).getBool();
boolean virtual = event.getParameter("Virtual", ValueType.BOOL).getBool();
BitPoolInfrastructureDTOBPRegisterPool data = new BitPoolInfrastructureDTOBPRegisterPool();
data.setPoolname(poolName);
data.setPublic(pub);
data.setVirtual(virtual);
BitPoolServerModelBitPoolEntity retval;
try {
retval = poolsApi.poolsRegisterPool(data);
} catch (ApiException e) {
LOGGER.debug("", e);
// TODO Auto-generated catch block
return;
}
if (retval == null) return;
Node child = node.createChild(retval.getName()).build();
BitpoolPool bp = makePoolObj(retval, child);
bp.init();
}
}
private BitpoolPool makePoolObj(BitPoolServerModelBitPoolEntity pool, Node child) {
child.setAttribute("Pool key", new Value(pool.getPoolKey()));
child.setAttribute("Registration date", new Value(link.safeToString(pool.getRegistrationDate())));
child.setAttribute("Public", new Value(pool.getPublic()));
child.setAttribute("Virtual", new Value(pool.getVirtual()));
child.setAttribute("Owner", new Value(pool.getOwner()));
child.setAttribute("Access mode", new Value(pool.getAccessMode().toString()));
child.setAttribute("Utc offset", new Value(pool.getUtcOffset()));
child.setAttribute("Time zone", new Value(pool.getTimeZone()));
return new BitpoolPool(getMe(), child);
}
void restoreLastSession() {
init();
if (node.getChildren() == null) return;
for (Node child: node.getChildren().values()) {
Value key = child.getAttribute("Pool key");
if (key != null) {
try {
BitPoolServerModelBitPoolEntity pool = poolsApi.poolsGetPool(key.getString());
BitpoolPool bp = makePoolObj(pool, child);
bp.restoreLastSession();
} catch (ApiException e) {
// TODO Auto-generated catch block
LOGGER.debug("", e);
node.removeChild(child);
}
} else if (child.getAction() == null && !child.isHidden()) {
node.removeChild(child);
}
}
}
BitpoolConn getMe() {
return this;
}
}