Exemplo n.º 1
0
        private void MqttOnMessageReceive(object sender, MqttMsgPublishEventArgs e)
        {
            RtTagMqtt[] tags;
            IRtMessage  msg = RtMessage.Create(e.Topic, RtValue.Create(e.Message), e);

            lock (rtTags)
            {
                tags = rtTags.Where(tt => tt.Topic == msg.Topic).ToArray();

                if (!extraTopics.Contains(e.Topic))
                {
                    extraTopics.Add(e.Topic);
                }
            }

            foreach (var tag in tags)
            {
                tag.RaiseOnUpdatedValue(msg.Value);
                RaiseOnUpdatedValue(tag);
            }

            if (autoAddExtraTopics && !tags.Any() && extraTopicsSbscribe.Any())
            {
                var tag = (RtTagMqtt)AddTag(e.Topic, e.Topic, null);
            }

            RaiseOnMessageReceive(msg);
        }
Exemplo n.º 2
0
        private IRtValue TryParseValue(string value)
        {
            lock (this)
            {
                try
                {
                    regex ??= new Regex(TagRegexValuePattern);

                    var match = regex.Match(value);
                    if (match.Success)
                    {
                        // TODO FIXME: hay que ver si es el grupo uno por defecto. y de agregar un name el agrupo
                        var parsed = match.Groups[1].Value;
                        Status = base.Status;
                        return(RtValue.Create(parsed));
                    }
                    else
                    {
                        Status = false;
                        return(RtValue.Create(string.Empty));
                    }
                }
                catch
                {
                    Status = false;
                    return(RtValue.Create(string.Empty));
                }
            }
        }
Exemplo n.º 3
0
    public (IRtValue value, bool status, string name) ReadValue(string readExpression, ITagRuleDefinition <string> arg)
    {
        if (readExpression.StartsWith("'") && readExpression.EndsWith("'"))
        {
            return(RtValue.Create(readExpression.Trim('\'')), true, readExpression);
        }

        IRtTag tagRead;

        switch (readExpression)
        {
        case "TagExpression":
            tagRead = arg.TagExpression;
            return(tagRead.Value, tagRead.Status, tagRead.ToString());

        case "PrevValue":
            return(RtValue.Create(arg.PrevValue), true, readExpression);

        case "Value":
            return(RtValue.Create(arg.Value), true, readExpression);

        case "UpdatedUnixTimestamp":
            return(RtValue.Create(arg.Updated.ToUnixTimestamp()), true, "UpdatedUnixTimestamp");

        default:
            tagRead = GetTag(readExpression);
            return(tagRead.Value, tagRead.Status, tagRead.ToString());
        }
    }
Exemplo n.º 4
0
        public IRtValue Eval()
        {
            lock (this)
            {
                try
                {
                    DateTime?_updated = null;
                    foreach (var e in ArgumentsTags.Select((tag, index) => (tag, index)))
                    {
                        expression.getArgument(e.index).setArgumentValue(e.tag.Value.Numeric);
                        _updated = !_updated.HasValue || e.tag.Updated > _updated ? e.tag.Updated : _updated;
                    }

                    var value = expression.calculate();
                    RaiseOnUpdatedValue(this, RtValue.Create(value), _updated ?? DateTime.Now, GetStatus());
                    return(base.Value);
                }
                catch (Exception e)
                {
                    var msg = e.Message;
                    RaiseOnUpdatedValue(this, RtValue.Create(double.NaN), DateTime.Now, false);
                    return(base.Value);
                }
            }
        }
Exemplo n.º 5
0
 private IRtValue TryParseValue(string json)
 {
     lock (this)
     {
         try
         {
             var jsonObject = JsonSerializer.Deserialize <JsonElement>(json);
             var obj        = jsonObject.JsonGetValue(TagJsonValuePattern);
             var parsed     = Convert.ToString(obj);
             Status = base.Status;
             return(RtValue.Create(parsed));
         }
         catch
         {
             Status = false;
             return(RtValue.Create(string.Empty));
         }
     }
 }
        private IRtValue TryParseValue(IRtTag tag)
        {
            lock (this)
            {
                try
                {
                    var parsed = tag.Value.Binary.Parser(TagBinaryDataParserPattern);
                    Status = parsed != null && base.Status;

                    return((parsed is string parserStr)
                        ? RtValue.Create(parserStr)
                        : RtValue.Create(Convert.ToDouble(parsed)));
                }
                catch
                {
                    Status = false;
                    return(RtValue.Create(string.Empty));
                }
            }
        }
    public (IRtValue value, bool status, string name, DateTime updated) ReadValue(string readExpression, ITagRuleDefinition <string> arg)
    {
        IRtTag tagRead;

        switch (readExpression)
        {
        case "TagExpression":
            tagRead = arg.TagExpression;
            return(tagRead.Value, tagRead.Status, tagRead.TagName, tagRead.Updated);

        case "PrevValue":
            return(RtValue.Create(arg.PrevValue), true, readExpression, arg.PrevUpdated);

        case "Value":
            return(RtValue.Create(arg.Value), true, readExpression, arg.Updated);

        default:
            tagRead = GetTag(readExpression);
            return(tagRead.Value, tagRead.Status, tagRead.TagName, tagRead.Updated);
        }
    }
Exemplo n.º 8
0
        private IRtValue TryParseValue(IRtTag tag)
        {
            lock (this)
            {
                try
                {
                    DateTimeOffset dt     = tag.GetDateTime();
                    var            parsed = string.Empty;

                    switch (TagDateTimeValuePattern.ToUpper())
                    {
                    case "UNIXTIMESTAMP":
                        parsed = dt.LocalDateTime.ToUnixTimestamp().ToString();
                        break;

                    case "UNIXTIMEMILLISECONDS":
                        parsed = dt.ToUnixTimeMilliseconds().ToString();
                        break;

                    case "UNIXTIMESECONDS":
                        parsed = dt.ToUnixTimeSeconds().ToString();
                        break;

                    default:
                        parsed = dt.ToString(TagDateTimeValuePattern);
                        break;
                    }

                    Status = !string.IsNullOrEmpty(parsed) && base.Status;
                    return(RtValue.Create(parsed));
                }
                catch
                {
                    Status = false;
                    return(RtValue.Create(string.Empty));
                }
            }
        }
        internal IRtValue ReadTagValue(string topic)
        {
            var value = keyValueMemory?.GetValue(topic);

            return(value != null?RtValue.Create(value) : null);
        }