Esempio n. 1
0
        // Just return the processed text as-is
        public string ProcessText(IHttpContext context)
        {
            cumulus.LogDebugMessage("API tag: Processing API Text tag request");

            var data = new StreamReader(context.Request.InputStream).ReadToEnd();

            cumulus.LogDataMessage("API tag: Input string = " + data);

            tokenParser.InputText = data;
            var output = tokenParser.ToStringFromString();

            cumulus.LogDataMessage("API tag: Output string = " + output);

            return(output);
        }
Esempio n. 2
0
        public static void UpdateMQTTfeed(string feedType)
        {
            var template = "mqtt/";

            if (feedType == "Interval")
            {
                template += cumulus.MQTT.IntervalTemplate;
            }
            else
            {
                template += cumulus.MQTT.UpdateTemplate;
            }

            if (!File.Exists(template))
            {
                return;
            }

            // use template file
            cumulus.LogDebugMessage($"MQTT: Using template - {template}");

            // read the file
            var templateText = File.ReadAllText(template);
            var templateObj  = templateText.FromJson <MqttTemplate>();

            // process each of the topics in turn
            try
            {
                foreach (var feed in templateObj.topics)
                {
                    var mqttTokenParser = new TokenParser {
                        Encoding = new System.Text.UTF8Encoding(false)
                    };
                    mqttTokenParser.OnToken  += cumulus.TokenParserOnToken;
                    mqttTokenParser.InputText = feed.data;
                    string message = mqttTokenParser.ToStringFromString();

                    // send the message
                    _ = SendMessageAsync(feed.topic, message, feed.retain);
                }
            }
            catch (Exception ex)
            {
                cumulus.LogMessage($"UpdateMQTTfeed: Error process the template file [{template}], error = {ex.Message}");
            }
        }
Esempio n. 3
0
        // Just return the processed text as-is
        public string ProcessText(IHttpRequest request)
        {
            cumulus.LogDebugMessage("API tag: Processing API Text tag request");

            try
            {
                var data = new StreamReader(request.InputStream).ReadToEnd();

                cumulus.LogDataMessage($"API tag: Source = {request.RemoteEndPoint} Input string = {data}");

                tokenParser.InputText = data;
                var output = tokenParser.ToStringFromString();

                cumulus.LogDataMessage("API tag: Output string = " + output);

                return(output);
            }
            catch (Exception ex)
            {
                cumulus.LogMessage($"API tag: Error - {ex.Message}");
                return($"{{\"ERROR\":\"{ex.Message}\"}}");
            }
        }
Esempio n. 4
0
        public static void UpdateMQTTfeed(string feedType)
        {
            string topic;
            var    template = "mqtt/";

            if (feedType == "Interval")
            {
                template += cumulus.MQTT.IntervalTemplate;
                topic     = cumulus.MQTT.IntervalTopic;
            }
            else
            {
                template += cumulus.MQTT.UpdateTemplate;
                topic     = cumulus.MQTT.UpdateTopic;

                // 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.MQTT.UpdateTemplate != 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.MQTT.UpdateTemplate;
                    }
                    else
                    {
                        cumulus.LogMessage($"MQTT: Error, unable to find template file - {template}");
                        return;
                    }
                }
            }

            if (!File.Exists(template))
            {
                return;
            }

            // use template file
            cumulus.LogDebugMessage($"MQTT: Using template - {template}");
            bool retain;
            var  mqttTokenParser = new TokenParser {
                Encoding = new System.Text.UTF8Encoding(false)
            };

            mqttTokenParser.OnToken += cumulus.TokenParserOnToken;
            string message;

            if (feedType == "Interval")
            {
                mqttTokenParser.SourceFile = template;
                message = mqttTokenParser.ToString();
                retain  = cumulus.MQTT.IntervalRetained;
            }
            else
            {
                mqttTokenParser.InputText = dataupdateTemplateContent;
                message = mqttTokenParser.ToStringFromString();
                retain  = cumulus.MQTT.UpdateRetained;
            }

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