diff --git a/mtconnect-client/src/main/java/io/github/protocol/mtconnect/client/MTConnectClient.java b/mtconnect-client/src/main/java/io/github/protocol/mtconnect/client/MTConnectClient.java index 3e50ae4..90e6b12 100644 --- a/mtconnect-client/src/main/java/io/github/protocol/mtconnect/client/MTConnectClient.java +++ b/mtconnect-client/src/main/java/io/github/protocol/mtconnect/client/MTConnectClient.java @@ -2,6 +2,14 @@ import io.github.openfacade.http.HttpClient; import io.github.openfacade.http.HttpClientFactory; +import io.github.openfacade.http.HttpResponse; +import io.github.protocol.mtconnect.api.MTConnectDevices; +import io.github.protocol.mtconnect.common.XmlUtil; + +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; public class MTConnectClient { private final MTConnectClientConfiguration config; @@ -12,4 +20,29 @@ public MTConnectClient(MTConnectClientConfiguration configuration) { this.config = configuration; this.httpClient = HttpClientFactory.createHttpClient(configuration.httpConfig()); } + + public MTConnectDevices device(String Id) { + return null; + } + + public MTConnectDevices devices() throws ExecutionException, InterruptedException { + String url = String.format("http://%s:%s/devices", config.host(), config.port()); + CompletableFuture future = httpClient.get(url); + + CompletableFuture resp = future.thenCompose(response -> { + if (response.statusCode() >= 200 && response.statusCode() < 300) { + try { + String string = new String(response.body(), StandardCharsets.UTF_8); + MTConnectDevices body = XmlUtil.fromXml(string, MTConnectDevices.class); + return CompletableFuture.completedFuture(body); + } catch (Exception e) { + return CompletableFuture.failedFuture(e); + } + } else { + return CompletableFuture.failedFuture(new Exception("http error: " + Arrays.toString(response.body()))); + } + }); + + return resp.get(); + } } diff --git a/mtconnect-server/pom.xml b/mtconnect-server/pom.xml index 8d992e6..7f2f681 100644 --- a/mtconnect-server/pom.xml +++ b/mtconnect-server/pom.xml @@ -17,6 +17,12 @@ mtconnect-common ${project.version} + + io.github.protocol-laboratory + mtconnect-client + 0.0.1-SNAPSHOT + test + com.huaweicloud.sdk huaweicloud-sdk-iotda diff --git a/mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/MTConnectServer.java b/mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/MTConnectServer.java index 6eebed1..47f79d6 100644 --- a/mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/MTConnectServer.java +++ b/mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/MTConnectServer.java @@ -7,7 +7,9 @@ import io.github.openfacade.http.HttpServerFactory; import io.github.openfacade.http.SyncRequestHandler; import io.github.protocol.mtconnect.api.AssetRequest; +import io.github.protocol.mtconnect.api.DeviceRequest; import io.github.protocol.mtconnect.api.MTConnectAssets; +import io.github.protocol.mtconnect.api.MTConnectDevices; import io.github.protocol.mtconnect.common.XmlUtil; import io.netty.handler.codec.http.HttpResponseStatus; @@ -29,9 +31,14 @@ public MTConnectServer(MTConnectServerConfiguration configuration) { public CompletableFuture start() { this.httpServer.addSyncRoute("/assets", HttpMethod.GET, new MtAssetsHandler()); + this.httpServer.addSyncRoute("/devices", HttpMethod.GET, new MtDevicesHandler()); return httpServer.start(); } + public int httpPort() { + return httpServer.listenPort(); + } + class MtAssetsHandler implements SyncRequestHandler { @Override public HttpResponse handle(HttpRequest request) { @@ -47,4 +54,20 @@ public HttpResponse handle(HttpRequest request) { return new HttpResponse(HttpResponseStatus.OK.code(), body.getBytes(StandardCharsets.UTF_8)); } } + + class MtDevicesHandler implements SyncRequestHandler { + @Override + public HttpResponse handle(HttpRequest request) { + MTConnectDevices devices = mtProcessor.device(new DeviceRequest()); + // convert the response to http response + String body; + try { + body = XmlUtil.toXml(devices); + } catch (Exception e) { + throw new RuntimeException(e); + } + + return new HttpResponse(HttpResponseStatus.OK.code(), body.getBytes(StandardCharsets.UTF_8)); + } + } } diff --git a/mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/MemoryMtProcessor.java b/mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/MemoryMtProcessor.java index a5b6a86..4b63e79 100644 --- a/mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/MemoryMtProcessor.java +++ b/mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/MemoryMtProcessor.java @@ -14,7 +14,9 @@ */ public class MemoryMtProcessor implements MTProcessor { - Map mtConnectAssetsMap = new HashMap<>(); + private final Map mtConnectAssetsMap = new HashMap<>(); + + private MTConnectDevices devices; @Override public MTConnectAssets asset(AssetRequest assetRequest) { @@ -23,6 +25,10 @@ public MTConnectAssets asset(AssetRequest assetRequest) { @Override public MTConnectDevices device(DeviceRequest deviceRequest) { - return null; + return devices; + } + + public void updateDevices(MTConnectDevices devices) { + this.devices = devices; } } diff --git a/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectDeviceTest.java b/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectDeviceTest.java new file mode 100644 index 0000000..e3efd3d --- /dev/null +++ b/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectDeviceTest.java @@ -0,0 +1,61 @@ +package io.github.protocol.mtconnect.server; + +import io.github.openfacade.http.HttpClientConfig; +import io.github.openfacade.http.HttpServerConfig; +import io.github.openfacade.http.HttpServerEngine; +import io.github.protocol.mtconnect.api.Device; +import io.github.protocol.mtconnect.api.MTConnectDevices; +import io.github.protocol.mtconnect.client.MTConnectClient; +import io.github.protocol.mtconnect.client.MTConnectClientConfiguration; +import io.github.protocol.mtconnect.server.impl.MemoryMtProcessor; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.concurrent.ExecutionException; + +public class MTConnectDeviceTest { + + private int port; + private final String localHost = "127.0.0.1"; + + // start memory server + private MemoryMtProcessor startMemoryServer() { + MTConnectServerConfiguration configuration = new MTConnectServerConfiguration(); + HttpServerConfig httpServerConfig = new HttpServerConfig.Builder() + .engine(HttpServerEngine.Vertx) + .host(localHost) + .port(0) + .build(); + configuration.setHttpConfig(httpServerConfig); + MemoryMtProcessor mtProcessor = new MemoryMtProcessor(); + configuration.setMtProcessor(mtProcessor); + MTConnectServer mtConnectServer = new MTConnectServer(configuration); + mtConnectServer.start().join(); + + port = mtConnectServer.httpPort(); + + return mtProcessor; + } + + @Test + public void testDevices() throws ExecutionException, InterruptedException { + MemoryMtProcessor memoryMtProcessor = startMemoryServer(); + MTConnectDevices devices = new MTConnectDevices(); + Device device = new Device(); + device.setId("test_id"); + + devices.setDevices(Collections.singletonList(device)); + memoryMtProcessor.updateDevices(devices); + + MTConnectClientConfiguration configuration = new MTConnectClientConfiguration(); + HttpClientConfig httpClientConfig = new HttpClientConfig.Builder().build(); + configuration.setHttpConfig(httpClientConfig); + configuration.setHost(localHost); + configuration.setPort(port); + MTConnectClient mtConnectClient = new MTConnectClient(configuration); + + MTConnectDevices resp = mtConnectClient.devices(); + Assertions.assertEquals(device.getId(), resp.getDevices().get(0).getId()); + } +}