forked from IOT-DSA/dslink-java-bitpool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitpoolLink.java
More file actions
142 lines (123 loc) · 5.17 KB
/
BitpoolLink.java
File metadata and controls
142 lines (123 loc) · 5.17 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
package bitpool;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
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.Objects;
import org.dsa.iot.dslink.util.StringUtils;
import org.dsa.iot.dslink.util.handler.Handler;
public class BitpoolLink {
// private final static Logger LOGGER;
// static {
// LOGGER = LoggerFactory.getLogger(BitpoolLink.class);
// }
Node node;
final Map<Node, ScheduledFuture<?>> futures = new ConcurrentHashMap<Node, ScheduledFuture<?>>();
final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
private BitpoolLink(Node node) {
this.node = node;
}
public static void start(Node parent) {
final BitpoolLink link = new BitpoolLink(parent);
link.init();
}
private void init() {
restoreLastSession();
Action act = new Action(Permission.READ, new LoginHandler());
Parameter param = new Parameter("Password", ValueType.STRING);
param.setEditorType(EditorType.PASSWORD);
act.addParameter(param);
act.addParameter(new Parameter("Organisation", ValueType.STRING).setPlaceHolder("Bitpool Demonstration"));
act.addParameter(new Parameter("Place", ValueType.STRING).setPlaceHolder("office"));
act.addParameter(new Parameter("Polling interval", ValueType.NUMBER, new Value(5)));
act.addParameter(new Parameter("Load data manually", ValueType.BOOL, new Value(false)));
node.createChild("add connection").setAction(act).build().setSerializable(false);
}
private void restoreLastSession() {
if (node.getChildren() == null) return;
for (Node child: node.getChildren().values()) {
Value email = child.getAttribute("Email");
Value pass = child.getAttribute("Password");
Value org = child.getAttribute("Organisation");
Value place = child.getAttribute("Place");
Value interval = child.getAttribute("Polling interval");
Value manLoad = child.getAttribute("Load data manually");
if (email!=null && pass!=null && org!=null && place!=null && interval!=null && manLoad!=null) {
if (manLoad.getBool()) {
BitpoolConn conn = new BitpoolConn(getMe(), child);
conn.restoreLastSession();
} else {
child.clearChildren();
BitpoolConn conn = new BitpoolConn(getMe(), child);
conn.init();
}
} else if (!child.getName().equals("defs") && child.getAction() == null) {
node.removeChild(child);
}
}
}
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);
Node child = 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(getMe(), child);
conn.login(email, pass, org, place);
conn.init();
}
}
void setupStream(final BitpoolStream bs) {
bs.node.getListener().setOnSubscribeHandler(new Handler<Node>() {
public void handle(final Node event) {
if (futures.containsKey(event)) return;
long interval = (long) (1000 * bs.station.pool.conn.node.getAttribute("Polling interval").getNumber().doubleValue());
ScheduledThreadPoolExecutor stpe = Objects.getDaemonThreadPool();
ScheduledFuture<?> fut = stpe.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
bs.poll();
}
}, 0, interval, TimeUnit.MILLISECONDS);
futures.put(event, fut);
}
});
bs.node.getListener().setOnUnsubscribeHandler(new Handler<Node>() {
public void handle(final Node event) {
ScheduledFuture<?> fut = futures.remove(event);
if (fut != null) {
fut.cancel(false);
}
}
});
}
String safeToString(Date date) {
return (date == null) ? null : dateFormat.format(date);
}
BitpoolLink getMe() {
return this;
}
}