public async Task StartListening() { var mqttSuccess = await MessagingClient.StartListeningAsync(); //Start our check timer MqttCheckTimer = new Timer(async(o) => { if (MqttCheckTimer == null) { return; //If the timer is null, we've stopped listening } if (!ReceivedMqttCheckForInterval) { //We've missed a packet -- perform stop start here Console.WriteLine("MQTT disconnect detected, performing reconnect."); await StopListening(); await StartListening(); } else { ReceivedMqttCheckForInterval = false; try { await SendNotification(IntegrationId, "mqttcheck", 0); } catch (Exception e) { } } }, null, 0, MqttCheckTimerIntervalInMs); if (!mqttSuccess) { Console.WriteLine("MQTT connection failed. Attempting reconnect, but please check that the MQTT URL is valid and the server is reachable by the extension."); await StopListening(); await StartListening(); } }
/// <summary> /// Configure extension /// </summary> /// <returns>true if extension authenticates to server; false if there is an error</returns> private static Boolean ConfigureExtension() { var config = _config; string username = config["Username"]; string password = config["Password"]; string userId = config["UserId"]; string domain = config["Domain"]; var connectionInfo = new ConnectionInfo(username, password, domain, ApiUrl, MqttUrl); var authenticationManager = new ApiAuthenticationManager(connectionInfo); _apiClient = new ApiClient(authenticationManager); _messagingClient = new MessagingClient(authenticationManager); var success = _messagingClient.StartListeningAsync().Result; if (!success) { logger.Error("Failed to connect to mqtt"); return(false); } return(true); }