Beispiel #1
0
        public static void UpdateMQTTfeed(string feedType)
        {
            string message, topic;
            var    template = "mqtt/";

            if (feedType == "Interval")
            {
                template += cumulus.MQTTIntervalTemplate;
                topic     = cumulus.MQTTIntervalTopic;
            }
            else
            {
                template += cumulus.MQTTUpdateTemplate;
                topic     = cumulus.MQTTUpdateTopic;

                // Refresh our copy of the template contents if the filename has changed
                // We want to avoid reading the template file every few seconds if possible.
                if (cumulus.MQTTUpdateTemplate != dataupdateTemplateFile)
                {
                    if (File.Exists(template))
                    {
                        try
                        {
                            using (TextReader reader = new StreamReader(template, new System.Text.UTF8Encoding(false)))
                            {
                                dataupdateTemplateContent = reader.ReadToEnd();
                            }
                        }
                        catch (Exception e)
                        {
                            cumulus.LogMessage($"MQTT: Error reading template file {template} - {e.Message}");
                            return;
                        }
                        dataupdateTemplateFile = cumulus.MQTTUpdateTemplate;
                    }
                    else
                    {
                        cumulus.LogMessage($"MQTT: Error, unable to find template file - {template}");
                        return;
                    }
                }
            }

            if (File.Exists(template))
            {
                // use template file
                cumulus.LogDebugMessage($"MQTT: Using template - {template}");
                var mqttTokenParser = new TokenParser();
                var encoding        = new System.Text.UTF8Encoding(false);
                mqttTokenParser.encoding = encoding;
                mqttTokenParser.OnToken += cumulus.TokenParserOnToken;
                if (feedType == "Interval")
                {
                    mqttTokenParser.SourceFile = template;
                    message = mqttTokenParser.ToString();
                }
                else
                {
                    mqttTokenParser.InputText = dataupdateTemplateContent;
                    message = mqttTokenParser.ToStringFromString();
                }

                // send the message
                _ = SendMessageAsync(topic, message);
            }
        }