Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,4 +20,29 @@
this.config = configuration;
this.httpClient = HttpClientFactory.createHttpClient(configuration.httpConfig());
}

public MTConnectDevices device(String Id) {
return null;

Check warning on line 25 in mtconnect-client/src/main/java/io/github/protocol/mtconnect/client/MTConnectClient.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L25 was not covered by tests
}

public MTConnectDevices devices() throws ExecutionException, InterruptedException {
String url = String.format("http://%s:%s/devices", config.host(), config.port());
CompletableFuture<HttpResponse> future = httpClient.get(url);

CompletableFuture<MTConnectDevices> 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);

Check warning on line 39 in mtconnect-client/src/main/java/io/github/protocol/mtconnect/client/MTConnectClient.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L38 - L39 were not covered by tests
}
} else {
return CompletableFuture.failedFuture(new Exception("http error: " + Arrays.toString(response.body())));

Check warning on line 42 in mtconnect-client/src/main/java/io/github/protocol/mtconnect/client/MTConnectClient.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L42 was not covered by tests
}
});

return resp.get();
}
}
6 changes: 6 additions & 0 deletions mtconnect-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
<artifactId>mtconnect-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.github.protocol-laboratory</groupId>
<artifactId>mtconnect-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.huaweicloud.sdk</groupId>
<artifactId>huaweicloud-sdk-iotda</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -29,9 +31,14 @@

public CompletableFuture<Void> 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) {
Expand All @@ -47,4 +54,20 @@
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);

Check warning on line 67 in mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/MTConnectServer.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L66 - L67 were not covered by tests
}

return new HttpResponse(HttpResponseStatus.OK.code(), body.getBytes(StandardCharsets.UTF_8));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
*/
public class MemoryMtProcessor implements MTProcessor {

Map<String, MTConnectAssets> mtConnectAssetsMap = new HashMap<>();
private final Map<String, MTConnectAssets> mtConnectAssetsMap = new HashMap<>();

private MTConnectDevices devices;

@Override
public MTConnectAssets asset(AssetRequest assetRequest) {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}