forked from IOT-DSA/dslink-java-bitpool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitpoolPool.java
More file actions
210 lines (179 loc) · 7.05 KB
/
BitpoolPool.java
File metadata and controls
210 lines (179 loc) · 7.05 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
package bitpool;
import io.swagger.client.ApiException;
import io.swagger.client.model.BitPoolServerModelBitPoolEntity;
import io.swagger.client.model.BitPoolServerModelBitStationEntity;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
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.Parameter;
import org.dsa.iot.dslink.node.value.Value;
import org.dsa.iot.dslink.node.value.ValueType;
import org.dsa.iot.dslink.util.handler.Handler;
public class BitpoolPool {
Node node;
BitpoolConn conn;
public BitpoolPool(BitpoolConn conn, Node node) {
this.conn = conn;
this.node = node;
}
void init() {
if (conn.node.getAttribute("Load data manually").getBool()) {
Action act = new Action(Permission.READ, new Handler<ActionResult>() {
public void handle(ActionResult event) {
remove();
}
});
node.createChild("unload").setAction(act).build().setSerializable(false);
act = new Action(Permission.READ, new Handler<ActionResult>(){
public void handle(ActionResult event) {
loadStations();
}
});
node.createChild("load all stations").setAction(act).build().setSerializable(false);
act = new Action(Permission.READ, new LoadStationHandler());
act.addParameter(new Parameter("Station id", ValueType.NUMBER));
node.createChild("load station").setAction(act).build().setSerializable(false);
} else {
loadStations();
}
Action act = new Action(Permission.READ, new DeleteHandler());
node.createChild("delete").setAction(act).build().setSerializable(false);
makeUpdateTZAction();
act = new Action(Permission.READ, new CreateStationHandler());
act.addParameter(new Parameter("Station name", ValueType.STRING));
node.createChild("create station").setAction(act).build().setSerializable(false);
}
private void makeUpdateTZAction() {
Action act = new Action(Permission.READ, new UpdateTZHandler());
if (conn.timezones == null) return;
Set<String> enums = new HashSet<String>(conn.timezones.values());
String defaultTz = node.getAttribute("Time zone").getString();
act.addParameter(new Parameter("Time zone", ValueType.makeEnum(enums), new Value(defaultTz)));
Node anode = node.getChild("update time zone");
if (anode == null) node.createChild("update time zone").setAction(act).build().setSerializable(false);
else anode.setAction(act);
}
private class DeleteHandler implements Handler<ActionResult> {
public void handle(ActionResult event) {
try {
conn.poolsApi.poolsDeletePool(node.getAttribute("Pool key").getString());
} catch (ApiException e) {
// TODO Auto-generated catch block
}
remove();
}
}
private void remove() {
node.clearChildren();
node.getParent().removeChild(node);
}
private class UpdateTZHandler implements Handler<ActionResult> {
public void handle(ActionResult event) {
String tz = event.getParameter("Time zone", ValueType.STRING).getString();
//String tz = conn.timezones.get(tzKey);
BitPoolServerModelBitPoolEntity pool = null;
try {
conn.poolsApi.poolsSetPoolTimezone(node.getAttribute("Pool key").getString(), tz);
pool = conn.poolsApi.poolsGetPool(node.getAttribute("Pool key").getString());
} catch (ApiException e) {
// TODO Auto-generated catch block
return;
}
if (pool != null) {
node.setAttribute("Pool key", new Value(pool.getPoolKey()));
node.setAttribute("Registration date", new Value(conn.link.safeToString(pool.getRegistrationDate())));
node.setAttribute("Public", new Value(pool.getPublic()));
node.setAttribute("Virtual", new Value(pool.getVirtual()));
node.setAttribute("Owner", new Value(pool.getOwner()));
node.setAttribute("Access mode", new Value(pool.getAccessMode().toString()));
node.setAttribute("Utc offset", new Value(pool.getUtcOffset()));
node.setAttribute("Time zone", new Value(pool.getTimeZone()));
init();
}
}
}
private class CreateStationHandler implements Handler<ActionResult> {
public void handle(ActionResult event) {
String name = event.getParameter("Station name", ValueType.STRING).getString();
BitPoolServerModelBitStationEntity station;
try {
station = conn.poolsApi.poolsRegisterStation(node.getAttribute("Pool key").getString(), name);
} catch (ApiException e) {
// TODO Auto-generated catch block
return;
}
if (station == null) return;
Node child = node.createChild(station.getStationName()).build();
BitpoolStation bs = makeStationObj(station, child);
bs.init();
}
}
private class LoadStationHandler implements Handler<ActionResult> {
public void handle(ActionResult event) {
int id = event.getParameter("Station id", ValueType.NUMBER).getNumber().intValue();
try {
BitPoolServerModelBitStationEntity station = conn.poolsApi.poolsGetStation(node.getAttribute("Pool key").getString(), id);
if (node.getChild(station.getStationName()) == null) {
Node child = node.createChild(station.getStationName()).build();
BitpoolStation bs = makeStationObj(station, child);
bs.init();
}
} catch (ApiException e) {
// TODO Auto-generated catch block
}
}
}
private void loadStations() {
try {
List<BitPoolServerModelBitStationEntity> stations = conn.poolsApi.poolsGetStations(node.getAttribute("Pool key").getString());
for (BitPoolServerModelBitStationEntity station: stations) {
if (node.getChild(station.getStationName()) == null) {
Node child = node.createChild(station.getStationName()).build();
final BitpoolStation bs = makeStationObj(station, child);
bs.init();
// child.getListener().setOnListHandler(new Handler<Node>() {
// private boolean loaded = false;
// public void handle(Node event) {
// if (!loaded) bs.init();
// loaded = true;
// }
// });
}
}
} catch (ApiException e) {
// TODO Auto-generated catch block
}
}
private BitpoolStation makeStationObj(BitPoolServerModelBitStationEntity station, Node child) {
child.setAttribute("Pool key", new Value(station.getPoolKey()));
child.setAttribute("Station id", new Value(station.getStationID()));
child.setAttribute("Registration date", new Value(conn.link.safeToString(station.getRegistrationDate())));
return new BitpoolStation(getMe(), child);
}
void restoreLastSession() {
init();
if (node.getChildren() == null) return;
for (Node child: node.getChildren().values()) {
Value id = child.getAttribute("Station id");
if (id != null) {
try {
BitPoolServerModelBitStationEntity station = conn.poolsApi.poolsGetStation(node.getAttribute("Pool key").getString(), id.getNumber().intValue());
BitpoolStation bs = makeStationObj(station, child);
bs.restoreLastSession();
} catch (ApiException e) {
// TODO Auto-generated catch block
node.removeChild(child);
}
} else if (child.getAction() == null && !child.isHidden()) {
node.removeChild(child);
}
}
}
private BitpoolPool getMe() {
return this;
}
}