Skip to content

Instantly share code, notes, and snippets.

@michaeldimoudis
Created January 12, 2019 03:30
Show Gist options
  • Save michaeldimoudis/13ada33318e099641e9f57616e0b8346 to your computer and use it in GitHub Desktop.
Save michaeldimoudis/13ada33318e099641e9f57616e0b8346 to your computer and use it in GitHub Desktop.
Azure IoT Hub Initialize()
public async Task Initialize()
{
while (true)
{
try
{
Log.Information("Azure IoT Hub trying to initialize");
_deviceClient = null;
_deviceClient = DeviceClient.CreateFromConnectionString(Config.DeviceConnectionString, TransportType.Mqtt);
_deviceClient.SetConnectionStatusChangesHandler(async (s, r) => await ConnectionStatusChanged(s, r));
await _deviceClient.OpenAsync();
await RegisterTwinUpdateAsync();
var tc = await GetDesiredProperties();
await UpdateDeviceTwin(tc);
await RegisterDirectMethodHandlers();
// Receive Cloud to Device messages init can go here if required
Log.Information("Azure IoT Hub initialized");
return;
}
catch (Exception ex)
{
Log.Error("Azure IoT Hub trying to connect exception: {@ex}", ex);
}
Log.Error("Azure IoT Hub Initialize delaying 15 seconds before retrying.");
await Task.Delay(15.Seconds());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment