Skip to content

Commit

Permalink
Add device method sample
Browse files Browse the repository at this point in the history
  • Loading branch information
prmathur-microsoft committed Mar 23, 2017
1 parent da537d1 commit 60a3f0b
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 0 deletions.
38 changes: 38 additions & 0 deletions device/iot-device-samples/device-method-sample/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!-- Copyright (c) Microsoft. All rights reserved. --><!-- Licensed under the MIT license. See LICENSE file in the project root for full license information. -->
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.microsoft.azure.sdk.iot.samples.iot-device-samples</groupId>
<artifactId>device-method-sample</artifactId>
<name>Device Method Sample</name>
<developers>
<developer>
<id>microsoft</id>
<name>Microsoft</name>
</developer>
</developers>
<parent>
<groupId>com.microsoft.azure.sdk.iot.samples</groupId>
<artifactId>iot-device-samples</artifactId>
<version>1.0.21</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>samples.com.microsoft.azure.sdk.iot.device.DeviceMethodSample</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package samples.com.microsoft.azure.sdk.iot.device;

import com.microsoft.azure.sdk.iot.device.*;
import com.microsoft.azure.sdk.iot.device.DeviceTwin.*;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Scanner;

/**
* Device Method Sample for an IoT Hub. Default protocol is to use
* MQTT transport.
*/
public class DeviceMethodSample
{
private static final int METHOD_SUCCESS = 200;
private static final int METHOD_HUNG = 300;
private static final int METHOD_NOT_FOUND = 404;
private static final int METHOD_NOT_DEFINED = 404;

private static int method_command(Object command)
{
System.out.println("invoking" + command + " on this device");
// Insert code to invoke command here
return METHOD_SUCCESS;
}

private static int method_default(Object data)
{
System.out.println("default method for this device");
// Insert device specific code here
return METHOD_NOT_DEFINED;
}

protected static class DeviceMethodStatusCallBack implements IotHubEventCallback
{
public void execute(IotHubStatusCode status, Object context)
{
System.out.println("IoT Hub responded to device method operation with status " + status.name());
}
}

protected static class SampleDeviceMethodCallback implements com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodCallback
{
@Override
public DeviceMethodData call(String methodName, Object methodData, Object context)
{
DeviceMethodData deviceMethodData ;
switch (methodName)
{
case "command" :
{
int status = method_command(methodData);
deviceMethodData = new DeviceMethodData(status, "executed " + methodName);
break;
}
default:
{
int status = method_default(methodData);
deviceMethodData = new DeviceMethodData(status, "executed " + methodName);
}
}

return deviceMethodData;
}
}

/**
* 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...");

}
}
1 change: 1 addition & 0 deletions device/iot-device-samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<module>handle-messages</module>
<module>send-receive-sample</module>
<module>device-twin-sample</module>
<module>device-method-sample</module>
</modules>
<dependencies>
<dependency>
Expand Down

0 comments on commit 60a3f0b

Please sign in to comment.