From f3b480484be9899dca4925f25bd44dc419f661bb Mon Sep 17 00:00:00 2001 From: zhiheng123 <903292776@qq.com> Date: Sun, 17 Nov 2024 23:51:57 +0800 Subject: [PATCH 1/2] feat: add device api Signed-off-by: zhiheng123 <903292776@qq.com> --- .../mtconnect/client/MTConnectClient.java | 40 ++++++++++++ mtconnect-server/pom.xml | 6 ++ .../mtconnect/server/MTConnectServer.java | 4 ++ .../server/impl/MemoryMtProcessor.java | 5 ++ .../mtconnect/server/MTConnectDeviceTest.java | 61 +++++++++++++++++++ 5 files changed, 116 insertions(+) create mode 100644 mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectDeviceTest.java 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..5020b33 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 @@ -1,15 +1,55 @@ package io.github.protocol.mtconnect.client; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; 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 java.util.Arrays; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; public class MTConnectClient { private final MTConnectClientConfiguration config; private final HttpClient httpClient; + private static final ObjectMapper MAPPER = new ObjectMapper(); public MTConnectClient(MTConnectClientConfiguration configuration) { this.config = configuration; this.httpClient = HttpClientFactory.createHttpClient(configuration.httpConfig()); } + + public MTConnectDevices deivce(String Id) { + return null; + }; + + public static T toObject(String json, Class type) throws JsonProcessingException { + if (json == null || json.isEmpty()) { + return null; + } + return MAPPER.readValue(json, type); + } + + public MTConnectDevices deivces() 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 { + MTConnectDevices body = toObject(Arrays.toString(response.body()), MTConnectDevices.class); + return CompletableFuture.completedFuture(body); + } catch (JsonProcessingException 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..3b59249 100644 --- a/mtconnect-server/pom.xml +++ b/mtconnect-server/pom.xml @@ -37,6 +37,12 @@ vertx-web ${vertx.version} + + io.github.protocol-laboratory + mtconnect-client + 0.0.1-SNAPSHOT + test + 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..abeb629 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 @@ -32,6 +32,10 @@ public CompletableFuture start() { return httpServer.start(); } + public int getHttpPort() { + return httpServer.listenPort(); + } + class MtAssetsHandler implements SyncRequestHandler { @Override public HttpResponse handle(HttpRequest request) { 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..04c657c 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 @@ -15,6 +15,7 @@ public class MemoryMtProcessor implements MTProcessor { Map mtConnectAssetsMap = new HashMap<>(); + private MTConnectDevices devices; @Override public MTConnectAssets asset(AssetRequest assetRequest) { @@ -25,4 +26,8 @@ public MTConnectAssets asset(AssetRequest assetRequest) { public MTConnectDevices device(DeviceRequest deviceRequest) { return null; } + + public void updateDevice(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..7b1a6d0 --- /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.Test; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.concurrent.ExecutionException; + +public class MTConnectDeviceTest { + + private int port; + private final String localHost = "127.0.0.1"; + + // start memery 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.getHttpPort(); + + return mtProcessor; + } + + @Test + public void testDevices() throws ExecutionException, InterruptedException { + MemoryMtProcessor memoryMtProcessor = startMemoryServer(); + MTConnectDevices devices = new MTConnectDevices(); + Device device = new Device(); + + devices.setDevices(Collections.singletonList(device)); + memoryMtProcessor.updateDevice(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); + + // use client show device + MTConnectDevices resp = mtConnectClient.deivces(); + System.out.println(resp.getDevices()); + } +} From 6eda7d4409369bdafe65c3e974652b6ba549ed61 Mon Sep 17 00:00:00 2001 From: zhiheng123 <903292776@qq.com> Date: Mon, 18 Nov 2024 21:58:08 +0800 Subject: [PATCH 2/2] test: add the testcase of devices Signed-off-by: zhiheng123 <903292776@qq.com> Co-authored-by: ZhangJian He --- .../mtconnect/client/MTConnectClient.java | 25 +++++++------------ mtconnect-server/pom.xml | 12 ++++----- .../mtconnect/server/MTConnectServer.java | 21 +++++++++++++++- .../server/impl/MemoryMtProcessor.java | 7 +++--- .../mtconnect/server/MTConnectDeviceTest.java | 14 +++++------ 5 files changed, 46 insertions(+), 33 deletions(-) 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 5020b33..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 @@ -1,12 +1,12 @@ package io.github.protocol.mtconnect.client; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; 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; @@ -15,34 +15,27 @@ public class MTConnectClient { private final MTConnectClientConfiguration config; private final HttpClient httpClient; - private static final ObjectMapper MAPPER = new ObjectMapper(); public MTConnectClient(MTConnectClientConfiguration configuration) { this.config = configuration; this.httpClient = HttpClientFactory.createHttpClient(configuration.httpConfig()); } - - public MTConnectDevices deivce(String Id) { - return null; - }; - public static T toObject(String json, Class type) throws JsonProcessingException { - if (json == null || json.isEmpty()) { - return null; - } - return MAPPER.readValue(json, type); + public MTConnectDevices device(String Id) { + return null; } - public MTConnectDevices deivces() throws ExecutionException, InterruptedException { + 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 { - MTConnectDevices body = toObject(Arrays.toString(response.body()), MTConnectDevices.class); + String string = new String(response.body(), StandardCharsets.UTF_8); + MTConnectDevices body = XmlUtil.fromXml(string, MTConnectDevices.class); return CompletableFuture.completedFuture(body); - } catch (JsonProcessingException e) { + } catch (Exception e) { return CompletableFuture.failedFuture(e); } } else { @@ -51,5 +44,5 @@ public MTConnectDevices deivces() throws ExecutionException, InterruptedExceptio }); return resp.get(); - }; + } } diff --git a/mtconnect-server/pom.xml b/mtconnect-server/pom.xml index 3b59249..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 @@ -37,12 +43,6 @@ vertx-web ${vertx.version} - - io.github.protocol-laboratory - mtconnect-client - 0.0.1-SNAPSHOT - test - 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 abeb629..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,10 +31,11 @@ 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 getHttpPort() { + public int httpPort() { return httpServer.listenPort(); } @@ -51,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 04c657c..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,8 @@ */ public class MemoryMtProcessor implements MTProcessor { - Map mtConnectAssetsMap = new HashMap<>(); + private final Map mtConnectAssetsMap = new HashMap<>(); + private MTConnectDevices devices; @Override @@ -24,10 +25,10 @@ public MTConnectAssets asset(AssetRequest assetRequest) { @Override public MTConnectDevices device(DeviceRequest deviceRequest) { - return null; + return devices; } - public void updateDevice(MTConnectDevices 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 index 7b1a6d0..e3efd3d 100644 --- 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 @@ -8,9 +8,9 @@ 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.ArrayList; import java.util.Collections; import java.util.concurrent.ExecutionException; @@ -19,7 +19,7 @@ public class MTConnectDeviceTest { private int port; private final String localHost = "127.0.0.1"; - // start memery server + // start memory server private MemoryMtProcessor startMemoryServer() { MTConnectServerConfiguration configuration = new MTConnectServerConfiguration(); HttpServerConfig httpServerConfig = new HttpServerConfig.Builder() @@ -33,7 +33,7 @@ private MemoryMtProcessor startMemoryServer() { MTConnectServer mtConnectServer = new MTConnectServer(configuration); mtConnectServer.start().join(); - port = mtConnectServer.getHttpPort(); + port = mtConnectServer.httpPort(); return mtProcessor; } @@ -43,9 +43,10 @@ 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.updateDevice(devices); + memoryMtProcessor.updateDevices(devices); MTConnectClientConfiguration configuration = new MTConnectClientConfiguration(); HttpClientConfig httpClientConfig = new HttpClientConfig.Builder().build(); @@ -54,8 +55,7 @@ public void testDevices() throws ExecutionException, InterruptedException { configuration.setPort(port); MTConnectClient mtConnectClient = new MTConnectClient(configuration); - // use client show device - MTConnectDevices resp = mtConnectClient.deivces(); - System.out.println(resp.getDevices()); + MTConnectDevices resp = mtConnectClient.devices(); + Assertions.assertEquals(device.getId(), resp.getDevices().get(0).getId()); } }