Search in sources :

Example 1 with MethodRequest

use of com.microsoft.azure.sdk.iot.device.edge.MethodRequest in project azure-iot-sdk-java by Azure.

the class ModuleInvokeMethodSample method main.

/**
 * Receives requests from an IoT Hub. Default protocol is to use
 * MQTT transport.
 *
 * @param args
 * args[0] = IoT Hub connection string
 * args[1] = protocol (one of 'mqtt' or 'amqps' or 'mqtt_ws' or 'amqps_ws')
 * args[2] = the method name
 * args[3] = the string payload to send alongside the method invocation
 * args[4] = the device id of the device to invoke the method on
 * args[5] = (optional) the module id to invoke the method on
 */
public static void main(String[] args) throws IOException, URISyntaxException, ModuleClientException {
    System.out.println("Starting...");
    System.out.println("Beginning setup.");
    if (args.length != 4 && args.length != 5) {
        System.out.format("Expected 4 or 5 arguments but received: %d.\n" + "The program should be called with the following args: \n" + "1. (mqtt | https | amqps | amqps_ws | mqtt_ws)\n" + "2. The name of the method to invoke\n" + "3. The string payload to send alongside the method invocation\n" + "4. The id of the device to invoke the method on\n" + "5. (optional) The id of the module to invoke the method on\n", args.length);
        return;
    }
    IotHubClientProtocol protocol;
    String protocolStr = args[0];
    if (protocolStr.equalsIgnoreCase("https")) {
        throw new UnsupportedOperationException("Module Client does not support HTTPS communication");
    } else if (protocolStr.equalsIgnoreCase("amqps")) {
        protocol = IotHubClientProtocol.AMQPS;
    } else if (protocolStr.equalsIgnoreCase("mqtt")) {
        protocol = IotHubClientProtocol.MQTT;
    } else if (protocolStr.equalsIgnoreCase("amqps_ws")) {
        protocol = IotHubClientProtocol.AMQPS_WS;
    } else if (protocolStr.equalsIgnoreCase("mqtt_ws")) {
        protocol = IotHubClientProtocol.MQTT_WS;
    } else {
        System.out.format("Expected argument 2 to be one of 'mqtt', 'mqtt_ws', 'amqps' or 'amqps_ws' but received %s\n", protocolStr);
        return;
    }
    String methodName = args[1];
    String methodPayload = args[2];
    String deviceIdToInvokeOn = args[3];
    String moduleIdToInvokeOn = args.length > 4 ? args[4] : null;
    ModuleClient client = ModuleClient.createFromEnvironment(protocol);
    client.open();
    MethodRequest methodRequest = new MethodRequest(methodName, methodPayload);
    MethodResult result;
    try {
        if (moduleIdToInvokeOn == null || moduleIdToInvokeOn.isEmpty()) {
            result = invokeMethodOnDevice(methodName, deviceIdToInvokeOn, client, methodRequest);
        } else {
            result = invokeMethodOnModule(methodName, deviceIdToInvokeOn, moduleIdToInvokeOn, client, methodRequest);
        }
        System.out.println("Received response status: " + result.getStatus());
        System.out.println("Received response payload: " + result.getPayloadObject());
    } catch (ModuleClientException e) {
        System.out.println("Encountered an exception while invoking method");
        e.printStackTrace();
    } finally {
        client.closeNow();
    }
}
Also used : MethodRequest(com.microsoft.azure.sdk.iot.device.edge.MethodRequest) ModuleClientException(com.microsoft.azure.sdk.iot.device.exceptions.ModuleClientException) MethodResult(com.microsoft.azure.sdk.iot.device.edge.MethodResult)

Example 2 with MethodRequest

use of com.microsoft.azure.sdk.iot.device.edge.MethodRequest 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 3 with MethodRequest

use of com.microsoft.azure.sdk.iot.device.edge.MethodRequest 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 4 with MethodRequest

use of com.microsoft.azure.sdk.iot.device.edge.MethodRequest in project azure-iot-sdk-java by Azure.

the class MethodRequestTest method constructorSavesArguments.

// Tests_SRS_DIRECTMETHODREQUEST_34_003: [This constructor shall save the provided payload, methodname, and timeouts.]
@Test
public void constructorSavesArguments() {
    // arrange
    int expectedResponseTimeout = 3;
    int expectedConnectionTimeout = 4;
    // act
    MethodRequest request = new MethodRequest(expectedMethodName, expectedPayload, expectedResponseTimeout, expectedConnectionTimeout);
    // assert
    assertEquals(expectedResponseTimeout, Deencapsulation.getField(request, "responseTimeoutInSeconds"));
    assertEquals(expectedConnectionTimeout, Deencapsulation.getField(request, "connectionTimeoutInSeconds"));
    assertEquals(expectedMethodName, Deencapsulation.getField(request, "methodName"));
    assertEquals(expectedPayload, Deencapsulation.getField(request, "payload"));
}
Also used : MethodRequest(com.microsoft.azure.sdk.iot.device.edge.MethodRequest) Test(org.junit.Test)

Example 5 with MethodRequest

use of com.microsoft.azure.sdk.iot.device.edge.MethodRequest in project azure-iot-sdk-java by Azure.

the class MethodRequestTest method constructorUsesDefaultTimeouts.

// Tests_SRS_DIRECTMETHODREQUEST_34_001: [This constructor shall invoke the overloaded constructor with default values of responseTimeout=0 and connectionTimeout=0.]
@Test
public void constructorUsesDefaultTimeouts() {
    // act
    MethodRequest request = new MethodRequest(expectedMethodName, expectedPayload);
    // assert
    assertNull(Deencapsulation.getField(request, "responseTimeoutInSeconds"));
    assertNull(Deencapsulation.getField(request, "connectionTimeoutInSeconds"));
}
Also used : MethodRequest(com.microsoft.azure.sdk.iot.device.edge.MethodRequest) Test(org.junit.Test)

Aggregations

MethodRequest (com.microsoft.azure.sdk.iot.device.edge.MethodRequest)5 MethodResult (com.microsoft.azure.sdk.iot.device.edge.MethodResult)3 ModuleClientException (com.microsoft.azure.sdk.iot.device.exceptions.ModuleClientException)3 MainApiException (io.swagger.server.api.MainApiException)2 JsonObject (io.vertx.core.json.JsonObject)2 Test (org.junit.Test)2