Search in sources :

Example 6 with MethodResult

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));
        }
    }
}
Also used : MethodRequest(com.microsoft.azure.sdk.iot.device.edge.MethodRequest) ModuleClientException(com.microsoft.azure.sdk.iot.device.exceptions.ModuleClientException) JsonObject(io.vertx.core.json.JsonObject) MainApiException(io.swagger.server.api.MainApiException) MethodResult(com.microsoft.azure.sdk.iot.device.edge.MethodResult)

Example 7 with MethodResult

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));
        }
    }
}
Also used : MethodRequest(com.microsoft.azure.sdk.iot.device.edge.MethodRequest) ModuleClientException(com.microsoft.azure.sdk.iot.device.exceptions.ModuleClientException) JsonObject(io.vertx.core.json.JsonObject) MainApiException(io.swagger.server.api.MainApiException) MethodResult(com.microsoft.azure.sdk.iot.device.edge.MethodResult)

Example 8 with MethodResult

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);
}
Also used : HttpsTransportManager(com.microsoft.azure.sdk.iot.device.transport.https.HttpsTransportManager) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) MethodResult(com.microsoft.azure.sdk.iot.device.edge.MethodResult) Test(org.junit.Test)

Example 9 with MethodResult

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, "");
}
Also used : HttpsTransportManager(com.microsoft.azure.sdk.iot.device.transport.https.HttpsTransportManager) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) MethodResult(com.microsoft.azure.sdk.iot.device.edge.MethodResult) Test(org.junit.Test)

Example 10 with MethodResult

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;
}
Also used : MethodResult(com.microsoft.azure.sdk.iot.device.edge.MethodResult)

Aggregations

MethodResult (com.microsoft.azure.sdk.iot.device.edge.MethodResult)11 Test (org.junit.Test)5 HttpsTransportManager (com.microsoft.azure.sdk.iot.device.transport.https.HttpsTransportManager)4 MethodRequest (com.microsoft.azure.sdk.iot.device.edge.MethodRequest)3 ModuleClientException (com.microsoft.azure.sdk.iot.device.exceptions.ModuleClientException)3 IotHubTransportMessage (com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)3 MainApiException (io.swagger.server.api.MainApiException)2 JsonObject (io.vertx.core.json.JsonObject)2 HashMap (java.util.HashMap)1