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
@@ -1,14 +1,24 @@
package io.github.protocol.mtconnect.examples;

import io.github.openfacade.http.HttpClientConfig;
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.common.XmlUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MTConnectClientExample {
public static void main(String[] args) {
public static void main(String[] args) throws Exception {
MTConnectClientConfiguration configuration = new MTConnectClientConfiguration();
configuration.setHost("127.0.0.1");
configuration.setPort(36633);

HttpClientConfig httpClientConfig = new HttpClientConfig.Builder().build();
configuration.setHttpConfig(httpClientConfig);
MTConnectClient mtConnectClient = new MTConnectClient(configuration);

MTConnectDevices devices = mtConnectClient.devices();
log.info(XmlUtil.toXml(devices));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ public static void main(String[] args) {
.port(36633)
.build();
configuration.setHttpConfig(httpServerConfig);
configuration.setMtProcessor(new IoTDAMtProcessor("your_ak", "your_sk"));
IoTDAMtProcessor ioTDAMtProcessor = new IoTDAMtProcessor.Builder()
.setAk(System.getenv("CLOUD_SDK_AK"))
.setSk(System.getenv("CLOUD_SDK_SK"))
.setProjectId("your_project_id")
.setEndpoint("ec138732b4.st1.iotda-app.cn-north-4.myhuaweicloud.com")
.build();

configuration.setMtProcessor(ioTDAMtProcessor);
MTConnectServer mtConnectServer = new MTConnectServer(configuration);
mtConnectServer.start().join();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
package io.github.protocol.mtconnect.server.impl;

import com.huaweicloud.sdk.core.auth.AbstractCredentials;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.core.region.Region;
import com.huaweicloud.sdk.iotda.v5.IoTDAClient;
import com.huaweicloud.sdk.iotda.v5.model.ListDevicesRequest;
import com.huaweicloud.sdk.iotda.v5.model.ListDevicesResponse;
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.server.MTProcessor;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j

Check warning on line 21 in mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java

View check run for this annotation

Codecov / codecov/patch

mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java#L21

Added line #L21 was not covered by tests
@NoArgsConstructor
public class IoTDAMtProcessor implements MTProcessor {
private final String ak;

private final String sk;

public IoTDAMtProcessor(String ak, String sk) {
this.ak = ak;
this.sk = sk;
}
private String ak;
private String sk;
private String projectId;
private String endpoint;
private IoTDAClient client;

@Override
public MTConnectAssets asset(AssetRequest assetRequest) {
Expand All @@ -23,6 +34,59 @@

@Override
public MTConnectDevices device(DeviceRequest deviceRequest) {
ListDevicesRequest request = new ListDevicesRequest();

Check warning on line 37 in mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java

View check run for this annotation

Codecov / codecov/patch

mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java#L37

Added line #L37 was not covered by tests
try {
ListDevicesResponse response = client.listDevices(request);
log.info(response.toString());
} catch (ConnectionException | RequestTimeoutException e) {
log.error(e.getMessage());
} catch (ServiceResponseException e) {
log.error(e.getMessage());
log.error(String.valueOf(e.getHttpStatusCode()));
log.error(e.getRequestId());
log.error(e.getErrorCode());
log.error(e.getErrorMsg());
}

Check warning on line 49 in mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java

View check run for this annotation

Codecov / codecov/patch

mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java#L39-L49

Added lines #L39 - L49 were not covered by tests
return null;
}

public static class Builder {
private final IoTDAMtProcessor ioTDAMtProcessor = new IoTDAMtProcessor();

Check warning on line 54 in mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java

View check run for this annotation

Codecov / codecov/patch

mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java#L53-L54

Added lines #L53 - L54 were not covered by tests

public Builder setAk(String ak) {
ioTDAMtProcessor.ak = ak;
return this;

Check warning on line 58 in mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java

View check run for this annotation

Codecov / codecov/patch

mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java#L57-L58

Added lines #L57 - L58 were not covered by tests
}
public Builder setSk(String sk) {
ioTDAMtProcessor.sk = sk;
return this;

Check warning on line 62 in mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java

View check run for this annotation

Codecov / codecov/patch

mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java#L61-L62

Added lines #L61 - L62 were not covered by tests
}

public Builder setProjectId(String projectId) {
ioTDAMtProcessor.projectId = projectId;
return this;

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

View check run for this annotation

Codecov / codecov/patch

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

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

public Builder setEndpoint(String endpoint) {
ioTDAMtProcessor.endpoint = endpoint;
return this;

Check warning on line 72 in mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java

View check run for this annotation

Codecov / codecov/patch

mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java#L71-L72

Added lines #L71 - L72 were not covered by tests
}

public IoTDAMtProcessor build(){

ICredential auth = new BasicCredentials()

Check warning on line 77 in mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java

View check run for this annotation

Codecov / codecov/patch

mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java#L77

Added line #L77 was not covered by tests
// 标准版/企业版需要使用衍生算法,基础版请删除配置"withDerivedPredicate";
.withDerivedPredicate(AbstractCredentials.DEFAULT_DERIVED_PREDICATE) // Used in derivative ak/sk authentication scenarios
.withAk(ioTDAMtProcessor.ak)
.withSk(ioTDAMtProcessor.sk);

Check warning on line 81 in mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java

View check run for this annotation

Codecov / codecov/patch

mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java#L79-L81

Added lines #L79 - L81 were not covered by tests

ioTDAMtProcessor.client = IoTDAClient.newBuilder()
.withCredential(auth)

Check warning on line 84 in mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java

View check run for this annotation

Codecov / codecov/patch

mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java#L83-L84

Added lines #L83 - L84 were not covered by tests
// 标准版/企业版:需自行创建Region对象,基础版:请使用IoTDARegion的region对象,如"withRegion(IoTDARegion.CN_NORTH_4)"
.withRegion(new Region("cn-north-4", ioTDAMtProcessor.endpoint))
.build();

Check warning on line 87 in mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java

View check run for this annotation

Codecov / codecov/patch

mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java#L86-L87

Added lines #L86 - L87 were not covered by tests

return ioTDAMtProcessor;

Check warning on line 89 in mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java

View check run for this annotation

Codecov / codecov/patch

mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java#L89

Added line #L89 was not covered by tests
}
}
}