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 90e6b12..c29f8ff 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 @@ -3,6 +3,7 @@ import io.github.openfacade.http.HttpClient; import io.github.openfacade.http.HttpClientFactory; import io.github.openfacade.http.HttpResponse; +import io.github.protocol.mtconnect.api.MTConnectAssets; import io.github.protocol.mtconnect.api.MTConnectDevices; import io.github.protocol.mtconnect.common.XmlUtil; @@ -21,8 +22,25 @@ public MTConnectClient(MTConnectClientConfiguration configuration) { this.httpClient = HttpClientFactory.createHttpClient(configuration.httpConfig()); } - public MTConnectDevices device(String Id) { - return null; + public MTConnectAssets assets() throws ExecutionException, InterruptedException { + String url = String.format("http://%s:%s/assets", 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); + MTConnectAssets body = XmlUtil.fromXml(string, MTConnectAssets.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(); } public MTConnectDevices devices() throws ExecutionException, InterruptedException { 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 4b63e79..d09c881 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 @@ -6,21 +6,18 @@ import io.github.protocol.mtconnect.api.MTConnectDevices; import io.github.protocol.mtconnect.server.MTProcessor; -import java.util.HashMap; -import java.util.Map; /** * MemoryMtProcessor is a simple implementation of MtProcessor that stores all the data in memory. */ public class MemoryMtProcessor implements MTProcessor { - private final Map mtConnectAssetsMap = new HashMap<>(); - private MTConnectDevices devices; + private MTConnectAssets assets; @Override public MTConnectAssets asset(AssetRequest assetRequest) { - return mtConnectAssetsMap.get(assetRequest.getId()); + return assets; } @Override @@ -31,4 +28,8 @@ public MTConnectDevices device(DeviceRequest deviceRequest) { public void updateDevices(MTConnectDevices devices) { this.devices = devices; } + + public void updateAssets(MTConnectAssets assets) { + this.assets = assets; + } } diff --git a/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/IoTDAServerTest.java b/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/IoTDAServerTest.java index 1355954..fdc17e3 100644 --- a/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/IoTDAServerTest.java +++ b/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/IoTDAServerTest.java @@ -50,6 +50,9 @@ private MTConnectServer startIoTDAServer() { .build(); configuration.setHttpConfig(httpServerConfig); IoTDAMtProcessor ioTDAMtProcessor = new IoTDAMtProcessor.Builder() + .setAk("mock_ak") + .setSk("mock_sk") + .setEndpoint("mock_endpoint") .setIoTDAClient(mockClient) .build(); 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 e3efd3d..8c633c0 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 @@ -3,7 +3,9 @@ import io.github.openfacade.http.HttpClientConfig; import io.github.openfacade.http.HttpServerConfig; import io.github.openfacade.http.HttpServerEngine; +import io.github.protocol.mtconnect.api.CuttingTool; import io.github.protocol.mtconnect.api.Device; +import io.github.protocol.mtconnect.api.MTConnectAssets; import io.github.protocol.mtconnect.api.MTConnectDevices; import io.github.protocol.mtconnect.client.MTConnectClient; import io.github.protocol.mtconnect.client.MTConnectClientConfiguration; @@ -38,6 +40,27 @@ private MemoryMtProcessor startMemoryServer() { return mtProcessor; } + @Test + public void testAssets() throws ExecutionException, InterruptedException { + MemoryMtProcessor memoryMtProcessor = startMemoryServer(); + MTConnectAssets assets = new MTConnectAssets(); + CuttingTool cuttingTool = new CuttingTool(); + cuttingTool.setAssetId("asset_id"); + + assets.setCuttingTools(Collections.singletonList(cuttingTool)); + memoryMtProcessor.updateAssets(assets); + + MTConnectClientConfiguration configuration = new MTConnectClientConfiguration(); + HttpClientConfig httpClientConfig = new HttpClientConfig.Builder().build(); + configuration.setHttpConfig(httpClientConfig); + configuration.setHost(localHost); + configuration.setPort(port); + MTConnectClient mtConnectClient = new MTConnectClient(configuration); + + MTConnectAssets resp = mtConnectClient.assets(); + Assertions.assertEquals(cuttingTool.getAssetId(), resp.getCuttingTools().get(0).getAssetId()); + } + @Test public void testDevices() throws ExecutionException, InterruptedException { MemoryMtProcessor memoryMtProcessor = startMemoryServer();