use of com.microsoft.azure.sdk.iot.device.edge.MethodResult in project azure-iot-sdk-java by Azure.
the class ModuleGlue method invokeDeviceMethod.
public void invokeDeviceMethod(String connectionId, String deviceId, Object methodInvokeParameters, Handler<AsyncResult<Object>> handler) {
ModuleClient client = getClient(connectionId);
if (client == null) {
handler.handle(Future.failedFuture(new MainApiException(500, "invalid connection id")));
} else {
JsonObject params = (JsonObject) methodInvokeParameters;
String methodName = params.getString("methodName");
String payload = params.getString("payload");
int responseTimeout = params.getInteger("responseTimeoutInSeconds", 0);
int connectionTimeout = params.getInteger("connectTimeoutInSeconds", 0);
MethodRequest request = new MethodRequest(methodName, payload, responseTimeout, connectionTimeout);
try {
MethodResult result = client.invokeMethod(deviceId, request);
handler.handle(Future.succeededFuture(makeMethodResultThatEncodesCorrectly(result)));
} catch (ModuleClientException e) {
handler.handle(Future.failedFuture(e));
}
}
}
use of com.microsoft.azure.sdk.iot.device.edge.MethodResult in project azure-iot-sdk-java by Azure.
the class ModuleGlue method invokeModuleMethod.
public void invokeModuleMethod(String connectionId, String deviceId, String moduleId, Object methodInvokeParameters, Handler<AsyncResult<Object>> handler) {
ModuleClient client = getClient(connectionId);
if (client == null) {
handler.handle(Future.failedFuture(new MainApiException(500, "invalid connection id")));
} else {
JsonObject params = (JsonObject) methodInvokeParameters;
String methodName = params.getString("methodName");
String payload = params.getString("payload");
int responseTimeout = params.getInteger("responseTimeoutInSeconds", 0);
int connectionTimeout = params.getInteger("connectTimeoutInSeconds", 0);
MethodRequest request = new MethodRequest(methodName, payload, responseTimeout, connectionTimeout);
try {
MethodResult result = client.invokeMethod(deviceId, moduleId, request);
handler.handle(Future.succeededFuture(makeMethodResultThatEncodesCorrectly(result)));
} catch (ModuleClientException e) {
handler.handle(Future.failedFuture(e));
}
}
}
use of com.microsoft.azure.sdk.iot.device.edge.MethodResult in project azure-iot-sdk-java by Azure.
the class HttpsTransportManagerTest method invokeMethodOnModuleSuccess.
// Tests_SRS_HTTPSTRANSPORTMANAGER_34_017: [This function shall call invokeMethod with the provided request and
// a uri in the format twins/<device id>/modules/<module id>/methods?api-version=<api_version>.]
@Test
public void invokeMethodOnModuleSuccess() throws TransportException, IOException, URISyntaxException {
// arrange
final HttpsTransportManager transportManager = new HttpsTransportManager(mockConfig);
final String expectedDeviceId = "myDevice";
final String expectedModuleId = "myModule";
final String expectedSenderDeviceId = "mySenderDevice";
final String expectedSenderModuleId = "mySenderModule";
final String expectedMethodRequestJson = "someJson";
final String expectedResponseBody = "some body";
// assert
new Expectations(HttpsTransportManager.class) {
{
mockedMethodRequest.toJson();
result = expectedMethodRequestJson;
new IotHubTransportMessage(expectedMethodRequestJson);
result = mockedTransportMessage;
mockedTransportMessage.setIotHubMethod(IotHubMethod.POST);
mockedTransportMessage.setUriPath("/twins/" + expectedDeviceId + "/modules/" + expectedModuleId + "/methods");
mockConfig.getDeviceId();
result = expectedSenderDeviceId;
mockConfig.getModuleId();
result = expectedSenderModuleId;
transportManager.send(mockedTransportMessage, (Map) any);
result = mockResponseMessage;
mockResponseMessage.getStatus();
result = IotHubStatusCode.OK_EMPTY;
mockResponseMessage.getBytes();
result = expectedResponseBody.getBytes(StandardCharsets.UTF_8);
new MethodResult(expectedResponseBody);
}
};
// act
transportManager.invokeMethod(mockedMethodRequest, expectedDeviceId, expectedModuleId);
}
use of com.microsoft.azure.sdk.iot.device.edge.MethodResult in project azure-iot-sdk-java by Azure.
the class HttpsTransportManagerTest method invokeMethodOnDeviceSuccess.
// Tests_SRS_HTTPSTRANSPORTMANAGER_34_018: [If a moduleId is provided, this function shall call invokeMethod with the provided request and
// a uri in the format twins/<device id>/modules/<module id>/methods?api-version=<api_version>.]
// Tests_SRS_HTTPSTRANSPORTMANAGER_34_021: [This function shall set the methodrequest json as the body of the http message.]
// Tests_SRS_HTTPSTRANSPORTMANAGER_34_022: [This function shall set the http method to POST.]
// Tests_SRS_HTTPSTRANSPORTMANAGER_34_023: [This function shall set the http message's uri path to the provided uri path.]
// Tests_SRS_HTTPSTRANSPORTMANAGER_34_024 [This function shall set a custom property of 'x-ms-edge-moduleId' to the value of <device id>/<module id> of the sending module/device.]
// Tests_SRS_HTTPSTRANSPORTMANAGER_34_025 [This function shall send the built message.]
// Tests_SRS_HTTPSTRANSPORTMANAGER_34_027 [If the http response doesn't contain an error code, this function return a method result with the response message body as the method result body.]
@Test
public void invokeMethodOnDeviceSuccess() throws TransportException, IOException, URISyntaxException {
// arrange
final HttpsTransportManager transportManager = new HttpsTransportManager(mockConfig);
final String expectedDeviceId = "myDevice";
final String expectedModuleId = "myModule";
final String expectedSenderDeviceId = "mySenderDevice";
final String expectedSenderModuleId = "mySenderModule";
final String expectedMethodRequestJson = "someJson";
final String expectedResponseBody = "some body";
// assert
new Expectations(HttpsTransportManager.class) {
{
mockedMethodRequest.toJson();
result = expectedMethodRequestJson;
new IotHubTransportMessage(expectedMethodRequestJson);
result = mockedTransportMessage;
mockedTransportMessage.setIotHubMethod(IotHubMethod.POST);
mockedTransportMessage.setUriPath("/twins/" + expectedDeviceId + "/methods");
mockConfig.getDeviceId();
result = expectedSenderDeviceId;
mockConfig.getModuleId();
result = expectedSenderModuleId;
transportManager.send(mockedTransportMessage, (Map) any);
result = mockResponseMessage;
mockResponseMessage.getStatus();
result = IotHubStatusCode.OK_EMPTY;
mockResponseMessage.getBytes();
result = expectedResponseBody.getBytes(StandardCharsets.UTF_8);
new MethodResult(expectedResponseBody);
}
};
// act
transportManager.invokeMethod(mockedMethodRequest, expectedDeviceId, "");
}
use of com.microsoft.azure.sdk.iot.device.edge.MethodResult in project azure-iot-sdk-java by Azure.
the class ModuleInvokeMethodSample method invokeMethodOnModule.
private static MethodResult invokeMethodOnModule(String methodName, String deviceIdToInvokeOn, String moduleIdToInvokeOn, ModuleClient client, MethodRequest methodRequest) throws ModuleClientException, URISyntaxException, IOException {
System.out.println("Invoking method \"" + methodName + "\" on module \"" + moduleIdToInvokeOn + "\"");
MethodResult result = client.invokeMethod(deviceIdToInvokeOn, moduleIdToInvokeOn, methodRequest);
System.out.println("Finished Invoking method \"" + methodName + "\" on module \"" + moduleIdToInvokeOn + "\"");
return result;
}
Aggregations