public static async Task MakeVarRecTask(MqttConfig config, ModuleInitInfo info, string certDir, Func <bool> shutdown) { SynchronizationContext theSyncContext = SynchronizationContext.Current !; var mqttOptions = MakeMqttOptions(certDir, config, "VarRec"); var varRec = config.VarReceive !; string topic = (string.IsNullOrEmpty(config.TopicRoot) ? "" : config.TopicRoot + "/") + varRec.Topic; bool configChanged = false; Action onConfigChanged = () => { Console.WriteLine("MakeVarRecTask: onConfigChanged called"); configChanged = true; }; Connection clientFAST = await EnsureConnectOrThrow(info, null, onConfigChanged, varRec.ModuleID); while (!shutdown()) { IMqttClient?clientMQTT = await EnsureConnect(mqttOptions, null);; if (clientMQTT == null) { Console.WriteLine("Can not connect to MQTT broker"); await Task.Delay(5000); continue; } clientFAST = await EnsureConnectOrThrow(info, clientFAST, onConfigChanged, varRec.ModuleID); List <ObjectInfo> objs = await clientFAST.GetAllObjects(varRec.ModuleID); ObjectInfo[] writableObjs = objs.Where(obj => obj.Variables.Any(v => v.Writable)).ToArray(); MqttTopicFilter[] topics = writableObjs.Select(obj => new MqttTopicFilter() { QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce, Topic = $"{topic}/{obj.ID.LocalObjectID}" }).ToArray(); clientMQTT.UseApplicationMessageReceivedHandler((arg) => { var promise = new TaskCompletionSource <bool>(); theSyncContext !.Post(_ => { Task task = OnReceivedVarWriteRequest(varRec, clientFAST, arg); task.ContinueWith(completedTask => promise.CompleteFromTask(completedTask)); }, null); return(promise.Task); }); foreach (var top in topics) { Console.WriteLine($"Subscribing to topic {top.Topic}"); await clientMQTT.SubscribeAsync(top); } while (!configChanged && !shutdown()) { try { await clientMQTT.PingAsync(CancellationToken.None); } catch (Exception exp) { Exception e = exp.GetBaseException() ?? exp; Console.Error.WriteLine($"MakeVarRecTask: Connection broken during Ping. Trying to reconnect. Err: {e.Message}"); break; } try { clientFAST = await EnsureConnectOrThrow(info, clientFAST, onConfigChanged, varRec.ModuleID); } catch (Exception) { Console.Error.WriteLine("Connection to FAST core broken. Trying to reconnect..."); break; } await Time.WaitUntil(Timestamp.Now + Duration.FromSeconds(6), abort : () => configChanged || shutdown()); } configChanged = false; await CloseIntern(clientMQTT); } }