Skip to content

Commit f2ee0a6

Browse files
authored
1.1.4
api changes
1 parent acfc0d5 commit f2ee0a6

File tree

7 files changed

+89
-40
lines changed

7 files changed

+89
-40
lines changed

dslink.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dslink-java-v2-restadapter",
3-
"version": "1.1.3",
3+
"version": "1.1.4",
44
"description": "Java DSA to REST adpater DSLink",
55
"main": "bin/dslink-java-v2-restadapter",
66
"configs": {

src/main/java/org/iot/dsa/dslink/restadapter/AbstractRuleNode.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
package org.iot.dsa.dslink.restadapter;
22

33
import org.apache.commons.lang3.RandomStringUtils;
4+
import org.iot.dsa.dslink.DSIRequester;
45
import org.iot.dsa.node.DSBool;
56
import org.iot.dsa.node.DSIObject;
67
import org.iot.dsa.node.DSIValue;
78
import org.iot.dsa.node.DSInfo;
89
import org.iot.dsa.node.DSLong;
910
import org.iot.dsa.node.DSNode;
10-
import okhttp3.Response;
1111

1212
public abstract class AbstractRuleNode extends DSNode {
1313
private DSInfo bufferEnabled = getInfo(Constants.USE_BUFFER);
1414
private DSInfo maxBatchSize = getInfo(Constants.MAX_BATCH_SIZE);
1515
private String id;
16+
17+
public DSIRequester getRequester() {
18+
return MainNode.getRequester();
19+
}
1620

1721
public WebClientProxy getWebClientProxy() {
1822
return ((ConnectionNode) getParent()).getWebClientProxy();
1923
}
2024

21-
public abstract void responseRecieved(Response resp, int rowNum);
25+
public abstract void responseRecieved(ResponseWrapper resp, int rowNum);
2226

2327
@Override
2428
protected void declareDefaults() {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.iot.dsa.dslink.restadapter;
2+
3+
import java.io.IOException;
4+
import org.iot.dsa.logging.DSLogger;
5+
import org.iot.dsa.time.DSDateTime;
6+
import okhttp3.Response;
7+
8+
public class OkHttpResponseWrapper extends DSLogger implements ResponseWrapper {
9+
10+
private Response response;
11+
private String body = null;
12+
13+
public OkHttpResponseWrapper(Response response) {
14+
this.response = response;
15+
}
16+
17+
public Response getResponse() {
18+
return response;
19+
}
20+
21+
@Override
22+
public int getCode() {
23+
return response.code();
24+
}
25+
26+
@Override
27+
public String getData() {
28+
if (body == null) {
29+
try {
30+
body = response.body().string();
31+
} catch (IOException e) {
32+
warn("", e);
33+
} finally {
34+
response.close();
35+
}
36+
}
37+
return body;
38+
}
39+
40+
@Override
41+
public DSDateTime getTS() {
42+
return DSDateTime.valueOf(response.receivedResponseAtMillis());
43+
}
44+
45+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.iot.dsa.dslink.restadapter;
2+
3+
import org.iot.dsa.time.DSDateTime;
4+
5+
public interface ResponseWrapper {
6+
7+
public int getCode();
8+
9+
public String getData();
10+
11+
public DSDateTime getTS();
12+
13+
}

src/main/java/org/iot/dsa/dslink/restadapter/RuleNode.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.iot.dsa.dslink.restadapter;
22

3-
import java.io.IOException;
43
import org.iot.dsa.node.DSDouble;
54
import org.iot.dsa.node.DSIObject;
65
import org.iot.dsa.node.DSInfo;
@@ -12,7 +11,6 @@
1211
import org.iot.dsa.node.action.ActionResult;
1312
import org.iot.dsa.node.action.DSAction;
1413
import org.iot.dsa.time.DSDateTime;
15-
import okhttp3.Response;
1614

1715
public class RuleNode extends AbstractRuleNode {
1816

@@ -134,25 +132,17 @@ public double getMaxRefreshRate() {
134132
}
135133

136134
@Override
137-
public void responseRecieved(Response resp, int rowNum) {
135+
public void responseRecieved(ResponseWrapper resp, int rowNum) {
138136
if (resp == null) {
139137
put(lastRespCode, DSInt.valueOf(-1));
140138
put(lastRespData, DSString.valueOf("Failed to send update"));
141139
put(lastRespTs, DSString.valueOf(DSDateTime.currentTime()));
142140
} else {
143-
int status = resp.code();
144-
String data = null;
145-
try {
146-
data = resp.body().string();
147-
} catch (IOException e) {
148-
warn("", e);
149-
} finally {
150-
resp.close();
151-
}
152-
141+
int status = resp.getCode();
142+
String data = resp.getData();
153143
put(lastRespCode, DSInt.valueOf(status));
154144
put(lastRespData, DSString.valueOf(data));
155-
put(lastRespTs, DSString.valueOf(DSDateTime.valueOf(resp.receivedResponseAtMillis())));
145+
put(lastRespTs, DSString.valueOf(resp.getTS()));
156146
}
157147
}
158148

src/main/java/org/iot/dsa/dslink/restadapter/RuleTableNode.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.iot.dsa.dslink.restadapter;
22

3-
import java.io.IOException;
43
import java.util.ArrayList;
54
import java.util.List;
65
import org.iot.dsa.node.DSElement;
@@ -12,7 +11,6 @@
1211
import org.iot.dsa.node.action.ActionResult;
1312
import org.iot.dsa.node.action.DSAction;
1413
import org.iot.dsa.time.DSDateTime;
15-
import okhttp3.Response;
1614

1715
public class RuleTableNode extends AbstractRuleNode {
1816

@@ -28,7 +26,7 @@ public RuleTableNode(DSList table) {
2826
}
2927

3028
@Override
31-
public void responseRecieved(Response resp, int rowNum) {
29+
public void responseRecieved(ResponseWrapper resp, int rowNum) {
3230
DSList respTable = lastResponses.getElement().toList();
3331
DSMap respMap = respTable.getMap(rowNum);
3432

@@ -37,19 +35,12 @@ public void responseRecieved(Response resp, int rowNum) {
3735
respMap.put(Constants.LAST_RESPONSE_DATA, "Failed to send update");
3836
respMap.put(Constants.LAST_RESPONSE_TS, DSDateTime.currentTime().toString());
3937
} else {
40-
int status = resp.code();
41-
String data = null;
42-
try {
43-
data = resp.body().string();
44-
} catch (IOException e) {
45-
warn("", e);
46-
} finally {
47-
resp.close();
48-
}
38+
int status = resp.getCode();
39+
String data = resp.getData();
4940

5041
respMap.put(Constants.LAST_RESPONSE_CODE, status);
5142
respMap.put(Constants.LAST_RESPONSE_DATA, data);
52-
respMap.put(Constants.LAST_RESPONSE_TS, DSDateTime.valueOf(resp.receivedResponseAtMillis()).toString());
43+
respMap.put(Constants.LAST_RESPONSE_TS, resp.getTS().toString());
5344
}
5445
fire(VALUE_CHANGED_EVENT, lastResponses, null);
5546
}

src/main/java/org/iot/dsa/dslink/restadapter/SubscriptionRule.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class SubscriptionRule extends AbstractSubscribeHandler implements Update
4040
private long minRefreshRate;
4141
private long maxRefreshRate;
4242

43-
private int rowNum;
43+
protected int rowNum;
4444

4545
public SubscriptionRule(AbstractRuleNode node, String subPath, String restUrl, String method, DSMap urlParameters, String body, double minRefreshRate, double maxRefreshRate, int rowNum) {
4646
this.node = node;
@@ -63,7 +63,7 @@ public void run() {
6363
}
6464

6565
private void init() {
66-
DSIRequester requester = MainNode.getRequester();
66+
DSIRequester requester = node.getRequester();
6767
int qos = 0;
6868
requester.subscribe(this.subPath, DSLong.valueOf(qos), this);
6969
}
@@ -166,7 +166,7 @@ public void run() {
166166
}
167167
}
168168

169-
private boolean sendUpdate(final SubUpdate update) {
169+
public boolean sendUpdate(final SubUpdate update) {
170170

171171
DSMap urlParams = urlParameters.copy();
172172
String body = this.body;
@@ -192,10 +192,11 @@ private boolean sendUpdate(final SubUpdate update) {
192192

193193
node.info("Rule with sub path " + subPath + ": sending Update with value " + (update.value!=null ? update.value : "Null"));
194194

195-
Response resp = restInvoke(urlParams, body);
196-
return resp != null && resp.code() == 200;
195+
ResponseWrapper resp = doSend(urlParams, body);
196+
return resp != null && resp.getCode() / 100 == 2;
197197
}
198198

199+
@Override
199200
public Queue<SubUpdate> sendBatchUpdate(Queue<SubUpdate> updates) {
200201
if (!batchable) {
201202
Queue<SubUpdate> failed = new LinkedList<SubUpdate>();
@@ -231,24 +232,25 @@ public Queue<SubUpdate> sendBatchUpdate(Queue<SubUpdate> updates) {
231232
String body = sb.toString();
232233
node.info("Rule with sub path " + subPath + ": sending batch update");
233234

234-
Response resp = restInvoke(urlParams, body);
235-
if (resp != null && resp.code() == 200) {
235+
ResponseWrapper resp = doSend(urlParams, body);
236+
if (resp != null && resp.getCode() / 100 == 2) {
236237
return null;
237238
} else {
238239
return updatesCopy;
239240
}
240241

241242
}
242243

243-
private Response restInvoke(DSMap urlParams, String body) {
244+
protected ResponseWrapper doSend(DSMap urlParams, String body) {
244245
Response resp = null;
245246
try {
246247
resp = getWebClientProxy().invoke(method, restUrl, urlParams, body);
247248
} catch (Exception e) {
248249
node.warn("", e);
249250
}
250-
node.responseRecieved(resp, rowNum);
251-
return resp;
251+
ResponseWrapper respWrap = new OkHttpResponseWrapper(resp);
252+
node.responseRecieved(respWrap, rowNum);
253+
return respWrap;
252254
}
253255

254256
public void close() {
@@ -265,5 +267,9 @@ public int getMaxBatchSize() {
265267
public WebClientProxy getWebClientProxy() {
266268
return node.getWebClientProxy();
267269
}
270+
271+
public AbstractRuleNode getNode() {
272+
return node;
273+
}
268274

269275
}

0 commit comments

Comments
 (0)