Search in sources :

Example 1 with IotHubClientProtocol

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

the class DeviceMethodSample method main.

/**
     * Receives method calls from IotHub. Default protocol is to use
     * use MQTT transport.
     *
     * @param args 
     * args[0] = IoT Hub connection string
     */
public static void main(String[] args) throws IOException, URISyntaxException {
    System.out.println("Starting...");
    System.out.println("Beginning setup.");
    if (args.length != 1) {
        System.out.format("Expected the following argument but received: %d.\n" + "The program should be called with the following args: \n" + "[Device connection string] - String containing Hostname, Device Id & Device Key in the following formats: HostName=<iothub_host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>\n", args.length);
        return;
    }
    String connString = args[0];
    IotHubClientProtocol protocol = IotHubClientProtocol.MQTT;
    System.out.println("Successfully read input parameters.");
    System.out.format("Using communication protocol %s.\n", protocol.name());
    DeviceClient client = new DeviceClient(connString, protocol);
    System.out.println("Successfully created an IoT Hub client.");
    try {
        client.open();
        System.out.println("Opened connection to IoT Hub.");
        client.subscribeToDeviceMethod(new SampleDeviceMethodCallback(), null, new DeviceMethodStatusCallBack(), null);
        System.out.println("Subscribed to device method");
        System.out.println("Waiting for method trigger");
    } catch (Exception e) {
        System.out.println("On exception, shutting down \n" + " Cause: " + e.getCause() + " \n" + e.getMessage());
        client.close();
        System.out.println("Shutting down...");
    }
    System.out.println("Press any key to exit...");
    Scanner scanner = new Scanner(System.in);
    scanner.nextLine();
    client.close();
    System.out.println("Shutting down...");
}
Also used : Scanner(java.util.Scanner) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) IotHubClientProtocol(com.microsoft.azure.sdk.iot.device.IotHubClientProtocol) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Example 2 with IotHubClientProtocol

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

the class HandleMessages 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 (optional, one of 'mqtt' or 'amqps' or 'https' or 'amqps_ws')
     */
public static void main(String[] args) throws IOException, URISyntaxException {
    System.out.println("Starting...");
    System.out.println("Beginning setup.");
    if (args.length <= 0 || 3 <= args.length) {
        System.out.format("Expected 1 or 2 arguments but received %d.\n" + "The program should be called with the following args: \n" + "1. [Device connection string] - String containing Hostname, Device Id & Device Key in one of the following formats: HostName=<iothub_host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>\n" + "2. (mqtt | https | amqps | amqps_ws)\n", args.length);
        return;
    }
    String connString = args[0];
    IotHubClientProtocol protocol;
    if (args.length == 1) {
        protocol = IotHubClientProtocol.MQTT;
    } else {
        String protocolStr = args[1];
        if (protocolStr.equals("https")) {
            protocol = IotHubClientProtocol.HTTPS;
        } else if (protocolStr.equals("amqps")) {
            protocol = IotHubClientProtocol.AMQPS;
        } else if (protocolStr.equals("mqtt")) {
            protocol = IotHubClientProtocol.MQTT;
        } else if (protocolStr.equals("amqps_ws")) {
            protocol = IotHubClientProtocol.AMQPS_WS;
        } else {
            System.out.format("Expected argument 2 to be one of 'mqtt', 'https', 'amqps' or 'amqps_ws' but received %s\n" + "The program should be called with the following args: \n" + "1. [Device connection string] - String containing Hostname, Device Id & Device Key in one of the following formats: HostName=<iothub_host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>\n" + "2. (mqtt | https | amqps | amqps_ws)\n", protocolStr);
            return;
        }
    }
    System.out.println("Successfully read input parameters.");
    System.out.format("Using communication protocol %s.\n", protocol.name());
    DeviceClient client = new DeviceClient(connString, protocol);
    System.out.println("Successfully created an IoT Hub client.");
    if (protocol == IotHubClientProtocol.MQTT) {
        MessageCallbackMqtt callback = new MessageCallbackMqtt();
        Counter counter = new Counter(0);
        client.setMessageCallback(callback, counter);
    } else {
        MessageCallback callback = new MessageCallback();
        Counter counter = new Counter(0);
        client.setMessageCallback(callback, counter);
    }
    System.out.println("Successfully set message callback.");
    client.open();
    System.out.println("Opened connection to IoT Hub.");
    System.out.println("Beginning to receive messages...");
    System.out.println("Press any key to exit...");
    Scanner scanner = new Scanner(System.in);
    scanner.nextLine();
    client.close();
    System.out.println("Shutting down...");
}
Also used : Scanner(java.util.Scanner) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) IotHubClientProtocol(com.microsoft.azure.sdk.iot.device.IotHubClientProtocol)

Aggregations

DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)2 IotHubClientProtocol (com.microsoft.azure.sdk.iot.device.IotHubClientProtocol)2 Scanner (java.util.Scanner)2 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1