Created
January 12, 2019 03:30
-
-
Save michaeldimoudis/13ada33318e099641e9f57616e0b8346 to your computer and use it in GitHub Desktop.
Azure IoT Hub Initialize()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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