Skip to content

Commit 702598c

Browse files
feat: add device api (#20)
Signed-off-by: zhiheng123 <[email protected]> Co-authored-by: ZhangJian He <[email protected]>
1 parent 4793ba9 commit 702598c

File tree

5 files changed

+131
-2
lines changed

5 files changed

+131
-2
lines changed

mtconnect-client/src/main/java/io/github/protocol/mtconnect/client/MTConnectClient.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
import io.github.openfacade.http.HttpClient;
44
import io.github.openfacade.http.HttpClientFactory;
5+
import io.github.openfacade.http.HttpResponse;
6+
import io.github.protocol.mtconnect.api.MTConnectDevices;
7+
import io.github.protocol.mtconnect.common.XmlUtil;
8+
9+
import java.nio.charset.StandardCharsets;
10+
import java.util.Arrays;
11+
import java.util.concurrent.CompletableFuture;
12+
import java.util.concurrent.ExecutionException;
513

614
public class MTConnectClient {
715
private final MTConnectClientConfiguration config;
@@ -12,4 +20,29 @@ public MTConnectClient(MTConnectClientConfiguration configuration) {
1220
this.config = configuration;
1321
this.httpClient = HttpClientFactory.createHttpClient(configuration.httpConfig());
1422
}
23+
24+
public MTConnectDevices device(String Id) {
25+
return null;
26+
}
27+
28+
public MTConnectDevices devices() throws ExecutionException, InterruptedException {
29+
String url = String.format("http://%s:%s/devices", config.host(), config.port());
30+
CompletableFuture<HttpResponse> future = httpClient.get(url);
31+
32+
CompletableFuture<MTConnectDevices> resp = future.thenCompose(response -> {
33+
if (response.statusCode() >= 200 && response.statusCode() < 300) {
34+
try {
35+
String string = new String(response.body(), StandardCharsets.UTF_8);
36+
MTConnectDevices body = XmlUtil.fromXml(string, MTConnectDevices.class);
37+
return CompletableFuture.completedFuture(body);
38+
} catch (Exception e) {
39+
return CompletableFuture.failedFuture(e);
40+
}
41+
} else {
42+
return CompletableFuture.failedFuture(new Exception("http error: " + Arrays.toString(response.body())));
43+
}
44+
});
45+
46+
return resp.get();
47+
}
1548
}

mtconnect-server/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
<artifactId>mtconnect-common</artifactId>
1818
<version>${project.version}</version>
1919
</dependency>
20+
<dependency>
21+
<groupId>io.github.protocol-laboratory</groupId>
22+
<artifactId>mtconnect-client</artifactId>
23+
<version>0.0.1-SNAPSHOT</version>
24+
<scope>test</scope>
25+
</dependency>
2026
<dependency>
2127
<groupId>com.huaweicloud.sdk</groupId>
2228
<artifactId>huaweicloud-sdk-iotda</artifactId>

mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/MTConnectServer.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import io.github.openfacade.http.HttpServerFactory;
88
import io.github.openfacade.http.SyncRequestHandler;
99
import io.github.protocol.mtconnect.api.AssetRequest;
10+
import io.github.protocol.mtconnect.api.DeviceRequest;
1011
import io.github.protocol.mtconnect.api.MTConnectAssets;
12+
import io.github.protocol.mtconnect.api.MTConnectDevices;
1113
import io.github.protocol.mtconnect.common.XmlUtil;
1214
import io.netty.handler.codec.http.HttpResponseStatus;
1315

@@ -29,9 +31,14 @@ public MTConnectServer(MTConnectServerConfiguration configuration) {
2931

3032
public CompletableFuture<Void> start() {
3133
this.httpServer.addSyncRoute("/assets", HttpMethod.GET, new MtAssetsHandler());
34+
this.httpServer.addSyncRoute("/devices", HttpMethod.GET, new MtDevicesHandler());
3235
return httpServer.start();
3336
}
3437

38+
public int httpPort() {
39+
return httpServer.listenPort();
40+
}
41+
3542
class MtAssetsHandler implements SyncRequestHandler {
3643
@Override
3744
public HttpResponse handle(HttpRequest request) {
@@ -47,4 +54,20 @@ public HttpResponse handle(HttpRequest request) {
4754
return new HttpResponse(HttpResponseStatus.OK.code(), body.getBytes(StandardCharsets.UTF_8));
4855
}
4956
}
57+
58+
class MtDevicesHandler implements SyncRequestHandler {
59+
@Override
60+
public HttpResponse handle(HttpRequest request) {
61+
MTConnectDevices devices = mtProcessor.device(new DeviceRequest());
62+
// convert the response to http response
63+
String body;
64+
try {
65+
body = XmlUtil.toXml(devices);
66+
} catch (Exception e) {
67+
throw new RuntimeException(e);
68+
}
69+
70+
return new HttpResponse(HttpResponseStatus.OK.code(), body.getBytes(StandardCharsets.UTF_8));
71+
}
72+
}
5073
}

mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/MemoryMtProcessor.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
*/
1515
public class MemoryMtProcessor implements MTProcessor {
1616

17-
Map<String, MTConnectAssets> mtConnectAssetsMap = new HashMap<>();
17+
private final Map<String, MTConnectAssets> mtConnectAssetsMap = new HashMap<>();
18+
19+
private MTConnectDevices devices;
1820

1921
@Override
2022
public MTConnectAssets asset(AssetRequest assetRequest) {
@@ -23,6 +25,10 @@ public MTConnectAssets asset(AssetRequest assetRequest) {
2325

2426
@Override
2527
public MTConnectDevices device(DeviceRequest deviceRequest) {
26-
return null;
28+
return devices;
29+
}
30+
31+
public void updateDevices(MTConnectDevices devices) {
32+
this.devices = devices;
2733
}
2834
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package io.github.protocol.mtconnect.server;
2+
3+
import io.github.openfacade.http.HttpClientConfig;
4+
import io.github.openfacade.http.HttpServerConfig;
5+
import io.github.openfacade.http.HttpServerEngine;
6+
import io.github.protocol.mtconnect.api.Device;
7+
import io.github.protocol.mtconnect.api.MTConnectDevices;
8+
import io.github.protocol.mtconnect.client.MTConnectClient;
9+
import io.github.protocol.mtconnect.client.MTConnectClientConfiguration;
10+
import io.github.protocol.mtconnect.server.impl.MemoryMtProcessor;
11+
import org.junit.jupiter.api.Assertions;
12+
import org.junit.jupiter.api.Test;
13+
14+
import java.util.Collections;
15+
import java.util.concurrent.ExecutionException;
16+
17+
public class MTConnectDeviceTest {
18+
19+
private int port;
20+
private final String localHost = "127.0.0.1";
21+
22+
// start memory server
23+
private MemoryMtProcessor startMemoryServer() {
24+
MTConnectServerConfiguration configuration = new MTConnectServerConfiguration();
25+
HttpServerConfig httpServerConfig = new HttpServerConfig.Builder()
26+
.engine(HttpServerEngine.Vertx)
27+
.host(localHost)
28+
.port(0)
29+
.build();
30+
configuration.setHttpConfig(httpServerConfig);
31+
MemoryMtProcessor mtProcessor = new MemoryMtProcessor();
32+
configuration.setMtProcessor(mtProcessor);
33+
MTConnectServer mtConnectServer = new MTConnectServer(configuration);
34+
mtConnectServer.start().join();
35+
36+
port = mtConnectServer.httpPort();
37+
38+
return mtProcessor;
39+
}
40+
41+
@Test
42+
public void testDevices() throws ExecutionException, InterruptedException {
43+
MemoryMtProcessor memoryMtProcessor = startMemoryServer();
44+
MTConnectDevices devices = new MTConnectDevices();
45+
Device device = new Device();
46+
device.setId("test_id");
47+
48+
devices.setDevices(Collections.singletonList(device));
49+
memoryMtProcessor.updateDevices(devices);
50+
51+
MTConnectClientConfiguration configuration = new MTConnectClientConfiguration();
52+
HttpClientConfig httpClientConfig = new HttpClientConfig.Builder().build();
53+
configuration.setHttpConfig(httpClientConfig);
54+
configuration.setHost(localHost);
55+
configuration.setPort(port);
56+
MTConnectClient mtConnectClient = new MTConnectClient(configuration);
57+
58+
MTConnectDevices resp = mtConnectClient.devices();
59+
Assertions.assertEquals(device.getId(), resp.getDevices().get(0).getId());
60+
}
61+
}

0 commit comments

Comments
 (0)