I'm using amqp to send messages from my iot devices to Azure IoTHub. I'm writing a program in C# and I want to check if the devices are able to establish a connection with iot hub. What would be the best way of achieving this? Is it possible to create a callback function?
I noticed that one of my devices are on a closed network, that is the amqp port is blocked. But the device logs that It's sending messages to IoT Hub (no exceptions thrown) and when I check Iot Hub no messages have been received. This is why I want to check if the device is able to establish a connection with IoT Hub
The java sdk has a eventcallback class but not C#, or am I missing something?
protected static class EventCallback implements IotHubEventCallback {
public void execute(IotHubStatusCode status, Object context) {
System.out.println("IoT Hub responded to message with status " + status.name());
}
}
Edit: Would this be a good implementation?
try {
client = DeviceClient.CreateFromConnectionString("***", TransportType.Amqp);
var task = client.OpenAsync();
task.Wait(30000); // wait for 30 sec
if (task.IsCompleted) {
Console.WriteLine("Connected");
await client.CloseAsync();
}
else {
throw new Exception("Time out");
}
}
catch (Exception e) {
Console.WriteLine("Error");
}